-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
132 lines (114 loc) · 3.7 KB
/
app.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const queries = require('./queries'),
ws = require('ws'),
ApolloBoost = require('apollo-client'),
ApolloClient = ApolloBoost.default,
{ GraphQLClient } = require('graphql-request'),
{ WebSocketLink } = require("apollo-link-ws"),
{ InMemoryCache } = require("apollo-cache-inmemory"),
mqtt = require('mqtt'),
fs = require('fs'),
util = require('util');
var logFile = fs.createWriteStream(__dirname + '/debug.log', { flags: 'w' });
var logStdout = process.stdout;
let timeout = new Date().getTime();
console.log = function (d) {
const now = new Date().toISOString();
logFile.write(now + '- ' + util.format(d) + '\n');
logStdout.write(now + '- ' + util.format(d) + '\n');
};
let _clients = [];
const mqttClient = mqtt.connect(process.env.TIBBER_MQTT);
let isMqttConnected = false;
mqttClient.on('connect', function () {
console.log('connected to mqtt server: ' + process.env.TIBBER_MQTT);
isMqttConnected = true;
});
mqttClient.on('close', function () {
isMqttConnected = false;
});
mqttClient.on('error', function () {
isMqttConnected = false;
});
function getDefaultToken() {
console.log('using default token');
return '476c477d8a039529478ebd690d35ddd80e3308ffc49b59c65b142321aee963a4';
}
function getClient(token) {
if (!token)
token = getDefaultToken();
if (!token)
throw new Error("Access token not set");
if (!_clients[token])
_clients[token] = new GraphQLClient('https://api.tibber.com/v1-beta/gql', {
headers: {
Authorization: `Bearer ${token}`,
},
});
return _clients[token];
}
async function getHomes(token) {
let client = getClient(token);
return client.request(queries.getHomesQuery())
.then(data => {
return data;
})
.catch(e => {
console.error('Error while fetching data', e);
});
}
function subscribeToLive(token, homeId, callback) {
if (!token)
token = getDefaultToken();
if (!token)
throw new Error("Access token not set");
const wsLink = new WebSocketLink({
uri: 'wss://api.tibber.com/v1-beta/gql/subscriptions',
options: {
reconnect: true,
connectionParams: {
token: token,
}
},
webSocketImpl: ws
});
const wsClient = new ApolloClient({
link: wsLink,
cache: new InMemoryCache()
});
wsClient.subscribe({
query: queries.getSubscriptionQuery(homeId),
variables: {}
}).subscribe(callback, function (err) {
console.log('subscribe error', err);
});
}
(function () {
process.on('uncaughtException', function (e) {
console.log(e);
process.exit(1);
});
setInterval(() => {
const now = new Date().getTime();
if ((now - timeout) > (1000 * 60)) {
console.log('more than 1 minute since last update. exiting to try again.')
process.exit(1);
}
}, 2000);
const token = process.env.TIBBER_TOKEN || getDefaultToken();
getHomes(token).then(function (res) {
const primaryHome = res.viewer.homes[0];
const isPulseEnabled = primaryHome.features.realTimeConsumptionEnabled;
console.log('pulse enabled', isPulseEnabled);
if (!isPulseEnabled) {
console.error('Pulse is not enabled in this home.');
return;
}
subscribeToLive(token, primaryHome.id, function (result) {
console.info(result);
timeout = new Date().getTime();
if (isMqttConnected) {
mqttClient.publish(process.env.TIBBER_MQTT_TOPIC || 'ams', JSON.stringify(result));
}
})
});
})();