-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLDServer.js
491 lines (430 loc) · 14.1 KB
/
OLDServer.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
const express = require("express");
const app = express();
const server = require("http").Server(app);
const IO = require("socket.io")(server);
const mongod = require("mongod");
const MongoClient = require("mongodb").MongoClient;
const config = require("./config");
const Storage = require("./Storage");
const Clock = require("./www/clock");
const usb = require("usb");
const drivelist = require("drivelist");
const fs = require("fs");
var DB = { initialized: false };
var storage = new Storage();
var storageDevices = [];
var removablesConnected = 0;
var mountInterval;
var mountTimer;
var time = 0;
var deviceSettingsFile = "settings.json";
var deviceSettingsDefaultFile = "defaultSettings.json";
var deviceSettings;
/* Commands are described by a receiver (sensor, pump or relay) and the command to send
* receivers are enumerated:
* sensor-0 = pH
* sensor-1 = EC
* pump-0
* pump-1
* relay-0
* relay-1
*
* sensors have predefined commands, described in their documentation, e.g.:
* C,1 = read the sensors value every second
* Cal,mid,7.00 = calibrate the sensor's mid pH value to 7.00
*
* for pumps we use self-defined commandos:
* 1 = on
* 1,1000 = on for 1000 ms
* 0 = off
*
* A command to put the pump on for 5 seconds would look like:
* pump-0 1,5000
*
* A command to set the EC sensors name would look like:
* sensor-1 Name,Thomas
*/
function sendCommand(receiver, command) {
if (readMeasurementsOnly) return;
let commandString = receiver + " " + command + "\r";
//let commandString = "debug " + command.receiver + "," + command.name + "\r";
if (commandString == "") console.log("Command is empty");
// Respect the buffer size (30 bytes) defined in the Arduino code
if (commandString.length > 80)
console.log("TOO LONG / TOO MANY COMMANDS, WE LOST BYTES ON THE WAY !!!");
writeToArduino(commandString);
}
// Format and copy json to deviceSettings
function retrieveSettings(json) {
for (type in json) {
if (!!deviceSettings[type]) {
json[type].forEach((device, nr) => {
Object.assign(deviceSettings[type][nr], device);
});
}
}
}
// Write current settings to JSON file
function storeSettings() {
const json = JSON.stringify(deviceSettings, null, 2);
fs.writeFile(deviceSettingsFile, json, "utf8", (err) => {
if (err) console.log("Error storing settings: " + err);
});
}
// Adjust UI and send commands to apply new settings
function applySettings(json) {
for (type in deviceSettings) {
deviceSettings[type].forEach((device, nr) => {
if (device.control == "Manual") {
if (device.manualSwitch == true) {
// Turn device on if it is off
if (!device.isActive) {
sendCommand(type + "-" + nr, "1");
device.isActive = true;
}
} else {
// Turn device off if it is on
if (device.isActive) {
sendCommand(type + "-" + nr, "0");
device.isActive = false;
}
}
} else if (device.control == "Time")
// Test if the device should be on, based on the time configuration
checkTimeSettings(new Date().getHours());
});
}
}
// Load stored settings from JSON file
function loadSettings() {
// if no settings are stored, use default
let file = fs.existsSync(deviceSettingsFile)
? deviceSettingsFile
: deviceSettingsDefaultFile;
fs.readFile(file, "utf8", (err, data) => {
if (err) console.log("Error loading settings: " + err);
else {
// Decode the response
try {
deviceSettings = JSON.parse(data);
} catch (e) {
// Byte errors destroyed the format
console.log(data);
console.log(e);
}
}
});
}
// When received measurements are outside of their defined comfort zone,
// send commands to activate regulating devices
function measurementChanged(measurement, value) {
for (type in deviceSettings) {
deviceSettings[type].forEach((device, nr) => {
if (device.control == measurement) {
if (type == "relay") {
let activate = false;
// If we control high values (or both) and the value is too high, active
if (device.controlDir == "+" || device.controlDir == "+/-")
if (value > device.scales[measurement][3]) activate = true;
// If we control low values (or both) and the value is too low, activate
if (device.controlDir == "-" || device.controlDir == "+/-")
if (value < device.scales[measurement][2]) activate = true;
if (activate) {
if (!device.isActive) {
sendCommand(type + "-" + nr, "1");
device.isActive = true;
}
} // Deactivate
else {
if (device.isActive) {
sendCommand(type + "-" + nr, "0");
device.isActive = false;
}
}
} // Pump
else {
if (device.isActive) return;
// IMPORTANT: Currently scale might not be ordered,
// so don't check if the value is in the middle range ( val > measure 2 && val < measure 3),
// but check for + and - scales individually, if the value is inside their red ranges
// Check upper scale
if (device.controlDir == "+/-" || device.controlDir == "+")
if (value < device.scales[measurement][3]) return;
// Check lower scale
if (device.controlDir == "+/-" || device.controlDir == "-")
if (value > device.scales[measurement][2]) return;
let curTime = Date.now();
console.log(curTime - time);
time = curTime;
// Activate pump
sendCommand(type + "-" + nr, "1");
device.isActive = true;
// Activate for longer if the measure is further off the scale
let duration; // Time for which the pump will stay active
if (
value < device.scales[measurement][1] ||
value > device.scales[measurement][4]
)
duration = 2000;
else if (
value < device.scales[measurement][2] ||
value > device.scales[measurement][3]
)
duration = 1000;
// Deactivate pump after "duration" seconds
setTimeout(
(type, nr) => {
sendCommand(type + "-" + nr, "0");
},
duration,
type,
nr
);
// pause the pump for 30 seconds to avoid overcompensation before substances are mixed properly
let pause = 30000;
setTimeout(
(device) => {
device.isActive = false;
},
pause,
device
);
}
}
});
}
}
// Send commands for time-based control when a new hour starts
function checkTimeSettings(hour) {
for (var type in deviceSettings) {
deviceSettings[type].forEach((device, i) => {
if (device.control == "Time") {
// turn relay i on
if (device.times[hour]) sendCommand(type + "-" + i, "1");
// turn relay i off
else sendCommand(type + "-" + i, "0");
}
});
}
}
// Check for USB devices
usb.on("attach", function (device) {
// Try multiple times to establish the new device as storage, until it is mounted
// TODO: use interval per device connected based on device ID
mountInterval = setInterval(() => {
updateStorageDevices();
}, 500);
// If the device didn't mount within 10 seconds, stop trying
mountTimer = setTimeout(() => clearInterval(mountInterval), 10000);
});
usb.on("detach", function (device) {
updateStorageDevices();
});
function onDeviceMounted(drive) {
// Stop trying to mount the device
clearInterval(mountInterval);
// Check if there is a config file on the USB device
storage.config(drive.mountpoints[0].path);
// Copy all stored data to USB
if (drive.isRemovable)
storage.copyTo(drive.mountpoints[0].path + "/OLD-data");
}
function onDeviceUnmounted(drive) {
// Stop trying to mount if the device was removed too fast
clearInterval(mountInterval);
}
// Keep a list of connected devices
function updateStorageDevices() {
drivelist.list((error, drives) => {
if (error) throw error;
// Check if connected devices changed
let driveWasMounted = new Array(drives.length).fill(false);
let checked = new Array(storageDevices.length).fill(false);
for (var i = 0; i < drives.length; i++) {
for (var j = 0; j < storageDevices.length; j++) {
if (drives[i].description === storageDevices[j].description) {
driveWasMounted[i] = true;
checked[j] = true;
break;
}
}
}
// Treat newly connected devices
for (var i = 0; i < driveWasMounted.length; i++)
if (driveWasMounted[i] === false) onDeviceMounted(drives[i]);
// Treat disconnected devices
for (var i = 0; i < checked.length; i++)
if (checked[i] === false) onDeviceUnmounted(storageDevices[i]);
storageDevices = drives;
});
}
// Arduino communication
// If no Arduino is connected, emulate an Arduino
const mockArduino = config.arduino.mockingEnabled;
const serialport = mockArduino
? require("serialport/test")
: require("serialport");
var arduinoPortPath = config.arduino.portPath;
var arduinoBaudrate = config.arduino.baudRate;
const MockBinding = serialport.Binding; // Test base when no Arduino is connected
const readMeasurementsOnly = config.arduino.readMeasurementsOnly;
/* RUN AND CONNECT TO DATABASE */
const dbServer = new mongod(27017);
if (dbServer.isRunning) {
dbServer.open((err) => {
if (err === null) {
// You may now connect a client to the MongoDB
// server bound to port 27017.
} else console.log("Database error: ", err.message);
});
/* CONNECT DATABASE */
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("olddb");
dbo.createCollection("measurements", function (err, res) {
if (err) throw err;
console.log('Collection "measurements" exists!');
DB.measurements = dbo.collection("measurements");
DB.initialized = true;
});
});
}
function storeMeasurement(measure, value) {
if (DB.initialized) {
var doc = { name: measure, address: value };
DB.measurements.insertOne(doc, function (err, res) {
if (err) throw err;
console.log("1 document inserted");
});
}
}
function restoreMeasurements(measure) {
if (DB.initialized) {
var query = { address: "Temp" };
DB.measurements.find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});
}
}
updateStorageDevices();
loadSettings();
const clock = new Clock();
clock.callbackEveryHour = checkTimeSettings;
// Create a web server
app.use(express.static("www")); // Define website folder
app.use(
"/scripts",
express.static("node_modules/material-components-web/dist/")
);
app.get("/", handler);
server.listen(80, () => console.log("OLD server running on port 80!"));
// Send HTML file to client
function handler(req, res) {
res.sendFile("index.html");
}
// Send message to Arduino
function writeToArduino(message) {
arduino.write(message, function (err) {
if (err) console.log("Error on write: ", err.message);
else console.log("message sent: ", message);
});
}
// Get JSON messages from Arduino
function onDataReceived(data) {
// Do not try to parse json, if it definitely is no json
if (data[0] != "{") return;
let measures = null;
// Decode the response
try {
measures = JSON.parse(data);
} catch (e) {
// Byte errors, that destroy the format
console.log(data);
console.log(e);
}
if (measures) {
// Send newly measured values to clients
IO.emit("new measurements", measures);
// React to measurements that are out of their comfort zone
for (var m in measures) measurementChanged(m, measures[m]);
// Store measurements
storage.add(measures);
}
}
// If no Arduino is connected, create fake values for test purposes
if (mockArduino) {
arduinoPortPath = "arduinoMock";
MockBinding.createPort(arduinoPortPath, { echo: true, record: false });
setInterval(
() =>
onDataReceived(
"{" +
'"Temp":"' +
Math.random() * 10 +
'",' +
'"WaterTemp":"' +
Math.random() * 10 +
'",' +
'"PH":"' +
Math.random() * 10 +
'"' +
"}"
),
2000
);
}
// Establish a connection with the Arduino
const arduino = new serialport(arduinoPortPath, {
baudRate: arduinoBaudrate,
});
const arduinoParser = arduino.pipe(
new serialport.parsers.Readline({ delimiter: "\r\n" })
);
// When connection is established
arduino.on("open", function () {
console.log("Established connection to Arduino");
//writeToArduino('test');
//setInterval(() => writeToArduino('{"WaterTemp": 0.000,"Humidity": 450.000,"Temp": 0.000,"EC": 844.100,"TDS": 455.000,"SAL": 0.410,"GRAV": 1.000}\r\n'), 3000);
});
// When Arduino is sending data
/*
arduino.on('data', function(data)
{
console.log("received data: " + data);
});
*/
// When Arduino is sending data ending on \r\n
arduinoParser.on("data", function (data) {
console.log("received data: " + data);
onDataReceived(data);
});
// On error
arduino.on("error", function (error) {
console.log("Error in connection with Arduino: ");
console.log(error.message);
});
// When connection is closed
arduino.on("close", function () {
console.log("Lost connection to Arduino");
});
// Set up communication with clients
IO.sockets.on("connection", function (socket) {
console.log("Client connected");
// Send current settings to client
socket.emit("settingsApplied", deviceSettings);
// When client changed settings but cancels, send current settings to undo changes
socket.on("settingsCancelled", function () {
socket.emit("settingsApplied", deviceSettings);
});
// When client changes settings, apply them and synchronize with all other clients
socket.on("settingsChanged", function (json) {
if (!json) return;
retrieveSettings(json);
storeSettings();
applySettings();
// Send new settings to all clients but sender
socket.broadcast.emit("settingsApplied", deviceSettings);
});
});