incyclist-devices 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.MD +55 -0
  2. package/lib/antv2/base/adapter.d.ts +2 -2
  3. package/lib/base/adpater.d.ts +4 -0
  4. package/lib/base/adpater.js +16 -2
  5. package/lib/ble/adapter-factory.d.ts +24 -20
  6. package/lib/ble/adapter-factory.js +36 -13
  7. package/lib/ble/base/adapter.d.ts +6 -3
  8. package/lib/ble/base/adapter.js +83 -49
  9. package/lib/ble/base/comms.d.ts +74 -2
  10. package/lib/ble/base/comms.js +596 -3
  11. package/lib/ble/base/interface.d.ts +19 -10
  12. package/lib/ble/base/interface.js +148 -73
  13. package/lib/ble/base/peripheral.d.ts +7 -3
  14. package/lib/ble/base/peripheral.js +76 -22
  15. package/lib/ble/base/sensor.d.ts +2 -1
  16. package/lib/ble/base/sensor.js +22 -3
  17. package/lib/ble/ble-interface.d.ts +4 -7
  18. package/lib/ble/ble-interface.js +2 -16
  19. package/lib/ble/ble-peripheral.d.ts +0 -1
  20. package/lib/ble/ble-peripheral.js +11 -7
  21. package/lib/ble/characteristics/csc/features.d.ts +10 -0
  22. package/lib/ble/characteristics/csc/features.js +19 -0
  23. package/lib/ble/characteristics/csc/measurement.d.ts +33 -0
  24. package/lib/ble/characteristics/csc/measurement.js +109 -0
  25. package/lib/ble/characteristics/types.d.ts +6 -0
  26. package/lib/ble/characteristics/types.js +2 -0
  27. package/lib/ble/consts.d.ts +1 -0
  28. package/lib/ble/consts.js +2 -1
  29. package/lib/ble/cp/comm.d.ts +1 -1
  30. package/lib/ble/cp/comm.js +2 -2
  31. package/lib/ble/csc/adapter.d.ts +17 -0
  32. package/lib/ble/csc/adapter.js +66 -0
  33. package/lib/ble/csc/index.d.ts +3 -0
  34. package/lib/ble/csc/index.js +19 -0
  35. package/lib/ble/csc/sensor.d.ts +21 -0
  36. package/lib/ble/csc/sensor.js +64 -0
  37. package/lib/ble/csc/types.d.ts +6 -0
  38. package/lib/ble/csc/types.js +2 -0
  39. package/lib/ble/elite/comms.d.ts +1 -1
  40. package/lib/ble/elite/comms.js +2 -2
  41. package/lib/ble/factories/adapter-factory.d.ts +8 -6
  42. package/lib/ble/factories/adapter-factory.js +33 -0
  43. package/lib/ble/factories/types.d.ts +18 -0
  44. package/lib/ble/factories/types.js +2 -0
  45. package/lib/ble/fm/adapter.d.ts +4 -3
  46. package/lib/ble/fm/adapter.js +53 -48
  47. package/lib/ble/fm/comms.d.ts +1 -1
  48. package/lib/ble/fm/comms.js +3 -3
  49. package/lib/ble/fm/sensor.d.ts +1 -1
  50. package/lib/ble/fm/sensor.js +6 -5
  51. package/lib/ble/hr/comm.d.ts +1 -1
  52. package/lib/ble/hr/comm.js +2 -2
  53. package/lib/ble/index.js +2 -0
  54. package/lib/ble/tacx/adapter.d.ts +1 -1
  55. package/lib/ble/tacx/adapter.js +12 -10
  56. package/lib/ble/tacx/comms.d.ts +1 -1
  57. package/lib/ble/tacx/comms.js +2 -2
  58. package/lib/ble/tacx/sensor.js +9 -3
  59. package/lib/ble/types.d.ts +8 -2
  60. package/lib/ble/utils.d.ts +1 -0
  61. package/lib/ble/utils.js +15 -2
  62. package/lib/ble/wahoo/adapter.d.ts +1 -0
  63. package/lib/ble/wahoo/adapter.js +14 -0
  64. package/lib/ble/wahoo/comms.d.ts +1 -1
  65. package/lib/ble/wahoo/comms.js +2 -2
  66. package/lib/ble/wahoo/sensor.js +3 -6
  67. package/lib/direct-connect/base/interface.d.ts +2 -1
  68. package/lib/direct-connect/base/interface.js +20 -10
  69. package/lib/direct-connect/base/peripheral.d.ts +4 -4
  70. package/lib/direct-connect/base/peripheral.js +181 -68
  71. package/lib/direct-connect/bindings/types.d.ts +2 -1
  72. package/lib/direct-connect/messages/message.d.ts +1 -0
  73. package/lib/direct-connect/messages/message.js +16 -1
  74. package/lib/factories/adapters.js +0 -2
  75. package/lib/modes/ant-fe-adv-st-mode.d.ts +7 -1
  76. package/lib/modes/ant-fe-adv-st-mode.js +4 -3
  77. package/lib/types/adapter.d.ts +3 -0
  78. package/lib/types/interface.d.ts +1 -0
  79. package/lib/utils/task.d.ts +3 -0
  80. package/lib/utils/task.js +12 -0
  81. package/package.json +1 -1
