device.js 5.83 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 com, org, request, resource, use, java */
use(['../../helper.js', '../../helper.format.unit.js', '../../helper.heartbeat.js'],
        function(helper, UnitFormatter, Heartbeat) {
    'use strict';

    var PRIMARY_PHOTO_PATH = '/profile/photos/primary/image';
    var PREFERENCES_PATH = '/profile_screens/preferences';
    var STATUS_INFO_PATH = '/profile_screens/statusinfo';
    var LOG_FILES = ['error.log'];

    function getUsageString(info) {
        if (!info || !info['usage.used']) {
            return 'n/a';
        }

        function fu(size) {
            return UnitFormatter.formatUnit(size, UnitFormatter.UNITS_DI_IEC, 1);
        }
        // %d free on storage %s
        // %d of %d used.
        var usageStr = fu(info['usage.free']) + ' free on storage \'' + info['usage.volume'] + '\'. ';
        usageStr += fu(info['usage.used']) + ' of ' + fu(info['usage.total']) + ' used';

        return usageStr;
    }

    var resourceResolver = resource.resourceResolver;

    // Retrieve the device resource and its properties from the request suffix
    var devicePath = request.requestPathInfo.suffix;
    if (!devicePath) {
        return null;
    }

    var deviceResource = resourceResolver.getResource(devicePath);
    var deviceProfileResource = deviceResource.getChild(helper.constants.device.DEVICE_PROFILE_NAME);
    if (!deviceProfileResource) {
        return null;
    }

    var deviceScreenshotResource = deviceProfileResource.getChild(helper.constants.device.DEVICE_SCREENSHOT);
    var screenshot = null;
    if (deviceScreenshotResource) {
        var jcrContent = deviceScreenshotResource.getChild('jcr:content');
        var data = null;
        var datetime = null;
        var prop = null;
        if (jcrContent) {
            prop = jcrContent.adaptTo(org.apache.sling.api.resource.ValueMap);

        } else {
            prop = deviceScreenshotResource.adaptTo(org.apache.sling.api.resource.ValueMap);
        }
        data = prop.get('jcr:data', '');
        datetime = prop.get('jcr:lastModified', java.util.Calendar);

        screenshot = {
            data: data || null,
            datetime: datetime
        };
    }

    var deviceLogsResource = deviceProfileResource.getChild(helper.constants.device.DEVICE_LOGS);
    var logs = null;
    if (deviceLogsResource) {
        logs = [];
        LOG_FILES.forEach(function() {
            var logResource = resourceResolver.getResource(deviceLogsResource, 'error.log/jcr:content');
            if (logResource) {
                logs.push({
                    name: logResource.getParent().getName(),
                    path: logResource.getParent().getPath(),
                    datetime: logResource.adaptTo(org.apache.sling.api.resource.ValueMap).get('jcr:lastModified', java.util.Calendar)
                });
            }
        });
    }

    var deviceProperties = deviceProfileResource.adaptTo(org.apache.sling.api.resource.ValueMap);
    if (!deviceProperties) {
        return null;
    }

    var statusInfoResource = resourceResolver.getResource(devicePath + STATUS_INFO_PATH);
    var statusInfo = statusInfoResource ? statusInfoResource.adaptTo(org.apache.sling.api.resource.ValueMap) : {};

    var preferencesResource = resourceResolver.getResource(devicePath + PREFERENCES_PATH);
    var preferences = preferencesResource ? preferencesResource.adaptTo(org.apache.sling.api.resource.ValueMap) : {};

    var deviceConfigPath = deviceProperties.get(helper.constants.device.DEVICE_CONFIG_PATH);
    var deviceConfig = deviceConfigPath && resourceResolver.getResource(deviceConfigPath);
    var display = deviceConfig && deviceConfig.parent.adaptTo(com.day.cq.wcm.api.Page);

    var profilePhoto = resourceResolver.getResource(devicePath + PRIMARY_PHOTO_PATH);

    var details = {};

    var device = deviceResource.adaptTo(com.adobe.cq.screens.device.Device);
    var deviceMgr = resourceResolver.adaptTo(com.adobe.cq.screens.device.DeviceManager);
    var deviceStatus = deviceMgr && deviceMgr.getDeviceStatus(deviceProperties.id);
    details.usageString = getUsageString(statusInfo);

    var ping = deviceStatus && deviceStatus.getLastPing();
    var lastPing = ping.getTime();
    details.ping = ping ? {
        lastPing: new Date(lastPing)
    } : {};
    details.timestamp = statusInfo.timestamp;
    details.remoteAddress = statusInfo[helper.constants.device.DEVICE_REMOTE_ADDRESS] || 'n/a';
    details.firmwareVersion = statusInfo.firmwareVersion;

    if (statusInfo.startTimestamp) {
        details.uptime = new Date(statusInfo.timestamp - statusInfo.startTimestamp);
    }

    return {
        properties: deviceProperties,
        connectionInfo: Heartbeat.connectionInfo(deviceStatus),
        deviceManualConfigUpdate: device && device.isManualConfigUpdate(),
        display: display,
        photo: profilePhoto && profilePhoto.path,
        preferences: { // Todo: retrieve preferences from the device
            adminUI: preferences.enableAdminUI,
            osd: preferences.enableOSD,
            remoteDebugging: preferences.enableRemoteInspect,
            resolution: preferences.resolution,
            server: preferences.server
        },
        details: details,
        screenshot: screenshot,
        logs: logs
    };
});