-
Notifications
You must be signed in to change notification settings - Fork 3
/
smartboard-mock.js
70 lines (60 loc) · 1.98 KB
/
smartboard-mock.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
const debug = require('debug')('kcapp-smartboard-mock:board');
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const waitForUserInput = (throwCallback) => {
rl.question("Dart: ", (dart) => {
rl.pause();
if (dart === "exit"){
rl.close();
process.exit();
} else {
const score = dart.split("-")[0];
const multiplier = dart.split("-")[1] || 1 ;
throwCallback({ score: parseInt(score), multiplier: parseInt(multiplier) });
waitForUserInput(throwCallback);
}
});
}
/** Service containing board characteristics */
const SERVICE_SCORING = "fff0";
/** Characteristic to control the button */
const CHARACTERISTIC_BUTTON = "fff2";
/** Characteristic to subscribe to throw notifications */
const CHARACTERISTIC_THROW_NOTIFICATIONS = "fff1";
exports.startScan = () => {
debug("Started scanning")
}
exports.connect = (uuid, callback) => {
this.discoverCallback = (peripheral) => {
if (peripheral.uuid === uuid) {
callback(peripheral);
debug("Found device, stopped scanning");
this.peripheral = peripheral;
}
};
this.discoverCallback({ uuid: uuid, advertisement: { localName: 'joofunn Dartboard' }} );
}
exports.initialize = (peripheral, buttonNumber, throwCallback, playerChangeCallback) => {
debug(`Connected to ${peripheral.advertisement.localName} (${peripheral.uuid})`);
debug('Enabled listening');
debug('Subscribed to throw noftifications!');
debug(`Enter darts to send:`);
waitForUserInput(throwCallback);
}
exports.disconnect = (peripheral, callback) => {
debug(`Removing 'discover' callback`);
debug(`Unsubscribed from throw notifications`);
debug(`Disabled listening on characteristic ${CHARACTERISTIC_BUTTON}`);
debug(`Disconnected from ${peripheral.advertisement.localName}`);
callback();
}
/**
* Configure the smartboard module
*/
module.exports = () => {
debug(`Starting smartboard-mock!`);
return this;
};