@@ -55,10 +55,10 @@ class DirectConnectPeripheral {
55
55
  });
56
56
  }
57
57
  disconnect() {
58
- return __awaiter(this, void 0, void 0, function* () {
58
+ return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
59
59
  try {
60
60
  yield this.connectTask.stop();
61
- yield this.stopConnection();
61
+ yield this.stopConnection(connectionLost);
62
62
  delete this.socket;
63
63
  }
64
64
  catch (err) {
@@ -79,68 +79,126 @@ class DirectConnectPeripheral {
79
79
  }
80
80
  discoverServices() {
81
81
  return __awaiter(this, void 0, void 0, function* () {
82
+ var _a, _b;
82
83
  const seqNo = this.getNextSeqNo();
83
84
  const message = new messages_1.DiscoverServiceMessage();
84
85
  const request = message.createRequest(seqNo, {});
86
+ let response;
85
87
  this.logEvent({ message: 'DiscoverServices request', path: this.getPath(), raw: request.toString('hex') });
86
- const response = yield this.send(seqNo, request);
87
- const res = message.parseResponse(response);
88
- const uuids = res.body.serviceDefinitions.map(s => (0, utils_1.beautifyUUID)(s.serviceUUID));
89
- this.logEvent({ message: 'DiscoverServices response', path: this.getPath(), uuids, raw: request.toString('hex') });
90
- return res.body.serviceDefinitions.map(s => s.serviceUUID);
88
+ try {
89
+ response = yield this.send(seqNo, request);
90
+ const res = message.parseResponse(response);
91
+ const uuids = res.body.serviceDefinitions.map(s => (0, utils_1.beautifyUUID)(s.serviceUUID));
92
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
93
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
94
+ this.logEvent({ message: 'DiscoverServices failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
95
+ return [];
96
+ }
97
+ this.logEvent({ message: 'DiscoverServices response', path: this.getPath(), rc, uuids, raw: response.toString('hex') });
98
+ return res.body.serviceDefinitions.map(s => s.serviceUUID);
99
+ }
100
+ catch (err) {
101
+ this.logEvent({ message: 'DiscoverServices failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
102
+ return [];
103
+ }
91
104
  });
92
105
  }
93
106
  discoverCharacteristics(serviceUUID) {
94
107
  return __awaiter(this, void 0, void 0, function* () {
108
+ var _a, _b;
95
109
  const seqNo = this.getNextSeqNo();
96
110
  const message = new messages_1.DiscoverCharacteristicsMessage();
97
111
  const request = message.createRequest(seqNo, { serviceUUID: (0, utils_1.parseUUID)(serviceUUID) });
112
+ let response;
98
113
  this.logEvent({ message: 'DiscoverCharacteritics request', path: this.getPath(), service: (0, utils_1.beautifyUUID)(serviceUUID), raw: request.toString('hex') });
99
- const response = yield this.send(seqNo, request);
100
- const res = message.parseResponse(response);
101
- const service = (0, utils_1.beautifyUUID)(res.body.serviceUUID);
102
- const characteristics = res.body.characteristicDefinitions.map(cd => `${(0, utils_1.beautifyUUID)(cd.characteristicUUID)}:${cd.properties.join('/')}`);
103
- this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), service, characteristics, raw: request.toString('hex') });
104
- return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
114
+ try {
115
+ response = yield this.send(seqNo, request);
116
+ const res = message.parseResponse(response);
117
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
118
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
119
+ this.logEvent({ message: 'DiscoverCharacteritics failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
120
+ return [];
121
+ }
122
+ const service = (0, utils_1.beautifyUUID)(res.body.serviceUUID);
123
+ const characteristics = res.body.characteristicDefinitions.map(cd => `${(0, utils_1.beautifyUUID)(cd.characteristicUUID)}:${cd.properties.join('/')}`);
124
+ this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), rc, service, characteristics, raw: response.toString('hex') });
125
+ return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
126
+ }
127
+ catch (err) {
128
+ this.logEvent({ message: 'DiscoverCharacteritics failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
129
+ return [];
130
+ }
105
131
  });
106
132
  }
107
133
  subscribe(characteristicUUID, callback) {
108
134
  return __awaiter(this, void 0, void 0, function* () {
135
+ var _a, _b;
136
+ const uuid = (0, utils_1.parseUUID)(characteristicUUID);
137
+ if (this.subscribed.includes(uuid)) {
138
+ return true;
139
+ }
109
140
  const seqNo = this.getNextSeqNo();
110
141
  const message = new messages_1.EnableCharacteristicNotificationsMessage();
111
142
  const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: true });
143
+ let response;
112
144
  this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: true, raw: request.toString('hex') });
113
- const response = yield this.send(seqNo, request);
114
- const res = message.parseResponse(response);
115
- this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
116
- const confirmed = res.body.characteristicUUID;
117
- if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
118
- this.subscribed.push(characteristicUUID);
119
- this.eventEmitter.on((0, utils_1.parseUUID)(characteristicUUID), (data) => {
120
- callback(characteristicUUID, data);
121
- });
122
- return true;
145
+ try {
146
+ response = yield this.send(seqNo, request);
147
+ const res = message.parseResponse(response);
148
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
149
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
150
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
151
+ return false;
152
+ }
153
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: response.toString('hex') });
154
+ const confirmed = res.body.characteristicUUID;
155
+ if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
156
+ this.subscribed.push((0, utils_1.parseUUID)(characteristicUUID));
157
+ if (callback) {
158
+ this.eventEmitter.on((0, utils_1.parseUUID)(characteristicUUID), (data) => {
159
+ callback(characteristicUUID, data);
160
+ });
161
+ }
162
+ return true;
163
+ }
164
+ return false;
165
+ }
166
+ catch (err) {
167
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
168
+ return false;
123
169
  }
