incyclist-devices 2.2.9 → 2.3.0

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 (142) 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.js +1 -1
  8. package/lib/ble/adapter-factory.d.ts +20 -24
  9. package/lib/ble/adapter-factory.js +13 -36
  10. package/lib/ble/base/adapter.d.ts +16 -5
  11. package/lib/ble/base/adapter.js +169 -52
  12. package/lib/ble/base/comms.d.ts +2 -74
  13. package/lib/ble/base/comms.js +3 -596
  14. package/lib/ble/base/interface.d.ts +96 -0
  15. package/lib/ble/base/interface.js +544 -0
  16. package/lib/ble/base/peripheral.d.ts +34 -0
  17. package/lib/ble/base/peripheral.js +286 -0
  18. package/lib/ble/base/sensor.d.ts +31 -0
  19. package/lib/ble/base/sensor.js +130 -0
  20. package/lib/ble/base/types.d.ts +7 -0
  21. package/lib/ble/base/types.js +7 -0
  22. package/lib/ble/bindings/mock.js +6 -0
  23. package/lib/ble/bindings/types.d.ts +3 -4
  24. package/lib/ble/ble-interface.d.ts +7 -4
  25. package/lib/ble/ble-interface.js +16 -2
  26. package/lib/ble/ble-peripheral.d.ts +1 -0
  27. package/lib/ble/ble-peripheral.js +7 -11
  28. package/lib/ble/consts.d.ts +0 -6
  29. package/lib/ble/consts.js +1 -7
  30. package/lib/ble/cp/adapter.d.ts +3 -3
  31. package/lib/ble/cp/adapter.js +12 -13
  32. package/lib/ble/cp/comm.d.ts +1 -1
  33. package/lib/ble/cp/comm.js +2 -2
  34. package/lib/ble/cp/index.d.ts +1 -1
  35. package/lib/ble/cp/index.js +2 -2
  36. package/lib/ble/cp/sensor.d.ts +27 -0
  37. package/lib/ble/cp/sensor.js +107 -0
  38. package/lib/ble/elite/comms.d.ts +1 -1
  39. package/lib/ble/elite/comms.js +2 -2
  40. package/lib/ble/factories/adapter-factory.d.ts +32 -0
  41. package/lib/ble/factories/adapter-factory.js +104 -0
  42. package/lib/ble/factories/index.d.ts +2 -0
  43. package/lib/ble/factories/index.js +18 -0
  44. package/lib/ble/factories/interface-factory.d.ts +7 -0
  45. package/lib/ble/factories/interface-factory.js +18 -0
  46. package/lib/ble/fm/adapter.d.ts +9 -4
  47. package/lib/ble/fm/adapter.js +104 -108
  48. package/lib/ble/fm/comms.d.ts +1 -1
  49. package/lib/ble/fm/comms.js +3 -3
  50. package/lib/ble/fm/consts.d.ts +93 -0
  51. package/lib/ble/fm/consts.js +55 -1
  52. package/lib/ble/fm/index.d.ts +1 -1
  53. package/lib/ble/fm/index.js +2 -2
  54. package/lib/ble/fm/sensor.d.ts +44 -0
  55. package/lib/ble/fm/sensor.js +384 -0
  56. package/lib/ble/hr/adapter.d.ts +3 -3
  57. package/lib/ble/hr/adapter.js +5 -8
  58. package/lib/ble/hr/comm.d.ts +1 -1
  59. package/lib/ble/hr/comm.js +2 -2
  60. package/lib/ble/hr/index.d.ts +1 -1
  61. package/lib/ble/hr/index.js +2 -2
  62. package/lib/ble/hr/sensor.d.ts +17 -0
  63. package/lib/ble/hr/sensor.js +52 -0
  64. package/lib/ble/index.d.ts +4 -3
  65. package/lib/ble/index.js +29 -13
  66. package/lib/ble/tacx/adapter.d.ts +4 -2
  67. package/lib/ble/tacx/adapter.js +26 -66
  68. package/lib/ble/tacx/comms.d.ts +1 -1
  69. package/lib/ble/tacx/comms.js +2 -2
  70. package/lib/ble/tacx/consts.d.ts +23 -0
  71. package/lib/ble/tacx/consts.js +27 -0
  72. package/lib/ble/tacx/index.d.ts +1 -1
  73. package/lib/ble/tacx/index.js +2 -2
  74. package/lib/ble/tacx/sensor.d.ts +63 -0
  75. package/lib/ble/tacx/sensor.js +596 -0
  76. package/lib/ble/types.d.ts +81 -52
  77. package/lib/ble/utils.d.ts +7 -2
  78. package/lib/ble/utils.js +84 -9
  79. package/lib/ble/wahoo/adapter.d.ts +2 -2
  80. package/lib/ble/wahoo/adapter.js +4 -72
  81. package/lib/ble/wahoo/comms.d.ts +1 -1
  82. package/lib/ble/wahoo/comms.js +2 -2
  83. package/lib/ble/wahoo/consts.d.ts +16 -0
  84. package/lib/ble/wahoo/consts.js +7 -0
  85. package/lib/ble/wahoo/index.d.ts +1 -1
  86. package/lib/ble/wahoo/index.js +2 -2
  87. package/lib/ble/wahoo/sensor.d.ts +48 -0
  88. package/lib/ble/wahoo/sensor.js +356 -0
  89. package/lib/direct-connect/base/comms.d.ts +3 -0
  90. package/lib/direct-connect/base/comms.js +7 -0
  91. package/lib/direct-connect/base/interface.d.ts +75 -0
  92. package/lib/direct-connect/base/interface.js +306 -0
  93. package/lib/direct-connect/base/peripheral.d.ts +50 -0
  94. package/lib/direct-connect/base/peripheral.js +413 -0
  95. package/lib/direct-connect/base/sensor.d.ts +3 -0
  96. package/lib/direct-connect/base/sensor.js +7 -0
  97. package/lib/direct-connect/bindings/index.d.ts +1 -0
  98. package/lib/direct-connect/bindings/index.js +17 -0
  99. package/lib/direct-connect/bindings/types.d.ts +36 -0
  100. package/lib/direct-connect/bindings/types.js +2 -0
  101. package/lib/direct-connect/consts.d.ts +17 -0
  102. package/lib/direct-connect/consts.js +20 -0
  103. package/lib/direct-connect/index.d.ts +3 -0
  104. package/lib/direct-connect/index.js +22 -0
  105. package/lib/direct-connect/messages/CharacteristicNotification.d.ts +12 -0
  106. package/lib/direct-connect/messages/CharacteristicNotification.js +23 -0
  107. package/lib/direct-connect/messages/DiscoverCharacteristics.d.ts +22 -0
  108. package/lib/direct-connect/messages/DiscoverCharacteristics.js +43 -0
  109. package/lib/direct-connect/messages/DiscoverServices.d.ts +15 -0
  110. package/lib/direct-connect/messages/DiscoverServices.js +33 -0
  111. package/lib/direct-connect/messages/EnableCharacteristicNotifications.d.ts +18 -0
  112. package/lib/direct-connect/messages/EnableCharacteristicNotifications.js +35 -0
  113. package/lib/direct-connect/messages/ReadCharacteristic.d.ts +18 -0
  114. package/lib/direct-connect/messages/ReadCharacteristic.js +34 -0
  115. package/lib/direct-connect/messages/WriteCharacteristic.d.ts +18 -0
  116. package/lib/direct-connect/messages/WriteCharacteristic.js +36 -0
  117. package/lib/direct-connect/messages/error.d.ts +5 -0
  118. package/lib/direct-connect/messages/error.js +18 -0
  119. package/lib/direct-connect/messages/factory.d.ts +6 -0
  120. package/lib/direct-connect/messages/factory.js +44 -0
  121. package/lib/direct-connect/messages/index.d.ts +9 -0
  122. package/lib/direct-connect/messages/index.js +25 -0
  123. package/lib/direct-connect/messages/message.d.ts +20 -0
  124. package/lib/direct-connect/messages/message.js +90 -0
  125. package/lib/direct-connect/types.d.ts +24 -0
  126. package/lib/direct-connect/types.js +9 -0
  127. package/lib/direct-connect/utils.d.ts +5 -0
  128. package/lib/direct-connect/utils.js +73 -0
  129. package/lib/factories/adapters.js +16 -4
  130. package/lib/factories/interfaces.d.ts +2 -1
  131. package/lib/factories/interfaces.js +10 -7
  132. package/lib/index.d.ts +5 -4
  133. package/lib/index.js +6 -2
  134. package/lib/serial/bindings/tcp.d.ts +5 -4
  135. package/lib/serial/bindings/tcp.js +16 -23
  136. package/lib/types/device.d.ts +2 -1
  137. package/lib/types/device.js +1 -0
  138. package/lib/utils/operation.d.ts +17 -0
  139. package/lib/utils/operation.js +20 -0
  140. package/lib/utils/task.d.ts +44 -0
  141. package/lib/utils/task.js +127 -0
  142. package/package.json +3 -1
