incyclist-devices 1.4.12 → 1.4.15

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.
@@ -12,6 +12,7 @@ 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
+ exports.Daum8iSerial = exports.Daum8iTcp = void 0;
15
16
  const constants_1 = require("../constants");
16
17
  const tcpserial_1 = __importDefault(require("./tcpserial"));
17
18
  const utils_1 = require("./utils");
@@ -407,7 +408,7 @@ class Daum8i {
407
408
  incoming = bufferData;
408
409
  }
409
410
  const response = [...incoming];
410
- this.logger.logEvent({ message: 'sendCommand:RECV', data: utils_1.hexstr(response) });
411
+ this.logger.logEvent({ message: 'sendCommand:RECV', data: (0, utils_1.hexstr)(response) });
411
412
  for (let i = 0; i < incoming.length; i++) {
412
413
  const getRemaining = () => {
413
414
  let remaining = '';
@@ -440,11 +441,11 @@ class Daum8i {
440
441
  }
441
442
  else if (c === 0x17) {
442
443
  const remaining = getRemaining();
443
- this.logger.logEvent({ message: "sendCommand:received:", duration: Date.now() - this.state.sending.tsRequest, port: portName, cmd: `${cmd} [${utils_1.hexstr(cmd)}]`, remaining: utils_1.hexstr(remaining) });
444
+ this.logger.logEvent({ message: "sendCommand:received:", duration: Date.now() - this.state.sending.tsRequest, port: portName, cmd: `${cmd} [${(0, utils_1.hexstr)(cmd)}]`, remaining: (0, utils_1.hexstr)(remaining) });
444
445
  this.state.waitingForEnd = false;
445
446
  const cmdStr = cmd.substring(0, cmd.length - 2);
446
447
  const checksumExtracted = cmd.slice(-2);
447
- const checksumCalculated = utils_1.checkSum(utils_1.getAsciiArrayFromStr(cmdStr), []);
448
+ const checksumCalculated = (0, utils_1.checkSum)((0, utils_1.getAsciiArrayFromStr)(cmdStr), []);
448
449
  if (checksumExtracted === checksumCalculated) {
449
450
  this.sendACK();
450
451
  if (this.state.sending && this.state.sending.responseCheckIv) {
@@ -486,8 +487,8 @@ class Daum8i {
486
487
  this.state.busy = true;
487
488
  }
488
489
  else {
489
- const message = utils_1.buildMessage(command, payload);
490
- this.logger.logEvent({ message: 'sendCommand:waiting', port: this.portName, cmd: command, hex: utils_1.hexstr(message) });
490
+ const message = (0, utils_1.buildMessage)(command, payload);
491
+ this.logger.logEvent({ message: 'sendCommand:waiting', port: this.portName, cmd: command, hex: (0, utils_1.hexstr)(message) });
491
492
  const busyWait = () => {
492
493
  return new Promise((done) => {
493
494
  let start = Date.now();
@@ -508,7 +509,7 @@ class Daum8i {
508
509
  };
509
510
  const res = yield busyWait();
510
511
  if (!res) {
511
- this.logger.logEvent({ message: 'sendCommand:busy timeout', port: this.portName, cmd: command, hex: utils_1.hexstr(message), duration: Date.now() - tsRequest });
512
+ this.logger.logEvent({ message: 'sendCommand:busy timeout', port: this.portName, cmd: command, hex: (0, utils_1.hexstr)(message), duration: Date.now() - tsRequest });
512
513
  return reject(new Error('BUSY timeout'));
513
514
  }
514
515
  this.state.busy = true;
@@ -526,10 +527,10 @@ class Daum8i {
526
527
  const portName = this.portName;
527
528
  this.state.received = [];
528
529
  try {
529
- const message = utils_1.buildMessage(command, payload);
530
+ const message = (0, utils_1.buildMessage)(command, payload);
530
531
  const start = Date.now();
531
532
  const timeout = start + this.getTimeoutValue();
532
- this.logger.logEvent({ message: "sendCommand:sending:", port: this.portName, cmd: command, hex: utils_1.hexstr(message) });
533
+ this.logger.logEvent({ message: "sendCommand:sending:", port: this.portName, cmd: command, hex: (0, utils_1.hexstr)(message) });
533
534
  this.state.writeBusy = true;
534
535
  if (!this.connected || port === undefined) {
535
536
  this.logger.logEvent({ message: "sendCommand:error: not connected", port: this.portName });
@@ -589,10 +590,10 @@ class Daum8i {
589
590
  buffer.writeUInt16LE(0, 2);
590
591
  }
591
592
  const cmdData = Uint8Array.from(buffer);
592
- return this.sendDaum8iCommand('M70', cmdType, utils_1.bin2esc(cmdData))
593
+ return this.sendDaum8iCommand('M70', cmdType, (0, utils_1.bin2esc)(cmdData))
593
594
  .then((res) => {
594
595
  const resData = Uint8Array.from(res, x => x.charCodeAt(0));
595
- const cmd = utils_1.esc2bin(resData);
596
+ const cmd = (0, utils_1.esc2bin)(resData);
596
597
  return cmd;
597
598
  });
598
599
  }
@@ -617,7 +618,7 @@ class Daum8i {
617
618
  else if (str === '7')
618
619
  deviceType = 'lyps';
619
620
  else
620
- throw (new Error(`unknown device type ${typeof str === 'string' ? utils_1.ascii(str.charAt(0)) : str}`));
621
+ throw (new Error(`unknown device type ${typeof str === 'string' ? (0, utils_1.ascii)(str.charAt(0)) : str}`));
621
622
  return deviceType;
622
623
  });
623
624
  }
@@ -632,7 +633,7 @@ class Daum8i {
632
633
  else if (str === '2')
633
634
  deviceType = constants_1.ACTUAL_BIKE_TYPE.MOUNTAIN;
634
635
  else {
635
- throw (new Error(`unknown actual device type ${typeof str === 'string' ? utils_1.ascii(str.charAt(0)) : str}`));
636
+ throw (new Error(`unknown actual device type ${typeof str === 'string' ? (0, utils_1.ascii)(str.charAt(0)) : str}`));
636
637
  }
637
638
  this.state.actualBikeType = deviceType;
638
639
  return deviceType;
@@ -674,12 +675,12 @@ class Daum8i {
674
675
  getTrainingData() {
675
676
  return this.sendDaum8iCommand('X70', 'AF', [])
676
677
  .then((data) => {
677
- const td = utils_1.parseTrainingData(data);
678
+ const td = (0, utils_1.parseTrainingData)(data);
678
679
  return td;
679
680
  });
680
681
  }
681
682
  setLoadControl(enabled) {
682
- const val = enabled ? utils_1.ascii('1') : utils_1.ascii('0');
683
+ const val = enabled ? (0, utils_1.ascii)('1') : (0, utils_1.ascii)('0');
683
684
  return this.sendDaum8iCommand('S20', 'BF', [val])
684
685
  .then((data) => {
685
686
  const res = data === '1';
@@ -713,7 +714,7 @@ class Daum8i {
713
714
  setPerson(person) {
714
715
  const { sex, age, length, weight } = person;
715
716
  this.logger.logEvent({ message: 'setPerson() request', sex, age, length, weight });
716
- return this.sendReservedDaum8iCommand(utils_1.ReservedCommands.PERSON_SET, 'BF', utils_1.getPersonData(person))
717
+ return this.sendReservedDaum8iCommand(utils_1.ReservedCommands.PERSON_SET, 'BF', (0, utils_1.getPersonData)(person))
717
718
  .then((res) => {
718
719
  const buffer = Buffer.from(res);
719
720
  const success = buffer.readInt16LE(0) === utils_1.ReservedCommands.PERSON_SET;
@@ -737,9 +738,9 @@ class Daum8i {
737
738
  }
738
739
  programUploadStart(bikeType, route) {
739
740
  const payload = Buffer.alloc(40);
740
- const epp = route ? utils_1.routeToEpp(route) : undefined;
741
+ const epp = route ? (0, utils_1.routeToEpp)(route) : undefined;
741
742
  const eppLength = epp ? epp.length : 0;
742
- const bikeTypeVal = utils_1.getBikeType(bikeType);
743
+ const bikeTypeVal = (0, utils_1.getBikeType)(bikeType);
743
744
  const wBits = route.lapMode ? DS_BITS_ENDLESS_RACE : DS_BITS_OFF;
744
745
  payload.writeInt32LE(0, 0);
745
746
  payload.writeInt8(bikeTypeVal, 4);
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getPersonData = exports.parseTrainingData = exports.routeToEpp = exports.getBikeType = exports.BikeType = exports.ReservedCommands = exports.Int32ToIntArray = exports.Int16ToIntArray = exports.Float32ToIntArray = exports.Float32ToHex = exports.getAsciiArrayFromStr = exports.asciiArrayToString = exports.charArrayToString = exports.ascii = exports.append = exports.getHex = exports.hexstr = exports.getMessageData = exports.buildMessage = exports.checkSum = exports.esc2bin = exports.bin2esc = void 0;
6
7
  const win32filetime_1 = __importDefault(require("win32filetime"));
7
8
  const sum = (arr) => arr.reduce((a, b) => a + b, 0);
8
9
  function bin2esc(arr) {
@@ -9,7 +9,7 @@ export declare type SerialCommsProps = {
9
9
  port: string;
10
10
  settings?: any;
11
11
  };
12
- declare enum SerialCommsState {
12
+ export declare enum SerialCommsState {
13
13
  Idle = 0,
14
14
  Connecting = 1,
15
15
  Connected = 2,
@@ -17,6 +17,11 @@ declare enum SerialCommsState {
17
17
  Disconnected = 4,
18
18
  Error = 5
19
19
  }
20
+ export declare enum SendState {
21
+ Idle = 0,
22
+ Sending = 1,
23
+ Receiving = 2
24
+ }
20
25
  export default class KettlerSerialComms<T extends Command> extends EventEmitter {
21
26
  private logger;
22
27
  private port;
@@ -27,12 +32,17 @@ export default class KettlerSerialComms<T extends Command> extends EventEmitter
27
32
  private worker;
28
33
  private sendState;
29
34
  private currentCmd;
35
+ private currentTimeout;
30
36
  private protocol;
31
37
  constructor(opts: SerialCommsProps);
32
38
  getPort(): string;
33
39
  setPort(port: any): void;
40
+ getLogger(): EventLogger;
34
41
  isConnected(): boolean;
35
42
  stateIn: (allowedStates: SerialCommsState[]) => boolean;
43
+ _setState(state: SerialCommsState): void;
44
+ _setSendState(state: SendState): void;
45
+ _setCurrentCmd(cmd: T): void;
36
46
  onPortOpen(): void;
37
47
  onPortClose(): Promise<void>;
38
48
  onPortError(err: any): void;
@@ -40,9 +50,9 @@ export default class KettlerSerialComms<T extends Command> extends EventEmitter
40
50
  close(): void;
41
51
  startWorker(): void;
42
52
  stopWorker(): void;
53
+ clearTimeout(): void;
43
54
  onData(data: string | Buffer): void;
44
55
  write(cmd: Command): void;
45
56
  sendNextCommand(): Command | undefined;
46
57
  send(cmd: Command): void;
47
58
  }
48
- export {};
@@ -12,10 +12,11 @@ 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
+ exports.SendState = exports.SerialCommsState = void 0;
15
16
  const gd_eventlog_1 = require("gd-eventlog");
16
17
  const utils_1 = require("../utils");
17
18
  const events_1 = __importDefault(require("events"));
18
- const DEFAULT_RCV_TIMEOUT = 500;
19
+ const DEFAULT_RCV_TIMEOUT = 1500;
19
20
  const DEBUG_LOGGER = {
20
21
  log: (e, ...args) => console.log(e, ...args),
21
22
  logEvent: (event) => console.log(JSON.stringify(event))
@@ -28,13 +29,13 @@ var SerialCommsState;
28
29
  SerialCommsState[SerialCommsState["Disconnecting"] = 3] = "Disconnecting";
29
30
  SerialCommsState[SerialCommsState["Disconnected"] = 4] = "Disconnected";
30
31
  SerialCommsState[SerialCommsState["Error"] = 5] = "Error";
31
- })(SerialCommsState || (SerialCommsState = {}));
32
+ })(SerialCommsState = exports.SerialCommsState || (exports.SerialCommsState = {}));
32
33
  var SendState;
33
34
  (function (SendState) {
34
35
  SendState[SendState["Idle"] = 0] = "Idle";
35
36
  SendState[SendState["Sending"] = 1] = "Sending";
36
37
  SendState[SendState["Receiving"] = 2] = "Receiving";
37
- })(SendState || (SendState = {}));
38
+ })(SendState = exports.SendState || (exports.SendState = {}));
38
39
  const CRLF = '\r\n';
39
40
  class KettlerSerialComms extends events_1.default {
40
41
  constructor(opts) {
@@ -58,9 +59,21 @@ class KettlerSerialComms extends events_1.default {
58
59
  setPort(port) {
59
60
  this.port = port;
60
61
  }
62
+ getLogger() {
63
+ return this.logger;
64
+ }
61
65
  isConnected() {
62
66
  return this.state === SerialCommsState.Connected;
63
67
  }
68
+ _setState(state) {
69
+ this.state = state;
70
+ }
71
+ _setSendState(state) {
72
+ this.sendState = state;
73
+ }
74
+ _setCurrentCmd(cmd) {
75
+ this.currentCmd = cmd;
76
+ }
64
77
  onPortOpen() {
65
78
  this.logger.logEvent({ message: 'port opened', port: this.getPort() });
66
79
  this.state = SerialCommsState.Connected;
@@ -134,26 +147,34 @@ class KettlerSerialComms extends events_1.default {
134
147
  this.worker = undefined;
135
148
  }
136
149
  }
150
+ clearTimeout() {
151
+ if (this.currentTimeout) {
152
+ clearTimeout(this.currentTimeout);
153
+ this.currentTimeout = undefined;
154
+ }
155
+ }
137
156
  onData(data) {
138
- this.sendState = SendState.Idle;
139
- this.currentCmd = undefined;
157
+ this.clearTimeout();
140
158
  this.logger.logEvent({ message: "sendCommand:receiving:", data: data });
159
+ this.sendState = SendState.Idle;
141
160
  if (typeof data === 'string') {
142
- if (data.length > 2)
143
- data = data.trim();
144
- this.currentCmd.onResponse(data);
161
+ if (this.currentCmd.onResponse)
162
+ this.currentCmd.onResponse(data);
145
163
  }
146
164
  else {
147
- this.currentCmd.onResponse(data);
165
+ if (this.currentCmd.onResponse)
166
+ this.currentCmd.onResponse(data);
148
167
  }
168
+ this.currentCmd = undefined;
149
169
  }
150
170
  write(cmd) {
151
171
  this.sendState = SendState.Sending;
152
172
  const { logStr, message, timeout = (this.settings.timeout || DEFAULT_RCV_TIMEOUT) } = cmd;
153
- const msg = typeof message === 'string' ? message : utils_1.hexstr(message);
173
+ const msg = typeof message === 'string' ? message : (0, utils_1.hexstr)(message);
154
174
  const onError = (err) => {
155
175
  this.logger.logEvent({ message: "sendCommand:error:", cmd: logStr, error: err.message, port: this.getPort() });
156
- cmd.onError(err);
176
+ if (cmd.onError)
177
+ cmd.onError(err);
157
178
  this.sendState = SendState.Idle;
158
179
  this.currentCmd = undefined;
159
180
  };
@@ -165,9 +186,9 @@ class KettlerSerialComms extends events_1.default {
165
186
  this.sendState = SendState.Receiving;
166
187
  this.currentCmd = cmd;
167
188
  if (timeout) {
168
- setTimeout(() => {
189
+ this.currentTimeout = setTimeout(() => {
169
190
  if (this.sendState === SendState.Receiving) {
170
- onError(new Error("timeout"));
191
+ onError(new Error("response timeout"));
171
192
  }
172
193
  }, timeout);
173
194
  }
@@ -188,6 +209,7 @@ class KettlerSerialComms extends events_1.default {
188
209
  this.write(cmd);
189
210
  }
190
211
  send(cmd) {
212
+ this.logger.logEvent({ message: 'send()', cmd: cmd.logStr, port: this.getPort(), queueSize: this.queue.size() });
191
213
  this.queue.enqueue(cmd);
192
214
  }
193
215
  }
@@ -2,6 +2,7 @@ import { DeviceSettings } from "../../DeviceProtocol";
2
2
  import DeviceAdapterBase, { DeviceAdapter, DeviceData, Bike } from "../../Device";
3
3
  import { DeviceProtocol } from "../../DeviceProtocol";
4
4
  import { EventLogger } from "gd-eventlog";
5
+ import SerialComms from "../comms";
5
6
  import { Command } from "../../types/command";
6
7
  import CyclingMode, { IncyclistBikeData } from "../../CyclingMode";
7
8
  import { User } from "../../types/user";
@@ -33,7 +34,6 @@ export default class KettlerRacerAdapter extends DeviceAdapterBase implements De
33
34
  private ignorePower;
34
35
  private logger;
35
36
  private paused;
36
- private comms;
37
37
  private iv;
38
38
  private requests;
39
39
  private data;
@@ -41,6 +41,7 @@ export default class KettlerRacerAdapter extends DeviceAdapterBase implements De
41
41
  private kettlerData;
42
42
  private updateBusy;
43
43
  private requestBusy;
44
+ private comms;
44
45
  constructor(protocol: DeviceProtocol, settings: DeviceSettings);
45
46
  isBike(): boolean;
46
47
  isPower(): boolean;
@@ -52,6 +53,8 @@ export default class KettlerRacerAdapter extends DeviceAdapterBase implements De
52
53
  setIgnoreHrm(ignore: boolean): void;
53
54
  setIgnorePower(ignore: boolean): void;
54
55
  setIgnoreBike(ignore: boolean): void;
56
+ _getComms(): SerialComms<KettlerRacerCommand>;
57
+ _setComms(comms: SerialComms<KettlerRacerCommand>): void;
55
58
  getLogger(): EventLogger;
56
59
  getUserSettings(): User;
57
60
  getWeight(): number;
@@ -65,7 +68,7 @@ export default class KettlerRacerAdapter extends DeviceAdapterBase implements De
65
68
  startTraining(): Promise<string>;
66
69
  unknownSN(): Promise<string>;
67
70
  setBaudrate(baudrate: number): Promise<string>;
68
- setPower(power: number): Promise<string>;
71
+ setPower(power: number): Promise<KettlerBikeData>;
69
72
  getExtendedStatus(): Promise<KettlerExtendedBikeData>;
70
73
  getStatus(): Promise<KettlerBikeData>;
71
74
  getDB(): Promise<string>;