124
- return false;
125
170
  });
126
171
  }
127
172
  unsubscribe(characteristicUUID) {
128
173
  return __awaiter(this, void 0, void 0, function* () {
174
+ var _a, _b;
129
175
  try {
130
176
  const seqNo = this.getNextSeqNo();
131
177
  const message = new messages_1.EnableCharacteristicNotificationsMessage();
132
178
  const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: false });
133
- this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: false, raw: request.toString('hex') });
134
- const response = yield this.send(seqNo, request);
135
- const res = message.parseResponse(response);
136
- this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
137
- const confirmed = res.body.characteristicUUID;
138
- if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
139
- this.subscribed.splice(this.subscribed.indexOf(characteristicUUID), 1);
140
- this.eventEmitter.removeAllListeners((0, utils_1.parseUUID)(characteristicUUID));
141
- return true;
179
+ let response;
180
+ try {
181
+ this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: false, raw: request.toString('hex') });
182
+ response = yield this.send(seqNo, request);
183
+ const res = message.parseResponse(response);
184
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
185
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
186
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
187
+ return false;
188
+ }
189
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: response.toString('hex') });
190
+ const confirmed = res.body.characteristicUUID;
191
+ if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
192
+ this.subscribed.splice(this.subscribed.indexOf((0, utils_1.parseUUID)(characteristicUUID)), 1);
193
+ this.eventEmitter.removeAllListeners((0, utils_1.parseUUID)(characteristicUUID));
194
+ return true;
195
+ }
196
+ return false;
197
+ }
198
+ catch (err) {
199
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
200
+ return false;
142
201
  }
143
- return false;
144
202
  }