@@ -0,0 +1,286 @@
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
+ yield this.getPeripheral().connectAsync();
33
+ this.ble.registerConnected(this);
34
+ this.connected = true;
35
+ return true;
36
+ });
37
+ }
38
+ disconnect() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ this.disconnecting = true;
41
+ if (!this.isConnected())
42
+ return true;
43
+ yield this.unsubscribeAll();
44
+ Object.keys(this.characteristics).forEach(uuid => {
45
+ const c = this.characteristics[uuid];
46
+ c.removeAllListeners();
47
+ });
48
+ if (!this.getPeripheral().disconnectAsync) {
49
+ this.getPeripheral().disconnectAsync = () => {
50
+ return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
51
+ };
52
+ }
53
+ yield this.getPeripheral().disconnectAsync();
54
+ this.connected = false;
55
+ this.disconnecting = false;
56
+ return true;
57
+ });
58
+ }
59
+ isConnected() {
60
+ return this.connected;
61
+ }
62
+ isConnecting() {
63
+ return false;
64
+ }
65
+ onDisconnect(callback) {
66
+ this.onDisconnectHandler = callback;
67
+ }
68
+ discoverServices() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ if (this.getPeripheral().discoverServicesAsync) {
71
+ const services = yield this.getPeripheral().discoverServicesAsync([]);
72
+ return services.map(s => s.uuid);
73
+ }
74
+ else {
75
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
76
+ return res.services.map(s => s.uuid);
77
+ }
78
+ });
79
+ }
80
+ discoverCharacteristics(serviceUUID) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([serviceUUID], []);
83
+ res.characteristics.forEach(c => this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c);
84
+ return res.characteristics.map(c => {
85
+ const { uuid, properties, name, _serviceUuid } = c;
86
+ return { uuid, properties, name, _serviceUuid };
87
+ });
88
+ });
89
+ }
90
+ subscribe(characteristicUUID, callback) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ try {
93
+ if (this.disconnecting || !this.connected)
94
+ return false;
95
+ const subscription = this.subscribed.find(s => s.uuid === characteristicUUID);
96
+ if (subscription) {
97
+ return true;
98
+ }
99
+ const c = this.getRawCharacteristic(characteristicUUID);
100
+ if (!c) {
101
+ return false;
102
+ }
103
+ return new Promise((resolve, reject) => {
104
+ const info = this.subscribed.find(s => s.uuid === characteristicUUID);
105
+ if (info) {
106
+ return Promise.resolve(true);
107
+ }
108
+ const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
109
+ this.logEvent({ message: 'subscribe request', characteristic: uuid, success: true });
110
+ c.subscribe((err) => {
111
+ if (err) {
112
+ this.logEvent({ message: 'subscribe result', characteristic: uuid, success: false, reason: err.message });
113
+ resolve(false);
114
+ }
115
+ else {
116
+ if (callback) {
117
+ const onData = (data) => {
118
+ try {
119
+ callback(characteristicUUID, data);
120
+ }
121
+ catch (_a) { }
122
+ };
123
+ this.subscribed.push({ uuid: characteristicUUID, callback: onData });
124
+ c.on('data', onData);
125
+ }
126
+ else {
127
+ this.subscribed.push({ uuid: characteristicUUID, callback: null });
128
+ }
129
+ this.logEvent({ message: 'subscribe result', characteristic: uuid, success: true });
130
+ resolve(true);
131
+ }
132
+ });
133
+ });
134
+ }
135
+ catch (err) {
136
+ this.logEvent({ message: 'Error', fn: 'subscribe', error: err.message, stack: err.stack });
137
+ return false;
138
+ }
139
+ });
140
+ }
141
+ unsubscribe(characteristicUUID) {
142
+ try {
143
+ const subscription = this.subscribed.find(s => s.uuid === characteristicUUID);
144
+ if (!subscription) {
145
+ return Promise.resolve(true);
146
+ }
147
+ const c = this.getRawCharacteristic(characteristicUUID);
148
+ if (!c) {
149
+ return Promise.resolve(false);
150
+ }
151
+ return new Promise((resolve, reject) => {
152
+ c.unsubscribe((err) => {
153
+ if (err) {
154
+ resolve(false);
155
+ }
156
+ else {
157
+ const info = this.subscribed.find(s => s.uuid === characteristicUUID);
158
+ if (info) {
159
+ this.subscribed.splice(this.subscribed.indexOf(info), 1);
160
+ if (info.callback)
161
+ c.off('data', info.callback);
162
+ }
163
+ resolve(true);
164
+ }
165
+ });
166
+ });
167
+ }
168
+ catch (err) {
169
+ this.logEvent({ message: 'Error', fn: 'unsubscribe', error: err.message, stack: err.stack });
170
+ return Promise.resolve(false);
171
+ }
172
+ }
173
+ subscribeSelected(characteristics, callback) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ try {
176
+ if (Object.keys(this.characteristics).length === 0) {
177
+ yield this.discoverAllCharacteristics();
178
+ }
179
+ const retry = [];
180
+ for (let i = 0; i < characteristics.length; i++) {
181
+ const c = this.getRawCharacteristic(characteristics[i]);
182
+ if (c === null || c === void 0 ? void 0 : c.properties.includes('notify')) {
183
+ const success = yield this.subscribe(c.uuid, callback);
184
+ if (!success)
185
+ retry.push(c);
186
+ }
187
+ }
188
+ for (let i = 0; i < retry.length; i++) {
189
+ const c = retry[i];
190
+ yield this.subscribe(c.uuid, callback);
191
+ }
192
+ return true;
193
+ }
194
+ catch (err) {
195
+ this.logEvent({ message: 'Error', fn: 'subscribeSelected', error: err.message, stack: err.stack });
196
+ return false;
197
+ }
198
+ });
199
+ }
200
+ discoverAllCharacteristics() {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ try {
203
+ const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
204
+ const found = [];
205
+ res.characteristics.forEach(c => {
206
+ this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c;
207
+ found.push(c.uuid);
208
+ });
209
+ return found;
210
+ }
211
+ catch (err) {
212
+ this.logEvent({ message: 'Error', fn: 'discoverAllCharacteristics', error: err.message, stack: err.stack });
213
+ return [];
214
+ }
215
+ });
216
+ }
217
+ subscribeAll(callback) {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ const characteristics = yield this.discoverAllCharacteristics();
220
+ const success = yield this.subscribeSelected(characteristics, callback);
221
+ return success;
222
+ });
223
+ }
224
+ unsubscribeAll() {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ const promises = [];
227
+ this.subscribed.forEach(d => {
228
+ promises.push(this.unsubscribe(d.uuid));
229
+ });
230
+ yield Promise.allSettled(promises);
231
+ return true;
232
+ });
233
+ }
234
+ read(characteristicUUID) {
235
+ if (this.disconnecting || !this.connected)
236
+ return Promise.resolve(Buffer.from([]));
237
+ const c = this.characteristics[(0, utils_1.beautifyUUID)(characteristicUUID)];
238
+ if (!c) {
239
+ return Promise.reject(new Error('characteristic not found'));
240
+ }
241
+ return new Promise((resolve, reject) => {
242
+ c.read((err, data) => {
243
+ if (err) {
244
+ reject(err);
245
+ }
246
+ else {
247
+ resolve(data);
248
+ }
249
+ });
250
+ });
251
+ }
252
+ write(characteristicUUID, data, options) {
253
+ if (this.disconnecting || !this.connected)
254
+ return Promise.resolve(Buffer.from([]));
255
+ const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
256
+ const c = this.characteristics[uuid];
257
+ if (!c) {
258
+ return Promise.reject(new Error('characteristic not found'));
259
+ }
260
+ return new Promise((resolve, reject) => {
261
+ this.subscribe(characteristicUUID, null).then(success => {
262
+ if (this.disconnecting || !this.connected)
263
+ return Promise.resolve(Buffer.from([]));
264
+ c.on('data', (data) => {
265
+ c.removeAllListeners('data');
266
+ resolve(data);
267
+ });
268
+ this.logEvent({ message: 'write request', characteristic: uuid, data: data.toString('hex'), withoutResponse: (options === null || options === void 0 ? void 0 : options.withoutResponse) === true });
269
+ c.write(data, (options === null || options === void 0 ? void 0 : options.withoutResponse) === true, (err) => {
270
+ if (err)
271
+ reject(err);
272
+ });
273
+ if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
274
+ resolve(Buffer.from([]));
275
+ }
276
+ });
277
+ });
278
+ }
279
+ getRawCharacteristic(uuid) {
280
+ return this.characteristics[(0, utils_1.beautifyUUID)(uuid)];
281
+ }
282
+ logEvent(event) {
283
+ this.ble.logEvent(event);
284
+ }
285
+ }
286
+ exports.BlePeripheral = BlePeripheral;
@@ -0,0 +1,31 @@
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
+ getProfile(): LegacyProfile;
16
+ getProtocol(): BleProtocol;
17
+ getServiceUUids(): string[];
18
+ isMatching(serviceUUIDs: string[]): boolean;
19
+ hasPeripheral(): boolean;
20
+ startSensor(reconnect?: boolean): Promise<boolean>;
21
+ protected getRequiredCharacteristics(): Array<string>;
22
+ protected subscribe(): Promise<boolean>;
23
+ stopSensor(): Promise<boolean>;
24
+ reconnectSensor(): Promise<void>;
25
+ reset(): void;
26
+ isConnected(): boolean;
27
+ read(characteristicUUID: string): Promise<Buffer>;
28
+ write(characteristicUUID: string, data: Buffer, options?: BleWriteProps): Promise<Buffer>;
29
+ onData(characteristic: string, data: Buffer): boolean;
30
+ protected getDefaultLogger(): EventLogger;
31
+ }
@@ -0,0 +1,130 @@
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
+ getProfile() {
32
+ const C = this.constructor;
33
+ return C['profile'];
34
+ }
35
+ getProtocol() {
36
+ const C = this.constructor;
37
+ return C['protocol'];
38
+ }
39
+ getServiceUUids() {
40
+ const C = this.constructor;
41
+ return C['services'];
42
+ }
43
+ isMatching(serviceUUIDs) {
44
+ const uuids = serviceUUIDs.map(uuid => (0, utils_2.beautifyUUID)(uuid));
45
+ const required = this.getServiceUUids();
46
+ if (!required)
47
+ return true;
48
+ let missing = false;
49
+ required.forEach(uuid => {
50
+ if (!uuids.includes((0, utils_2.beautifyUUID)(uuid))) {
51
+ missing = true;
52
+ }
53
+ });
54
+ return (missing === false);
55
+ }
56
+ hasPeripheral() {
57
+ return !!this.peripheral;
58
+ }
59
+ startSensor(reconnect) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ if (!reconnect)
62
+ this.stopRequested = false;
63
+ if (!this.peripheral) {
64
+ this.logEvent({ message: 'no peripheral' });
65
+ return false;
66
+ }
67
+ const connected = yield this.peripheral.connect();
68
+ if (!connected)
69
+ return false;
70
+ if (!reconnect)
71
+ this.peripheral.onDisconnect(this.reconnectSensor.bind(this));
72
+ return yield this.subscribe();
73
+ });
74
+ }
75
+ getRequiredCharacteristics() {
76
+ return null;
77
+ }
78
+ subscribe() {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ const selected = this.getRequiredCharacteristics();
81
+ if (selected === null)
82
+ return yield this.peripheral.subscribeAll(this.onDataHandler);
83
+ if (selected.length === 0)
84
+ return true;
85
+ return yield this.peripheral.subscribeSelected(selected, this.onDataHandler);
86
+ });
87
+ }
88
+ stopSensor() {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ this.removeAllListeners();
91
+ if (!this.peripheral)
92
+ return true;
93
+ this.stopRequested = true;
94
+ return yield this.peripheral.disconnect();
95
+ });
96
+ }
97
+ reconnectSensor() {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ let success = false;
100
+ do {
101
+ success = yield this.startSensor(true);
102
+ if (!success) {
103
+ yield (0, utils_1.sleep)(5000);
104
+ }
105
+ } while (!success || this.stopRequested);
106
+ });
107
+ }
108
+ reset() {
109
+ throw new Error("Method not implemented.");
110
+ }
111
+ isConnected() {
112
+ var _a;
113
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.isConnected();
114
+ }
115
+ read(characteristicUUID) {
116
+ var _a;
117
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.read(characteristicUUID);
118
+ }
119
+ write(characteristicUUID, data, options) {
120
+ var _a;
121
+ return (_a = this.peripheral) === null || _a === void 0 ? void 0 : _a.write(characteristicUUID, data, options);
122
+ }
123
+ onData(characteristic, data) {
124
+ return true;
125
+ }
126
+ getDefaultLogger() {
127
+ return new gd_eventlog_1.EventLogger(this.constructor.name);
128
+ }
129
+ }
130
+ 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;
@@ -12,13 +12,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const ble_interface_1 = __importDefault(require("./ble-interface"));
16
- const gd_eventlog_1 = require("gd-eventlog");
17
15
  const events_1 = __importDefault(require("events"));
