incyclist-devices 2.3.0-beta.3 → 2.3.0-beta.5

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,6 +79,7 @@ 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, {});
@@ -88,7 +89,12 @@ class DirectConnectPeripheral {
88
89
  response = yield this.send(seqNo, request);
89
90
  const res = message.parseResponse(response);
90
91
  const uuids = res.body.serviceDefinitions.map(s => (0, utils_1.beautifyUUID)(s.serviceUUID));
91
- this.logEvent({ message: 'DiscoverServices response', path: this.getPath(), uuids, raw: request.toString('hex') });
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') });
92
98
  return res.body.serviceDefinitions.map(s => s.serviceUUID);
93
99
  }
94
100
  catch (err) {
@@ -99,6 +105,7 @@ class DirectConnectPeripheral {
99
105
  }
100
106
  discoverCharacteristics(serviceUUID) {
101
107
  return __awaiter(this, void 0, void 0, function* () {
108
+ var _a, _b;
102
109
  const seqNo = this.getNextSeqNo();
103
110
  const message = new messages_1.DiscoverCharacteristicsMessage();
104
111
  const request = message.createRequest(seqNo, { serviceUUID: (0, utils_1.parseUUID)(serviceUUID) });
@@ -107,9 +114,14 @@ class DirectConnectPeripheral {
107
114
  try {
108
115
  response = yield this.send(seqNo, request);
109
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
+ }
110
122
  const service = (0, utils_1.beautifyUUID)(res.body.serviceUUID);
111
123
  const characteristics = res.body.characteristicDefinitions.map(cd => `${(0, utils_1.beautifyUUID)(cd.characteristicUUID)}:${cd.properties.join('/')}`);
112
- this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), service, characteristics, raw: request.toString('hex') });
124
+ this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), rc, service, characteristics, raw: response.toString('hex') });
113
125
  return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
114
126
  }
