-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor-server.js
118 lines (102 loc) · 3.4 KB
/
sensor-server.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
const http = require('http');
const ws = require('ws');
const Sensor = require("./Sensor.js");
const httpPort = process.env.npm_config_http_port ?? 5004;
const serialport = process.env.npm_config_serial_port ?? 'COM3';
let server = http.createServer((req, res) => {
res.writeHead(200);
});
server.listen(httpPort, () => console.log('Started server on', httpPort));
const wss = new ws.Server({server, path: '/fluorescence-sensor'});
wss.on('connection', handleConnection);
let connections = new Array;
let sensor = new Sensor();
sensor.init(serialport);
sensor.parser.on('data', handleData);
sensor.port.pause();
let counter = 0;
let measurement = Array(4095).fill(0);
let startTime = getCurrentDate();
function handleData(buffer) {
let numbers = JSON.parse(JSON.stringify(buffer)).data;
let result = getResult(numbers);
if (result === -1) {
return;
}
measurement[result]++;
counter++;
// Broadcast result every 100 counts
if(counter % 100 === 0) {
broadcast(JSON.stringify(measurement));
console.log('Broadcasted data');
console.log('Start time: ' + startTime);
console.log('Current time: ' + getCurrentDate());
console.log('Total counts: ' + counter);
}
}
function getResult(numbers) {
let result = -1;
// MCA returns total 5 numbers, first three are 77, 67, 65 and last two are measurement result
if (numbers[0] === 77 && numbers[1] === 67 && numbers[2] === 65) {
result = numbers[3] * 256 + numbers[4];
}
if (numbers[1] === 77 && numbers[2] === 67 && numbers[3] === 65) {
result = numbers[4] * 256 + numbers[0];
}
if (numbers[2] === 77 && numbers[3] === 67 && numbers[4] === 65) {
result = numbers[0] * 256 + numbers[1];
}
if (numbers[3] === 77 && numbers[4] === 67 && numbers[0] === 65) {
result = numbers[1] * 256 + numbers[2];
}
if (numbers[4] === 77 && numbers[0] === 67 && numbers[1] === 65) {
result = numbers[2] * 256 + numbers[3];
}
if (result > 4095) {
result = -1;
}
return result;
}
function broadcast(data) {
for (connection in connections) {
connections[connection].send(data);
}
}
function handleConnection(client) {
console.log('New connection');
connections.push(client);
client.on('error', error => {
console.log(error);
});
client.on('message', (message) => {
handleCommand(message);
});
client.on('close', () => {
console.log('Connection closed');
let position = connections.indexOf(client);
connections.splice(position, 1);
});
}
async function handleCommand(message) {
message = message.toString();
message = message.replace(/['"]+/g, '');
if (message === 'resume') {
console.log('Message ', message);
sensor.resume();
}
if (message === 'reset') {
console.log('Message ', message);
measurement = Array(4095).fill(0);
counter = 0;
}
if (message === 'pause') {
console.log('Message ', message);
sensor.pause();
}
}
function getCurrentDate() {
var today = new Date();
var date = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);
var time = ('0' + today.getHours()).slice(-2) + ":" + ('0' + today.getMinutes()).slice(-2) + ":" + ('0' + today.getSeconds()).slice(-2);
return date + ' ' + time
}