-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
213 lines (181 loc) Β· 5.33 KB
/
main.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
const appUtils = require('./app-utils');
const JSONData = appUtils.getJSONFile();
console.log(`π / JSONData`, JSONData.name);
const bleno = require('bleno');
console.log(`π / bleno.state`, bleno.state);
const FakeCharacteristic = require('./fake-characteristic');
const EventEmitter = require('events');
class Emitter extends EventEmitter {}
const events = new Emitter();
bleno.on('stateChange', (state) => {
console.log('onStateChange: ' + state);
if (state === 'poweredOn') {
/**
* @note we need to patch bleno for this one
* @see https://github.com/noble/noble/pull/865/files
* @todo monkey patch
*/
bleno.reset();
/**
* STANDARD (Services advertising)
*/
// bleno.startAdvertising('adadada', ["A7561523258BD820E0967C7C2190E980"], (error) => {
// // bleno.startAdvertising(JSONData.name, ["A7561523258BD820E0967C7C2190E980"], (error) => {
// console.log("Advertising cb: " + error);
// });
/**
* EIR FORMAT
*/
// let advertisementData = Buffer.from(JSONData.advertisementData);
// let scanData = Buffer.from(JSONData.scanData);
// let advertisementData = Buffer.from([3, 25, 0, 0, 2, 1, 5, 3, 3, 10, 24, 19, 9, 79, 67, 80, 67, 99, 54, 32, 35, 49, 57, 57, 57, 56, 56, 56, 48, 48, 48])
// <Buffer 03 19 00 00 02 01 05 03 03 0a 18 13 09 4f 43 50 43 63 36 20 23 31 39 39 39 38 38 38 30 30 30>
// O C P C c 6 # 1 9 9 9 8 8 8 0 0 0
let advertisementHeader = Buffer.from([
3,
25,
0,
0,
2,
1,
5,
3,
3,
10,
24,
19,
9,
]);
let completeLocalName = Buffer.from(JSONData.name);
let advertisementData = Buffer.concat(
[advertisementHeader, completeLocalName],
advertisementHeader.length + completeLocalName.length
);
let scanData = Buffer.from([
30,
255,
255,
255,
26,
255,
65,
25,
0,
76,
0,
0,
67,
0,
0,
83,
0,
0,
87,
0,
0,
0,
0,
0,
0,
0,
84,
0,
0,
0,
0,
]);
// <Buffer 1e ff ff ff 1a ff 41 19 00 4c 00 00 43 00 00 53 00 00 57 00 00 00 00 00 00 00 54 00 00 00 00>
bleno.startAdvertisingWithEIRData(advertisementData, scanData, () => {
console.log('Started advertising');
});
}
});
bleno.on('advertisingStart', (error) => {
console.log('Adverising started!: ' + error);
let servicesInstances = [];
for (let serviceJSON of JSONData.services) {
// console.log(serviceJSON.characteristics)
let service = {
uuid: serviceJSON.uuid,
characteristics: [],
};
let characteristics = [];
for (let characteristicJSON of serviceJSON.characteristics) {
// console.log(characteristicJSON)
let characteristic = characteristicJSON;
characteristic.events = events;
characteristic.JSONData = JSONData;
console.log('Creating char instance for', characteristic.uuid);
service.characteristics.push(new FakeCharacteristic(characteristic));
// random raw value from values in JSON (init)
let rawValue = appUtils.getRandomRawValue(characteristic.notifyValues);
if (rawValue) {
console.warn(
`Intervals are not set properly for charcateristic ${characteristic.characteristicName}`
);
}
// random between two intervals (init)
let interval = getRandomInterval(
characteristic.notifyIntervalMin,
characteristic.notifyIntervalMax
);
if (rawValue && interval) {
setControlledinterval(interval * 1000, characteristic);
}
}
console.log('Creating serv instance for', service.uuid);
let finalService = new bleno.PrimaryService(service);
servicesInstances.push(finalService);
}
bleno.setServices(servicesInstances, (error) => {
if (typeof error !== 'undefined') {
console.error('setServices error!', error);
}
});
});
bleno.on('accept', (event) => {
console.log('accept event!', event);
});
bleno.on('mtuChange', (event) => {
console.log('mtuChange event!');
});
bleno.on('disconnect', (event) => {
console.log('disconnect event!', event);
});
bleno.on('advertisingStop', (event) => {
console.log('advertisingStop event!', event);
});
bleno.on('servicesSet', (event) => {
console.log('servicesSet event!', event);
});
bleno.on('rssiUpdate', (event) => {
console.log('rssiUpdate event!', event);
});
bleno.on('advertisingStartError', (error) => {
console.log('advertisingStartError!', error);
});
bleno.on('servicesSetError', (error) => {
console.log('servicesSetError error!', error);
});
function getRandomInterval(min, max, charName) {
if (min < max) {
return Math.floor(Math.random() * max) + min;
} else {
return null;
}
}
function setControlledinterval(interval, characteristic) {
let int = setInterval(() => {
let rawValue = appUtils.getRandomRawValue(characteristic.notifyValues);
events.emit(characteristic.characteristicName, {
rawValue: Buffer.from(rawValue),
characteristicName: characteristic.characteristicName,
});
let interval = getRandomInterval(
characteristic.notifyIntervalMin,
characteristic.notifyIntervalMax
);
clearInterval(int);
setControlledinterval(interval * 1000, characteristic);
}, interval);
}