-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
65 lines (57 loc) · 1.7 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
import {FlywheelBikeClient} from './flywheel';
import {PelotonBikeClient} from './peloton';
import {Ic4BikeClient} from './ic4';
import {BotBikeClient} from './bot';
import {macAddress} from '../util/mac-address';
import fs from 'fs';
const factories = {
'flywheel': createFlywheelBikeClient,
'peloton': createPelotonBikeClient,
'ic4': createIc4BikeClient,
'bot': createBotBikeClient,
'autodetect': autodetectBikeClient,
};
export function getBikeTypes() {
return Object.keys(factories);
}
export function createBikeClient(options, noble) {
const {bike} = options;
const factory = factories[bike];
if (!factory) {
throw new Error(`unrecognized bike type ${bike}`);
}
return factory(options, noble);
}
function createFlywheelBikeClient(options, noble) {
const filters = {};
if (options.flywheelAddress) {
filters.address = (v) => v == macAddress(options.flywheelAddress);
}
if (options.flywheelName) {
filters.name = (v) => new RegExp(options.flywheelName).test(v);
}
process.env['NOBLE_HCI_DEVICE_ID'] = options.flywheelAdapter;
return new FlywheelBikeClient(noble, filters);
}
function createPelotonBikeClient(options, noble) {
const {pelotonPath} = options;
return new PelotonBikeClient(pelotonPath);
}
function createIc4BikeClient(options, noble) {
return new Ic4BikeClient(noble);
}
function createBotBikeClient(options, noble) {
const args = [
options.botPower,
options.botCadence,
options.botHost,
options.botPort,
]
return new BotBikeClient(...args);
}
function autodetectBikeClient(options, noble) {
if (fs.existsSync(options.pelotonPath)) {
return createPelotonBikeClient(options, noble);
}
return createFlywheelBikeClient(options, noble);
}