-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsClient.js
More file actions
44 lines (37 loc) · 1.17 KB
/
wsClient.js
File metadata and controls
44 lines (37 loc) · 1.17 KB
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
let wsClient = function (scaleData, scaleSm) {
const WebSocket = require('ws');
const ws = new WebSocket('https://4f568726.ngrok.io');
ws.on('open', function open() {
console.log('WS scale connected');
});
ws.on('message', function incoming(data, flags) {
// console.log(data.match(scaleRe)[0]);
//Keep scaleData at 1023
scaleData.shift();
let currWeight = data.match( /(-)?\d+\.\d+/)[0];
scaleData.push(currWeight);
//average -- not sure if necessary
let average = 0;
let avgLength = 10;
scaleData.slice(scaleData.length - avgLength).forEach( (data) => {
average += parseFloat(data);
});
average = average/avgLength;
scaleSm.setNextState(average);
if ( scaleSm.transitionReady() ) {
scaleSm.transition();
}
// console.log(`weight: ${currWeight}`);
// console.log(`currState: ${scaleSm.getCurrState()}`);
// console.log(`nextState: ${scaleSm.getNextState()}`);
});
ws.on('close', function close() {
console.log('WS scale closed');
});
process.on('SIGINT', function() {
console.log('Closing WS connection.');
ws.close();
process.exit();
});
};
module.exports = wsClient;