incyclist-devices 2.0.35 → 2.0.36

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 (54) hide show
  1. package/lib/antv2/modes/ant-fe-adv-st-mode.d.ts +2 -2
  2. package/lib/antv2/modes/ant-fe-erg-mode.d.ts +2 -2
  3. package/lib/antv2/modes/ant-fe-st-mode.d.ts +2 -2
  4. package/lib/base/adpater.d.ts +2 -1
  5. package/lib/base/adpater.js +7 -6
  6. package/lib/interfaces.d.ts +1 -1
  7. package/lib/modes/ble-erg-mode.d.ts +2 -2
  8. package/lib/modes/ble-st-mode.d.ts +2 -2
  9. package/lib/modes/cycling-mode.d.ts +7 -7
  10. package/lib/modes/power-base.d.ts +3 -3
  11. package/lib/modes/power-base.js +3 -2
  12. package/lib/modes/power-meter.d.ts +2 -2
  13. package/lib/serial/comms.d.ts +62 -0
  14. package/lib/serial/comms.js +280 -0
  15. package/lib/serial/daum/DaumAdapter.d.ts +13 -8
  16. package/lib/serial/daum/DaumAdapter.js +49 -12
  17. package/lib/serial/daum/ERGCyclingMode.d.ts +2 -2
  18. package/lib/serial/daum/ERGCyclingMode.js +1 -1
  19. package/lib/serial/daum/SmartTrainerCyclingMode.d.ts +3 -2
  20. package/lib/serial/daum/SmartTrainerCyclingMode.js +10 -5
  21. package/lib/serial/daum/classic/adapter.d.ts +4 -6
  22. package/lib/serial/daum/classic/adapter.js +5 -23
  23. package/lib/serial/daum/classic/comms.d.ts +33 -65
  24. package/lib/serial/daum/classic/comms.js +148 -332
  25. package/lib/serial/daum/classic/mock.js +5 -3
  26. package/lib/serial/daum/classic/modes/daum-classic.d.ts +2 -2
  27. package/lib/serial/daum/classic/modes/daum-classic.js +1 -1
  28. package/lib/serial/daum/classic/types.d.ts +59 -0
  29. package/lib/serial/daum/classic/types.js +2 -0
  30. package/lib/serial/daum/classic/utils.d.ts +11 -10
  31. package/lib/serial/daum/classic/utils.js +33 -68
  32. package/lib/serial/daum/premium/adapter.d.ts +4 -7
  33. package/lib/serial/daum/premium/adapter.js +7 -30
  34. package/lib/serial/daum/premium/comms.d.ts +28 -105
  35. package/lib/serial/daum/premium/comms.js +262 -466
  36. package/lib/serial/daum/premium/consts.d.ts +6 -0
  37. package/lib/serial/daum/premium/consts.js +9 -0
  38. package/lib/serial/daum/premium/mock.d.ts +32 -1
  39. package/lib/serial/daum/premium/mock.js +131 -8
  40. package/lib/serial/daum/premium/modes/daum-classic.d.ts +2 -2
  41. package/lib/serial/daum/premium/modes/daum-classic.js +1 -1
  42. package/lib/serial/daum/premium/types.d.ts +35 -1
  43. package/lib/serial/daum/premium/types.js +29 -0
  44. package/lib/serial/daum/premium/utils.d.ts +11 -18
  45. package/lib/serial/daum/premium/utils.js +25 -18
  46. package/lib/serial/serial-interface.js +3 -2
  47. package/lib/types/adapter.d.ts +2 -0
  48. package/lib/types/device.d.ts +11 -0
  49. package/lib/types/route.d.ts +0 -5
  50. package/lib/types/route.js +0 -7
  51. package/lib/utils/calculations.js +1 -5
  52. package/lib/utils/utils.d.ts +2 -1
  53. package/lib/utils/utils.js +39 -3
  54. package/package.json +3 -2
@@ -8,401 +8,217 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const gd_eventlog_1 = require("gd-eventlog");
13
- const utils_1 = require("./utils");
14
- const utils_2 = require("../../../utils/utils");
15
- const __1 = require("../..");
15
+ const utils_1 = require("../../../utils/utils");
16
+ const comms_1 = __importDefault(require("../../comms"));
17
+ const types_1 = require("../premium/types");
18
+ const utils_2 = require("./utils");
16
19
  const ByteLength = require('@serialport/parser-byte-length');
