-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
391 lines (335 loc) · 10.5 KB
/
index.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
const EventEmitter = require("events");
const noble = require("noble");
const {
S1_SERVICE, S1_CHAR_DATA_OUTPUT, S1_CHAR_COMMAND_INPUT, S1_CHAR_MYSTERY_OUTPUT,
S2_SERVICE, S2_CHAR_MYSTERY_OUTPUT,
} = require("./uuids");
const {
ackCmd, getMaxLevel, setWorkoutMode, setWorkoutParams, setResistanceLevel,
getWorkoutState, setWorkoutControlState
} = require("./packets");
const { Response } = require("./response");
const DEFAULT_QUEUES = {
disconnected: [],
connected: [ackCmd(), getMaxLevel()],
starting: [],
started: [getWorkoutState()],
}
/**
* Client is a bluetooth client for iConsole-based bluetooth-enabled exercise bike.
*/
class Client {
/**
* Construct the client. You should use scan instead unless you manually
* did something with `noble` to get the peripheral object.
*
* **WARNING**: Unstable API, will change once refactored to sblendid. You
* should pin the version if you use this.
*
* @param {noble.Peripheral} peripheral noble peripheral.
*/
constructor(peripheral) {
this.peripheral = peripheral;
/** @type {Characteristic} */
this.commandInput = null;
/** @type {Characteristic} */
this.dataOutput = null;
this.state = "disconnected";
this.queue = [];
this.defaultQueuePos = 0;
this.events = new EventEmitter();
}
/**
* Connect to the bike.
*
* @returns {Promise<void>}
*/
connect() {
if (this.state !== "disconnected") {
throw new Error("Already connected!")
}
return new Promise((resolve, reject) => {
this.peripheral.connect(err => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.peripheral.discoverServices([], (err, services) => {
if (err != null) {
this.disconnect();
return reject(err);
}
const s1 = services.find(s => s.uuid === S1_SERVICE);
const s2 = services.find(s => s.uuid === S2_SERVICE);
s1.discoverCharacteristics([], (err, characteristics) => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.commandInput = characteristics.find(c => c.uuid === S1_CHAR_COMMAND_INPUT);
this.dataOutput = characteristics.find(c => c.uuid === S1_CHAR_DATA_OUTPUT);
this.mysterousOutput = characteristics.find(c => c.uuid === S1_CHAR_MYSTERY_OUTPUT);
this.dataOutput.subscribe(err => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.dataOutput.on("data", (data) => {
this.handleData(this.dataOutput.uuid, data);
})
this.mysterousOutput.subscribe(err => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.mysterousOutput.on("data", (data) => {
this.handleData(this.mysterousOutput, data);
})
s2.discoverCharacteristics([], (err, characteristics) => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.mysterousOutput2 = characteristics.find(c => c.uuid === S2_CHAR_MYSTERY_OUTPUT);
this.mysterousOutput2.subscribe(err => {
if (err != null) {
this.disconnect();
return reject(err);
}
this.mysterousOutput2.on("data", (data) => {
this.handleData(this.mysterousOutput2.uuid, data);
})
this.state = "connected";
this.defaultQueuePos = 0;
this.queue = [
{message: ackCmd()},
{message: getMaxLevel()},
];
this.sendLoop();
resolve();
});
});
});
});
});
});
});
});
}
/**
* Teardown and clean up everything.
*/
disconnect() {
if (this.state === "disconnected") {
return
}
this.peripheral.disconnect();
noble.stopScanning();
this.events.emit("disconnect");
this.state = "disconnected";
this.defaultQueuePos = 0;
const queue = this.queue.splice(0);
for (const {reject} of queue) {
reject(new Error("Disconnected"))
}
}
/**
* Disconnect (if connected), and remove all event listeners.
*/
destroy() {
this.disconnect();
this.events.removeAllListeners();
}
/**
* Start starts the workout. Paramters will be added once the protocol is understood better.
*
* @param {*} opts
* @param {number} opts.timeInMinute Minute goal, will stop once timer runs down.
* @param {number} opts.distanceInKM Distance goal, will stop once distance is reached.
* @param {number} opts.calories Calorie goal, will stop once it counts down.
* @param {number} opts.pulse Not yet understood, set if you're feeling adventurous.
* @param {number} opts.watt Not yet understood, set if you're feeling adventurous.
* @param {number} opts.workoutMode Not yet understood, set if you're feeling adventurous.
* @param {number} opts.unit Not yet understood, set if you're feeling adventurous.
* @param {number} opts.level Start at resistance level. Equialent to calling `setLevel` after start.
*/
async start({
timeInMinute = 0,
distanceInKM = 0,
calories = 0,
pulse = 0,
watt = 0,
workoutMode = 0,
unit = 0,
level = 0,
} = {}) {
this.state = "starting";
this.defaultQueuePos = 0;
await Promise.all([
this.queueCommand(ackCmd()),
this.queueCommand(setWorkoutMode(workoutMode)),
this.queueCommand(setWorkoutParams(timeInMinute, distanceInKM, calories, pulse, watt, unit)),
this.queueCommand(setWorkoutControlState(1)),
this.queueCommand(setResistanceLevel(level)),
]);
this.state = "started";
this.defaultQueuePos = 0;
}
/**
* Set resistance level. You should listen to `maxLevel` event to get the max value for this.
*
* @param {number} level
*/
async setLevel(level) {
return await this.queueCommand(setResistanceLevel(level));
}
/**
* Resume the exercise.
*/
async resume() {
return await this.queueCommand(setWorkoutControlState(1));
}
/**
* Pause the exercise.
*/
async pause() {
return await this.queueCommand(setWorkoutControlState(2));
}
/**
* Queue a command for writing. The promise will resolve if the command
* is sent, or reject if the bike disconnects before it's sent. This allows
* for commands to be written such that they don't interfere with one another.
*
* The queue will be advanced after 500ms without a response, so invalid messages
* will not do any harm.
*
* @param {number[] | Buffer | Uint8Array} data Data to write.
*/
queueCommand(data) {
return new Promise((resolve, reject) => {
const message = Buffer.from(data);
this.queue.push({message, resolve, reject})
})
}
/**
* Write a command. Use `queueCommand` or the helper methods instead.
*
* @param {number[] | Buffer | Uint8Array} data Data to write.
*/
writeCommand(data) {
this.events.emit("send", data);
return new Promise((resolve, reject) => {
this.commandInput.write(Buffer.from(data), true, (err) => {
if (err != null) {
reject(err);
} else {
resolve();
}
})
})
}
/**
* This is a handler for the event data. Should not be called from outside.
*
* @param {Buffer} data
*/
handleData(uuid, data) {
if (uuid === S1_CHAR_DATA_OUTPUT) {
const resp = new Response(data);
const respData = resp.parse();
this.events.emit(data.kind, respData);
this.events.emit("data", respData);
if (this.lastCommand === resp.kind - 0x10) {
this.lastCommandResolve();
}
} else {
this.events.emit("mysteryData", data, uuid)
}
}
/**
* Loop over the send queue, or send default messages..
*/
async sendLoop() {
try {
while (this.state !== "disconnected") {
let {message, resolve} = (this.queue[0] || {});
if (message != null) {
this.queue.splice(0, 1);
} else {
const defaultQueue = DEFAULT_QUEUES[this.state];
message = defaultQueue[this.defaultQueuePos];
this.defaultQueuePos = (this.defaultQueuePos + 1) % defaultQueue.length;
if (message == null) {
await this.wait(100);
continue;
}
}
await this.writeCommand(message);
await this.wait(500, message[1]);
if (resolve != null) {
resolve();
}
}
} catch (err) {
if (this.state !== "disconnected") {
this.events.emit("error", err);
this.disconnect();
}
}
}
/**
* Scan for a peripheral. It will return a client you can call connect on if it
* finds it. Or a timeout error if the timeout is reached..
*
* @param {string} addr The uuid of the peripheral
* @param {number} timeout How many milliseconds to scan for
* @returns {Client}
*/
static scan(addr, timeout = 30000) {
let done = false;
return new Promise((resolve, reject) => {
noble.startScanning([]);
noble.on('discover', (peripheral) => {
if (peripheral.address !== addr) {
return
}
if (!done) {
noble.stopScanning();
done = true;
resolve(new Client(peripheral));
}
});
setTimeout(() => {
if (!done) {
noble.stopScanning();
done = true;
reject(new Error(`Timeout exceeded (${(timeout / 1000).toFixed(2)}s)`))
}
}, timeout)
})
}
/**
* This waits for `ms` or command arriving + 200ms, whichever comes
* first.
*
* @param {number} ms Amount of time to wait.
* @param {number} command Command whose response to wait for.
*/
wait(ms, command) {
return new Promise(resolve => {
let done = false;
let resolve2 = () => {
if (!done) {
done = true;
resolve();
}
}
if (command != null) {
this.lastCommand = command;
this.lastCommandResolve = () => setTimeout(resolve2, 200);
}
setTimeout(resolve2, ms)
})
}
}
module.exports = Client;