-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (54 loc) · 1.65 KB
/
index.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
// Most of this is just pulled from the ambient-weather-api example
const AmbientWeatherApi = require('ambient-weather-api');
const WebSocket = require('ws');
// set of all connections to WS server
let connections = new Set();
// lastData received
let lastData = null;
const verboseLog = (message) => {
// should be a command line argument, but I don't want to bring in all the parsing
// this will mostly be a docker thing, so having an extra environment variable for this seems ok
if (process.env.AMBIENT_VERBOSE) {
console.log(message);
}
};
const sendUpdate = (data) => {
lastData = data;
connections.forEach((conn) => {
conn.send(JSON.stringify(data));
});
}
const apiKey = process.env.AMBIENT_API_KEY;
const applicationKey = process.env.AMBIENT_APPLICATION_KEY;
if (!apiKey || !applicationKey) {
console.error('Missing Ambient Weather API or Application Key');
process.exit();
}
const wsPort = parseInt(process.env.AMBIENT_WS_PORT) || 8080
const wss = new WebSocket.Server({ port: wsPort });
wss.on('connection', (ws) => {
connections.add(ws);
if (lastData) {
ws.send(JSON.stringify(lastData));
}
});
wss.on('close', (ws) => {
connections.delete(ws);
});
const api = new AmbientWeatherApi({
apiKey,
applicationKey: applicationKey
});
api.connect();
api.on('connect', () => verboseLog('Connected to Ambient Weather Realtime API!'));
api.on('subscribed', data => {
verboseLog(`subscribed: `);
verboseLog(data);
sendUpdate(data.devices[0].lastData);
});
api.on('data', data => {
verboseLog(`received: `);
verboseLog(data);
sendUpdate(data);
});
api.subscribe(apiKey);