-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive.js
61 lines (49 loc) · 1.67 KB
/
live.js
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
const host = 'wss://websocket-api.tibber.com/v1-beta/gql/subscriptions';
var homeId = '96a14971-525a-4420-aae9-e5aedaa129ff' // demo id
var tibberToken = '5K4MVS-OjfWhK_4yrjOlFe1F6kJXPVf7eQYggo8ebAE' // demo token
function createId() {
function _p8(s) {
var p = (Math.random().toString(16) + "000000000").substr(2, 8);
return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p;
}
return _p8() + _p8(true) + _p8(true) + _p8();
}
const id = createId()
console.log(id)
const options = {
headers: {
"User-Agent": window.navigator.userAgent
}
};
let ws = new WebSocket(host, 'graphql-transport-ws', options);
ws.onopen = function(){
json= '{"type":"connection_init","payload":{"token":"'+tibberToken+'"}}';
ws.send(json)
}
ws.onmessage = function(msg) {
//console.log(msg)
reply = JSON.parse(msg.data)
console.log(msg.data)
if (reply["type"] == "connection_ack") {
console.log(reply["type"])
query = '{"id": "'+id+'","type": "subscribe","payload": {"query": "subscription{liveMeasurement(homeId:\\\"'+homeId+'\\\"){timestamp power powerProduction}}"}}';
ws.send(query);
}
if (reply["type"] == "next") {
console.log(reply["type"])
display.innerHTML = 'live Consumption: ' + reply["payload"]["data"]["liveMeasurement"]["power"] + '</br>live Production: ' + reply["payload"]["data"]["liveMeasurement"]["powerProduction"]
}
}
ws.addEventListener('error', (event) => {
console.log('WebSocket error: ', event);
});
function stopSocket() {
try {
query = '{"id": "'+id+'","type": "stop"}'
ws.send(query)
console.log("Client stopped")
display.innerHTML = 'Client stopped'
} catch (e) {
console.log("Stop error")
}
}