ctucx.git: smartie-pwa

[js] smarthome web-gui

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 
"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();