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,35 @@
1
+ import { BleCharacteristic, BlePeripheralAnnouncement, BleRawCharacteristic, BleRawPeripheral, BleService, BleWriteProps, IBlePeripheral } from "../types";
2
+ import { BleInterface } from "./interface";
3
+ export declare class BlePeripheral implements IBlePeripheral {
4
+ protected announcement: BlePeripheralAnnouncement;
5
+ protected connected: boolean;
6
+ protected characteristics: Record<string, BleRawCharacteristic>;
7
+ protected onDisconnectHandler?: () => void;
8
+ protected ble: BleInterface;
9
+ protected subscribed: Array<{
10
+ uuid: string;
11
+ callback: (data: Buffer) => void;
12
+ }>;
13
+ protected disconnecting: boolean;
14
+ constructor(announcement: BlePeripheralAnnouncement);
15
+ get services(): BleService[];
16
+ protected getPeripheral(): BleRawPeripheral;
17
+ connect(): Promise<boolean>;
18
+ disconnect(): Promise<boolean>;
19
+ isConnected(): boolean;
20
+ isConnecting(): boolean;
21
+ onDisconnect(callback: () => void): void;
22
+ discoverServices(): Promise<string[]>;
23
+ discoverCharacteristics(serviceUUID: string): Promise<BleCharacteristic[]>;
24
+ subscribe(characteristicUUID: string, callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
25
+ unsubscribe(characteristicUUID: string): Promise<boolean>;
26
+ subscribeSelected(characteristics: string[], callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
27
+ discoverAllCharacteristics(): Promise<string[]>;
28
+ discoverSomeCharacteristics(characteristics: string[]): Promise<string[]>;
29
+ subscribeAll(callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
30
+ unsubscribeAll(): Promise<boolean>;
31
+ read(characteristicUUID: string): Promise<Buffer>;
32
+ write(characteristicUUID: string, data: Buffer, options?: BleWriteProps): Promise<Buffer>;
33
+ protected getRawCharacteristic(uuid: string): BleRawCharacteristic;
34
+ logEvent(event: any): void;
35
+ }
@@ -0,0 +1,311 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BlePeripheral = void 0;
13
+ const utils_1 = require("../utils");
14
+ const interface_1 = require("./interface");
15
+ class BlePeripheral {
16
+ constructor(announcement) {
17
+ this.announcement = announcement;
18
+ this.connected = false;
19
+ this.characteristics = {};
20
+ this.subscribed = [];
21
+ this.disconnecting = false;
22
+ this.ble = interface_1.BleInterface.getInstance();
23
+ }
24
+ get services() {
25
+ return this.announcement.peripheral.services;
26
+ }
27
+ getPeripheral() {
28
+ return this.announcement.peripheral;
29
+ }
30
+ connect() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ if (this.isConnected())
33
+ return true;
34
+ yield this.getPeripheral().connectAsync();
35
+ this.ble.registerConnected(this);
36
+ this.connected = true;
37
+ return this.connected;
38
+ });
39
+ }
40
+ disconnect() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ this.disconnecting = true;
43
+ if (!this.isConnected())
44
+ return true;
45
+ yield this.unsubscribeAll();
46
+ Object.keys(this.characteristics).forEach(uuid => {
47
+ const c = this.characteristics[uuid];
48
+ c.removeAllListeners();
49
+ });
50
+ if (!this.getPeripheral().disconnectAsync) {
51
+ this.getPeripheral().disconnectAsync = () => {
52
+ return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
53
+ };
54
+ }
55
+ yield this.getPeripheral().disconnectAsync();
56
+ this.connected = false;
57
+ this.disconnecting = false;
58
+ return !this.connected;
59
+ });
60
+ }
61
+ isConnected() {
62
+ return this.connected;
63
+ }
64
+ isConnecting() {
65
+ return false;
66
+ }
67
+ onDisconnect(callback) {
68
+ this.onDisconnectHandler = callback;
69
+ }
70
+ discoverServices() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ if (this.getPeripheral().discoverServicesAsync) {
73
+ this.logEvent({ message: 'discover services' });
74
+ const services = yield this.getPeripheral().discoverServicesAsync([]);
75
+ return services.map(s => s.uuid);
76
+ }
77
+ else {
78
+ this.logEvent({ message: 'discover services and characteristics' });
79
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
80
+ return res.services.map(s => s.uuid);
81
+ }
82
+ });
83
+ }
84
+ discoverCharacteristics(serviceUUID) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([serviceUUID], []);
87
+ res.characteristics.forEach(c => this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c);
88
+ return res.characteristics.map(c => {
89
+ const { uuid, properties, name, _serviceUuid } = c;
90
+ return { uuid, properties, name, _serviceUuid };
91
+ });
92
+ });
93
+ }
94
+ subscribe(characteristicUUID, callback) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ try {
97
+ if (this.disconnecting || !this.connected)
98
+ return false;
99
+ const onData = (data) => {
100
+ try {
101
+ callback(characteristicUUID, data);
102
+ }
103
+ catch (_a) { }
104
+ };
105
+ const subscription = this.subscribed.find(s => s.uuid === characteristicUUID);
106
+ if (subscription) {
107
+ const c = this.getRawCharacteristic(characteristicUUID);
108
+ if (c)
109
+ c.on('data', onData);
110
+ return true;
111
+ }
112
+ const c = this.getRawCharacteristic(characteristicUUID);
113
+ if (!c) {
114
+ return false;
115
+ }
116
+ return new Promise((resolve, reject) => {
117
+ const info = this.subscribed.find(s => s.uuid === characteristicUUID);
118
+ if (info) {
119
+ return Promise.resolve(true);
120
+ }
121
+ const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
122
+ this.logEvent({ message: 'subscribe request', characteristic: uuid, success: true });
123
+ c.subscribe((err) => {
124
+ if (err) {
125
+ this.logEvent({ message: 'subscribe result', characteristic: uuid, success: false, reason: err.message });
126
+ resolve(false);
127
+ }
128
+ else {
129
+ if (callback) {
130
+ this.subscribed.push({ uuid: characteristicUUID, callback: onData });
131
+ c.on('data', onData);
132
+ }
133
+ else {
134
+ this.subscribed.push({ uuid: characteristicUUID, callback: null });
135
+ }
136
+ this.logEvent({ message: 'subscribe result', characteristic: uuid, success: true });
137
+ resolve(true);
138
+ }
139
+ });
140
+ });
141
+ }
142
+ catch (err) {
143
+ this.logEvent({ message: 'Error', fn: 'subscribe', error: err.message, stack: err.stack });
144
+ return false;
145
+ }
146
+ });
147
+ }
148
+ unsubscribe(characteristicUUID) {
149
+ try {
150
+ const subscription = this.subscribed.find(s => s.uuid === characteristicUUID);
151
+ if (!subscription) {
152
+ return Promise.resolve(true);
153
+ }
154
+ const c = this.getRawCharacteristic(characteristicUUID);
155
+ if (!c) {
156
+ return Promise.resolve(false);
157
+ }
158
+ return new Promise((resolve, reject) => {
159
+ c.unsubscribe((err) => {
160
+ if (err) {
161
+ resolve(false);
162
+ }
163
+ else {
164
+ const info = this.subscribed.find(s => s.uuid === characteristicUUID);
165
+ if (info) {
166
+ this.subscribed.splice(this.subscribed.indexOf(info), 1);
167
+ if (info.callback)
168
+ c.off('data', info.callback);
169
+ }
170
+ resolve(true);
171
+ }
172
+ });
173
+ });
174
+ }
175
+ catch (err) {
176
+ this.logEvent({ message: 'Error', fn: 'unsubscribe', error: err.message, stack: err.stack });
177
+ return Promise.resolve(false);
178
+ }
179
+ }
180
+ subscribeSelected(characteristics, callback) {
181
+ return __awaiter(this, void 0, void 0, function* () {
182
+ try {
183
+ if (Object.keys(this.characteristics).length === 0) {
184
+ yield this.discoverAllCharacteristics();
185
+ }
186
+ const retry = [];
187
+ for (let i = 0; i < characteristics.length; i++) {
188
+ const c = this.getRawCharacteristic(characteristics[i]);
189
+ if (c === null || c === void 0 ? void 0 : c.properties.includes('notify')) {
190
+ const success = yield this.subscribe(c.uuid, callback);
191
+ if (!success)
192
+ retry.push(c);
193
+ }
194
+ }
195
+ for (let i = 0; i < retry.length; i++) {
196
+ const c = retry[i];
197
+ yield this.subscribe(c.uuid, callback);
198
+ }
199
+ return true;
200
+ }
201
+ catch (err) {
202
+ this.logEvent({ message: 'Error', fn: 'subscribeSelected', error: err.message, stack: err.stack });
203
+ return false;
204
+ }
205
+ });
206
+ }
207
+ discoverAllCharacteristics() {
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ try {
210
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
211
+ const found = [];
212
+ res.characteristics.forEach(c => {
213
+ this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c;
214
+ found.push(c.uuid);
215
+ });
216
+ return found;
217
+ }
218
+ catch (err) {
219
+ this.logEvent({ message: 'Error', fn: 'discoverAllCharacteristics', error: err.message, stack: err.stack });
220
+ return [];
221
+ }
222
+ });
223
+ }
224
+ discoverSomeCharacteristics(characteristics) {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ try {
227
+ const target = characteristics.map(c => (0, utils_1.fullUUID)(c));
228
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], target);
229
+ const found = [];
230
+ res.characteristics.forEach(c => {
231
+ this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c;
232
+ found.push(c.uuid);
233
+ });
234
+ return found;
235
+ }
236
+ catch (err) {
237
+ this.logEvent({ message: 'Error', fn: 'discoverAllCharacteristics', error: err.message, stack: err.stack });
238
+ return [];
239
+ }
240
+ });
241
+ }
242
+ subscribeAll(callback) {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ const characteristics = yield this.discoverAllCharacteristics();
245
+ const success = yield this.subscribeSelected(characteristics, callback);
246
+ return success;
247
+ });
248
+ }
249
+ unsubscribeAll() {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ const promises = [];
252
+ this.subscribed.forEach(d => {
253
+ promises.push(this.unsubscribe(d.uuid));
254
+ });
255
+ yield Promise.allSettled(promises);
256
+ return true;
257
+ });
258
+ }
259
+ read(characteristicUUID) {
260
+ if (this.disconnecting || !this.connected)
261
+ return Promise.resolve(Buffer.from([]));
262
+ const c = this.characteristics[(0, utils_1.beautifyUUID)(characteristicUUID)];
263
+ if (!c) {
264
+ return Promise.reject(new Error('characteristic not found'));
265
+ }
266
+ return new Promise((resolve, reject) => {
267
+ c.read((err, data) => {
268
+ if (err) {
269
+ reject(err);
270
+ }
271
+ else {
272
+ resolve(data);
273
+ }
274
+ });
275
+ });
276
+ }
277
+ write(characteristicUUID, data, options) {
278
+ if (this.disconnecting || !this.connected)
279
+ return Promise.resolve(Buffer.from([]));
280
+ const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
281
+ const c = this.characteristics[uuid];
282
+ if (!c) {
283
+ return Promise.reject(new Error('characteristic not found'));
284
+ }
285
+ return new Promise((resolve, reject) => {
286
+ this.subscribe(characteristicUUID, null).then(success => {
287
+ if (this.disconnecting || !this.connected)
288
+ return Promise.resolve(Buffer.from([]));
289
+ c.on('data', (data) => {
290
+ c.removeAllListeners('data');
291
+ resolve(data);
292
+ });
293
+ this.logEvent({ message: 'write request', characteristic: uuid, data: data.toString('hex'), withoutResponse: (options === null || options === void 0 ? void 0 : options.withoutResponse) === true });
294
+ c.write(data, (options === null || options === void 0 ? void 0 : options.withoutResponse) === true, (err) => {
295
+ if (err)
296
+ reject(err);
297
+ });
298
+ if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
299
+ resolve(Buffer.from([]));
300
+ }
301
+ });
302
+ });
303
+ }
304
+ getRawCharacteristic(uuid) {
305
+ return this.characteristics[(0, utils_1.beautifyUUID)(uuid)];
306
+ }
307
+ logEvent(event) {
308
+ this.ble.logEvent(event);
309
+ }
310
+ }
311
+ exports.BlePeripheral = BlePeripheral;
@@ -0,0 +1,32 @@
1
+ import { LegacyProfile } from "../../antv2/types";
2
+ import { BleWriteProps, IBlePeripheral, BleProtocol, IBleSensor } from "../types";
3
+ import { EventLogger } from "gd-eventlog";
4
+ import EventEmitter from "events";
5
+ export declare class TBleSensor extends EventEmitter implements IBleSensor {
6
+ protected peripheral: IBlePeripheral;
7
+ static readonly protocol: BleProtocol;
8
+ protected logger: EventLogger;
9
+ protected stopRequested: boolean;
10
+ protected onDataHandler: any;
11
+ logEvent(event: any, ...args: any[]): void;
12
+ constructor(peripheral: IBlePeripheral, props?: {
13
+ logger?: EventLogger;
14
+ });
15
+ getDetectionPriority(): number;
16
+ getProfile(): LegacyProfile;
17
+ getProtocol(): BleProtocol;
18
+ getServiceUUids(): string[];
19
+ isMatching(serviceUUIDs: string[]): boolean;
20
+ hasPeripheral(): boolean;
21
+ startSensor(reconnect?: boolean): Promise<boolean>;
22
+ protected getRequiredCharacteristics(): Array<string>;
23
+ subscribe(): Promise<boolean>;
24
+ stopSensor(): Promise<boolean>;
25
+ reconnectSensor(): Promise<void>;
26
+ reset(): void;
27
+ isConnected(): boolean;
28
+ read(characteristicUUID: string): Promise<Buffer>;
29
+ write(characteristicUUID: string, data: Buffer, options?: BleWriteProps): Promise<Buffer>;
30
+ onData(characteristic: string, data: Buffer): boolean;
31
+ protected getDefaultLogger(): EventLogger;
32
+ }
@@ -0,0 +1,135 @@
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.TBleSensor = void 0;
16
+ const utils_1 = require("../../utils/utils");
17
+ const gd_eventlog_1 = require("gd-eventlog");
18
+ const utils_2 = require("../utils");
19
+ const events_1 = __importDefault(require("events"));
20
+ class TBleSensor extends events_1.default {
21
+ logEvent(event, ...args) {
22
+ this.logger.logEvent(event, ...args);
23
+ }
24
+ constructor(peripheral, props) {
25
+ super();
26
+ this.peripheral = peripheral;
27
+ this.logger = (props === null || props === void 0 ? void 0 : props.logger) || this.getDefaultLogger();
28
+ this.reset();
29
+ this.onDataHandler = this.onData.bind(this);
30
+ }
31
+ getDetectionPriority() {
32
+ var _a;
33
+ const C = this.constructor;
34
+ return (_a = C['detectionPriority']) !== null && _a !== void 0 ? _a : 0;
35
+ }
36
+ getProfile() {
37
+ const C = this.constructor;
38
+ return C['profile'];
39
+ }
40
+ getProtocol() {
41
+ const C = this.constructor;
42
+ return C['protocol'];
43
+ }
44
+ getServiceUUids() {
45
+ const C = this.constructor;
46
+ return C['services'];
47
+ }
48
+ isMatching(serviceUUIDs) {
49
+ const uuids = serviceUUIDs.map(uuid => (0, utils_2.beautifyUUID)(uuid));
50
+ const required = this.getServiceUUids();
51
+ if (!required)
52
+ return true;
53
+ let missing = false;
54
+ required.forEach(uuid => {
55
+ if (!uuids.includes((0, utils_2.beautifyUUID)(uuid))) {
56
+ missing = true;
57
+ }
58
+ });
59
+ return (missing === false);
60
+ }
61
+ hasPeripheral() {
62
+ return !!this.peripheral;
63
+ }
64
+ startSensor(reconnect) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ if (!reconnect)
67
+ this.stopRequested = false;
68
+ if (!this.peripheral) {
69
+ this.logEvent({ message: 'no peripheral' });
70
+ return false;
71
+ }
72
+ const connected = yield this.peripheral.connect();
73
+ if (!connected)
74
+ return false;
75
+ if (!reconnect)
76
+ this.peripheral.onDisconnect(this.reconnectSensor.bind(this));
77
+ return true;
78
+ });
79
+ }
80
+ getRequiredCharacteristics() {
81
+ return null;
82
+ }
83
+ subscribe() {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ const selected = this.getRequiredCharacteristics();
86
+ if (selected === null)
87
+ return yield this.peripheral.subscribeAll(this.onDataHandler);
88
+ if (selected.length === 0)
89
+ return true;
90
+ return yield this.peripheral.subscribeSelected(selected, this.onDataHandler);
91
+ });
92
+ }
93
+ stopSensor() {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ this.removeAllListeners();
96
+ if (!this.peripheral)
97
+ return true;
98
+ this.stopRequested = true;
99
+ return yield this.peripheral.disconnect();
100
+ });
101
+ }
102
+ reconnectSensor() {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ let success = false;
105
+ do {
106
+ success = yield this.startSensor(true);
107
+ if (!success) {
108
+ yield (0, utils_1.sleep)(5000);
109
+ }
110
+ } while (!success || this.stopRequested);
111
+ });
112
+ }
113
+ reset() {
114
+ throw new Error("Method not implemented.");
115
+ }
116
+ isConnected() {
117
+ var _a;
118
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.isConnected();
119
+ }
120
+ read(characteristicUUID) {
121
+ var _a;
122
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.read(characteristicUUID);
123
+ }
124
+ write(characteristicUUID, data, options) {
125
+ var _a;
126
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.write(characteristicUUID, data, options);
127
+ }
128
+ onData(characteristic, data) {
129
+ return true;
130
+ }
131
+ getDefaultLogger() {
132
+ return new gd_eventlog_1.EventLogger(this.constructor.name);
133
+ }
134
+ }
135
+ exports.TBleSensor = TBleSensor;
@@ -1,2 +1,9 @@
1
+ import { IBleInterface } from "../types";
1
2
  export interface BleDeviceData {
2
3
  }
