-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
326 lines (312 loc) · 12.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
require("dotenv").config();
const fs = require("fs");
const Promise = require("promise");
const readFile = Promise.denodeify(fs.readFile);
const http = require("http");
const Particle = require("particle-api-js");
const sqlite3 = require("better-sqlite3");
const express = require("express");
const path = require("path");
const chalk = require("chalk");
const Command = require("./command.js");
const WebSocket = require("ws");
const eventmodule = require("./event.js");
const config = require("./config.json");
const dbFile = config.database || "raw_events.sqlite3";
const particle = new Particle();
const password = process.env.PARTICLE_TOKEN;
const username = process.env.PARTICLE_USER;
var accessToken;
var eventDB;
const nodeArg = process.argv;
function devString(deviceId) {
return eventDB.devString(deviceId);
}
function startApp(db) {
console.log(chalk.gray("Starting application..."));
eventDB = new eventmodule.EventDatabase(db);
const app = createExpressApp(db);
// Do not connect to Particle cloud for playbackOnly
connectToParticleCloud(db, eventDB, nodeArg[2], nodeArg[3]);
try {
const port = config.port || "3000";
app.set("port", port);
const server = http.createServer(app);
createWebSocketServer(server, eventDB);
server.listen(port);
console.log(chalk.green("Server started: http://localhost:%s"), port);
} catch (err) {
console.error(err);
throw err;
}
}
function ensureDatabase() {
return new Promise(function (resolve, reject) {
fs.open(dbFile, "r", function (err, fd) {
if (err) {
console.log(chalk.gray("Creating database: %s"), dbFile);
readFile("schema.sql", "utf8")
.then(createDatabase)
.then(resolve, reject);
} else {
console.log(chalk.gray("Using existing database: %s"), dbFile);
resolve(new sqlite3(dbFile));
}
});
});
}
function createDatabase(schema) {
return new Promise(function (resolve, reject) {
try {
var db = new sqlite3(dbFile);
db.prepare(schema).run();
resolve(db);
} catch (err) {
reject(err);
}
});
}
function createExpressApp() {
var app = express();
// app.use(app.router);
// app.use(express.logger());
app.use(express.static(path.join(__dirname, "public")));
app.use("/", express.static(path.join(__dirname, "index.html")));
app.get("/device/:id", function (req, res) {
var generationId = req.query.generation;
var serialNo = req.query.since;
var sql =
"select * from raw_events where device_id = ? and generation_id = ? and serial_no > ?";
var params = [req.params.id, generationId, serialNo];
try {
const rows = db.prepare(sql).all(params);
var events = rows.map(function (row) {
return {
coreid: row.device_id,
published_at: row.published_at,
name: "collector/replay",
data: row.raw_data,
};
});
res.setHeader("Content-Type", "text/plain");
res.send(JSON.stringify(events));
} catch (err) {
console.log(chalk.red(err));
return res.send(500, err);
}
});
return app;
}
function connectToParticleCloud(db, eventDB, streamOption, replayOption) {
particle
.login({
username,
password,
})
.then(
function (data) {
accessToken = data.body.access_token;
console.log(
chalk.gray("Login to cloud completed. Listing devices...")
);
particle.listDevices({ auth: accessToken }).then(
function (Devices) {
const myDevices = Devices.body;
console.log(
chalk.gray("Got %s devices from cloud."),
myDevices.length
);
myDevices.forEach(function (dev) {
eventDB.setAttributes(dev.id, dev);
});
if (replayOption === "allDeviceReplay") {
requestAllDeviceReplay(db);
}
if (streamOption !== "noStream") {
openStream(db, eventDB);
}
},
function (err) {
console.log(
chalk.red("List devices call failed: %s"),
err
);
connectToParticleCloud();
}
);
},
function (err) {
console.log(chalk.red("Login failed: %s"), err);
}
);
}
function openStream(db, eventDB) {
console.log(chalk.gray("Connecting to event stream."));
particle
.getEventStream({ deviceId: "mine", auth: accessToken })
.then(function (stream) {
stream.on("event", function (event) {
console.log("Event: ", event);
if (event.code == "ETIMEDOUT") {
console.error(chalk.red(Date() + " Timeout error"));
} else {
eventDB.handleEvent(event);
}
});
})
.catch(function (err) {
console.log(chalk.red("Stream failed: %s"), err);
});
}
function requestAllDeviceReplay(db) {
var sql =
"select raw_events.device_id as device_id, raw_events.generation_id as generation_id, max(raw_events.serial_no) as serial_no from raw_events, (select device_id, max(generation_id) as generation_id from raw_events group by device_id) as gens where raw_events.device_id = gens.device_id and raw_events.generation_id = gens.generation_id group by raw_events.device_id";
console.log(chalk.blue("Requesting playback!!!"));
for (const row of db.prepare(sql).iterate()) {
try {
if (row.device_id == null) {
// Ignoring empty row returned by sqlite aggregate function on empty result.
console.log(chalk.gray("No devices to request a replay from."));
} else if (typeof row.generation_id === "undefined") {
console.error(
chalk.red(
"Got undefined generation for device %s. Don't know what to request. Waiting for new events. POSSIBLE DATA LOSS!"
),
row.device_id
);
} else {
requestDeviceReplay(
row.device_id,
row.generation_id,
row.serial_no
);
}
} catch (err) {
throw err;
}
}
}
// function requestAllSeasonReplay(db) {
// var sql = "select raw_events.device_id as device_id, raw_events.generation_id as generation_id, min(raw_events.serial_no) as serial_no from raw_events, (select device_id, min(generation_id) as generation_id from raw_events group by device_id) as gens where raw_events.device_id = gens.device_id and raw_events.generation_id = gens.generation_id group by raw_events.device_id";
// console.log(chalk.blue('Requesting playback!!!'));
// for (const row of db.prepare(sql).iterate()) {
// try {
// if (row.device_id == null) {
// // Ignoring empty row returned by sqlite aggregate function on empty result.
// console.log(chalk.gray("No devices to request a replay from."));
// } else if (typeof row.generation_id === "undefined") {
// console.error(chalk.red("Got undefined generation for device %s. Don't know what to request. Waiting for new events. POSSIBLE DATA LOSS!"), row.device_id);
// } else {
// requestDeviceReplay(row.device_id, row.generation_id, row.serial_no);
// }
// } catch (err) {
// throw err;
// }
// };
// }
function requestDeviceReplay(deviceId, generationId, serialNo) {
console.log(
chalk.gray("Requesting replay on %s at %s,%s"),
devString(deviceId),
generationId,
serialNo
);
particle.getDevice(deviceId, function (err, device) {
if (device != null) {
device.callFunction(
"replay",
"" + serialNo + ", " + generationId,
function (err, data) {
// Return codes:
// 0 Success
// -1 Failure
// -2 A replay is already in progress
// -99 Invalid generation ID
if (err) {
console.error(
chalk.red(
"Replay request failed: '%s' on %s at %s,%s with data: %s. EVENTS MAY BE LOST! Ensure the device is online, then restart the collector to request a new replay."
),
err,
devString(deviceId),
generationId,
serialNo,
data
);
} else {
if (data.return_value == 0) {
console.log(
chalk.green("Replay request on %s successful."),
devString(deviceId)
);
} else {
console.log(
chalk.yellow(
"Replay request refused by %s at %s,%s with code %s. EVENTS MAY BE LOST! Waiting for events."
),
devString(deviceId),
generationId,
serialNo,
data.return_value
);
}
}
}
);
}
});
}
function createWebSocketServer(server, eventDB, commandHandler) {
const wss = new WebSocket.Server({ server: server });
const connectedClients = [];
wss.on("connection", function connection(ws, req) {
connectedClients.push(ws);
console.log(
chalk.gray(
new Date() +
" Connection accepted. Connections: " +
connectedClients.length
),
req.socket.remoteAddress
);
ws.on("message", function incoming(message) {
const db = new sqlite3(dbFile, { readonly: true });
try {
console.log(chalk.gray("Received Message: %s"), message);
var command = JSON.parse(message);
const commandHandler = Command.CommandHandler(
db,
config.blacklist
);
commandHandler.onCommand(command, ws);
} catch (exception) {
console.error(chalk.red(exception));
db.close();
}
});
ws.on("close", function (reasonCode, description) {
connectedClients.splice(connectedClients.indexOf(connection), 1);
console.log(
chalk.gray(
new Date() +
" Peer " +
connection.remoteAddress +
" disconnected. Connections: " +
connectedClients.length
)
);
});
});
eventDB.onEvent(function (event) {
connectedClients.forEach(function (connection) {
if (connection.subscribed) {
connection.send(JSON.stringify(event));
}
});
});
}
ensureDatabase()
.then(startApp)
.catch(function (err) {
console.error(chalk.red(err));
});