145
203
  catch (err) {
146
204
  this.logEvent({ messsage: 'EnableCharacteristicNotifications failed', reason: err.message });
@@ -158,9 +216,8 @@ class DirectConnectPeripheral {
158
216
  catch (err) {
159
217
  this.logEvent({ message: 'could not discover services', reason: err.message });
160
218
  }
161
- const res = [];
162
- for (let i = 0; i < services.length; i++) {
163
- const service = services[i];
219
+ for (const element of services) {
220
+ const service = element;
164
221
  let characteristics = [];
165
222
  try {
166
223
  characteristics = yield this.discoverCharacteristics(service);
@@ -171,8 +228,8 @@ class DirectConnectPeripheral {
171
228
  }
172
229
  if (!(characteristics === null || characteristics === void 0 ? void 0 : characteristics.length))
173
230
  continue;
174
- for (let j = 0; j < characteristics.length; j++) {
175
- const characteristic = characteristics[j];
231
+ for (const element of characteristics) {
232
+ const characteristic = element;
176
233
  if (characteristic.properties.includes('notify'))
177
234
  yield this.subscribe(characteristic.uuid, callback);
178
235
  }
@@ -187,69 +244,124 @@ class DirectConnectPeripheral {
187
244
  }
188
245
  subscribeSelected(characteristics, callback) {
189
246
  return __awaiter(this, void 0, void 0, function* () {
247
+ let services = [];
248
+ let supported = [];
249
+ try {
250
+ services = yield this.discoverServices();
251
+ }
252
+ catch (err) {
253
+ this.logEvent({ message: 'could not discover services', reason: err.message });
254
+ }
255
+ for (const element of services) {
256
+ const service = element;
257
+ try {
258
+ const c = yield this.discoverCharacteristics(service);
259
+ supported = supported.concat(c);
260
+ }
261
+ catch (err) {
262
+ this.logEvent({ message: 'could not discover characteristics', service, reason: err.message });
263
+ return false;
264
+ }
265
+ }
190
266
  const retry = [];
191
- for (let i = 0; i < characteristics.length; i++) {
192
- const uuid = characteristics[i];
193
- const success = yield this.subscribe(uuid, callback);
194
- if (!success)
195
- retry.push(uuid);
267
+ this.logEvent({ message: 'characteristics supported', requested: characteristics.map(c => (0, utils_1.beautifyUUID)(c)), supported: supported.map(c => (0, utils_1.beautifyUUID)(c.uuid)) });
268
+ for (const element of characteristics) {
269
+ const uuid = element;
270
+ const found = supported.find(c => (0, utils_1.beautifyUUID)(c.uuid) === (0, utils_1.beautifyUUID)(uuid));
271
+ if (!found) {
272
+ this.logEvent({ message: 'characteristic not supported', requested: (0, utils_1.beautifyUUID)(uuid) });
273
+ }
274
+ else {
275
+ const success = yield this.subscribe(uuid, callback);
276
+ if (!success)
277
+ retry.push(uuid);
278
+ }
196
279
  }
197
- for (let i = 0; i < retry.length; i++) {
198
- const c = retry[i];
280
+ for (const element of retry) {
281
+ const c = element;
199
282
  yield this.subscribe(c.uuid, callback);
200
283
  }
201
284
  return true;
202
285
  });
203
286
  }
204
287
  unsubscribeAll() {
205
- return __awaiter(this, void 0, void 0, function* () {
288
+ return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
289
+ if (connectionLost) {
290
+ this.subscribed = [];
291
+ return;
292
+ }
206
293
  const promises = [];
207
294
  this.subscribed.forEach(characteristicUUID => {
208
- promises.push(this.unsubscribe(characteristicUUID));
295
+ promises.push(this.unsubscribe((0, utils_1.parseUUID)(characteristicUUID)));
209
296
  });
210
297
  yield Promise.allSettled(promises);
211
- return true;
212
298
  });
213
299
  }
214
300
  read(characteristicUUID) {
215
301
  return __awaiter(this, void 0, void 0, function* () {
302
+ var _a, _b;
216
303
  const seqNo = this.getNextSeqNo();
217
304
  const message = new messages_1.ReadCharacteristicMessage();
218
305
  const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID) });
219
- this.logEvent({ message: 'ReadCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), raw: request.toString('hex') });
220
- const response = yield this.send(seqNo, request);
221
- const res = message.parseResponse(response);
222
- this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
223
- data: Buffer.from(res.body.characteristicData).toString('hex'),
224
- raw: request.toString('hex') });
225
- return Buffer.from(res.body.characteristicData);
306
+ let response;
307
+ try {
308
+ this.logEvent({ message: 'ReadCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), raw: request.toString('hex') });
309
+ response = yield this.send(seqNo, request);
310
+ const res = message.parseResponse(response);
311
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
312
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
313
+ this.logEvent({ message: 'ReadCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
314
+ return Buffer.from([]);
315
+ }
316
+ this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
317
+ data: Buffer.from(res.body.characteristicData).toString('hex'),
318
+ raw: response.toString('hex') });
319
+ return Buffer.from(res.body.characteristicData);
320
+ }
321
+ catch (err) {
322
+ this.logEvent({ message: 'ReadCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
323
+ return Buffer.from([]);
324
+ }
226
325
  });
227
326
  }
228
327
  write(characteristicUUID, data, options) {
229
328
  return __awaiter(this, void 0, void 0, function* () {
230
329
  return new Promise(resolve => {
330
+ const seqNo = this.getNextSeqNo();
331
+ const message = new messages_1.WriteCharacteristicMessage();
332
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), characteristicData: data });
333
+ let response;
334
+ let characteristic = characteristicUUID;
231
335
  if (!(options === null || options === void 0 ? void 0 : options.withoutResponse)) {
336
+ this.subscribe(characteristicUUID);
232
337
  const uuid = (0, utils_1.parseUUID)(characteristicUUID);
233
338
  this.eventEmitter.once(uuid, (data) => {
234
339
  resolve(data);
235
340
  });
236
341
  }
237
- const seqNo = this.getNextSeqNo();
238
- const message = new messages_1.WriteCharacteristicMessage();
239
- const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), characteristicData: data });
240
342
  this.logEvent({ message: 'WriteCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID),
241
343
  data: data.toString('hex'),
242
344
  raw: request.toString('hex') });
243
- this.send(seqNo, request).then((response) => {
345
+ this.send(seqNo, request).then((data) => {
346
+ var _a, _b;
347
+ response = data;
244
348
  const res = message.parseResponse(response);
245
- this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
246
- raw: request.toString('hex') });
349
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
350
+ if (((_b = res === null || res === void 0 ? void 0 : res.header) === null || _b === void 0 ? void 0 : _b.respCode) !== consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY) {
351
+ this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
352
+ resolve(Buffer.from([]));
353
+ return;
354
+ }
355
+ characteristic = (0, utils_1.beautifyUUID)(res.body.characteristicUUID);
356
+ this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), rc, characteristic,
357
+ raw: response.toString('hex') });
247
358
  if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
248
359
  resolve(Buffer.from([]));
249
360
  }
250
361
  })
251
362
  .catch(err => {
252
- this.logEvent({ message: 'WriteCharacteristic error', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), error: err.message });
363
+ this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), characteristic, raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
364
+ resolve(Buffer.from([]));
253
365
  });
254
366
  });
