incyclist-devices 1.4.102 → 1.5.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.
- package/LICENSE +0 -0
- package/lib/DeviceSupport.d.ts +36 -34
- package/lib/DeviceSupport.js +82 -78
- package/lib/ant/AntAdapter.d.ts +50 -50
- package/lib/ant/AntAdapter.js +109 -109
- package/lib/ant/AntScanner.d.ts +60 -60
- package/lib/ant/AntScanner.js +651 -651
- package/lib/ant/antfe/AntFEAdapter.d.ts +83 -83
- package/lib/ant/antfe/AntFEAdapter.js +652 -652
- package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +9 -9
- package/lib/ant/antfe/ant-fe-adv-st-mode.js +51 -51
- package/lib/ant/antfe/ant-fe-erg-mode.d.ts +6 -6
- package/lib/ant/antfe/ant-fe-erg-mode.js +14 -14
- package/lib/ant/antfe/ant-fe-st-mode.d.ts +5 -5
- package/lib/ant/antfe/ant-fe-st-mode.js +13 -13
- package/lib/ant/anthrm/AntHrmAdapter.d.ts +16 -16
- package/lib/ant/anthrm/AntHrmAdapter.js +130 -130
- package/lib/ant/antpwr/pwr-adapter.d.ts +49 -49
- package/lib/ant/antpwr/pwr-adapter.js +251 -251
- package/lib/ant/utils.d.ts +1 -1
- package/lib/ant/utils.js +23 -23
- package/lib/antv2/adapter-factory.d.ts +11 -0
- package/lib/antv2/adapter-factory.js +40 -0
- package/lib/antv2/ant-binding.d.ts +13 -0
- package/lib/antv2/ant-binding.js +27 -0
- package/lib/antv2/ant-device.d.ts +51 -0
- package/lib/antv2/ant-device.js +115 -0
- package/lib/antv2/ant-interface.d.ts +37 -0
- package/lib/antv2/ant-interface.js +239 -0
- package/lib/antv2/fe.d.ts +28 -0
- package/lib/antv2/fe.js +237 -0
- package/lib/antv2/hr.d.ts +18 -0
- package/lib/antv2/hr.js +93 -0
- package/lib/antv2/incyclist-protocol.d.ts +37 -0
- package/lib/antv2/incyclist-protocol.js +126 -0
- package/lib/antv2/pwr.d.ts +28 -0
- package/lib/antv2/pwr.js +163 -0
- package/lib/antv2/sensor-factory.d.ts +5 -0
- package/lib/antv2/sensor-factory.js +20 -0
- package/lib/ble/ble-device.d.ts +63 -63
- package/lib/ble/ble-device.js +444 -444
- package/lib/ble/ble-erg-mode.d.ts +18 -18
- package/lib/ble/ble-erg-mode.js +132 -132
- package/lib/ble/ble-interface.d.ts +100 -100
- package/lib/ble/ble-interface.js +721 -719
- package/lib/ble/ble-peripheral.d.ts +36 -36
- package/lib/ble/ble-peripheral.js +200 -200
- package/lib/ble/ble-st-mode.d.ts +15 -15
- package/lib/ble/ble-st-mode.js +95 -95
- package/lib/ble/ble.d.ts +129 -129
- package/lib/ble/ble.js +86 -86
- package/lib/ble/consts.d.ts +14 -14
- package/lib/ble/consts.js +17 -17
- package/lib/ble/fm.d.ts +125 -125
- package/lib/ble/fm.js +745 -745
- package/lib/ble/hrm.d.ts +48 -48
- package/lib/ble/hrm.js +134 -134
- package/lib/ble/incyclist-protocol.d.ts +31 -31
- package/lib/ble/incyclist-protocol.js +147 -147
- package/lib/ble/pwr.d.ts +89 -89
- package/lib/ble/pwr.js +321 -321
- package/lib/ble/tacx.d.ts +90 -90
- package/lib/ble/tacx.js +731 -731
- package/lib/ble/wahoo-kickr.d.ts +98 -98
- package/lib/ble/wahoo-kickr.js +496 -496
- package/lib/calculations.d.ts +13 -13
- package/lib/calculations.js +150 -150
- package/lib/{CyclingMode.d.ts → cycling-mode.d.ts} +76 -76
- package/lib/{CyclingMode.js → cycling-mode.js} +79 -79
- package/lib/daum/DaumAdapter.d.ts +66 -66
- package/lib/daum/DaumAdapter.js +396 -396
- package/lib/daum/DaumPowerMeterCyclingMode.d.ts +8 -8
- package/lib/daum/DaumPowerMeterCyclingMode.js +21 -21
- package/lib/daum/ERGCyclingMode.d.ts +26 -26
- package/lib/daum/ERGCyclingMode.js +201 -201
- package/lib/daum/SmartTrainerCyclingMode.d.ts +41 -41
- package/lib/daum/SmartTrainerCyclingMode.js +344 -344
- package/lib/daum/classic/DaumClassicAdapter.d.ts +18 -18
- package/lib/daum/classic/DaumClassicAdapter.js +146 -146
- package/lib/daum/classic/DaumClassicCyclingMode.d.ts +13 -13
- package/lib/daum/classic/DaumClassicCyclingMode.js +97 -97
- package/lib/daum/classic/DaumClassicProtocol.d.ts +27 -27
- package/lib/daum/classic/DaumClassicProtocol.js +185 -185
- package/lib/daum/classic/bike.d.ts +64 -64
- package/lib/daum/classic/bike.js +456 -456
- package/lib/daum/classic/utils.d.ts +13 -13
- package/lib/daum/classic/utils.js +143 -143
- package/lib/daum/constants.d.ts +19 -19
- package/lib/daum/constants.js +22 -22
- package/lib/daum/premium/DaumClassicCyclingMode.d.ts +14 -14
- package/lib/daum/premium/DaumClassicCyclingMode.js +86 -86
- package/lib/daum/premium/DaumPremiumAdapter.d.ts +12 -12
- package/lib/daum/premium/DaumPremiumAdapter.js +131 -131
- package/lib/daum/premium/DaumPremiumProtocol.d.ts +32 -32
- package/lib/daum/premium/DaumPremiumProtocol.js +207 -207
- package/lib/daum/premium/bike.d.ts +123 -123
- package/lib/daum/premium/bike.js +894 -894
- package/lib/daum/premium/tcpserial.d.ts +33 -33
- package/lib/daum/premium/tcpserial.js +123 -123
- package/lib/daum/premium/utils.d.ts +62 -62
- package/lib/daum/premium/utils.js +376 -376
- package/lib/{Device.d.ts → device.d.ts} +92 -92
- package/lib/{Device.js → device.js} +71 -71
- package/lib/kettler/comms.d.ts +59 -59
- package/lib/kettler/comms.js +242 -242
- package/lib/kettler/ergo-racer/ERGCyclingMode.d.ts +25 -25
- package/lib/kettler/ergo-racer/ERGCyclingMode.js +144 -144
- package/lib/kettler/ergo-racer/adapter.d.ts +101 -101
- package/lib/kettler/ergo-racer/adapter.js +639 -639
- package/lib/kettler/ergo-racer/protocol.d.ts +41 -41
- package/lib/kettler/ergo-racer/protocol.js +203 -203
- package/lib/modes/power-base.d.ts +20 -20
- package/lib/modes/power-base.js +70 -70
- package/lib/modes/power-meter.d.ts +20 -20
- package/lib/modes/power-meter.js +78 -78
- package/lib/modes/simulator.d.ts +29 -29
- package/lib/modes/simulator.js +140 -140
- package/lib/{DeviceProtocol.d.ts → protocol.d.ts} +74 -74
- package/lib/{DeviceProtocol.js → protocol.js} +41 -41
- package/lib/{DeviceRegistry.d.ts → registry.d.ts} +8 -8
- package/lib/{DeviceRegistry.js → registry.js} +33 -33
- package/lib/simulator/Simulator.d.ts +69 -69
- package/lib/simulator/Simulator.js +288 -288
- package/lib/types/command.d.ts +8 -8
- package/lib/types/command.js +2 -2
- package/lib/types/route.d.ts +24 -24
- package/lib/types/route.js +9 -9
- package/lib/types/user.d.ts +11 -11
- package/lib/types/user.js +9 -9
- package/lib/utils.d.ts +14 -14
- package/lib/utils.js +114 -114
- package/package.json +47 -46
package/lib/kettler/comms.js
CHANGED
|
@@ -1,242 +1,242 @@
|
|
|
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.SendState = exports.SerialCommsState = void 0;
|
|
16
|
-
const gd_eventlog_1 = require("gd-eventlog");
|
|
17
|
-
const utils_1 = require("../utils");
|
|
18
|
-
const events_1 = __importDefault(require("events"));
|
|
19
|
-
const DEFAULT_RCV_TIMEOUT = 1500;
|
|
20
|
-
const DEFAULT_OPEN_TIMEOUT = 3000;
|
|
21
|
-
const DEBUG_LOGGER = {
|
|
22
|
-
log: (e, ...args) => console.log(e, ...args),
|
|
23
|
-
logEvent: (event) => console.log(JSON.stringify(event))
|
|
24
|
-
};
|
|
25
|
-
var SerialCommsState;
|
|
26
|
-
(function (SerialCommsState) {
|
|
27
|
-
SerialCommsState[SerialCommsState["Idle"] = 0] = "Idle";
|
|
28
|
-
SerialCommsState[SerialCommsState["Connecting"] = 1] = "Connecting";
|
|
29
|
-
SerialCommsState[SerialCommsState["Connected"] = 2] = "Connected";
|
|
30
|
-
SerialCommsState[SerialCommsState["Disconnecting"] = 3] = "Disconnecting";
|
|
31
|
-
SerialCommsState[SerialCommsState["Disconnected"] = 4] = "Disconnected";
|
|
32
|
-
SerialCommsState[SerialCommsState["Error"] = 5] = "Error";
|
|
33
|
-
})(SerialCommsState = exports.SerialCommsState || (exports.SerialCommsState = {}));
|
|
34
|
-
var SendState;
|
|
35
|
-
(function (SendState) {
|
|
36
|
-
SendState[SendState["Idle"] = 0] = "Idle";
|
|
37
|
-
SendState[SendState["Sending"] = 1] = "Sending";
|
|
38
|
-
SendState[SendState["Receiving"] = 2] = "Receiving";
|
|
39
|
-
})(SendState = exports.SendState || (exports.SendState = {}));
|
|
40
|
-
const CRLF = '\r\n';
|
|
41
|
-
class KettlerSerialComms extends events_1.default {
|
|
42
|
-
constructor(opts) {
|
|
43
|
-
super();
|
|
44
|
-
this.stateIn = (allowedStates) => {
|
|
45
|
-
return allowedStates.indexOf(this.state) >= 0;
|
|
46
|
-
};
|
|
47
|
-
this.logger = process.env.DEBUG ? DEBUG_LOGGER : (opts.logger || new gd_eventlog_1.EventLogger(opts.protocol.getName()));
|
|
48
|
-
this.port = opts.port || process.env.COM_PORT;
|
|
49
|
-
this.sp = undefined;
|
|
50
|
-
this.queue = new utils_1.Queue();
|
|
51
|
-
this.state = SerialCommsState.Idle;
|
|
52
|
-
this.sendState = SendState.Idle;
|
|
53
|
-
this.settings = opts.settings || {};
|
|
54
|
-
this.currentCmd = undefined;
|
|
55
|
-
this.currentTimeout = undefined;
|
|
56
|
-
this.protocol = opts.protocol;
|
|
57
|
-
}
|
|
58
|
-
getPort() {
|
|
59
|
-
return this.port;
|
|
60
|
-
}
|
|
61
|
-
setPort(port) {
|
|
62
|
-
this.port = port;
|
|
63
|
-
}
|
|
64
|
-
getLogger() {
|
|
65
|
-
return this.logger;
|
|
66
|
-
}
|
|
67
|
-
isConnected() {
|
|
68
|
-
return this.state === SerialCommsState.Connected;
|
|
69
|
-
}
|
|
70
|
-
_setState(state) {
|
|
71
|
-
this.state = state;
|
|
72
|
-
}
|
|
73
|
-
_setSendState(state) {
|
|
74
|
-
this.sendState = state;
|
|
75
|
-
}
|
|
76
|
-
_setCurrentCmd(cmd) {
|
|
77
|
-
this.currentCmd = cmd;
|
|
78
|
-
}
|
|
79
|
-
stopCurrentTimeoutCheck() {
|
|
80
|
-
if (this.currentTimeout) {
|
|
81
|
-
clearTimeout(this.currentTimeout);
|
|
82
|
-
this.currentTimeout = undefined;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
onPortOpen() {
|
|
86
|
-
this.logger.logEvent({ message: 'port opened', port: this.getPort() });
|
|
87
|
-
this.state = SerialCommsState.Connected;
|
|
88
|
-
this.sendState = SendState.Idle;
|
|
89
|
-
this.stopCurrentTimeoutCheck();
|
|
90
|
-
this.startWorker();
|
|
91
|
-
this.emit('opened');
|
|
92
|
-
}
|
|
93
|
-
onPortClose() {
|
|
94
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
-
this.logger.logEvent({ message: 'port closed', port: this.getPort() });
|
|
96
|
-
this.stopWorker();
|
|
97
|
-
if (this.sendState === SendState.Sending) {
|
|
98
|
-
}
|
|
99
|
-
this.state = SerialCommsState.Disconnected;
|
|
100
|
-
this.sendState = SendState.Idle;
|
|
101
|
-
this.stopCurrentTimeoutCheck();
|
|
102
|
-
this.queue.clear();
|
|
103
|
-
this.emit('closed');
|
|
104
|
-
this.sp.removeAllListeners();
|
|
105
|
-
this.sp = undefined;
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
onPortError(err) {
|
|
109
|
-
let ignore = false;
|
|
110
|
-
if (this.stateIn([SerialCommsState.Connected, SerialCommsState.Disconnected]))
|
|
111
|
-
ignore = true;
|
|
112
|
-
if (this.state === SerialCommsState.Disconnecting && (err.message === 'Port is not open' || err.message === 'Writing to COM port (GetOverlappedResult): Operation aborted'))
|
|
113
|
-
ignore = true;
|
|
114
|
-
if (this.state === SerialCommsState.Connecting && (err.message === 'Port is already open' || err.message === 'Port is opening'))
|
|
115
|
-
ignore = true;
|
|
116
|
-
if (!ignore) {
|
|
117
|
-
this.logger.logEvent({ message: "port error:", port: this.getPort(), error: err.message, stack: err.stack, state: this.state });
|
|
118
|
-
this.emit('error', err);
|
|
119
|
-
this.stopCurrentTimeoutCheck();
|
|
120
|
-
if (this.state === SerialCommsState.Connecting || this.state === SerialCommsState.Disconnecting) {
|
|
121
|
-
this.state = SerialCommsState.Error;
|
|
122
|
-
this.sp.removeAllListeners();
|
|
123
|
-
this.sp = undefined;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
open() {
|
|
128
|
-
this.logger.logEvent({ message: "open()", port: this.getPort() });
|
|
129
|
-
if (this.stateIn([SerialCommsState.Connected, SerialCommsState.Connecting, SerialCommsState.Disconnecting])) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
const SerialPort = this.protocol.getSerialPort();
|
|
134
|
-
if (this.sp === undefined) {
|
|
135
|
-
this.sp = new SerialPort(this.getPort(), this.settings);
|
|
136
|
-
this.sp.on('open', () => { this.onPortOpen(); });
|
|
137
|
-
this.sp.on('close', () => { this.onPortClose(); });
|
|
138
|
-
this.sp.on('error', (error) => { this.onPortError(error); });
|
|
139
|
-
}
|
|
140
|
-
this.state = SerialCommsState.Connecting;
|
|
141
|
-
const parser = this.sp.pipe(new SerialPort.parsers.Readline({ delimiter: CRLF }));
|
|
142
|
-
parser.on('data', (data) => { this.onData(data); });
|
|
143
|
-
this.sp.open();
|
|
144
|
-
const timeout = this.settings.openTimeout || DEFAULT_OPEN_TIMEOUT;
|
|
145
|
-
this.currentTimeout = setTimeout(() => {
|
|
146
|
-
this.logger.logEvent({ message: "open() timeout", port: this.getPort() });
|
|
147
|
-
this.onPortError(new Error("open() timeout"));
|
|
148
|
-
}, timeout);
|
|
149
|
-
}
|
|
150
|
-
catch (err) {
|
|
151
|
-
this.logger.logEvent({ message: "error", fn: 'open()', error: err.message });
|
|
152
|
-
this.state = SerialCommsState.Disconnected;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
close() {
|
|
156
|
-
this.logger.logEvent({ message: 'close()', port: this.getPort() });
|
|
157
|
-
if (this.stateIn([SerialCommsState.Idle, SerialCommsState.Disconnected, SerialCommsState.Disconnecting])) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
this.state = SerialCommsState.Disconnecting;
|
|
161
|
-
this.sp.close();
|
|
162
|
-
}
|
|
163
|
-
startWorker() {
|
|
164
|
-
this.worker = setInterval(() => {
|
|
165
|
-
this.sendNextCommand();
|
|
166
|
-
}, 50);
|
|
167
|
-
}
|
|
168
|
-
stopWorker() {
|
|
169
|
-
if (this.worker) {
|
|
170
|
-
clearInterval(this.worker);
|
|
171
|
-
this.worker = undefined;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
clearTimeout() {
|
|
175
|
-
if (this.currentTimeout) {
|
|
176
|
-
clearTimeout(this.currentTimeout);
|
|
177
|
-
this.currentTimeout = undefined;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
onData(data) {
|
|
181
|
-
this.clearTimeout();
|
|
182
|
-
this.logger.logEvent({ message: "sendCommand:receiving:", data: data });
|
|
183
|
-
this.sendState = SendState.Idle;
|
|
184
|
-
if (typeof data === 'string') {
|
|
185
|
-
if (this.currentCmd.onResponse)
|
|
186
|
-
this.currentCmd.onResponse(data);
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
if (this.currentCmd.onResponse)
|
|
190
|
-
this.currentCmd.onResponse(data);
|
|
191
|
-
}
|
|
192
|
-
this.currentCmd = undefined;
|
|
193
|
-
}
|
|
194
|
-
write(cmd) {
|
|
195
|
-
this.sendState = SendState.Sending;
|
|
196
|
-
const { logStr, message, timeout = (this.settings.timeout || DEFAULT_RCV_TIMEOUT) } = cmd;
|
|
197
|
-
const msg = typeof message === 'string' ? message : (0, utils_1.hexstr)(message);
|
|
198
|
-
const onError = (err) => {
|
|
199
|
-
this.logger.logEvent({ message: "sendCommand:error:", cmd: logStr, error: err.message, port: this.getPort() });
|
|
200
|
-
if (cmd.onError)
|
|
201
|
-
cmd.onError(err);
|
|
202
|
-
this.sendState = SendState.Idle;
|
|
203
|
-
this.currentCmd = undefined;
|
|
204
|
-
this.stopCurrentTimeoutCheck();
|
|
205
|
-
};
|
|
206
|
-
try {
|
|
207
|
-
this.logger.logEvent({ message: "sendCommand:sending:", cmd: logStr, msg, port: this.getPort() });
|
|
208
|
-
if (typeof (message) !== 'string')
|
|
209
|
-
throw new Error('message must be a string');
|
|
210
|
-
this.sp.write(msg + CRLF, (err) => {
|
|
211
|
-
this.sendState = SendState.Receiving;
|
|
212
|
-
this.currentCmd = cmd;
|
|
213
|
-
if (err)
|
|
214
|
-
onError(err);
|
|
215
|
-
});
|
|
216
|
-
this.currentTimeout = setTimeout(() => {
|
|
217
|
-
if (this.sendState === SendState.Sending) {
|
|
218
|
-
onError(new Error("send timeout"));
|
|
219
|
-
}
|
|
220
|
-
if (this.sendState === SendState.Receiving) {
|
|
221
|
-
onError(new Error("response timeout"));
|
|
222
|
-
}
|
|
223
|
-
}, timeout);
|
|
224
|
-
}
|
|
225
|
-
catch (err) {
|
|
226
|
-
onError(err);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
sendNextCommand() {
|
|
230
|
-
if (this.sendState !== SendState.Idle) {
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
const cmd = this.queue.dequeue();
|
|
234
|
-
if (cmd)
|
|
235
|
-
this.write(cmd);
|
|
236
|
-
}
|
|
237
|
-
send(cmd) {
|
|
238
|
-
this.logger.logEvent({ message: 'add command to queue', cmd: cmd.logStr, msg: cmd.message, port: this.getPort(), queueSize: this.queue.size() });
|
|
239
|
-
this.queue.enqueue(cmd);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
exports.default = KettlerSerialComms;
|
|
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.SendState = exports.SerialCommsState = void 0;
|
|
16
|
+
const gd_eventlog_1 = require("gd-eventlog");
|
|
17
|
+
const utils_1 = require("../utils");
|
|
18
|
+
const events_1 = __importDefault(require("events"));
|
|
19
|
+
const DEFAULT_RCV_TIMEOUT = 1500;
|
|
20
|
+
const DEFAULT_OPEN_TIMEOUT = 3000;
|
|
21
|
+
const DEBUG_LOGGER = {
|
|
22
|
+
log: (e, ...args) => console.log(e, ...args),
|
|
23
|
+
logEvent: (event) => console.log(JSON.stringify(event))
|
|
24
|
+
};
|
|
25
|
+
var SerialCommsState;
|
|
26
|
+
(function (SerialCommsState) {
|
|
27
|
+
SerialCommsState[SerialCommsState["Idle"] = 0] = "Idle";
|
|
28
|
+
SerialCommsState[SerialCommsState["Connecting"] = 1] = "Connecting";
|
|
29
|
+
SerialCommsState[SerialCommsState["Connected"] = 2] = "Connected";
|
|
30
|
+
SerialCommsState[SerialCommsState["Disconnecting"] = 3] = "Disconnecting";
|
|
31
|
+
SerialCommsState[SerialCommsState["Disconnected"] = 4] = "Disconnected";
|
|
32
|
+
SerialCommsState[SerialCommsState["Error"] = 5] = "Error";
|
|
33
|
+
})(SerialCommsState = exports.SerialCommsState || (exports.SerialCommsState = {}));
|
|
34
|
+
var SendState;
|
|
35
|
+
(function (SendState) {
|
|
36
|
+
SendState[SendState["Idle"] = 0] = "Idle";
|
|
37
|
+
SendState[SendState["Sending"] = 1] = "Sending";
|
|
38
|
+
SendState[SendState["Receiving"] = 2] = "Receiving";
|
|
39
|
+
})(SendState = exports.SendState || (exports.SendState = {}));
|
|
40
|
+
const CRLF = '\r\n';
|
|
41
|
+
class KettlerSerialComms extends events_1.default {
|
|
42
|
+
constructor(opts) {
|
|
43
|
+
super();
|
|
44
|
+
this.stateIn = (allowedStates) => {
|
|
45
|
+
return allowedStates.indexOf(this.state) >= 0;
|
|
46
|
+
};
|
|
47
|
+
this.logger = process.env.DEBUG ? DEBUG_LOGGER : (opts.logger || new gd_eventlog_1.EventLogger(opts.protocol.getName()));
|
|
48
|
+
this.port = opts.port || process.env.COM_PORT;
|
|
49
|
+
this.sp = undefined;
|
|
50
|
+
this.queue = new utils_1.Queue();
|
|
51
|
+
this.state = SerialCommsState.Idle;
|
|
52
|
+
this.sendState = SendState.Idle;
|
|
53
|
+
this.settings = opts.settings || {};
|
|
54
|
+
this.currentCmd = undefined;
|
|
55
|
+
this.currentTimeout = undefined;
|
|
56
|
+
this.protocol = opts.protocol;
|
|
57
|
+
}
|
|
58
|
+
getPort() {
|
|
59
|
+
return this.port;
|
|
60
|
+
}
|
|
61
|
+
setPort(port) {
|
|
62
|
+
this.port = port;
|
|
63
|
+
}
|
|
64
|
+
getLogger() {
|
|
65
|
+
return this.logger;
|
|
66
|
+
}
|
|
67
|
+
isConnected() {
|
|
68
|
+
return this.state === SerialCommsState.Connected;
|
|
69
|
+
}
|
|
70
|
+
_setState(state) {
|
|
71
|
+
this.state = state;
|
|
72
|
+
}
|
|
73
|
+
_setSendState(state) {
|
|
74
|
+
this.sendState = state;
|
|
75
|
+
}
|
|
76
|
+
_setCurrentCmd(cmd) {
|
|
77
|
+
this.currentCmd = cmd;
|
|
78
|
+
}
|
|
79
|
+
stopCurrentTimeoutCheck() {
|
|
80
|
+
if (this.currentTimeout) {
|
|
81
|
+
clearTimeout(this.currentTimeout);
|
|
82
|
+
this.currentTimeout = undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
onPortOpen() {
|
|
86
|
+
this.logger.logEvent({ message: 'port opened', port: this.getPort() });
|
|
87
|
+
this.state = SerialCommsState.Connected;
|
|
88
|
+
this.sendState = SendState.Idle;
|
|
89
|
+
this.stopCurrentTimeoutCheck();
|
|
90
|
+
this.startWorker();
|
|
91
|
+
this.emit('opened');
|
|
92
|
+
}
|
|
93
|
+
onPortClose() {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
this.logger.logEvent({ message: 'port closed', port: this.getPort() });
|
|
96
|
+
this.stopWorker();
|
|
97
|
+
if (this.sendState === SendState.Sending) {
|
|
98
|
+
}
|
|
99
|
+
this.state = SerialCommsState.Disconnected;
|
|
100
|
+
this.sendState = SendState.Idle;
|
|
101
|
+
this.stopCurrentTimeoutCheck();
|
|
102
|
+
this.queue.clear();
|
|
103
|
+
this.emit('closed');
|
|
104
|
+
this.sp.removeAllListeners();
|
|
105
|
+
this.sp = undefined;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
onPortError(err) {
|
|
109
|
+
let ignore = false;
|
|
110
|
+
if (this.stateIn([SerialCommsState.Connected, SerialCommsState.Disconnected]))
|
|
111
|
+
ignore = true;
|
|
112
|
+
if (this.state === SerialCommsState.Disconnecting && (err.message === 'Port is not open' || err.message === 'Writing to COM port (GetOverlappedResult): Operation aborted'))
|
|
113
|
+
ignore = true;
|
|
114
|
+
if (this.state === SerialCommsState.Connecting && (err.message === 'Port is already open' || err.message === 'Port is opening'))
|
|
115
|
+
ignore = true;
|
|
116
|
+
if (!ignore) {
|
|
117
|
+
this.logger.logEvent({ message: "port error:", port: this.getPort(), error: err.message, stack: err.stack, state: this.state });
|
|
118
|
+
this.emit('error', err);
|
|
119
|
+
this.stopCurrentTimeoutCheck();
|
|
120
|
+
if (this.state === SerialCommsState.Connecting || this.state === SerialCommsState.Disconnecting) {
|
|
121
|
+
this.state = SerialCommsState.Error;
|
|
122
|
+
this.sp.removeAllListeners();
|
|
123
|
+
this.sp = undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
open() {
|
|
128
|
+
this.logger.logEvent({ message: "open()", port: this.getPort() });
|
|
129
|
+
if (this.stateIn([SerialCommsState.Connected, SerialCommsState.Connecting, SerialCommsState.Disconnecting])) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
const SerialPort = this.protocol.getSerialPort();
|
|
134
|
+
if (this.sp === undefined) {
|
|
135
|
+
this.sp = new SerialPort(this.getPort(), this.settings);
|
|
136
|
+
this.sp.on('open', () => { this.onPortOpen(); });
|
|
137
|
+
this.sp.on('close', () => { this.onPortClose(); });
|
|
138
|
+
this.sp.on('error', (error) => { this.onPortError(error); });
|
|
139
|
+
}
|
|
140
|
+
this.state = SerialCommsState.Connecting;
|
|
141
|
+
const parser = this.sp.pipe(new SerialPort.parsers.Readline({ delimiter: CRLF }));
|
|
142
|
+
parser.on('data', (data) => { this.onData(data); });
|
|
143
|
+
this.sp.open();
|
|
144
|
+
const timeout = this.settings.openTimeout || DEFAULT_OPEN_TIMEOUT;
|
|
145
|
+
this.currentTimeout = setTimeout(() => {
|
|
146
|
+
this.logger.logEvent({ message: "open() timeout", port: this.getPort() });
|
|
147
|
+
this.onPortError(new Error("open() timeout"));
|
|
148
|
+
}, timeout);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
this.logger.logEvent({ message: "error", fn: 'open()', error: err.message });
|
|
152
|
+
this.state = SerialCommsState.Disconnected;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
close() {
|
|
156
|
+
this.logger.logEvent({ message: 'close()', port: this.getPort() });
|
|
157
|
+
if (this.stateIn([SerialCommsState.Idle, SerialCommsState.Disconnected, SerialCommsState.Disconnecting])) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.state = SerialCommsState.Disconnecting;
|
|
161
|
+
this.sp.close();
|
|
162
|
+
}
|
|
163
|
+
startWorker() {
|
|
164
|
+
this.worker = setInterval(() => {
|
|
165
|
+
this.sendNextCommand();
|
|
166
|
+
}, 50);
|
|
167
|
+
}
|
|
168
|
+
stopWorker() {
|
|
169
|
+
if (this.worker) {
|
|
170
|
+
clearInterval(this.worker);
|
|
171
|
+
this.worker = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
clearTimeout() {
|
|
175
|
+
if (this.currentTimeout) {
|
|
176
|
+
clearTimeout(this.currentTimeout);
|
|
177
|
+
this.currentTimeout = undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
onData(data) {
|
|
181
|
+
this.clearTimeout();
|
|
182
|
+
this.logger.logEvent({ message: "sendCommand:receiving:", data: data });
|
|
183
|
+
this.sendState = SendState.Idle;
|
|
184
|
+
if (typeof data === 'string') {
|
|
185
|
+
if (this.currentCmd.onResponse)
|
|
186
|
+
this.currentCmd.onResponse(data);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
if (this.currentCmd.onResponse)
|
|
190
|
+
this.currentCmd.onResponse(data);
|
|
191
|
+
}
|
|
192
|
+
this.currentCmd = undefined;
|
|
193
|
+
}
|
|
194
|
+
write(cmd) {
|
|
195
|
+
this.sendState = SendState.Sending;
|
|
196
|
+
const { logStr, message, timeout = (this.settings.timeout || DEFAULT_RCV_TIMEOUT) } = cmd;
|
|
197
|
+
const msg = typeof message === 'string' ? message : (0, utils_1.hexstr)(message);
|
|
198
|
+
const onError = (err) => {
|
|
199
|
+
this.logger.logEvent({ message: "sendCommand:error:", cmd: logStr, error: err.message, port: this.getPort() });
|
|
200
|
+
if (cmd.onError)
|
|
201
|
+
cmd.onError(err);
|
|
202
|
+
this.sendState = SendState.Idle;
|
|
203
|
+
this.currentCmd = undefined;
|
|
204
|
+
this.stopCurrentTimeoutCheck();
|
|
205
|
+
};
|
|
206
|
+
try {
|
|
207
|
+
this.logger.logEvent({ message: "sendCommand:sending:", cmd: logStr, msg, port: this.getPort() });
|
|
208
|
+
if (typeof (message) !== 'string')
|
|
209
|
+
throw new Error('message must be a string');
|
|
210
|
+
this.sp.write(msg + CRLF, (err) => {
|
|
211
|
+
this.sendState = SendState.Receiving;
|
|
212
|
+
this.currentCmd = cmd;
|
|
213
|
+
if (err)
|
|
214
|
+
onError(err);
|
|
215
|
+
});
|
|
216
|
+
this.currentTimeout = setTimeout(() => {
|
|
217
|
+
if (this.sendState === SendState.Sending) {
|
|
218
|
+
onError(new Error("send timeout"));
|
|
219
|
+
}
|
|
220
|
+
if (this.sendState === SendState.Receiving) {
|
|
221
|
+
onError(new Error("response timeout"));
|
|
222
|
+
}
|
|
223
|
+
}, timeout);
|
|
224
|
+
}
|
|
225
|
+
catch (err) {
|
|
226
|
+
onError(err);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
sendNextCommand() {
|
|
230
|
+
if (this.sendState !== SendState.Idle) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const cmd = this.queue.dequeue();
|
|
234
|
+
if (cmd)
|
|
235
|
+
this.write(cmd);
|
|
236
|
+
}
|
|
237
|
+
send(cmd) {
|
|
238
|
+
this.logger.logEvent({ message: 'add command to queue', cmd: cmd.logStr, msg: cmd.message, port: this.getPort(), queueSize: this.queue.size() });
|
|
239
|
+
this.queue.enqueue(cmd);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
exports.default = KettlerSerialComms;
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import CyclingMode, { CyclingModeProperty, IncyclistBikeData, UpdateRequest } from '../../
|
|
2
|
-
import KettlerAdapter from "./adapter";
|
|
3
|
-
import PowerBasedCyclingModeBase from '../../modes/power-base';
|
|
4
|
-
export declare type ERGEvent = {
|
|
5
|
-
rpmUpdated?: boolean;
|
|
6
|
-
gearUpdated?: boolean;
|
|
7
|
-
starting?: boolean;
|
|
8
|
-
tsStart?: number;
|
|
9
|
-
};
|
|
10
|
-
export default class ERGCyclingMode extends PowerBasedCyclingModeBase implements CyclingMode {
|
|
11
|
-
static isERG: boolean;
|
|
12
|
-
prevRequest: UpdateRequest;
|
|
13
|
-
hasBikeUpdate: boolean;
|
|
14
|
-
chain: number[];
|
|
15
|
-
cassette: number[];
|
|
16
|
-
event: ERGEvent;
|
|
17
|
-
constructor(adapter: KettlerAdapter, props?: any);
|
|
18
|
-
getName(): string;
|
|
19
|
-
getDescription(): string;
|
|
20
|
-
getProperties(): CyclingModeProperty[];
|
|
21
|
-
getProperty(name: string): CyclingModeProperty;
|
|
22
|
-
getBikeInitRequest(): UpdateRequest;
|
|
23
|
-
sendBikeUpdate(request: UpdateRequest): UpdateRequest;
|
|
24
|
-
updateData(bikeData: IncyclistBikeData): any;
|
|
25
|
-
}
|
|
1
|
+
import CyclingMode, { CyclingModeProperty, IncyclistBikeData, UpdateRequest } from '../../cycling-mode';
|
|
2
|
+
import KettlerAdapter from "./adapter";
|
|
3
|
+
import PowerBasedCyclingModeBase from '../../modes/power-base';
|
|
4
|
+
export declare type ERGEvent = {
|
|
5
|
+
rpmUpdated?: boolean;
|
|
6
|
+
gearUpdated?: boolean;
|
|
7
|
+
starting?: boolean;
|
|
8
|
+
tsStart?: number;
|
|
9
|
+
};
|
|
10
|
+
export default class ERGCyclingMode extends PowerBasedCyclingModeBase implements CyclingMode {
|
|
11
|
+
static isERG: boolean;
|
|
12
|
+
prevRequest: UpdateRequest;
|
|
13
|
+
hasBikeUpdate: boolean;
|
|
14
|
+
chain: number[];
|
|
15
|
+
cassette: number[];
|
|
16
|
+
event: ERGEvent;
|
|
17
|
+
constructor(adapter: KettlerAdapter, props?: any);
|
|
18
|
+
getName(): string;
|
|
19
|
+
getDescription(): string;
|
|
20
|
+
getProperties(): CyclingModeProperty[];
|
|
21
|
+
getProperty(name: string): CyclingModeProperty;
|
|
22
|
+
getBikeInitRequest(): UpdateRequest;
|
|
23
|
+
sendBikeUpdate(request: UpdateRequest): UpdateRequest;
|
|
24
|
+
updateData(bikeData: IncyclistBikeData): any;
|
|
25
|
+
}
|