4
+ export interface IInterfaceFactory {
5
+ getInterface(): IBleInterface<any>;
6
+ }
7
+ export declare class InterfaceFactory implements IInterfaceFactory {
8
+ getInterface(): IBleInterface<any>;
9
+ }
@@ -1,2 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InterfaceFactory = void 0;
4
+ class InterfaceFactory {
5
+ getInterface() {
6
+ throw new Error('Not implemented');
7
+ }
8
+ }
9
+ exports.InterfaceFactory = InterfaceFactory;
@@ -82,6 +82,12 @@ class MockPeripheral extends events_1.default {
82
82
  };
83
83
  this.services = p.services;
84
84
  }
85
+ disconnectAsync() {
86
+ throw new Error('Method not implemented.');
87
+ }
88
+ discoverServicesAsync(serviceUUIDs) {
89
+ throw new Error('Method not implemented.');
90
+ }
85
91
  connectAsync() {
86
92
  return __awaiter(this, void 0, void 0, function* () {
87
93
  });
@@ -1,13 +1,12 @@
1
1
  import EventEmitter from "events";
2
- import { BleCharacteristic } from "../types";
3
- export type Property = 'read' | 'write' | 'notify' | 'indicate';
2
+ import { BleCharacteristic, BleProperty } from "../types";
4
3
  export type Descriptor = {
5
4
  uuid: string;
6
5
  value: string | Buffer;
7
6
  };
8
7
  export type Characteristic = {
9
8
  uuid: string;
10
- properties: Property[];
9
+ properties: BleProperty[];
11
10
  secure?: boolean;
12
11
  value: Buffer;
13
12
  descriptors: Descriptor[];
@@ -19,7 +18,7 @@ export declare const RESULT_INVALID_ATTRIBUTE_LENGTH = 13;
19
18
  export declare const RESULT_UNLIKELY_ERROR = 14;
20
19
  export declare class MockCharacteristic extends EventEmitter implements BleCharacteristic {
21
20
  uuid: string;
22
- properties: Property[];
21
+ properties: BleProperty[];
23
22
  secure?: boolean | undefined;
24
23
  value: Buffer;
25
24
  descriptors: Descriptor[];
@@ -1,8 +1,8 @@
1
1
  import { EventLogger } from 'gd-eventlog';
2
2
  import BleAdapterFactory from './adapter-factory';
3
- import { BleInterfaceProps, BlePeripheral, BleDeviceSettings, BleProtocol, BleBinding, BleInterfaceState, BleScanProps, BleCharacteristic } from './types';
3
+ import { BleInterfaceProps, BlePeripheral, BleDeviceSettings, BleProtocol, BleBinding, BleInterfaceState, BleScanProps, BleCharacteristic, IBleInterface } from './types';
4
4
  import { BleComms } from './base/comms';
5
- import { IncyclistInterface, IncyclistScanProps } from '../types';
5
+ import { IncyclistScanProps } from '../types';
6
6
  import BlePeripheralCache from './peripheral-cache';
7
7
  import EventEmitter from 'events';
8
8
  export interface ScanState {
@@ -31,7 +31,7 @@ export interface BleDeviceClassInfo {
31
31
  services: string[];
32
32
  id: string;
33
33
  }
34
- export default class BleInterface extends EventEmitter implements IncyclistInterface {
34
+ export default class BleInterface extends EventEmitter implements IBleInterface {
35
35
  scanState: ScanState;
36
36
  connectState: ConnectState;
37
37
  peripheralCache: BlePeripheralCache;
@@ -62,7 +62,8 @@ export default class BleInterface extends EventEmitter implements IncyclistInter
62
62
  pauseLogging(debugOnly?: boolean): void;
63
63
  resumeLogging(): void;
64
64
  protected isDebugEnabled(): boolean;
65
- protected logEvent(event: any): void;
65
+ logEvent(event: any): void;
66
+ logError(err: Error, fn: string, args?: any): void;
66
67
  protected onStateChange(state: BleInterfaceState): void;
67
68
  protected onError(err: any): void;
68
69
  connect(to?: number): Promise<boolean>;
@@ -79,6 +80,8 @@ export default class BleInterface extends EventEmitter implements IncyclistInter
79
80
  }): Promise<any>;
80
81
  scanForDevice(comms: BleComms, props: IncyclistScanProps): Promise<BlePeripheral>;
81
82
  scan(props?: BleScanProps): Promise<BleDeviceSettings[]>;
83
+ protected legacyScan(props?: BleScanProps): Promise<BleDeviceSettings[]>;
82
84
  stopScan(): Promise<boolean>;
83
85
  isScanning(): boolean;
86
+ getLogger(): EventLogger;
84
87
  }
@@ -118,6 +118,10 @@ class BleInterface extends events_1.default {
118
118
  console.log('~~~ BLE', event);
119
119
  }
120
120
  }
121
+ logError(err, fn, args) {
122
+ const logInfo = args || {};
123
+ this.logEvent(Object.assign(Object.assign({ message: 'Error', fn }, logInfo), { error: err.message, stack: err.stack }));
124
+ }
121
125
  onStateChange(state) {
122
126
  if (state !== 'poweredOn') {
123
127
  this.logEvent({ message: 'Ble disconnected', });
@@ -142,10 +146,12 @@ class BleInterface extends events_1.default {
142
146
  this.resumeLogging();
143
147
  const timeout = this.props.timeout || to || 2000;
144
148
  const connect = new Promise((resolve, reject) => {
145
- this.logEvent({ message: 'Ble connect request' });
149
+ this.logEvent({ message: 'Ble connect request', timeout });
146
150
  if (!this.getBinding())
147
151
  return reject(new Error('no binding defined'));
152
+ this.connectState.isConnecting = true;
148
153
  this.connectState.timeout = setTimeout(() => {
154
+ console.log('connect timeout');
149
155
  this.connectState.isConnected = false;
150
156
  this.connectState.isConnecting = false;
151
157
  this.connectState.timeout = null;
@@ -494,8 +500,13 @@ class BleInterface extends events_1.default {
494
500
  }
495
501
  scan() {
496
502
  return __awaiter(this, arguments, void 0, function* (props = {}) {
497
- this.logEvent({ message: 'starting scan ..' });
503
+ return yield this.legacyScan(props);
504
+ });
505
+ }
506
+ legacyScan() {
507
+ return __awaiter(this, arguments, void 0, function* (props = {}) {
498
508
  this.resumeLogging();
509
+ this.logEvent({ message: 'starting scan ..' });
499
510
  const { timeout, protocol, protocols } = props;
500
511
  const requestedProtocols = protocols || [];
501
512
  if (protocol && !requestedProtocols.find(p => p === protocol))
@@ -618,5 +629,8 @@ class BleInterface extends events_1.default {
618
629
  isScanning() {
619
630
  return this.scanState.isScanning === true;
620
631
  }
632
+ getLogger() {
633
+ return this.logger;
634
+ }
621
635
  }
622
636
  exports.default = BleInterface;
@@ -18,6 +18,7 @@ export default class BlePeripheralConnector implements IBlePeripheralConnector {
18
18
  private emitter;
19
19
  constructor(peripheral: BlePeripheral);
20
20
  logEvent(event: any): void;
21
+ logError(err: Error, fn: string, args?: any): void;
21
22
  connect(): Promise<void>;
22
23
  reconnect(): Promise<void>;
23
24
  onDisconnect(): void;