incyclist-devices 1.4.19 → 1.4.22

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.
@@ -1,22 +1,45 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
2
14
  var __importStar = (this && this.__importStar) || function (mod) {
3
15
  if (mod && mod.__esModule) return mod;
4
16
  var result = {};
5
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
- result["default"] = mod;
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
7
19
  return result;
8
20
  };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
9
30
  var __importDefault = (this && this.__importDefault) || function (mod) {
10
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
11
32
  };
12
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.Simulator = void 0;
13
35
  const DeviceProtocol_1 = __importStar(require("../DeviceProtocol"));
14
36
  const DeviceRegistry_1 = __importDefault(require("../DeviceRegistry"));
15
37
  const Device_1 = __importDefault(require("../Device"));
16
38
  const gd_eventlog_1 = require("gd-eventlog");
17
- const calculations_1 = __importDefault(require("../calculations"));
39
+ const simulator_mode_1 = __importDefault(require("./simulator-mode"));
40
+ const DEFAULT_SETTINGS = { name: 'Simulator', port: '', isBot: false };
18
41
  class Simulator extends Device_1.default {
19
- constructor(protocol) {
42
+ constructor(protocol, props = DEFAULT_SETTINGS) {
20
43
  const proto = protocol || DeviceRegistry_1.default.findByName('Simulator');
21
44
  super(proto);
22
45
  this.logger = new gd_eventlog_1.EventLogger(Simulator.NAME);
@@ -27,8 +50,16 @@ class Simulator extends Device_1.default {
27
50
  this.time = undefined;
28
51
  this.iv = undefined;
29
52
  this.started = false;
53
+ this.paused = false;
30
54
  this.slope = 0;
31
55
  this.limit = {};
56
+ this.startTS = undefined;
57
+ this.data = { isPedalling: false, power: 0, pedalRpm: 0, speed: 0, heartrate: 0, distanceInternal: 0 };
58
+ this.isBot = props.isBot || false;
59
+ this.ignoreHrm = false;
60
+ const name = this.getCyclingMode().getName();
61
+ const modeSettings = this.isBot ? props.settings || {} : this.getCyclingMode().getSettings();
62
+ this.setCyclingMode(name, modeSettings);
32
63
  }
33
64
  isBike() { return true; }
34
65
  isHrm() { return false; }
@@ -36,28 +67,66 @@ class Simulator extends Device_1.default {
36
67
  getID() { return Simulator.NAME; }
37
68
  getName() { return Simulator.NAME; }
38
69
  getPort() { return 'local'; }
39
- start(props) {
40
- this.startProps = props;
41
- return new Promise((resolve) => {
42
- this.logger.logEvent({ message: 'start', iv: this.iv });
43
- if (this.started) {
44
- return resolve({ started: true, error: undefined });
70
+ setIgnoreHrm(ignore) {
71
+ this.ignoreHrm = ignore;
72
+ }
73
+ getSupportedCyclingModes() {
74
+ const supported = [];
75
+ supported.push(simulator_mode_1.default);
76
+ return supported;
77
+ }
78
+ getDefaultCyclingMode() {
79
+ return new simulator_mode_1.default(this);
80
+ }
81
+ getCyclingMode() {
82
+ if (!this.cyclingMode)
83
+ this.setCyclingMode(this.getDefaultCyclingMode());
84
+ return this.cyclingMode;
85
+ }
86
+ setCyclingMode(mode, settings) {
87
+ let selectedMode;
88
+ if (typeof mode === 'string') {
89
+ const supported = this.getSupportedCyclingModes();
90
+ const CyclingModeClass = supported.find(M => { const m = new M(this); return m.getName() === mode; });
91
+ if (CyclingModeClass) {
92
+ this.cyclingMode = new CyclingModeClass(this, settings);
93
+ return;
45
94
  }
46
- this.paused = (this.speed === 0);
47
- this.started = true;
48
- this.time = Date.now();
49
- if (this.iv !== undefined) {
50
- clearInterval(this.iv);
51
- this.iv = undefined;
52
- }
53
- this.speed = 30;
54
- this.iv = setInterval(() => this.update(), 1000);
55
- resolve({ started: true, error: undefined });
95
+ selectedMode = this.getDefaultCyclingMode();
96
+ }
97
+ else {
98
+ selectedMode = mode;
99
+ }
100
+ this.cyclingMode = selectedMode;
101
+ this.cyclingMode.setSettings(settings);
102
+ }
103
+ start(props) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ this.startProps = props;
106
+ return new Promise((resolve) => {
107
+ if (!this.isBot)
108
+ this.logger.logEvent({ message: 'start', iv: this.iv });
109
+ if (this.started) {
110
+ return resolve({ started: true, error: undefined });
111
+ }
112
+ this.started = true;
113
+ this.time = Date.now();
114
+ this.startTS = this.time;
115
+ if (this.iv !== undefined) {
116
+ clearInterval(this.iv);
117
+ this.iv = undefined;
118
+ }
119
+ this.iv = setInterval(() => this.update(), 1000);
120
+ if (!this.isBot)
121
+ this.logger.logEvent({ message: 'started' });
122
+ resolve({ started: true, error: undefined });
123
+ });
56
124
  });
57
125
  }
58
126
  stop() {
59
127
  return new Promise((resolve, reject) => {
60
- this.logger.logEvent({ message: 'stop', iv: this.iv });
128
+ if (!this.isBot)
129
+ this.logger.logEvent({ message: 'stop', iv: this.iv });
61
130
  this.started = false;
62
131
  clearInterval(this.iv);
63
132
  this.iv = undefined;
@@ -69,7 +138,8 @@ class Simulator extends Device_1.default {
69
138
  return new Promise((resolve, reject) => {
70
139
  if (!this.started)
71
140
  return reject(new Error('illegal state - pause() has been called before start()'));
72
- this.logger.logEvent({ message: 'pause', iv: this.iv });
141
+ if (!this.isBot)
142
+ this.logger.logEvent({ message: 'pause', iv: this.iv });
73
143
  this.paused = true;
74
144
  resolve(true);
75
145
  });
@@ -78,7 +148,8 @@ class Simulator extends Device_1.default {
78
148
  return new Promise((resolve, reject) => {
79
149
  if (!this.started)
80
150
  reject(new Error('illegal state - resume() has been called before start()'));
81
- this.logger.logEvent({ message: 'resume', iv: this.iv });
151
+ if (!this.isBot)
152
+ this.logger.logEvent({ message: 'resume', iv: this.iv });
82
153
  this.paused = false;
83
154
  resolve(true);
84
155
  });
@@ -114,29 +185,26 @@ class Simulator extends Device_1.default {
114
185
  }
115
186
  }
116
187
  update() {
117
- let prevTime = this.time;
118
- this.time = Date.now();
119
- let timespan = this.time - prevTime;
120
- if (this.limit.slope) {
121
- this.slope = this.limit.slope;
122
- }
123
- if (this.speed === undefined)
124
- this.speed = 30;
125
- this.power = calculations_1.default.calculatePower(75, this.speed / 3.6, this.slope);
126
- if (this.limit.targetPower) {
127
- this.power = this.limit.targetPower;
128
- this.speed = calculations_1.default.calculateSpeed(75, this.power, this.slope);
129
- }
130
- if (this.limit.maxPower && this.power > this.limit.maxPower) {
131
- this.power = this.limit.maxPower;
132
- this.speed = calculations_1.default.calculateSpeed(75, this.power, this.slope);
133
- }
134
- else if (this.limit.minPower && this.power < this.limit.minPower) {
135
- this.power = this.limit.minPower;
136
- this.speed = calculations_1.default.calculateSpeed(75, this.power, this.slope);
137
- }
138
- let distance = this.calculateDistance(this.speed, timespan / 1000);
139
- let data = { speed: this.speed, cadence: Math.round(this.cadence), power: Math.round(this.power), timespan, distance };
188
+ const startDelay = this.getCyclingMode().getSetting('delay');
189
+ const timeSinceStart = Date.now() - this.startTS;
190
+ if (startDelay && timeSinceStart < startDelay * 1000)
191
+ return;
192
+ const prevDist = this.data.distanceInternal;
193
+ this.data = this.getCyclingMode().updateData(this.data);
194
+ let data = {
195
+ speed: this.data.speed,
196
+ slope: this.data.slope,
197
+ power: this.data.power,
198
+ cadence: this.data.pedalRpm,
199
+ distance: this.data.distanceInternal - prevDist,
200
+ heartrate: Math.round(this.data.power - 10 + Math.random() * 20),
201
+ timestamp: Date.now(),
202
+ deviceTime: Math.round((Date.now() - this.startTS) / 1000),
203
+ deviceDistanceCounter: this.data.distanceInternal
204
+ };
205
+ this.paused = (this.data.speed === 0);
206
+ if (this.ignoreHrm)
207
+ delete data.heartrate;
140
208
  if (this.onDataFn) {
141
209
  this.onDataFn(data);
142
210
  }
@@ -145,15 +213,9 @@ class Simulator extends Device_1.default {
145
213
  return timeS * speedKps / 3.6;
146
214
  }
147
215
  sendUpdate(request) {
148
- this.logger.logEvent({ message: 'bike update request', request });
149
- const r = request || { refresh: true };
150
- if (r.refresh) {
151
- if (Object.keys(r).length === 1)
152
- return this.limit;
153
- delete r.refresh;
154
- }
155
- this.limit = r;
156
- return this.limit;
216
+ if (this.paused)
217
+ return;
218
+ return this.getCyclingMode().sendBikeUpdate(request);
157
219
  }
158
220
  }
159
221
  exports.Simulator = Simulator;
@@ -161,9 +223,12 @@ Simulator.NAME = 'Simulator';
161
223
  class SimulatorProtocol extends DeviceProtocol_1.default {
162
224
  constructor() {
163
225
  super();
164
- this.devices.push(new Simulator(this));
226
+ this.devices = [];
165
227
  }
166
228
  add(settings) {
229
+ let device = new Simulator(this, settings);
230
+ this.devices.push(device);
231
+ return device;
167
232
  }
168
233
  getName() {
169
234
  return SimulatorProtocol.NAME;
@@ -0,0 +1,28 @@
1
+ import { EventLogger } from "gd-eventlog";
2
+ import CyclingMode, { CyclingModeBase, CyclingModeProperty, IncyclistBikeData, UpdateRequest } from "../CyclingMode";
3
+ import { Simulator } from "./Simulator";
4
+ export declare type ERGEvent = {
5
+ rpmUpdated?: boolean;
6
+ gearUpdated?: boolean;
7
+ starting?: boolean;
8
+ tsStart?: number;
9
+ };
10
+ export default class SimulatorCyclingMode extends CyclingModeBase implements CyclingMode {
11
+ logger: EventLogger;
12
+ data: IncyclistBikeData;
13
+ prevRequest: UpdateRequest;
14
+ prevUpdateTS: number;
15
+ hasBikeUpdate: boolean;
16
+ chain: number[];
17
+ cassette: number[];
18
+ event: ERGEvent;
19
+ constructor(adapter: Simulator, props?: any);
20
+ getName(): string;
21
+ getDescription(): string;
22
+ getProperties(): CyclingModeProperty[];
23
+ getProperty(name: string): CyclingModeProperty;
24
+ getBikeInitRequest(): UpdateRequest;
25
+ sendBikeUpdate(request: UpdateRequest): UpdateRequest;
26
+ updateData(bikeData: IncyclistBikeData): IncyclistBikeData;
27
+ calculateTargetPower(request: any, updateMode?: boolean): void;
28
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const gd_eventlog_1 = require("gd-eventlog");
7
+ const CyclingMode_1 = require("../CyclingMode");
8
+ const calculations_1 = __importDefault(require("../calculations"));
9
+ const config = {
10
+ name: "Simulator",
11
+ description: "Simulates a ride with constant speed or power output",
12
+ properties: [
13
+ { key: 'mode', name: 'Simulation Type', description: '', type: CyclingMode_1.CyclingModeProperyType.SingleSelect, options: ['Speed', 'Power'], default: 'Power' },
14
+ { key: 'delay', name: 'Start Delay', description: 'Delay (in s) at start of training', type: CyclingMode_1.CyclingModeProperyType.Integer, default: 2, min: 0, max: 30 },
15
+ { key: 'power', name: 'Power', description: 'Power (in W) at start of training', condition: (s) => !s.simType || s.simType === 'Power', type: CyclingMode_1.CyclingModeProperyType.Integer, default: 150, min: 25, max: 800 },
16
+ { key: 'speed', name: 'Speed', description: 'Speed (in km/h) at start of training', condition: (s) => s.simType === 'Speed', type: CyclingMode_1.CyclingModeProperyType.Integer, default: 30, min: 5, max: 50 },
17
+ ]
18
+ };
19
+ class SimulatorCyclingMode extends CyclingMode_1.CyclingModeBase {
20
+ constructor(adapter, props) {
21
+ super(adapter, props);
22
+ this.prevUpdateTS = 0;
23
+ this.hasBikeUpdate = false;
24
+ this.event = {};
25
+ this.logger = adapter.logger || new gd_eventlog_1.EventLogger('SIMMode');
26
+ this.data = {};
27
+ this.logger.logEvent({ message: 'constructor', props });
28
+ }
29
+ getName() {
30
+ return config.name;
31
+ }
32
+ getDescription() {
33
+ return config.description;
34
+ }
35
+ getProperties() {
36
+ return config.properties;
37
+ }
38
+ getProperty(name) {
39
+ return config.properties.find(p => p.name === name);
40
+ }
41
+ getBikeInitRequest() {
42
+ return {};
43
+ }
44
+ sendBikeUpdate(request) {
45
+ this.logger.logEvent({ message: 'bike update request', request });
46
+ const r = request || { refresh: true };
47
+ if (r.refresh) {
48
+ if (Object.keys(r).length === 1)
49
+ return this.prevRequest || {};
50
+ delete r.refresh;
51
+ }
52
+ if (request.slope !== undefined) {
53
+ if (!this.data)
54
+ this.data = {};
55
+ this.data.slope = request.slope;
56
+ }
57
+ this.prevRequest = request ? JSON.parse(JSON.stringify(request)) : {};
58
+ return r;
59
+ }
60
+ updateData(bikeData) {
61
+ const prevData = JSON.parse(JSON.stringify(this.data || {}));
62
+ const prevSpeed = prevData.speed;
63
+ const prevRequest = this.prevRequest || {};
64
+ const data = this.data || {};
65
+ const mode = this.getSetting('mode');
66
+ delete this.event.gearUpdated;
67
+ delete this.event.rpmUpdated;
68
+ try {
69
+ let rpm = 90;
70
+ let power = (mode === 'Power' || !mode) ? this.getSetting('power') : bikeData.power || 0;
71
+ let slope = (prevData.slope !== undefined ? prevData.slope : prevRequest.slope || 0);
72
+ let speed = mode === 'Speed' ? this.getSetting('speed') : bikeData.speed || 0;
73
+ let m = 75;
74
+ let distanceInternal = prevData.distanceInternal || 0;
75
+ let ts = Date.now();
76
+ let duration = this.prevUpdateTS === 0 ? 0 : ((ts - this.prevUpdateTS) / 1000);
77
+ if (mode === 'Power' || !mode) {
78
+ speed = calculations_1.default.calculateSpeed(m, power, slope, { bikeType: 'race' });
79
+ }
80
+ else if (mode === 'Speed') {
81
+ power = calculations_1.default.calculatePower(m, speed / 3.6, slope, { bikeType: 'race' });
82
+ }
83
+ if (prevRequest.targetPower) {
84
+ power = prevRequest.targetPower;
85
+ speed = calculations_1.default.calculateSpeed(m, power, slope, { bikeType: 'race' });
86
+ }
87
+ if (prevRequest.maxPower && power > prevRequest.maxPower) {
88
+ power = prevRequest.maxPower;
89
+ speed = calculations_1.default.calculateSpeed(m, power, slope, { bikeType: 'race' });
90
+ }
91
+ else if (prevRequest.minPower && power < prevRequest.minPower) {
92
+ power = prevRequest.minPower;
93
+ speed = calculations_1.default.calculateSpeed(m, power, slope, { bikeType: 'race' });
94
+ }
95
+ let v = speed / 3.6;
96
+ distanceInternal += Math.round(v * duration);
97
+ data.speed = parseFloat(speed.toFixed(1));
98
+ data.power = Math.round(power);
99
+ data.distanceInternal = distanceInternal;
100
+ data.slope = slope;
101
+ data.pedalRpm = rpm;
102
+ if (data.time !== undefined)
103
+ data.time += duration;
104
+ else
105
+ data.time = 0;
106
+ data.heartrate = bikeData.heartrate;
107
+ data.isPedalling = true;
108
+ this.prevUpdateTS = ts;
109
+ }
110
+ catch (err) {
111
+ this.logger.logEvent({ message: 'error', fn: 'updateData()', error: err.message || err });
112
+ }
113
+ this.logger.logEvent({ message: "updateData result", data, bikeData, prevRequest, prevSpeed });
114
+ this.data = data;
115
+ return data;
116
+ }
117
+ calculateTargetPower(request, updateMode = true) {
118
+ }
119
+ }
120
+ exports.default = SimulatorCyclingMode;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RouteType = void 0;
3
4
  var RouteType;
4
5
  (function (RouteType) {
5
6
  RouteType["FREE_RIDE"] = "free ride";
package/lib/types/user.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Gender = void 0;
3
4
  var Gender;
4
5
  (function (Gender) {
5
6
  Gender["MALE"] = "M";
package/lib/utils.js CHANGED
@@ -9,24 +9,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.sleep = (ms) => {
12
+ exports.Queue = exports.hexstr = exports.intVal = exports.floatVal = exports.runWithRetries = exports.sleep = void 0;
13
+ const sleep = (ms) => {
13
14
  return new Promise(resolve => setTimeout(resolve, ms));
14
15
  };
16
+ exports.sleep = sleep;
15
17
  function runWithRetries(fn, maxRetries, timeBetween) {
16
18
  return new Promise((resolve, reject) => {
17
19
  let retries = 0;
18
20
  let tLastFailure = undefined;
19
21
  let busy = false;
20
- const iv = setInterval(() => __awaiter(this, void 0, void 0, function* () {
22
+ let iv = setInterval(() => __awaiter(this, void 0, void 0, function* () {
21
23
  const tNow = Date.now();
22
- if (busy)
24
+ if (busy) {
23
25
  return;
26
+ }
24
27
  if (tLastFailure === undefined || tNow - tLastFailure > timeBetween) {
25
28
  try {
26
29
  busy = true;
27
30
  const data = yield fn();
28
- busy = false;
29
31
  clearInterval(iv);
32
+ iv = undefined;
33
+ busy = false;
30
34
  return resolve(data);
31
35
  }
32
36
  catch (err) {
@@ -34,6 +38,7 @@ function runWithRetries(fn, maxRetries, timeBetween) {
34
38
  retries++;
35
39
  if (retries >= maxRetries) {
36
40
  clearInterval(iv);
41
+ iv = undefined;
37
42
  busy = false;
38
43
  return reject(err);
39
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "1.4.19",
3
+ "version": "1.4.22",
4
4
  "dependencies": {
5
5
  "@serialport/parser-byte-length": "^9.0.1",
6
6
  "@serialport/parser-delimiter": "^9.0.1",