"use strict"; class State { constructor() { this.connected = false; this.data = JSON.parse(localStorage.getItem("data") || "{}"); this.config = JSON.parse(localStorage.getItem("config") || JSON.stringify({ views: [ { type: "noconfig", url: "noconfig", name: "Start", icon: "settings" } ] })); this.accessToken = localStorage.getItem("accessToken"); this.data.lastUpdated = new Date(this.data.lastUpdated); this._subscribers = []; if (this.accessToken) this._initWS(); } subscribe(callback) { this._subscribers.push(callback); this._updateSubscribers(); } _setData(newData) { if (newData.data && newData.data.views) { console.log("received config: ", newData); this.config = newData.data; localStorage.setItem("config", JSON.stringify(this.config)); } else if (!newData.status) { this.data = newData; this.data.lastUpdated = new Date(); localStorage.setItem("data", JSON.stringify(this.data)); } else { if (newData.data && newData.data.startsWith("invalid accessToken")) { this.accessToken = null; } console.log("received response: ", newData); return; } this._updateSubscribers(); } _updateSubscribers() { for (let sub of this._subscribers) { sub(); } } _initWS() { this.ws = new WebSocket(`${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.hostname}/${window.location.pathname.split("/").slice(0, -1).join("/")}/ws`); // this.ws = new WebSocket('ws://localhost:5000/ws'); this.ws.onclose = () => { this.connected = false; this._initWS(); this._updateSubscribers(); console.log("connection was closed!") }; this.ws.onopen = () => { clearInterval(this._timeout); this.connected = true; this._updateSubscribers(); this.ws.send(JSON.stringify({ type: "GetClientConfigAction", deviceName: "", configName: "smarthome-pwa", accessToken: this.accessToken })); this.ws.send(JSON.stringify({ type: "SetSubscriptionStateAction", deviceName: "", subscribed: true, accessToken: this.accessToken })); }; this.ws.onmessage = (msg) => { clearInterval(this._timeout); this._timeout = setTimeout(() => { this.ws.close(); this.ws.onclose(); console.log("got no ping!") }, 5000); if (!msg.data.length) return; // keepalive this._setData(JSON.parse(msg.data)); }; } setAccessToken(newToken) { this.accessToken = newToken; localStorage.setItem("accessToken", this.accessToken); this._updateSubscribers(); this._initWS(); } } export const state = new State();