255
367
  });
@@ -288,9 +400,10 @@ class DirectConnectPeripheral {
288
400
  onPortClose() {
289
401
  return __awaiter(this, void 0, void 0, function* () {
290
402
  this.socket.removeAllListeners();
403
+ this.socket.on('error', () => { });
291
404
  this.logEvent({ message: 'port closed', path: this.getPath() });
292
405
  try {
293
- yield this.disconnect();
406
+ yield this.disconnect(true);
294
407
  }
295
408
  catch (_a) { }
296
409
  if (this.onDisconnectHandler)
@@ -303,11 +416,11 @@ class DirectConnectPeripheral {
303
416
  return path;
304
417
  }
305
418
  stopConnection() {
306
- return __awaiter(this, void 0, void 0, function* () {
419
+ return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
307
420
  this.eventEmitter.removeAllListeners();
308
421
  if (!this.isConnected())
309
422
  return true;
310
- yield this.unsubscribeAll();
423
+ yield this.unsubscribeAll(connectionLost);
311
424
  this.socket.removeAllListeners();
312
425
  return new Promise(done => {
313
426
  const onClosed = () => {
@@ -362,7 +475,7 @@ class DirectConnectPeripheral {
362
475
  }
363
476
  if (incoming.length > header.length + 6) {
364
477
  this.remainingBuffer = Buffer.from(incoming.subarray(header.length + 6));
365
- incoming = incoming.subarray(0, header.length + 6);
478
+ incoming = Buffer.from(incoming.subarray(0, header.length + 6));
366
479
  }
367
480
  return incoming;
368
481
  }
@@ -386,7 +499,7 @@ class DirectConnectPeripheral {
386
499
  this.msgSeqNo = notification.header.seqNum;
387
500
  const uuid = (0, utils_1.parseUUID)(notification.body.characteristicUUID);
388
501
  this.logEvent({ message: 'Characteristic notification', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(notification.body.characteristicUUID),
389
- data: Buffer.from(notification.body.characteristicData).toString('hex') });
502
+ data: Buffer.from(notification.body.characteristicData).toString('hex'), raw: incoming.toString('hex') });
390
503
  this.eventEmitter.emit(uuid, notification.body.characteristicData);
391
504
  }
392
505
  else {
@@ -16,13 +16,14 @@ type KeyValue = {
16
16
  [key: string]: any;
17
17
  };
18
18
  export interface BrowserConfig {
19
- type: string;
19
+ type?: string;
20
20
  name?: string;
21
21
  protocol?: 'tcp' | 'udp';
22
22
  subtypes?: string[];
23
23
  txt?: KeyValue;
24
24
  }
25
25
  export interface MulticastDnsAnnouncement extends PeripheralAnnouncement {
26
+ type: string;
26
27
  address: string;
27
28
  protocol?: 'tcp' | 'udp';
28
29
  port: number;
@@ -18,3 +18,4 @@ export declare class Message<TReq extends TDCBody, TRes extends TDCBody> {
18
18
  parseResponseBody(body: Buffer): TRes;
19
19
  }
20
20
  export declare const parseHeader: (buffer: Buffer) => TDCMessageHeader;
21
+ export declare const RC: (code: any) => string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseHeader = exports.Message = void 0;
3
+ exports.RC = exports.parseHeader = exports.Message = void 0;
4
4
  const consts_1 = require("../consts");
5
5
  const error_1 = require("./error");
6
6
  class Message {
@@ -88,3 +88,18 @@ const parseHeader = (buffer) => {
88
88
  return { msgVersion, msgId, seqNum, respCode, length };
89
89
  };
90
90
  exports.parseHeader = parseHeader;
91
+ const RC = (code) => {
92
+ switch (code) {
93
+ case consts_1.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY: return 'success';
94
+ case consts_1.DC_RC_UNKNOWN_MESSAGE_TYPE: return 'Unknown Message Type';
95
+ case consts_1.DC_RC_UNEXPECTED_ERROR: return 'Unexpected Error';
96
+ case consts_1.DC_RC_SERVICE_NOT_FOUND: return 'Service Not Found';
97
+ case consts_1.DC_RC_CHARACTERISTIC_NOT_FOUND: return 'Characteristic Not Found';
98
+ case consts_1.DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED: return 'Characteristic Operation Not Supported';
99
+ case consts_1.DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE: return 'Characteristic Write Failed';
100
+ case consts_1.DC_RC_UNKNOWN_PROTOCOL_VERSION: return 'Unknown Protocol Version';
101
+ default:
102
+ return `Unknown (${code})`;
103
+ }
104
+ };
105
+ exports.RC = RC;
@@ -27,7 +27,6 @@ class AdapterFactory {
27
27
  if (existing)
28
28
  return existing;
29
29
  const ifaceName = typeof settings.interface === 'string' ? settings.interface : settings.interface.getName();
30
- console.log(`creating adapter for ${ifaceName}`);
31
30
  let adapter;
32
31
  switch (ifaceName) {
33
32
  case device_1.INTERFACE.SERIAL:
@@ -43,7 +42,6 @@ class AdapterFactory {
43
42
  case device_1.INTERFACE.DC:
44
43
  {
45
44
  const factory = ble_1.BleAdapterFactory.getInstance('wifi');
46
- console.log('creating wifi adapter for direct connect', factory);
47
45
  adapter = ble_1.BleAdapterFactory.getInstance('wifi').createInstance(settings, props);
48
46
  }
49
47
  break;
@@ -3,8 +3,14 @@ import SmartTrainerCyclingMode from "./antble-smarttrainer";
3
3
  import { UpdateRequest } from "./types";
4
4
  export default class AntAdvSimCyclingMode extends SmartTrainerCyclingMode {
5
5
  constructor(adapter: IncyclistDeviceAdapter, props?: any);
6
- getName(): string;
7
6
  getDescription(): string;
7
+ getConfig(): {
8
+ name: string;
9
+ isERG?: boolean;
10
+ isSIM?: boolean;
11
+ description: string;
12
+ properties: import("./types").CyclingModeProperty[];
13
+ };
8
14
  checkForResetOrEmpty(request: UpdateRequest): UpdateRequest | undefined;
9
15
  protected checkForTempPowerAdjustments(request: UpdateRequest, newRequest?: UpdateRequest): void;
10
16
  protected checkEmptyRequest(newRequest: UpdateRequest): void;
@@ -10,12 +10,13 @@ class AntAdvSimCyclingMode extends antble_smarttrainer_1.default {
10
10
  super(adapter, props);
11
11
  this.initLogger('AdvmartTrainerMode');
12
12
  }
13
- getName() {
14
- return 'Advanced Smart Trainer';
15
- }
16
13
  getDescription() {
17
14
  return 'Sends Slope to device. Respects Limits (from workout or settings). Calculates speed based on power and slope. ';
18
15
  }
16
+ getConfig() {
17
+ const config = super.getConfig();
18
+ return Object.assign(Object.assign({}, config), { name: 'Advanced Smart Trainer' });
19
+ }
19
20
  checkForResetOrEmpty(request) {
20
21
  if (!request || request.reset) {
21
22
  this.prevRequest = {};
@@ -46,5 +46,8 @@ export interface IAdapter extends EventEmitter, IBike, ISensor {
46
46
  resume(): Promise<boolean>;
47
47
  connect(): Promise<boolean>;
48
48
  close(): Promise<boolean>;
49
+ resetData(): void;
50
+ onScanStart(): void;
51
+ onScanStop(): void;
49
52
  onData(callback: OnDeviceDataCallback): any;
50
53
  }
@@ -5,6 +5,7 @@ export type InterfaceProps = {
5
5
  binding?: any;
6
6
  logger?: EventLogger;
7
7
  log?: boolean;
8
+ enabled?: boolean;
8
9
  };
9
10
  export interface IncyclistInterface extends EventEmitter {
10
11
  getName(): string;
@@ -30,15 +30,18 @@ export declare class InteruptableTask<T extends TaskState, P> {
30
30
  protected props?: TaskProps<T, P>;
31
31
  protected internalEvents: EventEmitter<[never]>;
32
32
  protected promise?: Promise<P>;
33
+ protected onStopNotifiers: Array<() => void>;
33
34
  constructor(promise: Promise<any>, props?: TaskProps<T, P>);
34
35
  getPromise(): Promise<P>;
35
36
  getState(): T;
36
37
  run(): Promise<P>;
38
+ notifyOnStop(cb: () => void): void;
37
39
  start(): void;
38
40
  stop(): Promise<boolean>;
39
41
  isRunning(): boolean;
40
42
  protected clearTimeout(): void;
41
43
  protected onTimeout(): void;
44
+ protected sendStopNotification(): void;
42
45
  protected logEvent(event: any): void;
43
46
  }
44
47
  export {};
package/lib/utils/task.js CHANGED
@@ -20,6 +20,7 @@ class InteruptableTask {
20
20
  var _a;
21
21
  this.internalState = { isRunning: false };
22
22
  this.internalEvents = new events_1.default();
23
+ this.onStopNotifiers = [];
23
24
  this.state = ((_a = props === null || props === void 0 ? void 0 : props.state) !== null && _a !== void 0 ? _a : {});
24
25
  this.props = props;
25
26
  delete this.props.state;
@@ -39,6 +40,9 @@ class InteruptableTask {
39
40
  return this.internalState.promise;
40
41
  });
41
42
  }
43
+ notifyOnStop(cb) {
44
+ this.onStopNotifiers.push(cb);
45
+ }
42
46
  start() {
43
47
  this.internalState.promise = new Promise((resolve, reject) => {
44
48
  var _a;
@@ -59,8 +63,10 @@ class InteruptableTask {
59
63
  isRunning: false,
60
64
  };
61
65
  this.internalEvents.removeAllListeners();
66
+ this.sendStopNotification();
62
67
  if (this.getState().result === 'completed' || this.getState().result === 'error')
63
68
  return;
69
+ this.getState().result = 'stopped';
64
70
  if (this.props.onDone)
65
71
  resolve(this.props.onDone(this.getState()));
66
72
  else
@@ -119,6 +125,12 @@ class InteruptableTask {
119
125
  else
120
126
  resolve(null);
121
127
  }
128
+ sendStopNotification() {
129
+ this.onStopNotifiers.forEach((cb) => {
130
+ if (typeof cb === 'function')
131
+ cb();
132
+ });
133
+ }
122
134
  logEvent(event) {
123
135
  if (this.props.log)
124
136
  this.props.log(event);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",