18
16
  const utils_1 = require("./utils");
17
+ const interface_1 = require("./base/interface");
19
18
  class BlePeripheralConnector {
20
19
  constructor(peripheral) {
21
- this.ble = ble_interface_1.default.getInstance();
20
+ this.ble = interface_1.BleInterface.getInstance();
22
21
  this.peripheral = peripheral;
23
22
  this.emitter = new events_1.default();
24
23
  if (!this.peripheral || !this.ble)
@@ -26,16 +25,13 @@ class BlePeripheralConnector {
26
25
  this.state = { subscribed: [], isConnected: false, isConnecting: false, isInitialized: false, isInitializing: false, isSubscribing: false };
27
26
  this.services = [];
28
27
  this.characteristics = [];
29
- this.logger = new gd_eventlog_1.EventLogger('BLE');
28
+ this.logger = this.ble.getLogger();
30
29
  }
31
30
  logEvent(event) {
32
- if (this.logger) {
33
- this.logger.logEvent(event);
34
- }
35
- const w = global.window;
36
- if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG) {
37
- console.log('~~~ BLE', event);
38
- }
31
+ this.ble.logEvent(event);
32
+ }
33
+ logError(err, fn, args) {
34
+ this.ble.logError(err, fn, args);
39
35
  }
40
36
  connect() {
41
37
  return __awaiter(this, void 0, void 0, function* () {
@@ -1,6 +1,3 @@
1
- export declare const TACX_FE_C_BLE = "6e40fec1";
2
- export declare const TACX_FE_C_RX = "6e40fec2";
3
- export declare const TACX_FE_C_TX = "6e40fec3";
4
1
  export declare const FTMS = "1826";
5
2
  export declare const FTMS_CP = "2ad9";
6
3
  export declare const FTMS_STATUS = "2ada";
@@ -14,7 +11,4 @@ export declare const CSP_FEATURE = "2a65";
14
11
  export declare const HR_MEASUREMENT = "2a37";
15
12
  export declare const CSC = "1816";
16
13
  export declare const CSC_MEASUREMENT = "2a5b";
17
- export declare const WAHOO_ADVANCED_TRAINER_CP = "a026e005";
18
- export declare const WAHOO_ADVANCED_FTMS = "a026ee0b";
19
- export declare const WAHOO_ADVANCED_TRAINER_CP_FULL = "A026E005-0A7D-4AB3-97FA-F1500F9FEB8B";
20
14
  export declare const ELITE_TRAINER_SVC = "347b0001";
package/lib/ble/consts.js CHANGED
@@ -1,9 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ELITE_TRAINER_SVC = exports.WAHOO_ADVANCED_TRAINER_CP_FULL = exports.WAHOO_ADVANCED_FTMS = exports.WAHOO_ADVANCED_TRAINER_CP = exports.CSC_MEASUREMENT = exports.CSC = exports.HR_MEASUREMENT = exports.CSP_FEATURE = exports.CSP_MEASUREMENT = exports.CSP = exports.POWER_RANGE = exports.RES_LEVEL_RANGE = exports.FTMS_FEATURE = exports.INDOOR_BIKE_DATA = exports.FTMS_STATUS = exports.FTMS_CP = exports.FTMS = exports.TACX_FE_C_TX = exports.TACX_FE_C_RX = exports.TACX_FE_C_BLE = void 0;
4
- exports.TACX_FE_C_BLE = '6e40fec1';
5
- exports.TACX_FE_C_RX = '6e40fec2';
6
- exports.TACX_FE_C_TX = '6e40fec3';
3
+ exports.ELITE_TRAINER_SVC = exports.CSC_MEASUREMENT = exports.CSC = exports.HR_MEASUREMENT = exports.CSP_FEATURE = exports.CSP_MEASUREMENT = exports.CSP = exports.POWER_RANGE = exports.RES_LEVEL_RANGE = exports.FTMS_FEATURE = exports.INDOOR_BIKE_DATA = exports.FTMS_STATUS = exports.FTMS_CP = exports.FTMS = void 0;
7
4
  exports.FTMS = '1826';
8
5
  exports.FTMS_CP = '2ad9';
9
6
  exports.FTMS_STATUS = '2ada';
@@ -17,7 +14,4 @@ exports.CSP_FEATURE = '2a65';
17
14
  exports.HR_MEASUREMENT = '2a37';
18
15
  exports.CSC = '1816';
19
16
  exports.CSC_MEASUREMENT = '2a5b';
20
- exports.WAHOO_ADVANCED_TRAINER_CP = 'a026e005';
21
- exports.WAHOO_ADVANCED_FTMS = 'a026ee0b';
22
- exports.WAHOO_ADVANCED_TRAINER_CP_FULL = 'A026E005-0A7D-4AB3-97FA-F1500F9FEB8B';
23
17
  exports.ELITE_TRAINER_SVC = '347b0001';