-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
201 lines (165 loc) · 5.67 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
const ant = require('ant-plus');
const osc = require('osc');
const config = require(process.cwd() + '/config.json');
const fs = require('fs');
const OUTPUT_FILE_PATH = 'sessiondata.csv';
const RECONNECT_DELAY = config.reconnect_delay;
const osc_port = new osc.UDPPort({ remotePort: config.output_port });
osc_port.on('ready', () => {
console.info(`Output port ready! (${config.output_port})`);
});
osc_port.open();
let session_start_time = new Date();
let last_bpm = 0;
let avg_bpm = [];
let max_bpm = 0;
let min_bpm = 0;
function normalize(n, min, max) {
return (n - min) / (max - min);
}
function sendBPM(bpm) {
if (bpm != last_bpm) {
if (bpm > max_bpm)
max_bpm = bpm;
if (bpm && (!min_bpm || bpm < min_bpm))
min_bpm = bpm;
let packets = [];
for (let output of config.outputs) {
if (output.error == true) continue;
let value = 0;
let [source, type] = output.type.split(':', 2);
if (source == 'bpm') {
value = bpm;
}
else {
console.error(`Error on output ${output.path}:\nType must be bpm, as that is all that is supported at this time.`);
output['error'] = true;
continue;
}
if (type == 'f' || type == 'float') {
if (output.normalization_range) {
const [min, max] = output.normalization_range.split('-', 2);
value = normalize(value, min, max);
}
}
else if (type == 'i' || type == 'int') {
value = Math.floor(value);
}
else if (type == 'digits') {
if (output.digits_max)
value = Math.min(Math.floor(value), output.digits_max);
else value = Math.floor(value);
value = value.toString().split('').map(Number);
const separator = (output.digit_subpaths) ? '/' : '_';
for (let i = 0; i < value.length; i++) {
packets.push({
address: output.path + separator + (i + 1),
args: [ { type: 'i', value: value[i] } ]
})
}
continue;
}
else {
console.error(`Error on output ${output.path}:\nData type must be either float, int or .`);
output['error'] = true;
continue;
}
packets.push({
address: output.path,
args: [ { type: type, value: value } ]
});
}
if (packets.length > 0) {
osc_port.send({
timeTag: osc.timeTag(),
packets: packets
});
}
last_bpm = bpm;
}
}
// ANT+ setup
let stick = null;
let connected = null;
let reconnect_timeout = null;
// let reconnect_failed_timeout = null;
if (config.ant_version === 3) stick = new ant.GarminStick3();
else if (config.ant_version === 2) stick = new ant.GarminStick2();
else console.error('Invalid stick version in config! Must be either 2 or 3.');
let hr_sensor = new ant.HeartRateSensor(stick);
function push_hr_to_avg() {
if (last_bpm) avg_bpm.push(last_bpm);
}
hr_sensor.on('hbdata', (data) => {
if (connected === null)
console.log('Connected!');
else if (connected === false)
console.log('Reconnected!');
connected = true;
if (reconnect_timeout) {
clearTimeout(reconnect_timeout);
reconnect_timeout = null;
}
// if (reconnect_failed_timeout) {
// clearTimeout(reconnect_failed_timeout);
// reconnect_failed_timeout = null;
// }
sendBPM(data.ComputedHeartRate);
});
// hr_sensor.on('attach', () => {
// console.log('attach');
// });
hr_sensor.on('eventData', (data) => {
console.log('eventData', data);
});
// hr_sensor.on('attached', () => {
// if (connected === null)
// console.log('Connected!');
// else if (connected === false)
// console.log('Reconnected!');
// connected = true;
// });
function reconnect_handler() {
console.log('Reconnecting...');
hr_sensor.attach(0, 0);
}
hr_sensor.on('detached', () => {
connected = false;
console.log(`Lost connection!`);
reconnect_timeout = setTimeout(reconnect_handler, RECONNECT_DELAY * 1000);
});
// stick.on('shutdown', (data) => {
// console.log('shutting down', data);
// });
stick.on('startup', () => {
// console.info('ANT+ Stick started up!');
hr_sensor.attach(0, 0);
setInterval(push_hr_to_avg, 60 * 1000);
});
if (!stick.open()) {
console.log('ANT+ Stick not found!');
}
// else {
// console.info('Connected to ANT+ stick!');
// }
let saved_session_data = false;
function on_exit() {
if (!saved_session_data) {
console.log('Saving session data...');
saved_session_data = true;
let session_end_time = new Date();
let average_bpm = 0;
for (let i = 0; i < avg_bpm.length; i++)
average_bpm += avg_bpm[i];
average_bpm /= avg_bpm.length;
if (!fs.existsSync(OUTPUT_FILE_PATH))
fs.appendFileSync(OUTPUT_FILE_PATH, 'START_TIME,START_DATE,END_TIME,END_DATE,AVG_BPM,MAX_BPM,MIN_BPM\n');
let text = `${session_start_time.toLocaleTimeString()},${session_start_time.toLocaleDateString()},${session_end_time.toLocaleTimeString()},${session_end_time.toLocaleDateString()},${average_bpm},${max_bpm},${min_bpm}\n`;
fs.appendFileSync(OUTPUT_FILE_PATH, text);
if (DEBUG) console.log(text);
}
process.exit(0);
}
process.on('exit', on_exit);
process.on('SIGINT', on_exit);
process.on('SIGTERM', on_exit);