incyclist-devices 2.2.10-beta.0 → 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.
Files changed (151) hide show
  1. package/lib/antv2/base/interface.js +2 -2
  2. package/lib/antv2/factories/index.d.ts +1 -0
  3. package/lib/antv2/factories/index.js +17 -0
  4. package/lib/antv2/fe/adapter.js +5 -4
  5. package/lib/antv2/index.d.ts +1 -0
  6. package/lib/antv2/index.js +12 -8
  7. package/lib/base/adpater.d.ts +4 -0
  8. package/lib/base/adpater.js +17 -3
  9. package/lib/ble/adapter-factory.d.ts +20 -24
  10. package/lib/ble/adapter-factory.js +13 -36
  11. package/lib/ble/base/adapter.d.ts +19 -5
  12. package/lib/ble/base/adapter.js +211 -62
  13. package/lib/ble/base/comms.d.ts +2 -74
  14. package/lib/ble/base/comms.js +3 -596
  15. package/lib/ble/base/interface.d.ts +102 -0
  16. package/lib/ble/base/interface.js +591 -0
  17. package/lib/ble/base/peripheral.d.ts +35 -0
  18. package/lib/ble/base/peripheral.js +311 -0
  19. package/lib/ble/base/sensor.d.ts +32 -0
  20. package/lib/ble/base/sensor.js +135 -0
  21. package/lib/ble/base/types.d.ts +7 -0
  22. package/lib/ble/base/types.js +7 -0
  23. package/lib/ble/bindings/mock.js +6 -0
  24. package/lib/ble/bindings/types.d.ts +3 -4
  25. package/lib/ble/ble-interface.d.ts +7 -4
  26. package/lib/ble/ble-interface.js +16 -2
  27. package/lib/ble/ble-peripheral.d.ts +1 -0
  28. package/lib/ble/ble-peripheral.js +7 -11
  29. package/lib/ble/consts.d.ts +0 -6
  30. package/lib/ble/consts.js +1 -7
  31. package/lib/ble/cp/adapter.d.ts +3 -3
  32. package/lib/ble/cp/adapter.js +12 -13
  33. package/lib/ble/cp/comm.d.ts +1 -1
  34. package/lib/ble/cp/comm.js +2 -2
  35. package/lib/ble/cp/index.d.ts +1 -1
  36. package/lib/ble/cp/index.js +2 -2
  37. package/lib/ble/cp/sensor.d.ts +27 -0
  38. package/lib/ble/cp/sensor.js +107 -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 +34 -0
  42. package/lib/ble/factories/adapter-factory.js +137 -0
  43. package/lib/ble/factories/index.d.ts +2 -0
  44. package/lib/ble/factories/index.js +18 -0
  45. package/lib/ble/factories/interface-factory.d.ts +7 -0
  46. package/lib/ble/factories/interface-factory.js +18 -0
  47. package/lib/ble/factories/types.d.ts +18 -0
  48. package/lib/ble/factories/types.js +2 -0
  49. package/lib/ble/fm/adapter.d.ts +12 -6
  50. package/lib/ble/fm/adapter.js +100 -108
  51. package/lib/ble/fm/comms.d.ts +1 -1
  52. package/lib/ble/fm/comms.js +3 -3
  53. package/lib/ble/fm/consts.d.ts +93 -0
  54. package/lib/ble/fm/consts.js +55 -1
  55. package/lib/ble/fm/index.d.ts +1 -1
  56. package/lib/ble/fm/index.js +2 -2
  57. package/lib/ble/fm/sensor.d.ts +44 -0
  58. package/lib/ble/fm/sensor.js +384 -0
  59. package/lib/ble/hr/adapter.d.ts +3 -3
  60. package/lib/ble/hr/adapter.js +5 -8
  61. package/lib/ble/hr/comm.d.ts +1 -1
  62. package/lib/ble/hr/comm.js +2 -2
  63. package/lib/ble/hr/index.d.ts +1 -1
  64. package/lib/ble/hr/index.js +2 -2
  65. package/lib/ble/hr/sensor.d.ts +17 -0
  66. package/lib/ble/hr/sensor.js +52 -0
  67. package/lib/ble/index.d.ts +4 -3
  68. package/lib/ble/index.js +29 -13
  69. package/lib/ble/tacx/adapter.d.ts +4 -2
  70. package/lib/ble/tacx/adapter.js +26 -66
  71. package/lib/ble/tacx/comms.d.ts +1 -1
  72. package/lib/ble/tacx/comms.js +2 -2
  73. package/lib/ble/tacx/consts.d.ts +23 -0
  74. package/lib/ble/tacx/consts.js +27 -0
  75. package/lib/ble/tacx/index.d.ts +1 -1
  76. package/lib/ble/tacx/index.js +2 -2
  77. package/lib/ble/tacx/sensor.d.ts +63 -0
  78. package/lib/ble/tacx/sensor.js +596 -0
  79. package/lib/ble/types.d.ts +87 -52
  80. package/lib/ble/utils.d.ts +7 -2
  81. package/lib/ble/utils.js +93 -9
  82. package/lib/ble/wahoo/adapter.d.ts +2 -2
  83. package/lib/ble/wahoo/adapter.js +4 -72
  84. package/lib/ble/wahoo/comms.d.ts +1 -1
  85. package/lib/ble/wahoo/comms.js +2 -2
  86. package/lib/ble/wahoo/consts.d.ts +16 -0
  87. package/lib/ble/wahoo/consts.js +7 -0
  88. package/lib/ble/wahoo/index.d.ts +1 -1
  89. package/lib/ble/wahoo/index.js +2 -2
  90. package/lib/ble/wahoo/sensor.d.ts +48 -0
  91. package/lib/ble/wahoo/sensor.js +356 -0
  92. package/lib/direct-connect/base/comms.d.ts +3 -0
  93. package/lib/direct-connect/base/comms.js +7 -0
  94. package/lib/direct-connect/base/interface.d.ts +75 -0
  95. package/lib/direct-connect/base/interface.js +311 -0
  96. package/lib/direct-connect/base/peripheral.d.ts +50 -0
  97. package/lib/direct-connect/base/peripheral.js +522 -0
  98. package/lib/direct-connect/base/sensor.d.ts +3 -0
  99. package/lib/direct-connect/base/sensor.js +7 -0
  100. package/lib/direct-connect/bindings/index.d.ts +1 -0
  101. package/lib/direct-connect/bindings/index.js +17 -0
  102. package/lib/direct-connect/bindings/types.d.ts +37 -0
  103. package/lib/direct-connect/bindings/types.js +2 -0
  104. package/lib/direct-connect/consts.d.ts +17 -0
  105. package/lib/direct-connect/consts.js +20 -0
  106. package/lib/direct-connect/index.d.ts +3 -0
  107. package/lib/direct-connect/index.js +22 -0
  108. package/lib/direct-connect/messages/CharacteristicNotification.d.ts +12 -0
  109. package/lib/direct-connect/messages/CharacteristicNotification.js +23 -0
  110. package/lib/direct-connect/messages/DiscoverCharacteristics.d.ts +22 -0
  111. package/lib/direct-connect/messages/DiscoverCharacteristics.js +43 -0
  112. package/lib/direct-connect/messages/DiscoverServices.d.ts +15 -0
  113. package/lib/direct-connect/messages/DiscoverServices.js +33 -0
  114. package/lib/direct-connect/messages/EnableCharacteristicNotifications.d.ts +18 -0
  115. package/lib/direct-connect/messages/EnableCharacteristicNotifications.js +35 -0
  116. package/lib/direct-connect/messages/ReadCharacteristic.d.ts +18 -0
  117. package/lib/direct-connect/messages/ReadCharacteristic.js +34 -0
  118. package/lib/direct-connect/messages/WriteCharacteristic.d.ts +18 -0
  119. package/lib/direct-connect/messages/WriteCharacteristic.js +36 -0
  120. package/lib/direct-connect/messages/error.d.ts +5 -0
  121. package/lib/direct-connect/messages/error.js +18 -0
  122. package/lib/direct-connect/messages/factory.d.ts +6 -0
  123. package/lib/direct-connect/messages/factory.js +44 -0
  124. package/lib/direct-connect/messages/index.d.ts +9 -0
  125. package/lib/direct-connect/messages/index.js +25 -0
  126. package/lib/direct-connect/messages/message.d.ts +21 -0
  127. package/lib/direct-connect/messages/message.js +105 -0
  128. package/lib/direct-connect/types.d.ts +24 -0
  129. package/lib/direct-connect/types.js +9 -0
  130. package/lib/direct-connect/utils.d.ts +5 -0
  131. package/lib/direct-connect/utils.js +73 -0
  132. package/lib/factories/adapters.js +14 -4
  133. package/lib/factories/interfaces.d.ts +2 -1
  134. package/lib/factories/interfaces.js +10 -7
  135. package/lib/index.d.ts +5 -4
  136. package/lib/index.js +6 -2
  137. package/lib/modes/antble-smarttrainer.d.ts +2 -16
  138. package/lib/modes/antble-smarttrainer.js +1 -98
  139. package/lib/modes/types.d.ts +1 -5
  140. package/lib/serial/bindings/tcp.d.ts +5 -4
  141. package/lib/serial/bindings/tcp.js +16 -23
  142. package/lib/types/adapter.d.ts +3 -0
  143. package/lib/types/device.d.ts +2 -1
  144. package/lib/types/device.js +1 -0
  145. package/lib/utils/calculations.d.ts +0 -1
  146. package/lib/utils/calculations.js +6 -22
  147. package/lib/utils/operation.d.ts +17 -0
  148. package/lib/utils/operation.js +20 -0
  149. package/lib/utils/task.d.ts +47 -0
  150. package/lib/utils/task.js +139 -0
  151. package/package.json +3 -1