17
- const nop = () => { };
18
20
  const TIMEOUT_SEND = 2000;
19
- class Daum8008 {
20
- constructor(props) {
21
- this._timeoutSend = TIMEOUT_SEND;
22
- const { logger, serial, path } = props;
23
- this.logger = logger || new gd_eventlog_1.EventLogger('DaumClassic');
24
- this.portName = path;
25
- this.serial = serial;
26
- this.spp = (0, __1.useSerialPortProvider)();
27
- this.sp = undefined;
28
- this.error = undefined;
29
- this.opening = false;
30
- this.connected = false;
31
- this.closing = false;
32
- this.closed = undefined;
33
- this.cmdBusy = false;
34
- this.queue = new utils_2.Queue();
35
- this.isLoggingPaused = false;
36
- }
37
- static setSerialPort(spClass) {
38
- __1.SerialPortProvider.getInstance().setLegacyClass('serial', spClass);
39
- }
40
- static getClassName() {
41
- return "Daum8008";
42
- }
43
- getType() {
44
- return "DaumClassic";
45
- }
46
- getPort() {
47
- return this.portName;
21
+ class Daum8008 extends comms_1.default {
22
+ validatePath(path) {
23
+ return path;
48
24
  }
49
- isConnected() {
50
- return this.connected;
25
+ getDefaultLoggerName() {
26
+ return 'DaumClassic';
51
27
  }
52
- pauseLogging() {
53
- this.isLoggingPaused = true;
28
+ onConnected() {
54
29
  }
55
- resumeLogging() {
56
- this.isLoggingPaused = false;
30
+ getTimeoutValue() {
31
+ return TIMEOUT_SEND;
57
32
  }
58
- logEvent(e) {
59
- if (!this.isLoggingPaused)
60
- this.logger.logEvent(e);
61
- const w = global.window;
62
- if (w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) {
63
- console.log('~~~ DaumClassic', e);
64
- }
33
+ initForResponse(expected) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const parser = this.portPipe(new ByteLength({ length: expected }));
36
+ if (!parser)
37
+ return;
38
+ parser.on('data', (data) => {
39
+ this.portUnpipe();
40
+ this.recvState.data.enqueue({ type: 'Response', data });
41
+ });
42
+ });
65
43
  }
66
- connect() {
44
+ waitForResponse() {
67
45
  return __awaiter(this, void 0, void 0, function* () {
68
- if (this.isConnected() && this.sp) {
69
- return true;
70
- }
71
- try {
72
- this.logEvent({ message: 'connect attempt', port: this.portName });
73
- const port = yield this.serial.openPort(this.portName);
74
- if (port !== null) {
75
- this.logEvent({ message: 'connect success', port: this.portName });
76
- this.connected = true;
77
- this.sp = port;
78
- this.sp.on('close', this.onPortClose.bind(this));
79
- this.sp.on('error', this.onPortError.bind(this));
80
- return true;
81
- }
82
- else {
83
- this.logEvent({ message: 'connect failure' });
84
- return false;
46
+ const timeout = this.getTimeoutValue();
47
+ let waitingForResponse = true;
48
+ let start = Date.now();
49
+ let tsTimeout = start + timeout;
50
+ while (waitingForResponse && Date.now() < tsTimeout) {
51
+ const response = this.recvState.data.dequeue();
52
+ if (response) {
53
+ return response;
85
54
  }
55
+ yield (0, utils_1.sleep)(5);
86
56
  }
87
- catch (err) {
88
- this.logEvent({ message: 'connect failure', reason: err.message });
89
- return false;
57
+ throw new types_1.ResponseTimeout();
58
+ });
59
+ }
60
+ doSend(expected, payload) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ this.initForResponse(expected);
63
+ yield this.write(Buffer.from(payload));
64
+ const response = yield this.waitForResponse();
65
+ if (response.type === 'Error')
66
+ throw response.error;
67
+ if (response.data[0] !== payload[0]) {
68
+ this.portFlush();
69
+ throw new Error('illegal response');
90
70
  }
71
+ return response;
91
72
  });
92
73
  }
93
- close() {
74
+ send(request) {
94
75
  return __awaiter(this, void 0, void 0, function* () {
95
- if (!this.serial)
96
- return;
97
- this.closing = true;
98
- this.stopWorker();
99
- if (this.isConnected()) {
76
+ const { expected, command, logString } = request;
77
+ const payload = Array.isArray(command) ? new Uint8Array(command) : command;
78
+ let logPayload = {
79
+ port: this.path,
80
+ cmd: logString || 'BinaryCommand',
81
+ };
82
+ yield this.ensurePrevCmdFinish(logPayload);
83
+ this.sendCmdPromise = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
100
84
  try {
101
- yield this.flush();
85
+ this.logEvent(Object.assign(Object.assign({ message: "sendCommand:sending:" }, logPayload), { hex: Buffer.from(payload).toString('hex') }));
86
+ yield this.ensureConnection();
87
+ const res = yield this.doSend(expected, payload);
88
+ this.logEvent(Object.assign(Object.assign({ message: "sendCommand:received:" }, logPayload), { hex: Buffer.from(res.data).toString('hex') }));
89
+ this.sendCmdPromise = null;
90
+ resolve(res);
102
91
  }
103
- catch (_a) { }
104
- try {
105
- yield this.serial.closePort(this.portName);
92
+ catch (err) {
93
+ this.logEvent(Object.assign(Object.assign({ message: "sendCommand:error:" }, logPayload), { error: err.message }));
94
+ this.sendCmdPromise = null;
95
+ reject(err);
106
96
  }
107
- catch (_b) { }
108
- }
109
- this.connected = false;
110
- this.cmdBusy = false;
111
- if (this.sp) {
112
- this.sp.removeAllListeners();
113
- this.sp = null;
114
- }
115
- this.error = undefined;
116
- this.closing = false;
117
- this.closed = true;
118
- return;
97
+ }));
98
+ return this.sendCmdPromise;
119
99
  });
120
100
  }
121
- flush() {
101
+ sendCommand(logString, command, expected) {
122
102
  return __awaiter(this, void 0, void 0, function* () {
123
- if (!this.cmdBusy)
124
- return;
125
- return new Promise(done => {
126
- const tsStart = Date.now();
127
- const iv = setInterval(() => {
128
- if (!this.cmdBusy || (Date.now() - tsStart > TIMEOUT_SEND + 500)) {
129
- clearInterval(iv);
130
- done();
131
- }
132
- }, 100);
133
- });
103
+ const response = yield this.send({ logString, command, expected });
104
+ return response.data;
134
105
  });
135
106
  }
136
- onPortClose() {
107
+ checkCockpit(bikeNo = 0) {
137
108
  return __awaiter(this, void 0, void 0, function* () {
138
- this.logEvent({ message: "port closed", port: this.getPort() });
139
- yield this.close();
140
- });
141
- }
142
- onPortError(err) {
143
- if (this.closed && !this.opening)
144
- return;
145
- if ((this.closing || this.closed) && (err.message === 'Port is not open' || err.message === 'Writing to COM port (GetOverlappedResult): Operation aborted'))
146
- return;
147
- const state = { opening: this.opening, connected: this.connected, closing: this.closing, closed: this.closed, busy: this.cmdBusy };
148
- this.logEvent({ message: "port error:", port: this.getPort(), error: err.message, state });
149
- this.error = err;
150
- this.cmdBusy = false;
151
- }
152
- startWorker() {
153
- this.bikeCmdWorker = setInterval(() => {
154
- this.sendDaum8008CommandfromQueue();
155
- }, 50);
156
- }
157
- stopWorker() {
158
- this.logEvent({ message: "stop worker", port: this.getPort() });
159
- if (this.queue !== undefined)
160
- this.queue.clear();
161
- if (this.bikeCmdWorker !== undefined) {
162
- clearInterval(this.bikeCmdWorker);
163
- this.bikeCmdWorker = undefined;
164
- }
165
- }
166
- sendDaum8008CommandfromQueue() {
167
- if (!this.connected || this.closing || !this.sp)
168
- return;
169
- if (this.cmdStart !== undefined && this.error !== undefined) {
170
- if (this.cmdStart !== undefined) {
171
- const cmdInfo = this.cmdCurrent;
172
- var retry = 0;
173
- if (cmdInfo.options !== undefined && cmdInfo.options.retries !== undefined) {
174
- retry = this.cmdCurrent.options.retries;
175
- }
176
- if (cmdInfo.callbackErr !== undefined && retry === 0) {
177
- let cb = cmdInfo.callbackErr;
178
- let error = this.error;
179
- this.cmdBusy = false;
180
- this.cmdCurrent = undefined;
181
- this.cmdStart = undefined;
182
- this.error = undefined;
183
- return cb(500, { message: error });
184
- }
109
+ try {
110
+ const data = yield this.sendCommand(`checkCockpit(${bikeNo})`, [0x10, bikeNo], 3);
111
+ return { bike: data[1], version: data[2] };
185
112
  }
186
- }
187
- if (this.connected && this.cmdBusy && !this.closing) {
188
- if (this.cmdCurrent !== undefined && this.cmdCurrent.start !== undefined) {
189
- const cmdInfo = this.cmdCurrent;
190
- const timeout = (cmdInfo.options && cmdInfo.options.timeout) ? cmdInfo.options.timeout : this._timeoutSend;
191
- let d = Date.now() - cmdInfo.start;
192
- if (d > timeout) {
193
- this.logEvent({ message: 'sendCommmand:timeout', port: this.getPort() });
194
- const port = this.sp;
195
- port.unpipe();
196
- port.flush();
197
- if (this.cmdCurrent.callbackErr !== undefined) {
198
- let cb = this.cmdCurrent.callbackErr;
199
- this.cmdBusy = false;
200
- this.cmdCurrent = undefined;
201
- this.cmdStart = undefined;
202
- return cb(408, { message: "timeout" });
203
- }
204
- }
113
+ catch (err) {
114
+ if (err instanceof types_1.ResponseTimeout)
115
+ return { bike: bikeNo, version: undefined };
116
+ throw err;
205
117
  }
206
- return;
207
- }
208
- else {
209
- }
210
- if (this.cmdBusy)
211
- return;
212
- if (this.queue === undefined || this.queue.isEmpty()) {
213
- return;
214
- }
215
- const cmd = this.queue.dequeue();
216
- if (this.connected && !this.closing && !this.closed)
217
- this.send(cmd);
218
- }
219
- sendDaum8008Command(logStr, payload, expected, callback, callbackErr, options) {
220
- let cmdInfo = {
221
- logStr,
222
- payload,
223
- expected,
224
- callback: callback || nop,
225
- callbackErr: callbackErr || nop,
226
- options: options
227
- };
228
- this.queue.enqueue(cmdInfo);
229
- if (this.queue.size() > 1)
230
- this.logEvent({ message: "sendCommand:adding:", cmd: logStr, hex: (0, utils_1.hexstr)(payload), queueSize: this.queue.size() });
231
- if (this.bikeCmdWorker === undefined) {
232
- this.startWorker();
233
- }
234
- }
235
- send(cmdInfo) {
236
- this.cmdBusy = true;
237
- this.cmdCurrent = cmdInfo;
238
- const { logStr, payload, expected, callback, callbackErr } = cmdInfo;
239
- const done = () => {
240
- this.cmdBusy = false;
241
- this.cmdCurrent = undefined;
242
- this.cmdStart = undefined;
243
- };
244
- try {
245
- const serialPort = this.sp;
246
- const parser = serialPort.pipe(new ByteLength({ length: expected }));
247
- parser.on('data', (data) => {
248
- let duration = Date.now() - this.cmdStart;
249
- this.logEvent({ message: "sendCommand:received:", duration, hex: (0, utils_1.hexstr)(data), port: this.getPort() });
250
- serialPort.unpipe();
251
- if (callbackErr !== undefined) {
252
- if (data[0] !== payload[0]) {
253
- serialPort.flush();
254
- this.logEvent({ message: "sendCommand:illegal response", port: this.getPort() });
255
- done();
256
- return callbackErr(512, { message: "illegal response" });
257
- }
258
- }
259
- callback(data);
260
- done();
261
- });
262
- this.logEvent({ message: "sendCommand:sending:", cmd: logStr, hex: (0, utils_1.hexstr)(payload), port: this.getPort() });
263
- this.cmdCurrent.start = this.cmdStart = Date.now();
264
- serialPort.write(payload);
265
- }
266
- catch (err) {
267
- this.logEvent({ message: "sendCommand:error:", error: err.message, port: this.getPort() });
268
- done();
269
- callbackErr(500, { message: `Exception: ${err.message}` });
270
- }
271
- }
272
- checkCockpit(bikeNo = 0) {
273
- return new Promise((resolve, reject) => {
274
- this.sendDaum8008Command(`checkCockpit(${bikeNo})`, [0x10, bikeNo], 3, (data) => resolve({ bike: data[1], version: data[2] }), (status, err) => {
275
- if (status === 408)
276
- return resolve({ bike: bikeNo, version: undefined });
277
- reject((0, utils_1.buildError)(status, err));
278
- });
279
118
  });
280
119
  }
281
120
  getAddress() {
282
- return new Promise((resolve, reject) => {
283
- this.sendDaum8008Command(`getAddress()`, [0x11], 2, (data) => resolve({ bike: data[1] }), (status, err) => reject((0, utils_1.buildError)(status, err)));
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ const data = yield this.sendCommand(`getAddress()`, [0x11], 2);
123
+ return { bike: data[1] };
284
124
  });
285
125
  }
286
126
  getVersion(bikeNo = 0) {
287
- return new Promise((resolve, reject) => {
288
- this.sendDaum8008Command(`getVersion(${bikeNo})`, [0x73, bikeNo], 11, (data) => resolve({ bike: data[1], serialNo: (0, utils_1.hexstr)(data, 2, 8), cockpit: (0, utils_1.getCockpit)(data[10]) }), (status, err) => reject((0, utils_1.buildError)(status, err)));
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const data = yield this.sendCommand(`getVersion(${bikeNo})`, [0x73, bikeNo], 11);
129
+ return { bike: data[1], serialNo: (0, utils_2.getSerialNo)(data, 2, 8), cockpit: (0, utils_2.getCockpit)(data[10]) };
289
130
  });
290
131
  }
291
132
  resetDevice(bikeNo = 0) {
292
- return new Promise((resolve, reject) => {
293
- this.sendDaum8008Command(`resetDevice(${bikeNo})`, [0x12, bikeNo], 2, (data) => resolve({}), (status, err) => reject((0, utils_1.buildError)(status, err)));
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const data = yield this.sendCommand(`resetDevice(${bikeNo})`, [0x12, bikeNo], 2);
135
+ return { bike: data[1] };
294
136
  });
295
137
  }
296
138
  startProg(bikeNo = 0) {
297
- return new Promise((resolve, reject) => {
298
- this.sendDaum8008Command(`startProg(${bikeNo})`, [0x21, bikeNo], 3, (data) => resolve({ bike: data[1], pedalling: data[2] > 0 }), (status, err) => reject((0, utils_1.buildError)(status, err)));
139
+ return __awaiter(this, void 0, void 0, function* () {
140
+ const data = yield this.sendCommand(`startProg(${bikeNo})`, [0x21, bikeNo], 3);
141
+ return { bike: data[1], pedalling: data[2] > 0 };
299
142
  });
300
143
  }
301
144
  stopProg(bikeNo = 0) {
302
- return new Promise((resolve, reject) => {
303
- this.sendDaum8008Command(`stopProg(${bikeNo})`, [0x22, bikeNo], 3, (data) => resolve({ bike: data[1], pedalling: data[2] !== 0 }), (status, err) => reject((0, utils_1.buildError)(status, err)));
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ const data = yield this.sendCommand(`stopProg(${bikeNo})`, [0x22, bikeNo], 3);
147
+ return { bike: data[1], pedalling: data[2] > 0 };
304
148
  });
305
149
  }
306
150
  setProg(progNo = 0, bikeNo = 0) {
307
- return new Promise((resolve, reject) => {
308
- this.sendDaum8008Command(`setProg(${bikeNo},${progNo})`, [0x23, bikeNo, progNo], 4, (data) => resolve({ bike: data[1], progNo: data[2], pedalling: data[3] !== 0 }), (status, err) => reject((0, utils_1.buildError)(status, err)));
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ const data = yield this.sendCommand(`setProg(${bikeNo},${progNo})`, [0x23, bikeNo, progNo], 4);
153
+ return { bike: data[1], progNo: data[2], pedalling: data[3] !== 0 };
309
154
  });
310
155
  }
311
156
  setBikeType(bikeType, bikeNo = 0) {
312
- const bikeVal = (0, utils_1.getBikeType)(bikeType);
313
- return new Promise((resolve, reject) => {
314
- this.sendDaum8008Command(`setBikeType(${bikeNo},${bikeType})`, [0x69, bikeNo, 0, 0, bikeVal], 3, (data) => resolve({}), (status, err) => reject((0, utils_1.buildError)(status, err)));
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ const bikeVal = (0, utils_2.getBikeType)(bikeType);
159
+ const data = yield this.sendCommand(`setBikeType(${bikeNo},${bikeType})`, [0x69, bikeNo, 0, 0, bikeVal], 3);
160
+ return { bike: data[1] };
315
161
  });
316
162
  }
317
163
  setPerson(user = {}, bikeNo = 0) {
318
- const age = user.age !== undefined ? user.age : utils_1.DEFAULT_AGE;
319
- const gender = (0, utils_1.getGender)(user.sex);
320
- const length = (0, utils_1.getLength)(user.length);
321
- const maxPower = 800;
322
- const mUser = user.weight;
323
- const weight = (0, utils_1.getWeight)(mUser) + 10;
324
- var cmd = [0x24, bikeNo, 0];
325
- cmd.push(age);
326
- cmd.push(gender);
327
- cmd.push(length);
328
- cmd.push(weight);
329
- cmd.push(0);
330
- cmd.push(0);
331
- cmd.push(3);
332
- cmd.push(Math.round(maxPower / 5));
333
- cmd.push(0);
334
- cmd.push(0);
335
- cmd.push(0);
336
- cmd.push(0);
337
- return new Promise((resolve, reject) => {
338
- this.sendDaum8008Command(`setPerson(${bikeNo},${age},${gender},${length},${weight})`, cmd, 16, (data) => {
339
- let ok = true;
340
- cmd.forEach((v, i) => {
341
- if (data[i] !== v) {
342
- if (i === 10 && v >= 160) {
343
- if (data[i] === 0 || data[i] === 80)
344
- return;
345
- }
346
- reject((0, utils_1.buildError)(512, 'illegal response'));
347
- ok = false;
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ const age = user.age !== undefined ? user.age : utils_2.DEFAULT_AGE;
166
+ const gender = (0, utils_2.getGender)(user.sex);
167
+ const length = (0, utils_2.getLength)(user.length);
168
+ const maxPower = 800;
169
+ const mUser = user.weight;
170
+ const weight = (0, utils_2.getWeight)(mUser) + 10;
171
+ var cmd = [0x24, bikeNo, 0];
172
+ cmd.push(age);
173
+ cmd.push(gender);
174
+ cmd.push(length);
175
+ cmd.push(weight);
176
+ cmd.push(0);
177
+ cmd.push(0);
178
+ cmd.push(3);
179
+ cmd.push(Math.round(maxPower / 5));
180
+ cmd.push(0);
181
+ cmd.push(0);
182
+ cmd.push(0);
183
+ cmd.push(0);
184
+ const data = yield this.sendCommand(`setPerson(${bikeNo},${age},${gender},${length},${weight})`, cmd, 16);
185
+ cmd.forEach((v, i) => {
186
+ if (data[i] !== v) {
187
+ if (i === 10 && v >= 160) {
188
+ if (data[i] === 0 || data[i] === 80)
189
+ return;
348
190
  }
349
- });
350
- if (ok)
351
- resolve({ bike: data[1], age, gender, length, weight });
352
- }, (status, err) => reject((0, utils_1.buildError)(status, err)));
191
+ throw new Error('illegal response');
192
+ }
193
+ });
194
+ return ({ bike: data[1], age, gender, length, weight });
353
195
  });
354
196
  }
355
197
  runData(bikeNo = 0) {
356
- return new Promise((resolve, reject) => {
357
- this.sendDaum8008Command(`runData(${bikeNo})`, [0x40, bikeNo], 19, (data) => {
358
- try {
359
- const parsed = (0, utils_1.parseRunData)(data);
360
- resolve(parsed);
361
- }
362
- catch (e) {
363
- reject((0, utils_1.buildError)(500, e));
364
- }
365
- }, (status, err) => reject((0, utils_1.buildError)(status, err)));
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ const data = yield this.sendCommand(`runData(${bikeNo})`, [0x40, bikeNo], 19);
200
+ return (0, utils_2.parseRunData)(data);
366
201
  });
367
202
  }
368
203
  setGear(gear, bikeNo = 0) {
369
- let gearVal = gear;
370
- if (gear === undefined || gear < 1)
371
- gearVal = 1;
372
- if (gear > 28)
373
- gearVal = 28;
374
- return new Promise((resolve, reject) => {
375
- this.sendDaum8008Command(`setGear(${bikeNo},${gearVal})`, [0x53, bikeNo, gearVal], 3, (data) => resolve({ bike: data[1], gear: data[2] }), (status, err) => reject((0, utils_1.buildError)(status, err)));
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ const gearVal = (0, utils_2.between)(gear, 1, 28);
206
+ const data = yield this.sendCommand(`setGear(${bikeNo},${gearVal})`, [0x53, bikeNo, gearVal], 3);
207
+ return ({ bike: data[1], gear: data[2] });
376
208
  });
377
209
  }
378
210
  setPower(power, bikeNo = 0) {
379
- return new Promise((resolve, reject) => {
380
- if (power === undefined) {
381
- resolve({});
382
- return;
383
- }
384
- let powerRequest = power;
385
- if (power < 25)
386
- powerRequest = 25;
387
- if (power > 800)
388
- powerRequest = 800;
389
- const powerVal = Math.round(powerRequest / 5);
390
- this.sendDaum8008Command(`setPower(${bikeNo},${power})`, [0x51, bikeNo, powerVal], 3, (data) => resolve({ bike: data[1], power: (data[2] * 5) }), (status, err) => reject((0, utils_1.buildError)(status, err)));
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ const powerVal = Math.round((0, utils_2.between)(power, 25, 800) / 5);
213
+ const data = yield this.sendCommand(`setPower(${bikeNo},${power})`, [0x51, bikeNo, powerVal], 3);
214
+ return ({ bike: data[1], power: data[2] * 5 });
391
215
  });
392
216
  }
393
217
  setSlope(slope, bikeNo = 0) {
394
- return new Promise((resolve, reject) => {
395
- if (slope === undefined) {
396
- resolve({});
397
- return;
398
- }
399
- const cmd = [0x55, bikeNo];
400
- const arr = (0, utils_1.Float32ToIntArray)(slope);
401
- cmd.push(arr[3]);
402
- cmd.push(arr[2]);
403
- cmd.push(arr[1]);
404
- cmd.push(arr[0]);
405
- this.sendDaum8008Command(`setSlope(${bikeNo},${slope})`, cmd, 6, (data) => resolve({ bike: data[1], slope: slope }), (status, err) => reject((0, utils_1.buildError)(status, err)));
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ const cmd = (0, utils_2.buildSetSlopeCommand)(bikeNo, slope);
220
+ const data = yield this.sendCommand(`setSlope(${bikeNo},${slope})`, cmd, 6);
221
+ return ({ bike: data[1], slope });
406
222
  });
407
223
  }
408
224
  }
@@ -77,7 +77,7 @@ class DaumClassicMockImpl {
77
77
  exports.DaumClassicMockImpl = DaumClassicMockImpl;
78
78
  const DEFAULT_BIKE_DATA = {
79
79
  cockpitVersion: 3,
80
- serialNo: '4464:<;8',
80
+ serialNo: '3434353d3b383c3b',
81
81
  cockpitType: 50,
82
82
  isPedalling: false,
83
83
  bikeType: 0,
@@ -212,8 +212,10 @@ class DaumClassicMockBinding extends binding_mock_1.MockPortBinding {
212
212
  if (bikeNo >= 0 && bikeNo < 10) {
213
213
  const { cockpitType, serialNo } = this.simulator.bikes[bikeNo] || {};
214
214
  const response = Buffer.from([0x73, bikeNo, 0, 0, 0, 0, 0, 0, 0, 0, cockpitType]);
215
- for (let i = 0; i < serialNo.length && i < 8; i++)
216
- response.writeUInt8(serialNo.charCodeAt(i), i + 2);
215
+ for (let i = 0; i < serialNo.length && i < 8; i++) {
216
+ const s = new Uint8Array(Buffer.from(serialNo, 'hex'));
217
+ response.writeUInt8(s[i], i + 2);
218
+ }
217
219
  this.emitData(response);
218
220
  }
219
221
  }
@@ -1,8 +1,8 @@
1
1
  import CyclingMode, { CyclingModeProperty, IncyclistBikeData, Settings, UpdateRequest } from "../../../../modes/cycling-mode";
2
2
  import SmartTrainerCyclingMode from "../../SmartTrainerCyclingMode";
3
- import DaumAdapter from "../../DaumAdapter";
3
+ import { ControllableDevice } from "../../../../base/adpater";
4
4
  export default class DaumClassicCyclingMode extends SmartTrainerCyclingMode implements CyclingMode {
5
- constructor(adapter: DaumAdapter, props?: Settings);
5
+ constructor(adapter: ControllableDevice, props?: Settings);
6
6
  getName(): string;
7
7
  getDescription(): string;
8
8
  getProperties(): CyclingModeProperty[];
@@ -16,7 +16,7 @@ const config = {
16
16
  class DaumClassicCyclingMode extends SmartTrainerCyclingMode_1.default {
17
17
  constructor(adapter, props) {
18
18
  super(adapter, props);
19
- this.logger = adapter ? adapter.logger : undefined;
19
+ this.logger = adapter ? adapter.getLogger() : undefined;
20
20
  if (!this.logger)
21
21
  this.logger = new gd_eventlog_1.EventLogger('DaumClassic');
22
22
  }
@@ -0,0 +1,59 @@
1
+ import { IncyclistBikeData } from "../../..";
2
+ import { DaumBikeData, DeviceProperties } from "../../../types/device";
3
+ import { Queue } from "../../../utils/utils";
4
+ import { Request, Response } from "../../comms";
5
+ export type DaumClassiAdapterProps = {
6
+ path: string;
7
+ ifaceName?: string;
8
+ };
9
+ export interface DaumClassicDeviceProperties extends DeviceProperties {
10
+ gear?: number;
11
+ }
12
+ export interface DaumClassicRequest extends Request {
13
+ expected: number;
14
+ command: number[] | Uint8Array;
15
+ }
16
+ export interface DaumClassicResponse extends Response {
17
+ type: ResponseType;
18
+ data?: Uint8Array;
19
+ error?: Error;
20
+ }
21
+ export interface DaumClassicBikeData extends DaumBikeData {
22
+ }
23
+ export type ResponseType = 'Response' | 'Error';
24
+ export type DaumClassicCommsState = {
25
+ data: Queue<DaumClassicResponse>;
26
+ };
27
+ export type ClassicBikeResponse = {
28
+ bike: number;
29
+ };
30
+ export interface checkCockpitReponse extends ClassicBikeResponse {
31
+ version?: number;
32
+ }
33
+ export interface GetVersionReponse extends ClassicBikeResponse {
34
+ serialNo: string;
35
+ cockpit: string;
36
+ }
37
+ export interface SetPersonResponse extends ClassicBikeResponse {
38
+ gender: number;
39
+ age: number;
40
+ weight: number;
41
+ length: number;
42
+ }
43
+ export interface ProgResponse extends ClassicBikeResponse {
44
+ pedalling: boolean;
45
+ }
46
+ export interface SetProgResponse extends ProgResponse {
47
+ progNo: number;
48
+ }
49
+ export interface SetPowerRepsonse extends ClassicBikeResponse {
50
+ power: number;
51
+ }
52
+ export interface SetGearRepsonse extends ClassicBikeResponse {
53
+ gear: number;
54
+ }
55
+ export interface SetSlopeRepsonse extends ClassicBikeResponse {
56
+ slope: number;
57
+ }
58
+ export interface GetRunDataRepsonse extends ClassicBikeResponse, IncyclistBikeData {
59
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });