-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
99 lines (87 loc) · 2.69 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
// Setup multi role support and two different adapters for Peripheral and Central
process.env['NOBLE_MULTI_ROLE'] = 1
process.env['NOBLE_REPORT_ALL_HCI_EVENTS'] = 1
process.env['BLENO_HCI_DEVICE_ID'] = 0
process.env['NOBLE_HCI_DEVICE_ID'] = 1
const noble = require('@abandonware/noble');
const keiserParser = require('./keiserParser.js')
const KeiserBLE = require('./BLE/keiserBLE')
var fillInTimer = null;
var dataToSend = null;
var connectedCount = 0;
var targetDeviceId = -1;
console.log("Starting");
var keiserBLE = new KeiserBLE();
keiserBLE.on('advertisingStart', (client) => {
//oled.displayBLE('Started');
});
keiserBLE.on('accept', (client) => {
connectedCount++;
//oled.displayBLE('Connected');
});
keiserBLE.on('disconnect', (client) => {
connectedCount--;
//oled.displayBLE('Disconnected');
});
noble.on('stateChange', async (state) => {
console.log(`[Central] State changed to ${state}`);
if (state === 'poweredOn') {
console.log(`[Central] starting scan`);
await noble.startScanningAsync(null, true);
} else if (state === 'poweredOff') {
console.log('No adapter detected, exiting in 5 seconds');
setTimeout(() => {
process.exit();
}, 5000);
}
});
function sendFillInData() {
if (!dataToSend || (connectedCount < 1)) {
console.log("Aborting nothing to send");
}
console.log("Sending fill in data");
keiserBLE.notifyFTMS(dataToSend);
fillInTimer = setTimeout(sendFillInData, 1000);
}
noble.on('discover', (peripheral) => {
//console.log(`[Central] Found device ${peripheral.advertisement.localName} ${peripheral.address}`);
if (peripheral.advertisement.localName == "M3")
{
try
{
var result = keiserParser.parseAdvertisement(peripheral);
if (targetDeviceId == -1) {
if (result.realTime) {
console.log(`Attaching to bike id ${result.ordinalId}`);
targetDeviceId = result.ordinalId;
keiserBLE.setDeviceId(targetDeviceId);
} else {
return;
}
}
if (result.ordinalId == targetDeviceId) {
console.log(`Bike ${result.ordinalId}: ${result.realTime} ${result.cadence} ${result.power} ${result.gear} ${result.duration}`);
if (result.realTime) {
dataToSend = {
rpm: result.cadence,
power: result.power,
hr: result.heartRate,
speed: result.cadence * .73 // 30 cog 34 cassette for now
};
if (fillInTimer) {
clearTimeout(fillInTimer);
fillInTimer = null;
}
if (connectedCount > 0) {
keiserBLE.notifyFTMS(dataToSend);
fillInTimer = setTimeout(sendFillInData, 1000);
}
}
}
}
catch (err) {
console.log(`\tError parsing: ${err}`);
console.log(`\t ${err.stack}`);
}
}
});