incyclist-devices 2.1.24 → 2.1.25
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.
package/lib/ble/ble-interface.js
CHANGED
|
@@ -132,68 +132,98 @@ class BleInterface extends events_1.default {
|
|
|
132
132
|
this.logEvent({ message: 'error', error: err.message, stack: err.stack });
|
|
133
133
|
}
|
|
134
134
|
connect(to) {
|
|
135
|
-
this
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
try {
|
|
137
|
+
throw new Error();
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
console.log('~~~ DEBUG:Connect BLE', err.stack);
|
|
141
|
+
}
|
|
138
142
|
if (this.connectState.isConnected) {
|
|
139
|
-
return
|
|
143
|
+
return true;
|
|
140
144
|
}
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
this.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.
|
|
148
|
-
this.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const state = this.getBinding().state;
|
|
153
|
-
if (state === 'poweredOn') {
|
|
154
|
-
clearTimeout(this.connectState.timeout);
|
|
155
|
-
this.connectState.timeout = null;
|
|
156
|
-
this.getBinding().removeAllListeners('stateChange');
|
|
157
|
-
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
158
|
-
this.connectState.isConnected = true;
|
|
145
|
+
if (this.connectState.isConnecting) {
|
|
146
|
+
return yield this.connectState.connectPromise;
|
|
147
|
+
}
|
|
148
|
+
this.resumeLogging();
|
|
149
|
+
const timeout = this.props.timeout || to || 2000;
|
|
150
|
+
const connect = new Promise((resolve, reject) => {
|
|
151
|
+
this.logEvent({ message: 'Ble connect request' });
|
|
152
|
+
if (!this.getBinding())
|
|
153
|
+
return reject(new Error('no binding defined'));
|
|
154
|
+
this.connectState.timeout = setTimeout(() => {
|
|
155
|
+
this.connectState.isConnected = false;
|
|
159
156
|
this.connectState.isConnecting = false;
|
|
160
|
-
this.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
this.getBinding().
|
|
157
|
+
this.connectState.timeout = null;
|
|
158
|
+
this.logEvent({ message: 'Ble connect result: timeout' });
|
|
159
|
+
resolve(false);
|
|
160
|
+
}, timeout);
|
|
161
|
+
try {
|
|
162
|
+
const state = this.getBinding().state;
|
|
163
|
+
if (state === 'poweredOn') {
|
|
164
|
+
clearTimeout(this.connectState.timeout);
|
|
165
|
+
this.connectState.timeout = null;
|
|
166
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
167
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
166
168
|
this.connectState.isConnected = true;
|
|
167
169
|
this.connectState.isConnecting = false;
|
|
168
|
-
this.logEvent({ message: 'connect result:
|
|
169
|
-
|
|
170
|
-
return
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
clearTimeout(this.connectState.timeout);
|
|
175
|
-
this.connectState.timeout = null;
|
|
176
|
-
this.getBinding().removeAllListeners('stateChange');
|
|
177
|
-
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
170
|
+
this.logEvent({ message: 'connect result: already connected' });
|
|
171
|
+
resolve(true);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.getBinding().once('error', (err) => {
|
|
178
176
|
this.connectState.isConnected = true;
|
|
179
177
|
this.connectState.isConnecting = false;
|
|
180
|
-
this.logEvent({ message: '
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
178
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
179
|
+
this.getBinding().on('error', this.onError.bind(this));
|
|
180
|
+
return reject(err);
|
|
181
|
+
});
|
|
182
|
+
this.getBinding().on('stateChange', (state) => {
|
|
183
|
+
if (state === 'poweredOn') {
|
|
184
|
+
clearTimeout(this.connectState.timeout);
|
|
185
|
+
this.connectState.timeout = null;
|
|
186
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
187
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
188
|
+
this.connectState.isConnected = true;
|
|
189
|
+
this.connectState.isConnecting = false;
|
|
190
|
+
this.logEvent({ message: 'Ble connect result: success' });
|
|
191
|
+
return resolve(true);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
this.logEvent({ message: 'BLE state change', state });
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
187
198
|
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
this.connectState.isConnected = false;
|
|
201
|
+
this.connectState.isConnecting = false;
|
|
202
|
+
if (this.connectState.timeout)
|
|
203
|
+
clearTimeout(this.connectState.timeout);
|
|
204
|
+
this.connectState.timeout = null;
|
|
205
|
+
this.logEvent({ message: 'Ble connect result: error', error: err.message });
|
|
206
|
+
return reject(new Error('bluetooth unavailable, cause: ' + err.message));
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
const cleanup = () => {
|
|
210
|
+
if (this.connectState.timeout) {
|
|
211
|
+
clearTimeout(this.connectState.timeout);
|
|
212
|
+
this.connectState.timeout = null;
|
|
213
|
+
}
|
|
214
|
+
this.connectState.isConnecting = false;
|
|
215
|
+
delete this.connectState.connectPromise;
|
|
216
|
+
};
|
|
217
|
+
this.connectState.isConnecting = true;
|
|
218
|
+
this.connectState.connectPromise = connect;
|
|
219
|
+
try {
|
|
220
|
+
const connected = yield connect;
|
|
221
|
+
cleanup();
|
|
222
|
+
return connected;
|
|
188
223
|
}
|
|
189
224
|
catch (err) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (this.connectState.timeout)
|
|
193
|
-
clearTimeout(this.connectState.timeout);
|
|
194
|
-
this.connectState.timeout = null;
|
|
195
|
-
this.logEvent({ message: 'Ble connect result: error', error: err.message });
|
|
196
|
-
return reject(new Error('bluetooth unavailable, cause: ' + err.message));
|
|
225
|
+
cleanup();
|
|
226
|
+
throw err;
|
|
197
227
|
}
|
|
198
228
|
});
|
|
199
229
|
}
|
|
@@ -40,6 +40,7 @@ export default class SerialPortComms<T extends CommsState, C extends Request, R
|
|
|
40
40
|
pause(): void;
|
|
41
41
|
resume(): void;
|
|
42
42
|
logEvent(e: any): void;
|
|
43
|
+
waitForAnyOpenConnectionAttempt(): Promise<boolean>;
|
|
43
44
|
connect(): Promise<boolean>;
|
|
44
45
|
onConnected(): void;
|
|
45
46
|
portFlush(): void;
|
package/lib/serial/base/comms.js
CHANGED
|
@@ -66,11 +66,8 @@ class SerialPortComms {
|
|
|
66
66
|
console.log(`~~~ ${this.getDefaultLoggerName()}`, e);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
waitForAnyOpenConnectionAttempt() {
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
if (this.isConnected() && this.sp) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
71
|
if (this.connectState === 'Connecting') {
|
|
75
72
|
if (this.connectPromise) {
|
|
76
73
|
try {
|
|
@@ -81,6 +78,17 @@ class SerialPortComms {
|
|
|
81
78
|
}
|
|
82
79
|
return this.isConnected();
|
|
83
80
|
}
|
|
81
|
+
return null;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
connect() {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
if (this.isConnected() && this.sp) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
const connectResult = yield this.waitForAnyOpenConnectionAttempt();
|
|
90
|
+
if (connectResult !== null)
|
|
91
|
+
return connectResult;
|
|
84
92
|
try {
|
|
85
93
|
this.connectState = 'Connecting';
|
|
86
94
|
this.connectPromise = this.serial.openPort(this.path);
|
|
@@ -99,7 +107,7 @@ class SerialPortComms {
|
|
|
99
107
|
return false;
|
|
100
108
|
}
|
|
101
109
|
}
|
|
102
|
-
catch (
|
|
110
|
+
catch (_a) {
|
|
103
111
|
this.connectState = 'Disconnected';
|
|
104
112
|
return false;
|
|
105
113
|
}
|
|
@@ -229,7 +237,6 @@ class SerialPortComms {
|
|
|
229
237
|
portWrite(buffer) {
|
|
230
238
|
return __awaiter(this, void 0, void 0, function* () {
|
|
231
239
|
if (!this.sp) {
|
|
232
|
-
this.logEvent({ message: 'write failed', error: 'port is not opened' });
|
|
233
240
|
return;
|
|
234
241
|
}
|
|
235
242
|
try {
|
|
@@ -243,7 +250,6 @@ class SerialPortComms {
|
|
|
243
250
|
portRead(size) {
|
|
244
251
|
return __awaiter(this, void 0, void 0, function* () {
|
|
245
252
|
if (!this.sp) {
|
|
246
|
-
this.logEvent({ message: 'write failed', error: 'port is not opened' });
|
|
247
253
|
return;
|
|
248
254
|
}
|
|
249
255
|
return yield this.sp.read(size);
|
|
@@ -251,6 +257,9 @@ class SerialPortComms {
|
|
|
251
257
|
}
|
|
252
258
|
write(buffer) {
|
|
253
259
|
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
yield this.waitForAnyOpenConnectionAttempt();
|
|
261
|
+
if (!this.isConnected())
|
|
262
|
+
return;
|
|
254
263
|
if (this.writePromise) {
|
|
255
264
|
try {
|
|
256
265
|
yield this.writePromise;
|
|
@@ -340,17 +340,17 @@ class DaumAdapter extends adapter_1.SerialIncyclistDevice {
|
|
|
340
340
|
return __awaiter(this, void 0, void 0, function* () {
|
|
341
341
|
if (this.stopped)
|
|
342
342
|
return true;
|
|
343
|
-
this.logEvent({ message: 'stop request' });
|
|
343
|
+
this.logEvent({ message: 'stop request', port: this.getPort() });
|
|
344
344
|
if (this.paused)
|
|
345
345
|
this.resume();
|
|
346
346
|
try {
|
|
347
347
|
yield this.stopUpdatePull();
|
|
348
348
|
yield this.comms.close();
|
|
349
|
-
this.logEvent({ message: 'stop request completed' });
|
|
349
|
+
this.logEvent({ message: 'stop request completed', port: this.getPort() });
|
|
350
350
|
this.stopped = true;
|
|
351
351
|
}
|
|
352
352
|
catch (err) {
|
|
353
|
-
this.logEvent({ message: 'stop request failed', reason: err.message });
|
|
353
|
+
this.logEvent({ message: 'stop request failed', port: this.getPort(), reason: err.message });
|
|
354
354
|
throw (err);
|
|
355
355
|
}
|
|
356
356
|
return this.stopped;
|
|
@@ -360,7 +360,7 @@ class DaumAdapter extends adapter_1.SerialIncyclistDevice {
|
|
|
360
360
|
return __awaiter(this, void 0, void 0, function* () {
|
|
361
361
|
if (this.paused || this.stopped)
|
|
362
362
|
return;
|
|
363
|
-
this.logEvent({ message: 'sendUpdate', request, waiting: this.requests.length });
|
|
363
|
+
this.logEvent({ message: 'sendUpdate', port: this.getPort(), request, waiting: this.requests.length });
|
|
364
364
|
return yield this.processClientRequest(request);
|
|
365
365
|
});
|
|
366
366
|
}
|
|
@@ -378,7 +378,7 @@ class DaumAdapter extends adapter_1.SerialIncyclistDevice {
|
|
|
378
378
|
}
|
|
379
379
|
catch (err) {
|
|
380
380
|
try {
|
|
381
|
-
this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack });
|
|
381
|
+
this.logEvent({ message: 'bike update error', port: this.getPort(), error: err.message, stack: err.stack });
|
|
382
382
|
const incyclistData = this.updateData(this.deviceData);
|
|
383
383
|
this.transformData(incyclistData);
|
|
384
384
|
}
|
|
@@ -398,7 +398,7 @@ class DaumAdapter extends adapter_1.SerialIncyclistDevice {
|
|
|
398
398
|
if (cnt > 1) {
|
|
399
399
|
this.requests.forEach((request, idx) => {
|
|
400
400
|
if (idx !== cnt - 1) {
|
|
401
|
-
this.logEvent({ message: 'ignoring bike update request', request });
|
|
401
|
+
this.logEvent({ message: 'ignoring bike update request', port: this.getPort(), request });
|
|
402
402
|
}
|
|
403
403
|
});
|
|
404
404
|
this.requests = [this.requests[cnt - 1]];
|
|
@@ -409,7 +409,7 @@ class DaumAdapter extends adapter_1.SerialIncyclistDevice {
|
|
|
409
409
|
this.requests.shift();
|
|
410
410
|
}
|
|
411
411
|
catch (err) {
|
|
412
|
-
this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack, request });
|
|
412
|
+
this.logEvent({ message: 'bike update error', port: this.getPort(), error: err.message, stack: err.stack, request });
|
|
413
413
|
}
|
|
414
414
|
}
|
|
415
415
|
});
|
|
@@ -16,7 +16,6 @@ const utils_1 = require("../../../utils/utils");
|
|
|
16
16
|
const comms_1 = __importDefault(require("../../base/comms"));
|
|
17
17
|
const utils_2 = require("./utils");
|
|
18
18
|
const types_1 = require("../types");
|
|
19
|
-
const ByteLength = require('@serialport/parser-byte-length');
|
|
20
19
|
const TIMEOUT_SEND = 2000;
|
|
21
20
|
class Daum8008 extends comms_1.default {
|
|
22
21
|
constructor(props) {
|