@@ -0,0 +1,522 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DirectConnectPeripheral = void 0;
16
+ const task_1 = require("../../utils/task");
17
+ const interface_1 = __importDefault(require("./interface"));
18
+ const messages_1 = require("../messages");
19
+ const events_1 = __importDefault(require("events"));
20
+ const consts_1 = require("../consts");
21
+ const utils_1 = require("../../ble/utils");
22
+ class DirectConnectPeripheral {
23
+ static create(announcement) {
24
+ const { address, port } = announcement;
25
+ const key = `${address}:${port}`;
26
+ if (!this.instances[key])
27
+ this.instances[key] = new DirectConnectPeripheral(announcement);
28
+ return this.instances[key];
29
+ }
30
+ constructor(announcement) {
31
+ this.announcement = announcement;
32
+ this.msgSeqNo = 0;
33
+ this.onDataHandler = this.onData.bind(this);
34
+ this.onPortErrorHandler = this.onPortError.bind(this);
35
+ this.onPortCloseHandler = this.onPortClose.bind(this);
36
+ this.partialBuffer = null;
37
+ this.remainingBuffer = null;
38
+ this.eventEmitter = new events_1.default();
39
+ this.subscribed = [];
40
+ }
41
+ get services() {
42
+ const services = this.announcement.serviceUUIDs;
43
+ return services.map(s => ({ uuid: s }));
44
+ }
45
+ connect() {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ if (this.isConnected())
48
+ return true;
49
+ if (this.isConnecting())
50
+ yield this.connectTask.getPromise();
51
+ this.connectTask = new task_1.InteruptableTask(this.startConnection(), {
52
+ timeout: 1000,
53
+ });
54
+ return yield this.connectTask.run();
55
+ });
56
+ }
57
+ disconnect() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ try {
60
+ yield this.connectTask.stop();
61
+ yield this.stopConnection();
62
+ delete this.socket;
63
+ }
64
+ catch (err) {
65
+ return false;
66
+ }
67
+ return true;
68
+ });
69
+ }
70
+ isConnected() {
71
+ return this.socket !== undefined;
72
+ }
73
+ isConnecting() {
74
+ var _a;
75
+ return (_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.isRunning();
76
+ }
77
+ onDisconnect(callback) {
78
+ this.onDisconnectHandler = callback;
79
+ }
80
+ discoverServices() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ var _a, _b;
83
+ const seqNo = this.getNextSeqNo();
84
+ const message = new messages_1.DiscoverServiceMessage();
85
+ const request = message.createRequest(seqNo, {});
86
+ let response;
87
+ this.logEvent({ message: 'DiscoverServices request', path: this.getPath(), raw: request.toString('hex') });
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
+ }
104
+ });
105
+ }
106
+ discoverCharacteristics(serviceUUID) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ var _a, _b;
109
+ const seqNo = this.getNextSeqNo();
110
+ const message = new messages_1.DiscoverCharacteristicsMessage();
111
+ const request = message.createRequest(seqNo, { serviceUUID: (0, utils_1.parseUUID)(serviceUUID) });
112
+ let response;
113
+ this.logEvent({ message: 'DiscoverCharacteritics request', path: this.getPath(), service: (0, utils_1.beautifyUUID)(serviceUUID), raw: request.toString('hex') });
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
+ }
131
+ });
132
+ }
133
+ subscribe(characteristicUUID, callback) {
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
+ }
140
+ const seqNo = this.getNextSeqNo();
141
+ const message = new messages_1.EnableCharacteristicNotificationsMessage();
142
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: true });
143
+ let response;
144
+ this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: true, raw: request.toString('hex') });
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;
169
+ }
170
+ });
171
+ }
172
+ unsubscribe(characteristicUUID) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ var _a, _b;
175
+ try {
176
+ const seqNo = this.getNextSeqNo();
177
+ const message = new messages_1.EnableCharacteristicNotificationsMessage();
178
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: false });
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;
201
+ }
202
+ }
203
+ catch (err) {
204
+ this.logEvent({ messsage: 'EnableCharacteristicNotifications failed', reason: err.message });
205
+ return false;
206
+ }
207
+ });
208
+ }
209
+ subscribeAll(callback) {
210
+ return __awaiter(this, void 0, void 0, function* () {
211
+ try {
212
+ let services = [];
213
+ try {
214
+ services = yield this.discoverServices();
215
+ }
216
+ catch (err) {
217
+ this.logEvent({ message: 'could not discover services', reason: err.message });
218
+ }
219
+ for (const element of services) {
220
+ const service = element;
221
+ let characteristics = [];
222
+ try {
223
+ characteristics = yield this.discoverCharacteristics(service);
224
+ }
225
+ catch (err) {
226
+ this.logEvent({ message: 'could not discover characteristics', service, reason: err.message });
227
+ return false;
228
+ }
229
+ if (!(characteristics === null || characteristics === void 0 ? void 0 : characteristics.length))
230
+ continue;
231
+ for (const element of characteristics) {
232
+ const characteristic = element;
233
+ if (characteristic.properties.includes('notify'))
234
+ yield this.subscribe(characteristic.uuid, callback);
235
+ }
236
+ }
237
+ return true;
238
+ }
239
+ catch (err) {
240
+ this.logEvent({ message: 'could not subscribe', reason: err.message });
241
+ return false;
242
+ }
243
+ });
244
+ }
245
+ subscribeSelected(characteristics, callback) {
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
+ }
266
+ const retry = [];
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
+ }
279
+ }
280
+ for (const element of retry) {
281
+ const c = element;
282
+ yield this.subscribe(c.uuid, callback);
283
+ }
284
+ return true;
285
+ });
286
+ }
287
+ unsubscribeAll() {
288
+ return __awaiter(this, void 0, void 0, function* () {
289
+ const promises = [];
290
+ this.subscribed.forEach(characteristicUUID => {
291
+ promises.push(this.unsubscribe((0, utils_1.parseUUID)(characteristicUUID)));
292
+ });
293
+ yield Promise.allSettled(promises);
294
+ return true;
295
+ });
296
+ }
297
+ read(characteristicUUID) {
298
+ return __awaiter(this, void 0, void 0, function* () {
299
+ var _a, _b;
300
+ const seqNo = this.getNextSeqNo();
301
+ const message = new messages_1.ReadCharacteristicMessage();
302
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID) });
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
+ }
322
+ });
323
+ }
324
+ write(characteristicUUID, data, options) {
325
+ return __awaiter(this, void 0, void 0, function* () {
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;
332
+ if (!(options === null || options === void 0 ? void 0 : options.withoutResponse)) {
333
+ this.subscribe(characteristicUUID);
334
+ const uuid = (0, utils_1.parseUUID)(characteristicUUID);
335
+ this.eventEmitter.once(uuid, (data) => {
336
+ resolve(data);
337
+ });
338
+ }
339
+ this.logEvent({ message: 'WriteCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID),
340
+ data: data.toString('hex'),
341
+ raw: request.toString('hex') });
342
+ this.send(seqNo, request).then((data) => {
343
+ var _a, _b;
344
+ response = data;
345
+ const res = message.parseResponse(response);
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') });
355
+ if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
356
+ resolve(Buffer.from([]));
357
+ }
358
+ })
359
+ .catch(err => {
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([]));
362
+ });
363
+ });
364
+ });
365
+ }
366
+ startConnection() {
367
+ return __awaiter(this, void 0, void 0, function* () {
368
+ const { address, port } = this.announcement;
369
+ try {
370
+ const net = this.getBinding().net;
371
+ this.socket = net.createSocket();
372
+ return new Promise((resolve, reject) => {
373
+ this.socket.once('timeout', () => {
374
+ this.stopConnection().then(() => resolve(false));
375
+ });
376
+ this.socket.once('error', (err) => {
377
+ this.stopConnection().then(() => resolve(false));
378
+ });
379
+ this.socket.once('connect', () => {
380
+ this.socket.on('data', this.onDataHandler);
381
+ this.socket.on('error', this.onPortErrorHandler);
382
+ this.socket.on('close', this.onPortCloseHandler);
383
+ this.socket.on('end', this.onPortCloseHandler);
384
+ resolve(true);
385
+ });
386
+ this.socket.connect(port, address);
387
+ });
388
+ }
389
+ catch (err) {
390
+ return false;
391
+ }
392
+ });
393
+ }
394
+ onPortError(err) {
395
+ this.logEvent({ message: 'port error', path: this.getPath(), reason: err.message });
396
+ }
397
+ onPortClose() {
398
+ return __awaiter(this, void 0, void 0, function* () {
399
+ this.socket.removeAllListeners();
400
+ this.logEvent({ message: 'port closed', path: this.getPath() });
401
+ try {
402
+ yield this.disconnect();
403
+ }
404
+ catch (_a) { }
405
+ if (this.onDisconnectHandler)
406
+ this.onDisconnectHandler();
407
+ });
408
+ }
409
+ getPath() {
410
+ const { address, port } = this.announcement;
411
+ const path = `${address}:${port}`;
412
+ return path;
413
+ }
414
+ stopConnection() {
415
+ return __awaiter(this, void 0, void 0, function* () {
416
+ this.eventEmitter.removeAllListeners();
417
+ if (!this.isConnected())
418
+ return true;
419
+ yield this.unsubscribeAll();
420
+ this.socket.removeAllListeners();
421
+ return new Promise(done => {
422
+ const onClosed = () => {
423
+ this.socket.removeAllListeners();
424
+ delete this.socket;
425
+ done(true);
426
+ };
427
+ const onCloseError = (err) => {
428
+ this.logEvent({ message: 'port error', path: this.getPath(), reason: err.message });
429
+ this.socket.removeAllListeners();
430
+ delete this.socket;
431
+ done(false);
432
+ };
433
+ this.socket.on('end', onClosed);
434
+ this.socket.on('error', onCloseError);
435
+ this.socket.on('close', onClosed);
436
+ this.socket.destroy();
437
+ });
438
+ });
439
+ }
440
+ getNextSeqNo() {
441
+ this.msgSeqNo = (this.msgSeqNo + 1) % 256;
442
+ return this.msgSeqNo;
443
+ }
444
+ send(seqNo, data) {
445
+ return __awaiter(this, void 0, void 0, function* () {
446
+ this.socket.write(data);
447
+ return new Promise(done => {
448
+ this.eventEmitter.once(`response-${seqNo}`, (response) => {
449
+ done(response);
450
+ });
451
+ });
452
+ });
453
+ }
454
+ getNextMessage(data) {
455
+ let incoming = data;
456
+ if (this.partialBuffer) {
457
+ incoming = Buffer.concat([
458
+ Buffer.from(this.partialBuffer),
459
+ Buffer.from(data)
460
+ ]);
461
+ delete this.partialBuffer;
462
+ }
463
+ if (incoming.length < 6) {
464
+ this.partialBuffer = Buffer.from(incoming);
465
+ return null;
466
+ }
467
+ const header = (0, messages_1.parseHeader)(incoming);
468
+ if (incoming.length < header.length + 6) {
469
+ this.partialBuffer = Buffer.from(incoming);
470
+ return null;
471
+ }
472
+ if (incoming.length > header.length + 6) {
473
+ this.remainingBuffer = Buffer.from(incoming.subarray(header.length + 6));
474
+ incoming = Buffer.from(incoming.subarray(0, header.length + 6));
475
+ }
476
+ return incoming;
477
+ }
478
+ processRemaining() {
479
+ if (this.remainingBuffer) {
480
+ const next = Buffer.from(this.remainingBuffer);
481
+ delete this.remainingBuffer;
482
+ this.onData(next);
483
+ }
484
+ }
485
+ onData(data) {
486
+ return __awaiter(this, void 0, void 0, function* () {
487
+ const incoming = this.getNextMessage(data);
488
+ if (incoming === null)
489
+ return;
490
+ try {
491
+ const header = (0, messages_1.parseHeader)(incoming);
492
+ if (header.msgId === consts_1.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION) {
493
+ const message = new messages_1.CharacteristicNotificationMessage();
494
+ const notification = message.parseResponse(incoming);
495
+ this.msgSeqNo = notification.header.seqNum;
496
+ const uuid = (0, utils_1.parseUUID)(notification.body.characteristicUUID);
497
+ this.logEvent({ message: 'Characteristic notification', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(notification.body.characteristicUUID),
498
+ data: Buffer.from(notification.body.characteristicData).toString('hex'), raw: incoming.toString('hex') });
499
+ this.eventEmitter.emit(uuid, notification.body.characteristicData);
500
+ }
501
+ else {
502
+ this.eventEmitter.emit(`response-${header.seqNum}`, incoming);
503
+ }
504
+ }
505
+ catch (err) {
506
+ this.logEvent({ message: 'error', fn: 'onData', error: err.message, stack: err.stack, path: this.getPath(), raw: incoming.toString('hex') });
507
+ }
508
+ this.processRemaining();
509
+ });
510
+ }
511
+ getInterface() {
512
+ return interface_1.default.getInstance();
513
+ }
514
+ logEvent(event) {
515
+ this.getInterface().logEvent(event);
516
+ }
517
+ getBinding() {
518
+ return this.getInterface().getBinding();
519
+ }
520
+ }
521
+ exports.DirectConnectPeripheral = DirectConnectPeripheral;
522
+ DirectConnectPeripheral.instances = {};
@@ -0,0 +1,3 @@
1
+ import { BleComms } from "../../ble/base/comms";
2
+ export declare class DirectConnectComms extends BleComms {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DirectConnectComms = void 0;
4
+ const comms_1 = require("../../ble/base/comms");
5
+ class DirectConnectComms extends comms_1.BleComms {
6
+ }
7
+ exports.DirectConnectComms = DirectConnectComms;
@@ -0,0 +1 @@
1
+ export * from './types';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,37 @@
1
+ import EventEmitter from "events";
2
+ import { PeripheralAnnouncement } from "../../ble/types";
3
+ export interface Socket extends EventEmitter {
4
+ connect(port: number, host: string): Socket;
5
+ destroy(): void;
6
+ write(data: Buffer): boolean;
7
+ }
8
+ export interface NetBinding {
9
+ createSocket(): Socket;
10
+ }
11
+ export interface DirectConnectBinding {
12
+ mdns: MulticastDnsBinding;
13
+ net: NetBinding;
14
+ }
15
+ type KeyValue = {
16
+ [key: string]: any;
17
+ };
18
+ export interface BrowserConfig {
19
+ type?: string;
20
+ name?: string;
21
+ protocol?: 'tcp' | 'udp';
22
+ subtypes?: string[];
23
+ txt?: KeyValue;
24
+ }
25
+ export interface MulticastDnsAnnouncement extends PeripheralAnnouncement {
26
+ type: string;
27
+ address: string;
28
+ protocol?: 'tcp' | 'udp';
29
+ port: number;
30
+ serialNo?: string;
31
+ }
32
+ export interface MulticastDnsBinding {
33
+ connect(): void;
34
+ disconnect(): void;
35
+ find(opts: BrowserConfig | null, onup?: (service: MulticastDnsAnnouncement) => void): void;
36
+ }
37
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ export declare const DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = 0;
2
+ export declare const DC_RC_UNKNOWN_MESSAGE_TYPE = 1;
3
+ export declare const DC_RC_UNEXPECTED_ERROR = 2;
4
+ export declare const DC_RC_SERVICE_NOT_FOUND = 3;
5
+ export declare const DC_RC_CHARACTERISTIC_NOT_FOUND = 4;
6
+ export declare const DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = 5;
7
+ export declare const DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = 6;
8
+ export declare const DC_RC_UNKNOWN_PROTOCOL_VERSION = 7;
9
+ export declare const DC_MESSAGE_DISCOVER_SERVICES = 1;
10
+ export declare const DC_MESSAGE_DISCOVER_CHARACTERISTICS = 2;
11
+ export declare const DC_MESSAGE_READ_CHARACTERISTIC = 3;
12
+ export declare const DC_MESSAGE_WRITE_CHARACTERISTIC = 4;
13
+ export declare const DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = 5;
14
+ export declare const DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = 6;
15
+ export declare const DC_ERROR_INVALID_MESSAGE_TYPE = 1;
16
+ export declare const DC_ERROR_INVALID_MESSAGE_LENGTH = 2;
17
+ export declare const DC_ERROR_UNKNOWN_MESSAGE_TYPE = 3;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DC_ERROR_UNKNOWN_MESSAGE_TYPE = exports.DC_ERROR_INVALID_MESSAGE_LENGTH = exports.DC_ERROR_INVALID_MESSAGE_TYPE = exports.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = exports.DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = exports.DC_MESSAGE_WRITE_CHARACTERISTIC = exports.DC_MESSAGE_READ_CHARACTERISTIC = exports.DC_MESSAGE_DISCOVER_CHARACTERISTICS = exports.DC_MESSAGE_DISCOVER_SERVICES = exports.DC_RC_UNKNOWN_PROTOCOL_VERSION = exports.DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = exports.DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = exports.DC_RC_CHARACTERISTIC_NOT_FOUND = exports.DC_RC_SERVICE_NOT_FOUND = exports.DC_RC_UNEXPECTED_ERROR = exports.DC_RC_UNKNOWN_MESSAGE_TYPE = exports.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = void 0;
4
+ exports.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = 0;
5
+ exports.DC_RC_UNKNOWN_MESSAGE_TYPE = 1;
6
+ exports.DC_RC_UNEXPECTED_ERROR = 2;
7
+ exports.DC_RC_SERVICE_NOT_FOUND = 3;
8
+ exports.DC_RC_CHARACTERISTIC_NOT_FOUND = 4;
9
+ exports.DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = 5;
10
+ exports.DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = 6;
11
+ exports.DC_RC_UNKNOWN_PROTOCOL_VERSION = 7;
12
+ exports.DC_MESSAGE_DISCOVER_SERVICES = 0x01;
13
+ exports.DC_MESSAGE_DISCOVER_CHARACTERISTICS = 0x02;
14
+ exports.DC_MESSAGE_READ_CHARACTERISTIC = 0x03;
15
+ exports.DC_MESSAGE_WRITE_CHARACTERISTIC = 0x04;
16
+ exports.DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = 0x05;
17
+ exports.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = 0x06;
18
+ exports.DC_ERROR_INVALID_MESSAGE_TYPE = 1;
19
+ exports.DC_ERROR_INVALID_MESSAGE_LENGTH = 2;
20
+ exports.DC_ERROR_UNKNOWN_MESSAGE_TYPE = 3;
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './consts';
3
+ export * from './messages';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const interface_factory_1 = require("../ble/factories/interface-factory");
18
+ const interface_1 = require("./base/interface");
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./consts"), exports);
21
+ __exportStar(require("./messages"), exports);
22
+ interface_factory_1.BleMultiTransportInterfaceFactory.register('wifi', interface_1.DirectConnectInterfaceFactory);