adminview-system.js 6.45 KB
/*
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2014 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 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.
 */

define('screens/player/ui/adminview-system', [
    'underscore',
    'jquery',
    'screens/player/shared/serviceadmin',
    'screens/player/store/store',
    'screens/player/firmware/core/statusmodel/statusmodel',
    'screens/player/firmware/deviceinfo/deviceinfo',
    'backbone',
    './baseview'
], function(_, $, ServiceAdmin, Store, StatusModel, DeviceInfo, Backbone, BaseView) {
    'use strict';

    var DEFAULT_OPTIONS = {
    };

    function getUptime(duration) {
        var DAYS = 60 * 60 * 24;
        var HOURS = 60 * 60;
        var MINUTES = 60;

        var seconds = Math.round(duration / 1000);

        var days = Math.floor(seconds / DAYS);
        seconds -= days * DAYS;

        var hours = Math.floor(seconds / HOURS);
        seconds -= hours * HOURS;

        var minutes = Math.floor(seconds / MINUTES);
        seconds -= minutes * MINUTES;

        return {
            days: days,
            hours: hours,
            minutes: minutes,
            seconds: seconds
        };
    }

    var SystemAdminView = BaseView.extend(/** @lends SystemAdminView.prototype */{

        template: function(state) {
            var nextReboot = state.statusModel.nextReboot ? new Date(state.statusModel.nextReboot).toString() : null;
            var dim = state.statusModel.deviceDimensions;
            var res = 'Viewport: (' + dim.viewportWidth + 'x' + dim.viewportHeight + ')';
            res += ' Device: (' + dim.deviceWidth + 'x' + dim.deviceHeight + ')';

            var obj = _.extend({
                resolution: res,
                server: '(unknown)',
                bridgeState: '(unknown)',
                version: '(unknown)',
                device_id: state.statusModel.deviceId || '(not registered)',
                display_info: state.statusModel.displayPath || '(not assigned)',
                deviceInfo: state.deviceInfo,
                uptime: getUptime(Date.now() - state.statusModel.startTimestamp),
                nextRebootStr: nextReboot
            }, state.statusModel);

            var html = '<h1>System Information</h1>' +
                '<h2>AEM Screens Server</h2><h3 class="aem-ScreensPlayer-systemView-serverUrl">' + (obj && obj.server) + '</h3>' +
                '<h2>Device ID</h2><h3 class="aem-ScreensPlayer-systemView-deviceId">' + (obj && obj.device_id) + '</h3>' +
                '<h2>Display</h2><h3 class="aem-ScreensPlayer-systemView-displayInfo">' + (obj && obj.display_info) + '</h3>' +
                '<h2>Device Info</h2><h3 class="aem-ScreensPlayer-systemView-deviceModel">' + (obj.deviceInfo && obj.deviceInfo.os) + ' ' + (obj.deviceInfo && obj.deviceInfo.osVersion) + ' ' + '(' + (obj.deviceInfo && obj.deviceInfo.model) + ')</h3>' +
                '<h2>Firmware Version</h2><h3 class="aem-ScreensPlayer-systemView-firmwareVersion">' + (obj && obj.version) + '</h3>' +
                '<h2>WebView</h2><h3 class="aem-ScreensPlayer-systemView-webView">' + (obj.deviceInfo && obj.deviceInfo.webView) + '</h3>' +
                '<h2>Resolution</h2><h3 class="aem-ScreensPlayer-systemView-resolution">' + (obj && obj.resolution) + '</h3>' +
                '<h2>Status</h2><h3 class="aem-ScreensPlayer-systemView-bridgeState">' + (obj && obj.bridgeState) + '</h3>' +
                '<h2>Uptime</h2>' +
                '<h3 class="aem-ScreensPlayer-systemView-uptime">' +
                    ((obj && (obj.uptime.days)) ? obj.uptime.days : '') +
                    ((obj && (obj.uptime.days || obj.uptime.hours)) ? obj.uptime.hours + 'h ' : '') +
                    ((obj && (obj.uptime.days || obj.uptime.hours || obj.uptime.minutes)) ? obj.uptime.minutes + 'm ' : '') +
                    ((obj && obj.uptime.seconds) + 's') +
                '</h3>' +
                ((obj && obj.nextRebootStr) ? ('<h2>Next Reboot</h2><h3 class="aem-ScreensPlayer-systemView-nextRebootStr">' + obj.nextRebootStr + '</h3>') : '');
            return html;
        },

        className: 'aem-ScreensPlayer-admin-view',

        events: {
        },

        /**
         * @classdesc View that renders the admin menu
         * @class AdminView
         * @extends BaseView
         *
         * @param {Object} [options] An object of configurable options.
         */
        constructor: function(options) {
            this._initOptions(options, DEFAULT_OPTIONS);
            SystemAdminView.__super__.constructor.apply(this, arguments);

            this.$el.on('show', this.onShow.bind(this));
            this.$el.on('hide', this.onHide.bind(this));
        },

        /**
         * Event handler for when this view's contents are hidden
         */
        onHide: function() {
            var store = ServiceAdmin.getService(Store.serviceName);
            if (this._statusModelChangeListener) {
                store.unsubscribe(this._statusModelChangeListener);
                this._statusModelChangeListener = null;
            }
        },

        /**
         * Event handler for when this view's contents are shown
         */
        onShow: function() {
            var store = ServiceAdmin.getService(Store.serviceName);
            if (this._statusModelChangeListener) {
                store.unsubscribe(this._statusModelChangeListener);
            }

            this._statusModelChangeListener = store.subscribe(function(statusModel) {
                this.render({
                    statusModel: statusModel,
                    deviceInfo: store.getState()[DeviceInfo.NAMESPACE]
                });
            }.bind(this), StatusModel.NAMESPACE);

            this.render({
                statusModel: store.getState()[StatusModel.NAMESPACE],
                deviceInfo: store.getState()[DeviceInfo.NAMESPACE]
            });
        },


        render: function(state) {
            if (state) {
                this.$el.html(this.template(state));
            }

            return this;
        }

    });

    // return module exports
    return SystemAdminView;
});