helper.heartbeat.js 2.19 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2015 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */
/* globals java, sling, com, use */
use(function() {
    'use strict';

    // Acceptable delay in ms for devices connected to author directly
    var ACCEPTABLE_DELAY_LATE = 3 * 1000;

    // Acceptable delay in ms for devices connected via publish instances
    var ACCEPTABLE_REMOTE_DELAY_LATE = 30 * 1000;

    function connectionInfo(deviceStatus) {
        var lastPing = deviceStatus ? deviceStatus.getLastPing().getTime() : 0;
        var inactiveTime = java.lang.System.currentTimeMillis() - lastPing;

        // Read the ping frequency in seconds from the OSGi Configuration
        var deviceService = sling.getService(com.adobe.cq.screens.device.DeviceService);
        var pingFrequency = deviceService.getPingFrequency() * 1000;

        var PING_LATE_TIMEOUT = pingFrequency + ACCEPTABLE_DELAY_LATE;
        var PING_LOST_TIMEOUT = pingFrequency + 3 * ACCEPTABLE_DELAY_LATE;

        // If the device status originates from a publish, the ping delays need adjustment, see CQ-4227095
        if (deviceStatus && deviceStatus.isFromRemote()) {

            // Frequency by which the author obtains device stati from publish instances
            pingFrequency = 60 * 1000;

            PING_LATE_TIMEOUT = pingFrequency + ACCEPTABLE_REMOTE_DELAY_LATE;
            PING_LOST_TIMEOUT = pingFrequency + 3 * ACCEPTABLE_REMOTE_DELAY_LATE;
        }

        return {
            isPingLate: inactiveTime > PING_LATE_TIMEOUT,
            isPingLost: inactiveTime > PING_LOST_TIMEOUT
        };
    }

    return {
        connectionInfo: connectionInfo
    };
});