DisplayImpl.java 3.13 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.socketio.SocketIOEmitter
 *  com.adobe.granite.socketio.SocketIONamespace
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.cq.screens.sessions.impl;

import com.adobe.cq.screens.sessions.Device;
import com.adobe.cq.screens.sessions.Display;
import com.adobe.cq.screens.sessions.ScreensSession;
import com.adobe.cq.screens.sessions.impl.ScreensManagerImpl;
import com.adobe.cq.screens.sessions.impl.ScreensSessionImpl;
import com.adobe.granite.socketio.SocketIOEmitter;
import com.adobe.granite.socketio.SocketIONamespace;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class DisplayImpl
implements Display {
    private final String id;
    private final ScreensManagerImpl mgr;
    private final List<Device> devices = new LinkedList<Device>();
    private ScreensSessionImpl session;

    public DisplayImpl(String id, ScreensManagerImpl mgr) {
        this.id = id;
        this.mgr = mgr;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public List<Device> getDevices() {
        return this.devices;
    }

    @Override
    public ScreensSession getSession() {
        return this.session;
    }

    public ScreensSessionImpl getOrCreateSession(SocketIONamespace nsp) {
        if (this.session == null) {
            this.session = new ScreensSessionImpl(this, this.mgr);
            this.session.open(nsp.to(new String[]{this.id}));
        }
        return this.session;
    }

    @Override
    public JSONObject toJSON() {
        try {
            JSONObject obj = new JSONObject();
            obj.put("id", (Object)this.id);
            JSONArray d = new JSONArray();
            obj.put("devices", (Object)d);
            for (Device device : this.devices) {
                d.put((Object)device.toJSON());
            }
            if (this.session != null) {
                HashMap<String, Object> userData;
                ScreensManagerImpl.AuthInfo authInfo = this.session.getAuthInfo();
                if (authInfo != null) {
                    JSONObject auth = new JSONObject();
                    auth.put("userId", (Object)authInfo.getUserId());
                    auth.put("tokenId", (Object)authInfo.getTokenId());
                    obj.put("auth", (Object)auth);
                }
                if (!(userData = new HashMap<String, Object>(this.session.getUserData())).isEmpty()) {
                    obj.put("data", userData);
                }
            }
            return obj;
        }
        catch (JSONException e) {
            throw new IllegalStateException((Throwable)e);
        }
    }

    public void close() {
        if (this.session != null) {
            this.session.close();
        }
        this.session = null;
    }
}