incyclist-devices 2.3.0-beta.1 → 2.3.0-beta.10

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.
@@ -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,15 +244,41 @@ 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;
@@ -205,7 +288,7 @@ class DirectConnectPeripheral {
205
288
  return __awaiter(this, void 0, void 0, function* () {
206
289
  const promises = [];
207
290
  this.subscribed.forEach(characteristicUUID => {
208
- promises.push(this.unsubscribe(characteristicUUID));
291
+ promises.push(this.unsubscribe((0, utils_1.parseUUID)(characteristicUUID)));
209
292
  });
210
293
  yield Promise.allSettled(promises);
211
294
  return true;
@@ -213,43 +296,69 @@ class DirectConnectPeripheral {
213
296
  }
214
297
  read(characteristicUUID) {
215
298
  return __awaiter(this, void 0, void 0, function* () {
299
+ var _a, _b;
216
300
  const seqNo = this.getNextSeqNo();
217
301
  const message = new messages_1.ReadCharacteristicMessage();
218
302
  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);
303
+ let response;
304
+ try {
305
+ this.logEvent({ message: 'ReadCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), raw: request.toString('hex') });
306
+ response = yield this.send(seqNo, request);
307
+ const res = message.parseResponse(response);
308
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
309
+ 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) {
310
+ this.logEvent({ message: 'ReadCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
311
+ return Buffer.from([]);
312
+ }
313
+ this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
314
+ data: Buffer.from(res.body.characteristicData).toString('hex'),
315
+ raw: response.toString('hex') });
316
+ return Buffer.from(res.body.characteristicData);
317
+ }
318
+ catch (err) {
319
+ this.logEvent({ message: 'ReadCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
320
+ return Buffer.from([]);
321
+ }
226
322
  });
227
323
  }
228
324
  write(characteristicUUID, data, options) {
229
325
  return __awaiter(this, void 0, void 0, function* () {
230
326
  return new Promise(resolve => {
327
+ const seqNo = this.getNextSeqNo();
328
+ const message = new messages_1.WriteCharacteristicMessage();
329
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), characteristicData: data });
330
+ let response;
331
+ let characteristic = characteristicUUID;
231
332
  if (!(options === null || options === void 0 ? void 0 : options.withoutResponse)) {
333
+ this.subscribe(characteristicUUID);
232
334
  const uuid = (0, utils_1.parseUUID)(characteristicUUID);
233
335
  this.eventEmitter.once(uuid, (data) => {
234
336
  resolve(data);
235
337
  });
236
338
  }
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
339
  this.logEvent({ message: 'WriteCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID),
241
340
  data: data.toString('hex'),
242
341
  raw: request.toString('hex') });
243
- this.send(seqNo, request).then((response) => {
342
+ this.send(seqNo, request).then((data) => {
343
+ var _a, _b;
344
+ response = data;
244
345
  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') });
346
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
347
+ 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) {
348
+ this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
349
+ resolve(Buffer.from([]));
350
+ return;
351
+ }
352
+ characteristic = (0, utils_1.beautifyUUID)(res.body.characteristicUUID);
353
+ this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), rc, characteristic,
354
+ raw: response.toString('hex') });
247
355
  if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
248
356
  resolve(Buffer.from([]));
249
357
  }
250
358
  })
251
359
  .catch(err => {
252
- this.logEvent({ message: 'WriteCharacteristic error', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), error: err.message });
360
+ this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), characteristic, raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
361
+ resolve(Buffer.from([]));
253
362
  });
254
363
  });
255
364
  });
@@ -362,7 +471,7 @@ class DirectConnectPeripheral {
362
471
  }
363
472
  if (incoming.length > header.length + 6) {
364
473
  this.remainingBuffer = Buffer.from(incoming.subarray(header.length + 6));
365
- incoming = incoming.subarray(0, header.length + 6);
474
+ incoming = Buffer.from(incoming.subarray(0, header.length + 6));
366
475
  }
367
476
  return incoming;
368
477
  }
@@ -386,7 +495,7 @@ class DirectConnectPeripheral {
386
495
  this.msgSeqNo = notification.header.seqNum;
387
496
  const uuid = (0, utils_1.parseUUID)(notification.body.characteristicUUID);
388
497
  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') });
498
+ data: Buffer.from(notification.body.characteristicData).toString('hex'), raw: incoming.toString('hex') });
390
499
  this.eventEmitter.emit(uuid, notification.body.characteristicData);
391
500
  }
392
501
  else {
@@ -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;
@@ -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
  }
@@ -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-beta.1",
3
+ "version": "2.3.0-beta.10",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",