-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.js
108 lines (95 loc) · 2.84 KB
/
main.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
var S4 = require('./s4');
var peripheral = require('ble-cycling-power');
var network = require('./network');
var usb = require('./usb-peripheral');
var mainBle = function(testMode) {
var ble = new peripheral.BluetoothPeripheral('WaterRower S4');
if (testMode) {
var rower = new S4();
rower.fakeRower(ble.notify);
} else {
var listener = new network.MessageListener(ble.notify);
listener.start();
}
};
var mainUsb = function(callback, testMode) {
var rower = new S4();
if (testMode) {
rower.fakeRower(callback());
} else {
rower.findPort().then(function() {
rower.startRower(callback())();
}, function() {
// wait till we get the right serial
console.log('[Init] Awaiting WaterRower S4.2 to be connected to USB port');
// monitor USB attach and detach events
var usbPeripheral = new usb.UsbPeripheral();
usbPeripheral.monitorWr(function() {
rower.startRower(callback());
}, rower.stopRower(rower));
});
}
};
var main = function(args) {
var runMode = args[2];
var testMode = args[3] === '--test';
if (runMode === 'usb') {
var broadcasterNotify = function() {
var broadcaster = new network.MessageBroadcaster();
broadcaster.start();
return broadcaster.send;
};
mainUsb(broadcasterNotify, testMode);
} else if (runMode === 'ble') {
mainBle(testMode);
} else if (runMode === 'ant') {
var antNotify = function() {
var ant = require('ant-cycling-power');
var pm = new ant.PowerMeter();
var ts = 0;
var rev_count = 0;
return function(event) {
if ("watts" in event) {
if (ts == 0) {
ts = Date.now();
} else {
var now = Date.now();
var delta = (now - ts) / 1000 / 60;
ts = now;
var revs = event.rev_count - rev_count;
rev_count = event.rev_count;
var cadence = Math.round(revs / delta);
pm.broadcast(event.watts, cadence);
}
}
};
};
mainUsb(antNotify, testMode);
} else {
var listener = function() {
var ble = new peripheral.BluetoothPeripheral('WaterRower S4');
var ant = require('ant-cycling-power');
var pm = new ant.PowerMeter();
var ts = 0;
var rev_count = 0;
return function(event) {
if ("watts" in event) {
if (ts == 0) {
ts = Date.now();
} else {
var now = Date.now();
var delta = (now - ts) / 1000 / 60;
ts = now;
var revs = event.rev_count - rev_count;
rev_count = event.rev_count;
var cadence = Math.round(revs / delta);
pm.broadcast(event.watts, cadence);
ble.notify(event);
}
}
};
};
mainUsb(listener, runMode === '--test');
}
};
main(process.argv);