device.js
5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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
};
});