115
127
  catch (err) {
@@ -120,6 +132,7 @@ class DirectConnectPeripheral {
120
132
  }
121
133
  subscribe(characteristicUUID, callback) {
122
134
  return __awaiter(this, void 0, void 0, function* () {
135
+ var _a, _b;
123
136
  const seqNo = this.getNextSeqNo();
124
137
  const message = new messages_1.EnableCharacteristicNotificationsMessage();
125
138
  const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: true });
@@ -128,7 +141,12 @@ class DirectConnectPeripheral {
128
141
  try {
129
142
  response = yield this.send(seqNo, request);
130
143
  const res = message.parseResponse(response);
131
- this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
144
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
145
+ 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) {
146
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
147
+ return false;
148
+ }
149
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: response.toString('hex') });
132
150
  const confirmed = res.body.characteristicUUID;
133
151
  if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
134
152
  this.subscribed.push(characteristicUUID);
@@ -147,6 +165,7 @@ class DirectConnectPeripheral {
147
165
  }
148
166
  unsubscribe(characteristicUUID) {
149
167
  return __awaiter(this, void 0, void 0, function* () {
168
+ var _a, _b;
150
169
  try {
151
170
  const seqNo = this.getNextSeqNo();
152
171
  const message = new messages_1.EnableCharacteristicNotificationsMessage();
@@ -156,7 +175,12 @@ class DirectConnectPeripheral {
156
175
  this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: false, raw: request.toString('hex') });
157
176
  response = yield this.send(seqNo, request);
158
177
  const res = message.parseResponse(response);
159
- this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
178
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
179
+ 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) {
180
+ this.logEvent({ message: 'EnableCharacteristicNotifications failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
181
+ return false;
182
+ }
183
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: response.toString('hex') });
160
184
  const confirmed = res.body.characteristicUUID;
161
185
  if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
162
186
  this.subscribed.splice(this.subscribed.indexOf(characteristicUUID), 1);
@@ -214,12 +238,38 @@ class DirectConnectPeripheral {
214
238
  }
215
239
  subscribeSelected(characteristics, callback) {
216
240
  return __awaiter(this, void 0, void 0, function* () {
241
+ let services = [];
242
+ let supported = [];
243
+ try {
244
+ services = yield this.discoverServices();
245
+ }
246
+ catch (err) {
247
+ this.logEvent({ message: 'could not discover services', reason: err.message });
248
+ }
249
+ for (const element of services) {
250
+ const service = element;
251
+ try {
252
+ const c = yield this.discoverCharacteristics(service);
253
+ supported = supported.concat(c);
254
+ }
255
+ catch (err) {
256
+ this.logEvent({ message: 'could not discover characteristics', service, reason: err.message });
257
+ return false;
258
+ }
259
+ }
217
260
  const retry = [];
261
+ this.logEvent({ message: 'characteristics supported', requested: characteristics.map(c => (0, utils_1.beautifyUUID)(c)), supported: supported.map(c => (0, utils_1.beautifyUUID)(c.uuid)) });
218
262
  for (const element of characteristics) {
219
263
  const uuid = element;
220
- const success = yield this.subscribe(uuid, callback);
221
- if (!success)
222
- retry.push(uuid);
264
+ const found = supported.find(c => (0, utils_1.beautifyUUID)(c.uuid) === (0, utils_1.beautifyUUID)(uuid));
265
+ if (!found) {
266
+ this.logEvent({ message: 'characteristic not supported', requested: (0, utils_1.beautifyUUID)(uuid) });
267
+ }
268
+ else {
269
+ const success = yield this.subscribe(uuid, callback);
270
+ if (!success)
271
+ retry.push(uuid);
272
+ }
223
273
  }
224
274
  for (const element of retry) {
225
275
  const c = element;
@@ -240,6 +290,7 @@ class DirectConnectPeripheral {
240
290
  }
241
291
  read(characteristicUUID) {
242
292
  return __awaiter(this, void 0, void 0, function* () {
293
+ var _a, _b;
243
294
  const seqNo = this.getNextSeqNo();
244
295
  const message = new messages_1.ReadCharacteristicMessage();
245
296
  const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID) });
@@ -248,9 +299,14 @@ class DirectConnectPeripheral {
248
299
  this.logEvent({ message: 'ReadCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), raw: request.toString('hex') });
249
300
  response = yield this.send(seqNo, request);
250
301
  const res = message.parseResponse(response);
251
- this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
302
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
303
+ 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) {
304
+ this.logEvent({ message: 'ReadCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
305
+ return Buffer.from([]);
306
+ }
307
+ this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), rc, characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
252
308
  data: Buffer.from(res.body.characteristicData).toString('hex'),
253
- raw: request.toString('hex') });
309
+ raw: response.toString('hex') });
254
310
  return Buffer.from(res.body.characteristicData);
255
311
  }
256
312
  catch (err) {
@@ -277,17 +333,25 @@ class DirectConnectPeripheral {
277
333
  data: data.toString('hex'),
278
334
  raw: request.toString('hex') });
279
335
  this.send(seqNo, request).then((data) => {
336
+ var _a, _b;
280
337
  response = data;
281
338
  const res = message.parseResponse(response);
339
+ const rc = (0, messages_1.RC)((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.respCode);
340
+ 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) {
341
+ this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: rc });
342
+ resolve(Buffer.from([]));
343
+ return;
344
+ }
282
345
  characteristic = (0, utils_1.beautifyUUID)(res.body.characteristicUUID);
283
- this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), characteristic,
284
- raw: request.toString('hex') });
346
+ this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), rc, characteristic,
347
+ raw: response.toString('hex') });
285
348
  if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
286
349
  resolve(Buffer.from([]));
287
350
  }
288
351
  })
289
352
  .catch(err => {
290
353
  this.logEvent({ message: 'WriteCharacteristic failed', path: this.getPath(), characteristic, raw: response === null || response === void 0 ? void 0 : response.toString('hex'), reason: err.message });
354
+ resolve(Buffer.from([]));
291
355
  });
292
356
  });
293
357
  });
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.3.0-beta.3",
3
+ "version": "2.3.0-beta.5",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",