incyclist-devices 2.1.24 → 2.1.26

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.
@@ -21,6 +21,7 @@ export interface ConnectState {
21
21
  isConnected: boolean;
22
22
  timeout?: NodeJS.Timeout;
23
23
  isInitSuccess: boolean;
24
+ connectPromise?: Promise<boolean>;
24
25
  }
25
26
  export interface BleDeviceInfo {
26
27
  device: BleComms;
@@ -132,68 +132,92 @@ 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.resumeLogging();
136
- const timeout = this.props.timeout || to || 2000;
137
- return new Promise((resolve, reject) => {
135
+ return __awaiter(this, void 0, void 0, function* () {
138
136
  if (this.connectState.isConnected) {
139
- return resolve(true);
137
+ return true;
140
138
  }
141
- this.logEvent({ message: 'Ble connect request', });
142
- if (!this.getBinding())
143
- return Promise.reject(new Error('no binding defined'));
144
- this.connectState.timeout = setTimeout(() => {
145
- this.connectState.isConnected = false;
146
- this.connectState.isConnecting = false;
147
- this.connectState.timeout = null;
148
- this.logEvent({ message: 'Ble connect result: timeout' });
149
- resolve(false);
150
- }, timeout);
151
- try {
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;
139
+ if (this.connectState.isConnecting) {
140
+ return yield this.connectState.connectPromise;
141
+ }
142
+ this.resumeLogging();
143
+ const timeout = this.props.timeout || to || 2000;
144
+ const connect = new Promise((resolve, reject) => {
145
+ this.logEvent({ message: 'Ble connect request' });
146
+ if (!this.getBinding())
147
+ return reject(new Error('no binding defined'));
148
+ this.connectState.timeout = setTimeout(() => {
149
+ this.connectState.isConnected = false;
159
150
  this.connectState.isConnecting = false;
160
- this.logEvent({ message: 'connect result: success' });
161
- resolve(true);
162
- return;
163
- }
164
- else {
165
- this.getBinding().once('error', (err) => {
151
+ this.connectState.timeout = null;
152
+ this.logEvent({ message: 'Ble connect result: timeout' });
153
+ resolve(false);
154
+ }, timeout);
155
+ try {
156
+ const state = this.getBinding().state;
157
+ if (state === 'poweredOn') {
158
+ clearTimeout(this.connectState.timeout);
159
+ this.connectState.timeout = null;
160
+ this.getBinding().removeAllListeners('stateChange');
161
+ this.getBinding().on('stateChange', this.onStateChange.bind(this));
166
162
  this.connectState.isConnected = true;
167
163
  this.connectState.isConnecting = false;
168
- this.logEvent({ message: 'connect result: error', error: err.message });
169
- this.getBinding().on('error', this.onError.bind(this));
170
- return reject(err);
171
- });
172
- this.getBinding().on('stateChange', (state) => {
173
- if (state === 'poweredOn') {
174
- clearTimeout(this.connectState.timeout);
175
- this.connectState.timeout = null;
176
- this.getBinding().removeAllListeners('stateChange');
177
- this.getBinding().on('stateChange', this.onStateChange.bind(this));
164
+ this.logEvent({ message: 'connect result: already connected' });
165
+ resolve(true);
166
+ return;
167
+ }
168
+ else {
169
+ this.getBinding().once('error', (err) => {
178
170
  this.connectState.isConnected = true;
179
171
  this.connectState.isConnecting = false;
180
- this.logEvent({ message: 'Ble connect result: success' });
181
- return resolve(true);
182
- }
183
- else {
184
- this.logEvent({ message: 'BLE state change', state });
185
- }
186
- });
172
+ this.logEvent({ message: 'connect result: error', error: err.message });
173
+ this.getBinding().on('error', this.onError.bind(this));
174
+ return reject(err);
175
+ });
176
+ this.getBinding().on('stateChange', (state) => {
177
+ if (state === 'poweredOn') {
178
+ clearTimeout(this.connectState.timeout);
179
+ this.connectState.timeout = null;
180
+ this.getBinding().removeAllListeners('stateChange');
181
+ this.getBinding().on('stateChange', this.onStateChange.bind(this));
182
+ this.connectState.isConnected = true;
183
+ this.connectState.isConnecting = false;
184
+ this.logEvent({ message: 'Ble connect result: success' });
185
+ return resolve(true);
186
+ }
187
+ else {
188
+ this.logEvent({ message: 'BLE state change', state });
189
+ }
190
+ });
191
+ }
192
+ }
193
+ catch (err) {
194
+ this.connectState.isConnected = false;
195
+ this.connectState.isConnecting = false;
196
+ if (this.connectState.timeout)
197
+ clearTimeout(this.connectState.timeout);
198
+ this.connectState.timeout = null;
199
+ this.logEvent({ message: 'Ble connect result: error', error: err.message });
200
+ return reject(new Error('bluetooth unavailable, cause: ' + err.message));
187
201
  }
202
+ });
203
+ const cleanup = () => {
204
+ if (this.connectState.timeout) {
205
+ clearTimeout(this.connectState.timeout);
206
+ this.connectState.timeout = null;
207
+ }
208
+ this.connectState.isConnecting = false;
209
+ delete this.connectState.connectPromise;
210
+ };
211
+ this.connectState.isConnecting = true;
212
+ this.connectState.connectPromise = connect;
213
+ try {
214
+ const connected = yield connect;
215
+ cleanup();
216
+ return connected;
188
217
  }
189
218
  catch (err) {
190
- this.connectState.isConnected = false;
191
- this.connectState.isConnecting = false;
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));
219
+ cleanup();
220
+ throw err;
197
221
  }
198
222
  });
199
223
  }
@@ -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;
@@ -66,11 +66,8 @@ class SerialPortComms {
66
66
  console.log(`~~~ ${this.getDefaultLoggerName()}`, e);
67
67
  }
68
68
  }
69
- connect() {
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 (_b) {
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.1.24",
3
+ "version": "2.1.26",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",