DeviceStatusWriter.java 13.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.mobile.ui.AppCacheDataSource
 *  com.day.cq.wcm.api.Page
 *  javax.annotation.CheckForNull
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.screens.impl;

import com.adobe.cq.mobile.ui.AppCacheDataSource;
import com.adobe.cq.screens.DeviceMessageQueue;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceConfig;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.cq.screens.device.DeviceStatus;
import com.adobe.cq.screens.impl.JcrJSONWriter;
import com.day.cq.wcm.api.Page;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DeviceStatusWriter {
    private final Logger log = LoggerFactory.getLogger(DeviceStatusWriter.class);
    private final ResourceResolver resolver;
    private final JSONWriter w;
    private DeviceMessageQueue queue;

    public DeviceStatusWriter(ResourceResolver resolver, JSONWriter w) {
        this.resolver = resolver;
        this.w = w;
    }

    private void dumpDeviceJson(@CheckForNull DeviceStatus s, @CheckForNull Device device, @CheckForNull DeviceConfig config) throws JSONException {
        this.w.key("timestamp").value(System.currentTimeMillis());
        if (device != null) {
            ValueMap deviceProps = (ValueMap)device.adaptTo(ValueMap.class);
            this.w.key("title").value(deviceProps.get((Object)"jcr:title"));
            this.w.key("description").value(deviceProps.get((Object)"jcr:description"));
            this.w.key("lastModified").value(deviceProps.get("cq:lastModified", (Object)0));
            this.w.key("lastModifiedBy").value(deviceProps.get((Object)"cq:lastModifiedBy"));
            this.w.key("model").value(deviceProps.get((Object)"model"));
            this.w.key("type").value(deviceProps.get((Object)"type"));
            this.w.key("resolution").value(deviceProps.get((Object)"resolution"));
            this.w.key("offsetLeft").value(deviceProps.get("offsetLeft", (Object)0));
            this.w.key("offsetTop").value(deviceProps.get("offsetTop", (Object)0));
            this.w.key("id").value((Object)device.getId());
            this.w.key("path").value((Object)device.getPath());
            Resource screenshot = ((Resource)device.adaptTo(Resource.class)).getChild("screenshot");
            if (screenshot != null) {
                ValueMap screenshotProp = (ValueMap)screenshot.adaptTo(ValueMap.class);
                this.w.key("lastScreenshot").value(screenshotProp.get((Object)"timestamp"));
            }
            this.w.key("offlineIndicator").value(deviceProps.get("offlineIndicator", (Object)""));
        }
        if (s != null) {
            this.w.key("heartbeat").value(s.getLastPing().getTime());
        }
        if (config != null) {
            ValueMap configProps = (ValueMap)config.adaptTo(ValueMap.class);
            this.w.key("zoneTemplate").value(configProps.get("zoneTemplate", (Object)""));
            String zoneMapping = (String)configProps.get("zoneMapping", (Object)"");
            if (zoneMapping.length() > 0) {
                this.w.key("zoneMapping").value((Object)new JSONObject(zoneMapping));
            }
        }
    }

    private void dumpDisplayJson(Resource display) throws JSONException {
        ValueMap displayProps = (ValueMap)display.adaptTo(ValueMap.class);
        Page displayPage = (Page)display.getParent().adaptTo(Page.class);
        this.w.key("path").value((Object)displayPage.getPath());
        this.w.key("title").value(displayProps.get((Object)"jcr:title"));
        this.w.key("description").value(displayProps.get((Object)"jcr:description"));
        this.w.key("lastModified").value(displayProps.get("cq:lastModified", (Object)0));
        this.w.key("lastModifiedBy").value(displayProps.get("cq:lastModifiedBy", (Object)""));
        this.w.key("activeChannel").value(displayProps.get((Object)"activeChannel"));
        this.w.key("resolution").value(displayProps.get((Object)"resolution"));
        this.w.key("layout").object();
        this.w.key("numCols").value(displayProps.get("layoutNumCols", (Object)1));
        this.w.key("numRows").value(displayProps.get("layoutNumRows", (Object)1));
        this.w.endObject();
        Resource dataResource = display.getChild("data");
        if (dataResource != null) {
            try {
                new JcrJSONWriter(this.w).ignore("jcr:primaryType").dumpSingleNode((Node)dataResource.adaptTo(Node.class), 0, 1);
            }
            catch (RepositoryException e) {
                this.log.error("Error while writing display data", (Throwable)e);
            }
        } else {
            this.w.key("data").object().endObject();
        }
        Resource channels = display.getChild("channels");
        if (channels != null) {
            ValueMap channelsProps = (ValueMap)channels.adaptTo(ValueMap.class);
            this.w.key("idleTitle").value(channelsProps.get("idleTitle", (Object)""));
            this.w.key("idleText").value(channelsProps.get("idleText", (Object)""));
            this.w.key("idleTimeout").value(channelsProps.get("idleTimeout", (Object)10));
            this.w.key("strategy").value(channelsProps.get("strategy", (Object)"normal"));
            this.w.key("transition").value(channelsProps.get("transition", (Object)"normal"));
            this.w.key("channels").array();
            Iterator allChannels = channels.listChildren();
            while (allChannels.hasNext()) {
                Resource channel = (Resource)allChannels.next();
                ValueMap channelProps = channel.getValueMap();
                String path = (String)channelProps.get("path", (Object)"");
                Resource channelResource = path.length() == 0 ? null : channel.getResourceResolver().getResource(path);
                Page channelPage = channelResource == null ? null : (Page)channelResource.adaptTo(Page.class);
                if (channelPage == null) continue;
                this.w.object();
                this.w.key("path").value((Object)path);
                this.w.key("role").value(channelProps.get((Object)"role"));
                this.w.key("name").value((Object)channel.getName());
                this.w.key("title").value((Object)channelPage.getTitle());
                this.w.key("events").array();
                String[] events = (String[])channelProps.get("events", String[].class);
                if (events != null) {
                    for (String e : events) {
                        this.w.value((Object)e);
                    }
                }
                this.w.endArray();
                this.w.key("conditions").array();
                String[] conditions = (String[])channelProps.get("conditions", String[].class);
                if (conditions != null) {
                    for (String c : conditions) {
                        this.w.value((Object)c);
                    }
                }
                this.w.endArray();
                this.w.key("schedule").value(channelProps.get("schedule", (Object)""));
                if (channelPage.getLastModified() != null) {
                    this.w.key("lastModified").value(channelPage.getLastModified().getTimeInMillis());
                }
                this.w.key("showTooltip").value(channelProps.get("showTooltip", (Object)false));
                this.w.key("subChannels").object();
                Iterator subChannels = channelPage.listChildren();
                while (subChannels.hasNext()) {
                    Page subChannel = (Page)subChannels.next();
                    this.w.key(subChannel.getName()).object();
                    this.w.key("name").value((Object)subChannel.getName());
                    this.w.key("path").value((Object)subChannel.getPath());
                    if (subChannel.getLastModified() != null) {
                        this.w.key("lastModified").value(subChannel.getLastModified().getTimeInMillis());
                    }
                    this.w.endObject();
                }
                this.w.endObject();
                String query = "/jcr:root" + path + "//element(*, cq:ContentSyncConfig)";
                Iterator contentSyncConfig = channel.getResourceResolver().findResources(query, "xpath");
                boolean offline = contentSyncConfig.hasNext();
                if (offline) {
                    boolean forceOnline = (Boolean)((ValueMap)channelPage.getContentResource().adaptTo(ValueMap.class)).get("forceRemoteContent", (Object)false);
                    this.dumpOfflineConfig(forceOnline, (Resource)contentSyncConfig.next());
                }
                this.w.endObject();
            }
        }
        this.w.endArray();
        Resource meta = display.getChild("meta");
        if (meta != null) {
            this.w.key("meta").object();
            ValueMap metaVm = meta.getValueMap();
            for (Map.Entry e : metaVm.entrySet()) {
                this.w.key((String)e.getKey()).value(e.getValue());
            }
            this.w.endObject();
        }
        this.w.key("devices").object();
        Iterator deviceConfigs = displayPage.listChildren();
        DeviceManager dm = (DeviceManager)display.getResourceResolver().adaptTo(DeviceManager.class);
        while (deviceConfigs.hasNext()) {
            Page deviceConfig = (Page)deviceConfigs.next();
            DeviceConfig cfg = (DeviceConfig)deviceConfig.adaptTo(DeviceConfig.class);
            if (cfg == null) {
                this.log.warn("Display {} has unknown device config page: {}", (Object)display.getPath(), (Object)deviceConfig.getPath());
                continue;
            }
            String deviceId = cfg.getAssignedDeviceId();
            if (deviceId == null) continue;
            Device device = dm.getDevice(deviceId);
            if (device == null) {
                this.log.warn("Display {} references unknown device: {}", (Object)display.getPath(), (Object)deviceId);
                continue;
            }
            this.w.key(device.getId()).object();
            this.dumpDeviceJson(dm.getDeviceStatus(device.getId()), device, device.getDeviceConfig());
            this.w.endObject();
        }
        this.w.endObject();
    }

    private void dumpOfflineConfig(boolean forceOnline, Resource contentSyncResource) throws JSONException {
        AppCacheDataSource appCacheDS = (AppCacheDataSource)contentSyncResource.adaptTo(AppCacheDataSource.class);
        if (appCacheDS != null) {
            try {
                JSONObject obj = appCacheDS.getCacheUpdates();
                long lastUpdated = obj.optLong("lastUpdated", 0);
                this.w.key("offline").object();
                this.w.key("enabled").value(!forceOnline);
                if (lastUpdated > 0) {
                    this.w.key("timestamp").value(lastUpdated);
                    this.w.key("zipPath").value((Object)(contentSyncResource.getPath() + ".zip"));
                }
                this.w.endObject();
            }
            catch (Exception e) {
                this.log.error("error while retrieving offline updates", (Throwable)e);
            }
        }
    }

    public void setMessageQueue(DeviceMessageQueue queue) {
        this.queue = queue;
    }

    private void dumpNextCommand(@CheckForNull Device device, JSONWriter w) throws JSONException {
        String command;
        if (device != null && (command = this.queue.dequeue(device.getId())) != null) {
            w.key("command").value((Object)command);
        }
    }

    public void writeDevice(@CheckForNull Device device, @CheckForNull DeviceStatus status, @CheckForNull DeviceConfig config) throws JSONException {
        this.w.object();
        this.w.key("device").object();
        this.dumpDeviceJson(status, device, config);
        this.w.endObject();
        if (config != null) {
            String displayPath = config.getDisplayPath();
            Resource displayResource = this.resolver.getResource(displayPath);
            Resource resource = displayResource = displayResource == null ? null : displayResource.getChild("jcr:content");
            if (displayResource != null && this.resolver.isResourceType(displayResource, "screens/core/components/display")) {
                this.w.key("display").object();
                this.dumpDisplayJson(displayResource);
                this.w.endObject();
            }
        }
        if (this.queue != null) {
            this.dumpNextCommand(device, this.w);
        }
        this.w.endObject();
    }

    public void writeDisplay(Resource resource) throws JSONException {
        this.w.object();
        this.w.key("display").object();
        this.dumpDisplayJson(resource);
        this.w.endObject();
        this.w.endObject();
    }
}