incyclist-devices 2.1.31 → 2.1.33
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/lib/antv2/pwr/adapter.js +1 -0
- package/lib/ble/cp/adapter.js +5 -10
- package/lib/serial/daum/DaumAdapter.d.ts +1 -0
- package/lib/serial/daum/DaumAdapter.js +8 -1
- package/lib/serial/daum/classic/adapter.js +19 -4
- package/lib/serial/daum/premium/adapter.d.ts +1 -1
- package/lib/serial/daum/premium/adapter.js +24 -8
- package/package.json +1 -1
- package/lib/adapters.d.ts +0 -7
- package/lib/adapters.js +0 -49
- package/lib/antv2/adapter-factory.d.ts +0 -14
- package/lib/antv2/adapter-factory.js +0 -65
- package/lib/antv2/adapter.d.ts +0 -54
- package/lib/antv2/adapter.js +0 -291
- package/lib/antv2/ant-interface.d.ts +0 -35
- package/lib/antv2/ant-interface.js +0 -285
- package/lib/antv2/base/ant-interface.d.ts +0 -35
- package/lib/antv2/base/ant-interface.js +0 -285
- package/lib/antv2/binding.d.ts +0 -13
- package/lib/antv2/binding.js +0 -27
- package/lib/antv2/hr copy/adapter.d.ts +0 -11
- package/lib/antv2/hr copy/adapter.js +0 -30
- package/lib/antv2/hr copy/index.d.ts +0 -2
- package/lib/antv2/hr copy/index.js +0 -7
- package/lib/antv2/sensor-factory.d.ts +0 -5
- package/lib/antv2/sensor-factory.js +0 -20
- package/lib/interfaces.d.ts +0 -7
- package/lib/interfaces.js +0 -27
- package/lib/serial/SinglePathScanner.d.ts +0 -17
- package/lib/serial/SinglePathScanner.js +0 -87
- package/lib/serial/adapter-factory.d.ts +0 -14
- package/lib/serial/adapter-factory.js +0 -30
- package/lib/serial/adapter.d.ts +0 -17
- package/lib/serial/adapter.js +0 -67
- package/lib/serial/comm.d.ts +0 -7
- package/lib/serial/comm.js +0 -2
- package/lib/serial/comms.d.ts +0 -62
- package/lib/serial/comms.js +0 -280
- package/lib/serial/daum/classic/PROTOCOL_NAME.d.ts +0 -2
- package/lib/serial/daum/classic/PROTOCOL_NAME.js +0 -5
- package/lib/serial/daum/consts.d.ts +0 -0
- package/lib/serial/daum/consts.js +0 -0
- package/lib/serial/serial-interface.d.ts +0 -36
- package/lib/serial/serial-interface.js +0 -288
- package/lib/serial/serial-scanner.d.ts +0 -16
- package/lib/serial/serial-scanner.js +0 -87
- package/lib/serial/serialport.d.ts +0 -17
- package/lib/serial/serialport.js +0 -87
- package/lib/types/Command.d.ts +0 -8
- package/lib/types/Command.js +0 -2
- package/lib/types/command.d.ts +0 -0
- package/lib/types/command.js +0 -0
- package/lib/types/route.d.ts +0 -0
- package/lib/types/route.js +0 -0
- package/lib/types/types.d.ts +0 -8
- package/lib/types/types.js +0 -2
package/lib/antv2/adapter.js
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
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
|
-
const ant_interface_1 = __importDefault(require("./ant-interface"));
|
|
16
|
-
const adpater_1 = __importDefault(require("../base/adpater"));
|
|
17
|
-
const types_1 = require("./types");
|
|
18
|
-
const types_2 = require("../types");
|
|
19
|
-
const utils_1 = require("../utils/utils");
|
|
20
|
-
const utils_2 = require("./utils");
|
|
21
|
-
const consts_1 = require("./consts");
|
|
22
|
-
const INTERFACE_NAME = 'ant';
|
|
23
|
-
class AntAdapter extends adpater_1.default {
|
|
24
|
-
constructor(settings, props) {
|
|
25
|
-
super(settings, props);
|
|
26
|
-
this.startupRetryPause = 1000;
|
|
27
|
-
const antSettings = this.settings;
|
|
28
|
-
if ((0, types_1.isLegacyProfile)(antSettings.profile))
|
|
29
|
-
antSettings.profile = (0, utils_2.mapLegacyProfile)(antSettings.profile);
|
|
30
|
-
if (this.settings.interface !== 'ant')
|
|
31
|
-
throw new Error('Incorrect interface');
|
|
32
|
-
this.sensor = this.createSensor(settings);
|
|
33
|
-
this.deviceData = {};
|
|
34
|
-
this.data = {};
|
|
35
|
-
this.dataMsgCount = 0;
|
|
36
|
-
this.updateFrequency = consts_1.DEFAULT_UPDATE_FREQUENCY;
|
|
37
|
-
this.channel = undefined;
|
|
38
|
-
this.ant = ant_interface_1.default.getInstance();
|
|
39
|
-
}
|
|
40
|
-
createSensor(settings) {
|
|
41
|
-
throw new Error('Method not implemented.');
|
|
42
|
-
}
|
|
43
|
-
isEqual(settings) {
|
|
44
|
-
const as = this.settings;
|
|
45
|
-
if (as.interface !== settings.interface)
|
|
46
|
-
return false;
|
|
47
|
-
if (Number(as.deviceID) !== Number(settings.deviceID) || as.profile !== settings.profile)
|
|
48
|
-
return false;
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
connect() {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
const connected = yield ant_interface_1.default.getInstance().connect();
|
|
54
|
-
return connected;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
close() {
|
|
58
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
return true;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
resetData() {
|
|
63
|
-
this.dataMsgCount = 0;
|
|
64
|
-
const { DeviceID } = this.deviceData;
|
|
65
|
-
this.deviceData = { DeviceID };
|
|
66
|
-
this.lastDataTS = undefined;
|
|
67
|
-
}
|
|
68
|
-
isSame(device) {
|
|
69
|
-
if (!(device instanceof AntAdapter))
|
|
70
|
-
return false;
|
|
71
|
-
const adapter = device;
|
|
72
|
-
return (adapter.getID() === this.getID() && adapter.getProfile() === this.getProfile());
|
|
73
|
-
}
|
|
74
|
-
hasData() {
|
|
75
|
-
return this.dataMsgCount > 0;
|
|
76
|
-
}
|
|
77
|
-
mapData(deviceData) {
|
|
78
|
-
throw new Error('message not implemented');
|
|
79
|
-
}
|
|
80
|
-
transformData(data) {
|
|
81
|
-
throw new Error('message not implemented');
|
|
82
|
-
}
|
|
83
|
-
mapToAdapterData(deviceData) {
|
|
84
|
-
throw new Error('message not implemented');
|
|
85
|
-
}
|
|
86
|
-
onDeviceData(deviceData) {
|
|
87
|
-
this.dataMsgCount++;
|
|
88
|
-
this.lastDataTS = Date.now();
|
|
89
|
-
try {
|
|
90
|
-
const { ManId } = this.deviceData;
|
|
91
|
-
this.deviceData = Object.assign({}, deviceData);
|
|
92
|
-
if (!ManId && deviceData.ManId) {
|
|
93
|
-
this.emit('device-info', { device: this.getSettings(), manufacturer: (0, utils_2.getBrand)(deviceData.ManId) });
|
|
94
|
-
}
|
|
95
|
-
if (!this.started || this.isStopped())
|
|
96
|
-
return;
|
|
97
|
-
this.triggerTimeoutCheck();
|
|
98
|
-
if (!this.canEmitData())
|
|
99
|
-
return;
|
|
100
|
-
const logData = this.getLogData(deviceData, ['PairedDevices', 'RawData']);
|
|
101
|
-
this.logEvent({ message: 'onDeviceData', data: logData, paused: this.paused });
|
|
102
|
-
if (this.isControllable()) {
|
|
103
|
-
let incyclistData = this.mapData(deviceData);
|
|
104
|
-
incyclistData = this.getCyclingMode().updateData(incyclistData);
|
|
105
|
-
this.transformData(incyclistData);
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
this.mapToAdapterData(deviceData);
|
|
109
|
-
}
|
|
110
|
-
this.emitData(this.data);
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
console.log('~~~ERROR', err);
|
|
114
|
-
this.logEvent({ message: 'error', fn: 'onDeviceData()', error: err.message || err, stack: err.stack });
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
isWaitingForData() {
|
|
118
|
-
return this.ivWaitForData !== undefined && this.ivWaitForData !== null;
|
|
119
|
-
}
|
|
120
|
-
waitForData(timeout) {
|
|
121
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
-
const startTs = Date.now();
|
|
123
|
-
const timeoutTs = startTs + timeout;
|
|
124
|
-
if (this.hasData())
|
|
125
|
-
return true;
|
|
126
|
-
return new Promise((resolve, reject) => {
|
|
127
|
-
if (this.ivWaitForData)
|
|
128
|
-
return reject(new Error('busy'));
|
|
129
|
-
this.ivWaitForData = setInterval(() => {
|
|
130
|
-
const nowTs = Date.now();
|
|
131
|
-
const hasData = this.hasData();
|
|
132
|
-
if (nowTs > timeoutTs && !hasData) {
|
|
133
|
-
clearInterval(this.ivWaitForData);
|
|
134
|
-
this.ivWaitForData = undefined;
|
|
135
|
-
reject(new Error('No Data Received'));
|
|
136
|
-
}
|
|
137
|
-
if (hasData) {
|
|
138
|
-
clearInterval(this.ivWaitForData);
|
|
139
|
-
this.ivWaitForData = undefined;
|
|
140
|
-
resolve(true);
|
|
141
|
-
}
|
|
142
|
-
}, 500);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
getID() {
|
|
147
|
-
return this.sensor.getDeviceID().toString();
|
|
148
|
-
}
|
|
149
|
-
getName() {
|
|
150
|
-
const deviceID = this.sensor.getDeviceID();
|
|
151
|
-
const profile = this.sensor.getProfile();
|
|
152
|
-
return `Ant+${profile} ${deviceID}`;
|
|
153
|
-
}
|
|
154
|
-
getInterface() {
|
|
155
|
-
return INTERFACE_NAME;
|
|
156
|
-
}
|
|
157
|
-
getProfile() {
|
|
158
|
-
const settings = this.settings;
|
|
159
|
-
if (settings.protocol === undefined)
|
|
160
|
-
return settings.profile;
|
|
161
|
-
else {
|
|
162
|
-
return (0, utils_2.mapLegacyProfile)(settings.profile);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
getLogData(data, excludeList) {
|
|
166
|
-
const logData = JSON.parse(JSON.stringify(data));
|
|
167
|
-
excludeList.forEach((key) => {
|
|
168
|
-
delete logData[key];
|
|
169
|
-
});
|
|
170
|
-
return logData;
|
|
171
|
-
}
|
|
172
|
-
triggerTimeoutCheck() {
|
|
173
|
-
if (!this.ivDataTimeout && this.dataMsgCount > 0) {
|
|
174
|
-
this.startDataTimeoutCheck();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
startDataTimeoutCheck() {
|
|
178
|
-
if (this.ivDataTimeout)
|
|
179
|
-
return;
|
|
180
|
-
this.ivDataTimeout = setInterval(() => {
|
|
181
|
-
if (!this.lastDataTS)
|
|
182
|
-
return;
|
|
183
|
-
if (this.lastDataTS + consts_1.NO_DATA_TIMEOUT < Date.now()) {
|
|
184
|
-
this.emit('disconnected', Date.now() - this.lastDataTS);
|
|
185
|
-
}
|
|
186
|
-
}, 1000);
|
|
187
|
-
}
|
|
188
|
-
stopDataTimeoutCheck() {
|
|
189
|
-
if (!this.ivDataTimeout)
|
|
190
|
-
return;
|
|
191
|
-
clearInterval(this.ivDataTimeout);
|
|
192
|
-
this.ivDataTimeout = undefined;
|
|
193
|
-
this.lastDataTS = undefined;
|
|
194
|
-
this.dataMsgCount = 0;
|
|
195
|
-
}
|
|
196
|
-
check() {
|
|
197
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
198
|
-
try {
|
|
199
|
-
return yield this.start();
|
|
200
|
-
}
|
|
201
|
-
catch (error) {
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
checkCapabilities() {
|
|
207
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
return;
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
initControl() {
|
|
212
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
return;
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
sendUpdate(request) {
|
|
217
|
-
if (!this.isControllable())
|
|
218
|
-
return;
|
|
219
|
-
if (this.isPaused() || this.isStopped())
|
|
220
|
-
return;
|
|
221
|
-
if (!this.hasCapability(types_2.IncyclistCapability.Control))
|
|
222
|
-
this.getCyclingMode().sendBikeUpdate(request);
|
|
223
|
-
else
|
|
224
|
-
throw new Error('method not implemented');
|
|
225
|
-
}
|
|
226
|
-
start(props = {}) {
|
|
227
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
228
|
-
if (this.started && !this.stopped) {
|
|
229
|
-
if (this.paused)
|
|
230
|
-
this.resume();
|
|
231
|
-
return true;
|
|
232
|
-
}
|
|
233
|
-
this.stopped = false;
|
|
234
|
-
const connected = yield this.connect();
|
|
235
|
-
if (!connected)
|
|
236
|
-
throw new Error(`could not start device, reason:could not connect`);
|
|
237
|
-
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
238
|
-
this.resetData();
|
|
239
|
-
this.stopped = false;
|
|
240
|
-
this.resume();
|
|
241
|
-
const { startupTimeout = 20000 } = props;
|
|
242
|
-
let to = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
243
|
-
try {
|
|
244
|
-
yield this.stop();
|
|
245
|
-
}
|
|
246
|
-
catch (_a) { }
|
|
247
|
-
this.started = false;
|
|
248
|
-
reject(new Error(`could not start device, reason:timeout`));
|
|
249
|
-
}), startupTimeout);
|
|
250
|
-
let started = false;
|
|
251
|
-
do {
|
|
252
|
-
started = yield this.ant.startSensor(this.sensor, (data) => {
|
|
253
|
-
this.onDeviceData(data);
|
|
254
|
-
});
|
|
255
|
-
if (!started)
|
|
256
|
-
yield (0, utils_1.sleep)(this.startupRetryPause);
|
|
257
|
-
} while (!started);
|
|
258
|
-
try {
|
|
259
|
-
yield this.waitForData(startupTimeout - 100);
|
|
260
|
-
yield this.checkCapabilities();
|
|
261
|
-
if (this.hasCapability(types_2.IncyclistCapability.Control))
|
|
262
|
-
yield this.initControl();
|
|
263
|
-
this.started = true;
|
|
264
|
-
if (to)
|
|
265
|
-
clearTimeout(to);
|
|
266
|
-
resolve(true);
|
|
267
|
-
}
|
|
268
|
-
catch (err) {
|
|
269
|
-
}
|
|
270
|
-
}));
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
stop() {
|
|
274
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
275
|
-
let stopped;
|
|
276
|
-
try {
|
|
277
|
-
this.stopDataTimeoutCheck();
|
|
278
|
-
stopped = yield this.ant.stopSensor(this.sensor);
|
|
279
|
-
}
|
|
280
|
-
catch (err) {
|
|
281
|
-
this.logEvent({ message: 'stop sensor failed', reason: err.message });
|
|
282
|
-
}
|
|
283
|
-
this.started = false;
|
|
284
|
-
this.stopped = true;
|
|
285
|
-
this.paused = false;
|
|
286
|
-
this.removeAllListeners();
|
|
287
|
-
return stopped;
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
exports.default = AntAdapter;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import EventEmitter from "events";
|
|
3
|
-
import { EventLogger } from "gd-eventlog";
|
|
4
|
-
import { IAntDevice, IChannel, ISensor } from "incyclist-ant-plus";
|
|
5
|
-
import { AntDeviceSettings, AntScanProps, AntInterfaceProps, ConnectState } from "./types";
|
|
6
|
-
import { IncyclistInterface } from "../types";
|
|
7
|
-
import AntDeviceBinding from "./base/binding";
|
|
8
|
-
export default class AntInterface extends EventEmitter implements IncyclistInterface {
|
|
9
|
-
static _instance: AntInterface;
|
|
10
|
-
static INTERFACE_NAME: string;
|
|
11
|
-
static getInstance(props?: AntInterfaceProps): AntInterface;
|
|
12
|
-
static hasInstance(): boolean;
|
|
13
|
-
protected logger: EventLogger;
|
|
14
|
-
protected device: IAntDevice;
|
|
15
|
-
protected Binding: typeof AntDeviceBinding;
|
|
16
|
-
protected connectState: ConnectState;
|
|
17
|
-
protected props: AntInterfaceProps;
|
|
18
|
-
protected activeScan: IChannel;
|
|
19
|
-
constructor(props: AntInterfaceProps);
|
|
20
|
-
getName(): string;
|
|
21
|
-
getBinding(): typeof AntDeviceBinding;
|
|
22
|
-
setBinding(binding: typeof AntDeviceBinding): void;
|
|
23
|
-
setLogger(logger: EventLogger): void;
|
|
24
|
-
logEvent(event: any): void;
|
|
25
|
-
isConnected(): boolean;
|
|
26
|
-
connect(): Promise<boolean>;
|
|
27
|
-
disconnect(): Promise<boolean>;
|
|
28
|
-
onError(profile: any, error: any): void;
|
|
29
|
-
onData(profile: any, id: any, data: any, tag: any): void;
|
|
30
|
-
scan(props?: AntScanProps): Promise<AntDeviceSettings[]>;
|
|
31
|
-
stopScan(): Promise<boolean>;
|
|
32
|
-
startSensor(sensor: ISensor, onDeviceData: (data: any) => void): Promise<boolean>;
|
|
33
|
-
stopSensor(sensor: ISensor): Promise<boolean>;
|
|
34
|
-
isScanning(): boolean;
|
|
35
|
-
}
|
|
@@ -1,285 +0,0 @@
|
|
|
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
|
-
const events_1 = __importDefault(require("events"));
|
|
16
|
-
const gd_eventlog_1 = require("gd-eventlog");
|
|
17
|
-
const sensor_factory_1 = __importDefault(require("./factories/sensor-factory"));
|
|
18
|
-
class AntInterface extends events_1.default {
|
|
19
|
-
static getInstance(props = {}) {
|
|
20
|
-
if (AntInterface._instance === undefined)
|
|
21
|
-
AntInterface._instance = new AntInterface(props);
|
|
22
|
-
return AntInterface._instance;
|
|
23
|
-
}
|
|
24
|
-
static hasInstance() {
|
|
25
|
-
return AntInterface._instance !== undefined;
|
|
26
|
-
}
|
|
27
|
-
constructor(props) {
|
|
28
|
-
super();
|
|
29
|
-
this.props = props;
|
|
30
|
-
this.device = undefined;
|
|
31
|
-
this.connectState = {
|
|
32
|
-
connected: false,
|
|
33
|
-
connecting: false
|
|
34
|
-
};
|
|
35
|
-
const { binding, logger } = props;
|
|
36
|
-
if (logger)
|
|
37
|
-
this.logger = logger;
|
|
38
|
-
else {
|
|
39
|
-
this.logger = new gd_eventlog_1.EventLogger('Ant+');
|
|
40
|
-
}
|
|
41
|
-
if (binding) {
|
|
42
|
-
this.Binding = binding;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
getName() {
|
|
46
|
-
return AntInterface.INTERFACE_NAME;
|
|
47
|
-
}
|
|
48
|
-
getBinding() {
|
|
49
|
-
return this.Binding;
|
|
50
|
-
}
|
|
51
|
-
setBinding(binding) {
|
|
52
|
-
this.Binding = binding;
|
|
53
|
-
}
|
|
54
|
-
setLogger(logger) {
|
|
55
|
-
this.logger = logger;
|
|
56
|
-
}
|
|
57
|
-
logEvent(event) {
|
|
58
|
-
if (this.logger)
|
|
59
|
-
this.logger.logEvent(event);
|
|
60
|
-
const w = global.window;
|
|
61
|
-
if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.ANT_DEBUG) {
|
|
62
|
-
console.log('~~~ ANT', event);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
isConnected() {
|
|
66
|
-
return this.connectState.connected;
|
|
67
|
-
}
|
|
68
|
-
connect() {
|
|
69
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
if (this.isConnected())
|
|
71
|
-
return true;
|
|
72
|
-
if (!this.connectState.connecting) {
|
|
73
|
-
this.connectState.connecting = true;
|
|
74
|
-
this.logEvent({ message: 'ANT+ connecting ...' });
|
|
75
|
-
try {
|
|
76
|
-
const device = new this.Binding(Object.assign(Object.assign({}, this.props), { logger: this.logger }));
|
|
77
|
-
const opened = yield device.open();
|
|
78
|
-
if (!opened) {
|
|
79
|
-
this.logEvent({ message: 'ANT+ not connected' });
|
|
80
|
-
this.connectState.connecting = false;
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
this.device = device;
|
|
84
|
-
this.connectState.connected = true;
|
|
85
|
-
this.connectState.connecting = false;
|
|
86
|
-
this.logEvent({ message: 'ANT+ connected' });
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
catch (err) {
|
|
90
|
-
this.logEvent({ message: 'error', fn: 'connect', error: err.message, stack: err.stack });
|
|
91
|
-
this.connectState.connected = false;
|
|
92
|
-
this.connectState.connecting = false;
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
return new Promise(resolve => {
|
|
98
|
-
setInterval(() => {
|
|
99
|
-
if (!this.connectState.connecting)
|
|
100
|
-
resolve(this.connectState.connected);
|
|
101
|
-
}, 500);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
disconnect() {
|
|
107
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
-
if (!this.device)
|
|
109
|
-
return true;
|
|
110
|
-
this.logEvent({ message: 'ANT+ disconnecting ...' });
|
|
111
|
-
const closed = yield this.device.close();
|
|
112
|
-
this.connectState.connected = !closed;
|
|
113
|
-
this.logEvent({ message: 'ANT+ disconnected' });
|
|
114
|
-
return closed;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
onError(profile, error) {
|
|
118
|
-
this.logEvent({ message: 'ANT+ERROR:', profile, error });
|
|
119
|
-
}
|
|
120
|
-
onData(profile, id, data, tag) {
|
|
121
|
-
this.emit('data', profile, id, data, tag);
|
|
122
|
-
}
|
|
123
|
-
scan(props = {}) {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
this.logEvent({ message: 'starting scan ..' });
|
|
126
|
-
const detected = [];
|
|
127
|
-
const onDetected = (profile, deviceID) => {
|
|
128
|
-
if (deviceID && detected.find(s => s.deviceID === deviceID && s.profile === profile) === undefined) {
|
|
129
|
-
try {
|
|
130
|
-
detected.push({ interface: this.getName(), profile, deviceID });
|
|
131
|
-
this.emit('device', { interface: 'ant', profile, deviceID });
|
|
132
|
-
}
|
|
133
|
-
catch (err) {
|
|
134
|
-
this.logEvent({ message: 'error', fn: 'onDerected', error: err.message, stack: err.stack });
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
let channel;
|
|
139
|
-
if (!this.activeScan) {
|
|
140
|
-
while (!this.isConnected()) {
|
|
141
|
-
const connected = yield this.connect();
|
|
142
|
-
if (!connected)
|
|
143
|
-
return [];
|
|
144
|
-
}
|
|
145
|
-
channel = this.device.getChannel();
|
|
146
|
-
channel.setProps({ logger: this.logger });
|
|
147
|
-
if (!channel)
|
|
148
|
-
return [];
|
|
149
|
-
const sensors = sensor_factory_1.default.createAll();
|
|
150
|
-
sensors.forEach((sensor) => {
|
|
151
|
-
channel.attach(sensor);
|
|
152
|
-
});
|
|
153
|
-
channel.on('detected', onDetected);
|
|
154
|
-
channel.on('data', this.onData.bind(this));
|
|
155
|
-
try {
|
|
156
|
-
const success = yield channel.startScanner();
|
|
157
|
-
this.logEvent({ message: 'scan started', success });
|
|
158
|
-
}
|
|
159
|
-
catch (err) {
|
|
160
|
-
this.logEvent({ message: 'scan could not be started', error: err.message, stack: err.stack });
|
|
161
|
-
channel.off('detected', onDetected);
|
|
162
|
-
channel.off('data', this.onData.bind(this));
|
|
163
|
-
return [];
|
|
164
|
-
}
|
|
165
|
-
let iv;
|
|
166
|
-
return new Promise(resolve => {
|
|
167
|
-
this.activeScan = channel;
|
|
168
|
-
const start = Date.now();
|
|
169
|
-
const timeout = props.timeout ? start + props.timeout : undefined;
|
|
170
|
-
iv = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
171
|
-
if (this.activeScan && (!timeout || Date.now() < timeout))
|
|
172
|
-
return;
|
|
173
|
-
clearInterval(iv);
|
|
174
|
-
if (this.activeScan)
|
|
175
|
-
yield this.stopScan();
|
|
176
|
-
this.emit('stop-scan');
|
|
177
|
-
channel.off('detected', onDetected);
|
|
178
|
-
channel.off('data', this.onData.bind(this));
|
|
179
|
-
resolve(detected);
|
|
180
|
-
}), 100);
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
return new Promise(resolve => {
|
|
185
|
-
channel = this.activeScan;
|
|
186
|
-
channel.on('data', this.onData.bind(this));
|
|
187
|
-
channel.on('detected', onDetected);
|
|
188
|
-
this.once('stop-scan', () => {
|
|
189
|
-
channel.off('detected', onDetected);
|
|
190
|
-
channel.off('data', this.onData.bind(this));
|
|
191
|
-
resolve(detected);
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
stopScan() {
|
|
198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
this.logEvent({ message: 'stopping scan ..' });
|
|
200
|
-
if (!this.activeScan)
|
|
201
|
-
return false;
|
|
202
|
-
const channel = this.activeScan;
|
|
203
|
-
try {
|
|
204
|
-
const stopped = yield channel.stopScanner();
|
|
205
|
-
this.activeScan = undefined;
|
|
206
|
-
this.logEvent({ message: 'scan stopped' });
|
|
207
|
-
return stopped;
|
|
208
|
-
}
|
|
209
|
-
catch (err) {
|
|
210
|
-
this.logEvent({ message: 'error', fn: 'stopScan()', error: err.message, stack: err.stack });
|
|
211
|
-
return false;
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
startSensor(sensor, onDeviceData) {
|
|
216
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
-
if (!this.isConnected()) {
|
|
218
|
-
const connected = yield this.connect();
|
|
219
|
-
if (!connected)
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
let channel;
|
|
223
|
-
channel = this.device.getChannel();
|
|
224
|
-
channel.setProps({ logger: this.logger });
|
|
225
|
-
if (!channel)
|
|
226
|
-
return false;
|
|
227
|
-
channel.on('data', this.onData.bind(this));
|
|
228
|
-
sensor.setChannel(channel);
|
|
229
|
-
this.on('data', (profile, deviceID, data) => {
|
|
230
|
-
if (profile === sensor.getProfile() && deviceID === sensor.getDeviceID())
|
|
231
|
-
onDeviceData(data);
|
|
232
|
-
});
|
|
233
|
-
try {
|
|
234
|
-
return yield channel.startSensor(sensor);
|
|
235
|
-
}
|
|
236
|
-
catch (err) {
|
|
237
|
-
this.logEvent({ message: 'could not start sensor', error: err.message || err, stack: err.stack });
|
|
238
|
-
try {
|
|
239
|
-
yield channel.stopSensor(sensor);
|
|
240
|
-
}
|
|
241
|
-
catch (_a) { }
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
stopSensor(sensor) {
|
|
247
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
248
|
-
if (!this.isConnected() || !this.device) {
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
const channel = sensor.getChannel();
|
|
252
|
-
if (channel) {
|
|
253
|
-
try {
|
|
254
|
-
if (!channel.flush) {
|
|
255
|
-
this.logEvent({ message: 'old version of ant-channel detected' });
|
|
256
|
-
channel.flush = () => {
|
|
257
|
-
const c = channel;
|
|
258
|
-
c.messageQueue.forEach(msg => { msg.resolve(false); });
|
|
259
|
-
c.messageQueue = [];
|
|
260
|
-
c.isWriting = false;
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
channel.flush();
|
|
264
|
-
channel.removeAllListeners('data');
|
|
265
|
-
const stopped = yield channel.stopSensor(sensor);
|
|
266
|
-
return stopped;
|
|
267
|
-
}
|
|
268
|
-
catch (err) {
|
|
269
|
-
this.logEvent({ message: 'could not stop sensor', error: err.message || err, deviceID: sensor.getDeviceID(), stack: err.stack });
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
this.logEvent({ message: 'could not stop sensor', deviceID: sensor.getDeviceID(), error: 'no channel attached' });
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
isScanning() {
|
|
280
|
-
return (this.activeScan === undefined || this.activeScan === null);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
AntInterface._instance = undefined;
|
|
284
|
-
AntInterface.INTERFACE_NAME = 'ant';
|
|
285
|
-
exports.default = AntInterface;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import EventEmitter from "events";
|
|
3
|
-
import { EventLogger } from "gd-eventlog";
|
|
4
|
-
import { IAntDevice, IChannel, ISensor } from "incyclist-ant-plus";
|
|
5
|
-
import { AntDeviceSettings, AntScanProps, AntInterfaceProps, ConnectState } from "../types";
|
|
6
|
-
import { IncyclistInterface } from "../../types";
|
|
7
|
-
import AntDeviceBinding from "./binding";
|
|
8
|
-
export default class AntInterface extends EventEmitter implements IncyclistInterface {
|
|
9
|
-
static _instance: AntInterface;
|
|
10
|
-
static INTERFACE_NAME: string;
|
|
11
|
-
static getInstance(props?: AntInterfaceProps): AntInterface;
|
|
12
|
-
static hasInstance(): boolean;
|
|
13
|
-
protected logger: EventLogger;
|
|
14
|
-
protected device: IAntDevice;
|
|
15
|
-
protected Binding: typeof AntDeviceBinding;
|
|
16
|
-
protected connectState: ConnectState;
|
|
17
|
-
protected props: AntInterfaceProps;
|
|
18
|
-
protected activeScan: IChannel;
|
|
19
|
-
constructor(props: AntInterfaceProps);
|
|
20
|
-
getName(): string;
|
|
21
|
-
getBinding(): typeof AntDeviceBinding;
|
|
22
|
-
setBinding(binding: typeof AntDeviceBinding): void;
|
|
23
|
-
setLogger(logger: EventLogger): void;
|
|
24
|
-
logEvent(event: any): void;
|
|
25
|
-
isConnected(): boolean;
|
|
26
|
-
connect(): Promise<boolean>;
|
|
27
|
-
disconnect(): Promise<boolean>;
|
|
28
|
-
onError(profile: any, error: any): void;
|
|
29
|
-
onData(profile: any, id: any, data: any, tag: any): void;
|
|
30
|
-
scan(props?: AntScanProps): Promise<AntDeviceSettings[]>;
|
|
31
|
-
stopScan(): Promise<boolean>;
|
|
32
|
-
startSensor(sensor: ISensor, onDeviceData: (data: any) => void): Promise<boolean>;
|
|
33
|
-
stopSensor(sensor: ISensor): Promise<boolean>;
|
|
34
|
-
isScanning(): boolean;
|
|
35
|
-
}
|