incyclist-devices 1.5.37 → 2.0.0-beta.1

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 (251) hide show
  1. package/README.MD +0 -0
  2. package/lib/adapters.d.ts +6 -0
  3. package/lib/adapters.js +30 -0
  4. package/lib/antv2/adapter-factory.d.ts +20 -10
  5. package/lib/antv2/adapter-factory.js +49 -33
  6. package/lib/antv2/adapter.d.ts +64 -0
  7. package/lib/antv2/adapter.js +299 -0
  8. package/lib/antv2/ant-interface.d.ts +11 -9
  9. package/lib/antv2/ant-interface.js +15 -11
  10. package/lib/antv2/fe/adapter.d.ts +25 -0
  11. package/lib/antv2/{fe.js → fe/adapter.js} +48 -72
  12. package/lib/antv2/fe/index.d.ts +2 -0
  13. package/lib/antv2/fe/index.js +7 -0
  14. package/lib/antv2/hr/adapter.d.ts +14 -0
  15. package/lib/antv2/hr/adapter.js +73 -0
  16. package/lib/antv2/hr/index.d.ts +2 -0
  17. package/lib/antv2/hr/index.js +7 -0
  18. package/lib/antv2/index.d.ts +7 -0
  19. package/lib/antv2/index.js +20 -0
  20. package/lib/antv2/modes/ant-fe-adv-st-mode.d.ts +9 -0
  21. package/lib/{ant/antfe → antv2/modes}/ant-fe-adv-st-mode.js +1 -1
  22. package/lib/antv2/modes/ant-fe-erg-mode.d.ts +6 -0
  23. package/lib/{ant/antfe → antv2/modes}/ant-fe-erg-mode.js +1 -1
  24. package/lib/antv2/modes/ant-fe-st-mode.d.ts +5 -0
  25. package/lib/{ant/antfe → antv2/modes}/ant-fe-st-mode.js +1 -1
  26. package/lib/antv2/pwr/adapter.d.ts +22 -0
  27. package/lib/antv2/{pwr.js → pwr/adapter.js} +38 -76
  28. package/lib/antv2/pwr/index.d.ts +2 -0
  29. package/lib/antv2/pwr/index.js +7 -0
  30. package/lib/antv2/types.d.ts +19 -0
  31. package/lib/antv2/types.js +2 -0
  32. package/lib/antv2/utils.d.ts +1 -0
  33. package/lib/antv2/utils.js +181 -0
  34. package/lib/base/adpater.d.ts +56 -0
  35. package/lib/base/adpater.js +144 -0
  36. package/lib/ble/adapter-factory.d.ts +31 -0
  37. package/lib/ble/adapter-factory.js +96 -0
  38. package/lib/ble/adapter.d.ts +41 -0
  39. package/lib/ble/adapter.js +188 -0
  40. package/lib/ble/{ble-device.d.ts → ble-comms.d.ts} +9 -4
  41. package/lib/ble/{ble-device.js → ble-comms.js} +23 -11
  42. package/lib/ble/ble-interface.d.ts +15 -24
  43. package/lib/ble/ble-interface.js +32 -137
  44. package/lib/ble/ble-peripheral.d.ts +3 -3
  45. package/lib/ble/ble-peripheral.js +14 -14
  46. package/lib/ble/ble.d.ts +9 -81
  47. package/lib/ble/ble.js +1 -39
  48. package/lib/ble/consts.d.ts +2 -0
  49. package/lib/ble/consts.js +3 -1
  50. package/lib/ble/cp/adapter.d.ts +23 -0
  51. package/lib/ble/cp/adapter.js +124 -0
  52. package/lib/ble/cp/comm.d.ts +28 -0
  53. package/lib/ble/cp/comm.js +140 -0
  54. package/lib/ble/cp/index.d.ts +4 -0
  55. package/lib/ble/cp/index.js +10 -0
  56. package/lib/ble/cp/types.d.ts +13 -0
  57. package/lib/ble/cp/types.js +2 -0
  58. package/lib/ble/elite/adapter.d.ts +20 -0
  59. package/lib/ble/elite/adapter.js +127 -0
  60. package/lib/ble/elite/comms.d.ts +28 -0
  61. package/lib/ble/elite/comms.js +139 -0
  62. package/lib/ble/elite/index.d.ts +3 -0
  63. package/lib/ble/elite/index.js +10 -0
  64. package/lib/ble/fm/adapter.d.ts +21 -0
  65. package/lib/ble/fm/adapter.js +190 -0
  66. package/lib/ble/fm/comms.d.ts +50 -0
  67. package/lib/ble/{fm.js → fm/comms.js} +3 -224
  68. package/lib/ble/fm/consts.d.ts +6 -0
  69. package/lib/ble/fm/consts.js +9 -0
  70. package/lib/ble/fm/index.d.ts +5 -0
  71. package/lib/ble/fm/index.js +13 -0
  72. package/lib/ble/fm/types.d.ts +25 -0
  73. package/lib/ble/fm/types.js +2 -0
  74. package/lib/ble/hr/adapter.d.ts +17 -0
  75. package/lib/ble/hr/adapter.js +62 -0
  76. package/lib/ble/hr/comm.d.ts +15 -0
  77. package/lib/ble/hr/comm.js +52 -0
  78. package/lib/ble/hr/index.d.ts +4 -0
  79. package/lib/ble/hr/index.js +10 -0
  80. package/lib/ble/hr/types.d.ts +5 -0
  81. package/lib/ble/hr/types.js +2 -0
  82. package/lib/ble/index.d.ts +8 -0
  83. package/lib/ble/index.js +26 -0
  84. package/lib/ble/tacx/adapter.d.ts +10 -0
  85. package/lib/ble/tacx/adapter.js +93 -0
  86. package/lib/ble/{tacx.d.ts → tacx/comms.d.ts} +6 -38
  87. package/lib/ble/{tacx.js → tacx/comms.js} +13 -112
  88. package/lib/ble/tacx/index.d.ts +4 -0
  89. package/lib/ble/tacx/index.js +10 -0
  90. package/lib/ble/tacx/types.d.ts +25 -0
  91. package/lib/ble/tacx/types.js +2 -0
  92. package/lib/ble/types.d.ts +101 -0
  93. package/lib/ble/types.js +19 -0
  94. package/lib/ble/utils.d.ts +7 -0
  95. package/lib/ble/utils.js +95 -0
  96. package/lib/ble/wahoo/adapter.d.ts +9 -0
  97. package/lib/ble/wahoo/adapter.js +93 -0
  98. package/lib/ble/{wahoo-kickr.d.ts → wahoo/comms.d.ts} +5 -39
  99. package/lib/ble/{wahoo-kickr.js → wahoo/comms.js} +14 -112
  100. package/lib/ble/wahoo/index.d.ts +4 -0
  101. package/lib/ble/wahoo/index.js +10 -0
  102. package/lib/ble/wahoo/types.d.ts +19 -0
  103. package/lib/ble/wahoo/types.js +2 -0
  104. package/lib/device.d.ts +0 -94
  105. package/lib/device.js +0 -76
  106. package/lib/index.d.ts +12 -0
  107. package/lib/index.js +38 -0
  108. package/lib/interfaces.d.ts +6 -0
  109. package/lib/interfaces.js +23 -0
  110. package/lib/{ble → modes}/ble-erg-mode.d.ts +4 -4
  111. package/lib/{ble → modes}/ble-erg-mode.js +2 -2
  112. package/lib/{ble → modes}/ble-st-mode.d.ts +3 -3
  113. package/lib/{ble → modes}/ble-st-mode.js +1 -1
  114. package/lib/{cycling-mode.d.ts → modes/cycling-mode.d.ts} +8 -8
  115. package/lib/modes/power-base.d.ts +3 -3
  116. package/lib/modes/power-base.js +4 -4
  117. package/lib/modes/power-meter.d.ts +3 -3
  118. package/lib/modes/simulator.d.ts +2 -2
  119. package/lib/modes/simulator.js +1 -1
  120. package/lib/serial/adapter-factory.d.ts +12 -0
  121. package/lib/serial/adapter-factory.js +30 -0
  122. package/lib/serial/adapter.d.ts +16 -0
  123. package/lib/serial/adapter.js +42 -0
  124. package/lib/serial/bindings/tcp.d.ts +44 -0
  125. package/lib/serial/bindings/tcp.js +270 -0
  126. package/lib/serial/comm.d.ts +7 -0
  127. package/lib/serial/comm.js +2 -0
  128. package/lib/{daum → serial/daum}/DaumAdapter.d.ts +13 -22
  129. package/lib/{daum → serial/daum}/DaumAdapter.js +44 -90
  130. package/lib/{daum → serial/daum}/DaumPowerMeterCyclingMode.d.ts +2 -2
  131. package/lib/{daum → serial/daum}/DaumPowerMeterCyclingMode.js +1 -1
  132. package/lib/{daum → serial/daum}/ERGCyclingMode.d.ts +3 -3
  133. package/lib/{daum → serial/daum}/ERGCyclingMode.js +3 -3
  134. package/lib/{daum → serial/daum}/SmartTrainerCyclingMode.d.ts +3 -3
  135. package/lib/{daum → serial/daum}/SmartTrainerCyclingMode.js +4 -3
  136. package/lib/serial/daum/classic/adapter.d.ts +29 -0
  137. package/lib/{daum/classic/DaumClassicAdapter.js → serial/daum/classic/adapter.js} +77 -50
  138. package/lib/{daum/classic/bike.d.ts → serial/daum/classic/comms.d.ts} +14 -16
  139. package/lib/{daum/classic/bike.js → serial/daum/classic/comms.js} +68 -152
  140. package/lib/serial/daum/classic/mock.d.ts +96 -0
  141. package/lib/serial/daum/classic/mock.js +365 -0
  142. package/lib/{daum/classic/DaumClassicCyclingMode.d.ts → serial/daum/classic/modes/daum-classic.d.ts} +3 -3
  143. package/lib/{daum/classic/DaumClassicCyclingMode.js → serial/daum/classic/modes/daum-classic.js} +2 -2
  144. package/lib/{daum → serial/daum}/classic/utils.d.ts +1 -1
  145. package/lib/serial/daum/premium/adapter.d.ts +24 -0
  146. package/lib/{daum/premium/DaumPremiumAdapter.js → serial/daum/premium/adapter.js} +99 -46
  147. package/lib/{daum/premium/bike.d.ts → serial/daum/premium/comms.d.ts} +26 -54
  148. package/lib/{daum/premium/bike.js → serial/daum/premium/comms.js} +214 -420
  149. package/lib/serial/daum/premium/mock.d.ts +75 -0
  150. package/lib/serial/daum/premium/mock.js +290 -0
  151. package/lib/{daum/premium/DaumClassicCyclingMode.d.ts → serial/daum/premium/modes/daum-classic.d.ts} +3 -3
  152. package/lib/{daum/premium/DaumClassicCyclingMode.js → serial/daum/premium/modes/daum-classic.js} +2 -2
  153. package/lib/serial/daum/premium/types.d.ts +12 -0
  154. package/lib/serial/daum/premium/types.js +2 -0
  155. package/lib/{daum → serial/daum}/premium/utils.d.ts +8 -11
  156. package/lib/{daum → serial/daum}/premium/utils.js +22 -63
  157. package/lib/serial/index.d.ts +9 -0
  158. package/lib/serial/index.js +49 -0
  159. package/lib/{kettler → serial/kettler}/comms.d.ts +8 -6
  160. package/lib/{kettler → serial/kettler}/comms.js +71 -32
  161. package/lib/{kettler → serial/kettler}/ergo-racer/adapter.d.ts +13 -24
  162. package/lib/{kettler → serial/kettler}/ergo-racer/adapter.js +87 -130
  163. package/lib/serial/kettler/ergo-racer/mock.d.ts +66 -0
  164. package/lib/serial/kettler/ergo-racer/mock.js +216 -0
  165. package/lib/{kettler/ergo-racer/ERGCyclingMode.d.ts → serial/kettler/ergo-racer/modes/erg.d.ts} +4 -4
  166. package/lib/{kettler/ergo-racer/ERGCyclingMode.js → serial/kettler/ergo-racer/modes/erg.js} +2 -2
  167. package/lib/serial/serial-interface.d.ts +58 -0
  168. package/lib/serial/serial-interface.js +283 -0
  169. package/lib/simulator/Simulator.d.ts +10 -25
  170. package/lib/simulator/Simulator.js +19 -83
  171. package/lib/types/adapter.d.ts +32 -0
  172. package/lib/types/adapter.js +2 -0
  173. package/lib/types/capabilities.d.ts +8 -0
  174. package/lib/types/capabilities.js +12 -0
  175. package/lib/types/data.d.ts +12 -0
  176. package/lib/types/data.js +2 -0
  177. package/lib/types/device.d.ts +29 -0
  178. package/lib/types/device.js +11 -0
  179. package/lib/types/interface.d.ts +14 -0
  180. package/lib/types/interface.js +2 -0
  181. package/lib/types/route.d.ts +2 -2
  182. package/lib/types/user.d.ts +1 -1
  183. package/lib/{utils.d.ts → utils/utils.d.ts} +1 -0
  184. package/lib/{utils.js → utils/utils.js} +5 -1
  185. package/package.json +15 -11
  186. package/lib/DeviceSupport.d.ts +0 -36
  187. package/lib/DeviceSupport.js +0 -82
  188. package/lib/ant/AntAdapter.d.ts +0 -50
  189. package/lib/ant/AntAdapter.js +0 -109
  190. package/lib/ant/AntScanner.d.ts +0 -60
  191. package/lib/ant/AntScanner.js +0 -651
  192. package/lib/ant/antfe/AntFEAdapter.d.ts +0 -83
  193. package/lib/ant/antfe/AntFEAdapter.js +0 -652
  194. package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +0 -9
  195. package/lib/ant/antfe/ant-fe-erg-mode.d.ts +0 -6
  196. package/lib/ant/antfe/ant-fe-st-mode.d.ts +0 -5
  197. package/lib/ant/anthrm/AntHrmAdapter.d.ts +0 -16
  198. package/lib/ant/anthrm/AntHrmAdapter.js +0 -130
  199. package/lib/ant/antpwr/pwr-adapter.d.ts +0 -49
  200. package/lib/ant/antpwr/pwr-adapter.js +0 -251
  201. package/lib/ant/utils.d.ts +0 -1
  202. package/lib/ant/utils.js +0 -23
  203. package/lib/antv2/AntAdapter.d.ts +0 -48
  204. package/lib/antv2/AntAdapter.js +0 -104
  205. package/lib/antv2/ant-device.d.ts +0 -59
  206. package/lib/antv2/ant-device.js +0 -161
  207. package/lib/antv2/fe.d.ts +0 -32
  208. package/lib/antv2/hr.d.ts +0 -18
  209. package/lib/antv2/hr.js +0 -102
  210. package/lib/antv2/incyclist-protocol.d.ts +0 -37
  211. package/lib/antv2/incyclist-protocol.js +0 -126
  212. package/lib/antv2/pwr.d.ts +0 -28
  213. package/lib/ble/elite.d.ts +0 -90
  214. package/lib/ble/elite.js +0 -322
  215. package/lib/ble/fm.d.ts +0 -125
  216. package/lib/ble/hrm.d.ts +0 -48
  217. package/lib/ble/hrm.js +0 -134
  218. package/lib/ble/incyclist-protocol.d.ts +0 -31
  219. package/lib/ble/incyclist-protocol.js +0 -153
  220. package/lib/ble/pwr.d.ts +0 -89
  221. package/lib/ble/pwr.js +0 -322
  222. package/lib/daum/classic/DaumClassicAdapter.d.ts +0 -28
  223. package/lib/daum/classic/DaumClassicProtocol.d.ts +0 -27
  224. package/lib/daum/classic/DaumClassicProtocol.js +0 -185
  225. package/lib/daum/premium/DaumPremiumAdapter.d.ts +0 -16
  226. package/lib/daum/premium/DaumPremiumProtocol.d.ts +0 -32
  227. package/lib/daum/premium/DaumPremiumProtocol.js +0 -207
  228. package/lib/daum/premium/tcpserial.d.ts +0 -33
  229. package/lib/daum/premium/tcpserial.js +0 -123
  230. package/lib/kettler/ergo-racer/protocol.d.ts +0 -41
  231. package/lib/kettler/ergo-racer/protocol.js +0 -203
  232. package/lib/protocol.d.ts +0 -74
  233. package/lib/protocol.js +0 -41
  234. package/lib/registry.d.ts +0 -8
  235. package/lib/registry.js +0 -33
  236. package/lib/serialport/bindings/tcp.d.ts +0 -20
  237. package/lib/serialport/bindings/tcp.js +0 -33
  238. package/lib/serialport/index.d.ts +0 -2
  239. package/lib/serialport/index.js +0 -29
  240. package/lib/serialport/serialport.d.ts +0 -29
  241. package/lib/serialport/serialport.js +0 -87
  242. /package/lib/antv2/{ant-binding.d.ts → binding.d.ts} +0 -0
  243. /package/lib/antv2/{ant-binding.js → binding.js} +0 -0
  244. /package/lib/{cycling-mode.js → modes/cycling-mode.js} +0 -0
  245. /package/lib/{daum → serial/daum}/classic/utils.js +0 -0
  246. /package/lib/{daum → serial/daum}/constants.d.ts +0 -0
  247. /package/lib/{daum → serial/daum}/constants.js +0 -0
  248. /package/lib/{serialport.d.ts → serial/serialport.d.ts} +0 -0
  249. /package/lib/{serialport.js → serial/serialport.js} +0 -0
  250. /package/lib/{calculations.d.ts → utils/calculations.d.ts} +0 -0
  251. /package/lib/{calculations.js → utils/calculations.js} +0 -0
@@ -1,651 +0,0 @@
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
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
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);
19
- return result;
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
- };
30
- var __importDefault = (this && this.__importDefault) || function (mod) {
31
- return (mod && mod.__esModule) ? mod : { "default": mod };
32
- };
33
- Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.AntScanner = exports.AntProtocol = void 0;
35
- const gd_eventlog_1 = require("gd-eventlog");
36
- const protocol_1 = __importStar(require("../protocol"));
37
- const AntHrmAdapter_1 = __importDefault(require("./anthrm/AntHrmAdapter"));
38
- const pwr_adapter_1 = __importDefault(require("./antpwr/pwr-adapter"));
39
- const AntFEAdapter_1 = __importDefault(require("./antfe/AntFEAdapter"));
40
- const LOGGER_NAME = 'ANT+Scanner';
41
- const DEFAULT_SCAN_TIMEOUT = 30000;
42
- const TIMEOUT_STARTUP = 3000;
43
- const hex = (n, len) => {
44
- const c = "0";
45
- let s = n.toString(16);
46
- if (s.length < len)
47
- return `0x${c.repeat(len - s.length)}${s}`;
48
- return `0x${s}`;
49
- };
50
- const isStickPresent = (stick, retries) => __awaiter(void 0, void 0, void 0, function* () {
51
- let n = 0;
52
- while (n < retries) {
53
- try {
54
- return stick.is_present();
55
- }
56
- catch (e) {
57
- n++;
58
- yield new Promise(resolve => setTimeout(resolve, 1000));
59
- }
60
- }
61
- return false;
62
- });
63
- class AntProfile {
64
- constructor(profile, AntScannerClass, stick, message, onNewDevice, onData) {
65
- if (process.env.ANT_PROFILE_DEBUG)
66
- console.log('adding profile', profile, AntScannerClass, message, onNewDevice, onData);
67
- this.ids = [];
68
- this.scanner = new AntScannerClass(stick);
69
- this.scanner.on(message, data => {
70
- try {
71
- if (process.env.ANT_PROFILE_DEBUG)
72
- console.log(data);
73
- if (data.DeviceID) {
74
- if (this.ids.find(id => id === data.DeviceID)) {
75
- if (onData)
76
- onData(profile, data.DeviceID, data);
77
- return;
78
- }
79
- this.ids.push(data.DeviceID);
80
- if (onNewDevice)
81
- onNewDevice(profile, data.DeviceID);
82
- }
83
- }
84
- catch (err) {
85
- }
86
- });
87
- }
88
- getScanner() {
89
- return this.scanner;
90
- }
91
- getProfile() {
92
- return this.profile;
93
- }
94
- }
95
- class AntProtocol extends protocol_1.default {
96
- constructor(antClass) {
97
- super();
98
- this.logger = new gd_eventlog_1.EventLogger(LOGGER_NAME);
99
- this.ant = antClass;
100
- this.activeScans = {};
101
- this.sensors = {};
102
- this.sticks = [];
103
- this.scanning = false;
104
- this.profiles = [
105
- { name: 'Heartrate Monitor', Adapter: AntHrmAdapter_1.default },
106
- { name: 'Smart Trainer', Adapter: AntFEAdapter_1.default },
107
- { name: 'Power Meter', Adapter: pwr_adapter_1.default }
108
- ];
109
- }
110
- add(settings) {
111
- this.logger.logEvent({ message: 'adding device', settings });
112
- const { profile, deviceID, port } = settings;
113
- const profileInfo = this.profiles.find(i => i.name === profile);
114
- if (profileInfo) {
115
- let device;
116
- try {
117
- device = new profileInfo.Adapter(deviceID, port, undefined, this);
118
- this.devices.push(device);
119
- return device;
120
- }
121
- catch (err) {
122
- this.logger.logEvent({ message: 'adding device error', error: err.message });
123
- return;
124
- }
125
- }
126
- this.logger.logEvent({ message: 'adding device: profile not found' });
127
- }
128
- getAnt() {
129
- return this.ant || protocol_1.default.getAnt();
130
- }
131
- getName() { return 'Ant'; }
132
- getInterfaces() { return [protocol_1.INTERFACE.ANT]; }
133
- isBike() { return true; }
134
- isHrm() { return true; }
135
- isPower() { return true; }
136
- isScanning() { return Object.keys(this.activeScans).length > 0; }
137
- getSupportedProfiles() {
138
- return this.profiles.map(i => i.name);
139
- }
140
- getUSBDeviceInfo(d) {
141
- if (!d)
142
- return;
143
- return ({
144
- port: `usb:${d.busNumber}-${d.deviceAddress}`,
145
- vendor: d.deviceDescriptor.idVendor,
146
- product: d.deviceDescriptor.idProduct,
147
- inUse: d.inUse
148
- });
149
- }
150
- getStickInfo(sticks) {
151
- const isStick = i => {
152
- return (i && i.vendor === 0x0FCF && (i.product === 0x1008 || i.product === 0x1009));
153
- };
154
- const inUse = i => i && i.inUse;
155
- return sticks
156
- .map(d => this.getUSBDeviceInfo(d))
157
- .reduce((r, i) => r + `${r === '' ? '' : ','}[${i.port} ${hex(i.vendor, 4)} ${hex(i.product, 4)}${isStick(i) ? '*' : ''}${inUse(i) ? 'x' : ''}]`, '');
158
- }
159
- findStickByPort(port) {
160
- const info = this.sticks.find(i => i.port === port);
161
- if (info)
162
- return info.stick;
163
- }
164
- logStickInfo() {
165
- const sticks = this.ant.getSticks();
166
- const info = this.getStickInfo(sticks);
167
- this.logger.logEvent({ message: 'stick info', info });
168
- }
169
- getDetailedStickInfo(stick) {
170
- const devices = stick.getDevices();
171
- if (devices.length > 0) {
172
- const device = devices[0];
173
- try {
174
- const config = JSON.parse(JSON.stringify(device.configDescriptor));
175
- const interfaces = config ? config.interfaces : [];
176
- delete config.interfaces;
177
- this.logger.logEvent({ message: 'USB DeviceConfig', config, interfaces });
178
- }
179
- catch (err) {
180
- this.logger.logEvent({ message: 'USB Error', error: err.message });
181
- }
182
- }
183
- }
184
- getStick(onStart, onError) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- if (!this.ant)
187
- return;
188
- const startupAttempt = (stick, name) => {
189
- return new Promise((resolve, reject) => {
190
- this.logger.logEvent({ message: `${name} startup attempt` });
191
- if (stick.scanConnected) {
192
- onStart(this);
193
- resolve(stick);
194
- return;
195
- }
196
- if (isStickPresent(stick, 2)) {
197
- stick.on('error', (err) => {
198
- this.logger.logEvent({ message: `${name} startup error`, error: err.message });
199
- onError(err.message);
200
- });
201
- stick.on('startup', () => {
202
- if (stick.scanConnected)
203
- return;
204
- this.logger.logEvent({ message: `${name} startup completed` });
205
- stick.scanConnected = true;
206
- onStart(stick);
207
- resolve(stick);
208
- });
209
- const devices = stick.getDevices();
210
- if (devices && devices.length > 0) {
211
- devices.forEach(d => {
212
- d.closeFn = d.close;
213
- d.close = () => { };
214
- });
215
- }
216
- else {
217
- this.logger.logEvent({ message: `${name} startup failed: no devices ` });
218
- resolve(null);
219
- }
220
- this.getDetailedStickInfo(stick);
221
- let open = false;
222
- try {
223
- open = stick.open();
224
- if (open) {
225
- this.logger.logEvent({ message: `found ${name}` });
226
- const timeoutStartup = Date.now() + TIMEOUT_STARTUP;
227
- const iv = setInterval(() => {
228
- if (!stick.scanConnected && Date.now() > timeoutStartup) {
229
- clearInterval(iv);
230
- this.logger.logEvent({ message: `${name} startup timeout` });
231
- }
232
- if (stick.scanConnected)
233
- clearInterval(iv);
234
- }, 100);
235
- return stick;
236
- }
237
- }
238
- catch (openErr) {
239
- this.logger.logEvent({ message: 'Open Error', error: openErr.message });
240
- }
241
- if (devices && devices.length > 0) {
242
- devices.forEach(d => {
243
- d.close = d.closeFn;
244
- });
245
- }
246
- if (!open) {
247
- while (devices.length) {
248
- let device;
249
- try {
250
- device = devices.shift();
251
- device.open();
252
- const iface = device.interfaces[0];
253
- let kernelDriverActive = undefined;
254
- let claimRequested = false;
255
- try {
256
- kernelDriverActive = iface.isKernelDriverActive();
257
- if (kernelDriverActive) {
258
- iface.detachKernelDriver();
259
- }
260
- claimRequested = true;
261
- iface.claim();
262
- }
263
- catch (kernelErr) {
264
- this.logger.logEvent({ message: 'Kernel Error', error: kernelErr.message, kernelDriverActive, claimRequested });
265
- }
266
- break;
267
- }
268
- catch (deviceErr) {
269
- this.logger.logEvent({ message: 'Device Error', error: deviceErr.message });
270
- if (device) {
271
- try {
272
- device.close();
273
- }
274
- catch (_a) { }
275
- }
276
- }
277
- }
278
- }
279
- }
280
- this.logger.logEvent({ message: `${name} startup failed: no stick present` });
281
- resolve(null);
282
- });
283
- };
284
- let stick = undefined;
285
- stick = yield startupAttempt(new this.ant.GarminStick2(), 'GarminStick2');
286
- if (!stick)
287
- stick = yield startupAttempt(new this.ant.GarminStick3(), 'GarminStick3');
288
- if (!stick)
289
- onError('No stick found');
290
- else
291
- return stick;
292
- });
293
- }
294
- getFirstStick() {
295
- return __awaiter(this, void 0, void 0, function* () {
296
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
297
- if (!this.ant)
298
- return reject(new Error('Ant not supported'));
299
- if (this.sticks && this.sticks.length > 0 && this.sticks[0].connected) {
300
- this.logger.logEvent({ message: 'stick already connected' });
301
- return resolve(this.sticks[0]);
302
- }
303
- try {
304
- let start = Date.now();
305
- let timeout = start + 5000;
306
- let found = false;
307
- const iv = setInterval(() => {
308
- if (!found && Date.now() > timeout) {
309
- clearInterval(iv);
310
- reject(new Error('timeout'));
311
- }
312
- }, 100);
313
- this.getStick((stick) => {
314
- clearInterval(iv);
315
- const port = this.getUSBDeviceInfo(stick.device).port;
316
- if (!this.sticks.find(i => i.port === port)) {
317
- this.sticks.push({ port, stick, connected: true });
318
- }
319
- resolve({ port, stick });
320
- }, (reason) => {
321
- resolve(undefined);
322
- });
323
- }
324
- catch (err) {
325
- this.logger.logEvent({ message: 'getFirstStick error', error: err.message, stack: err.stack });
326
- }
327
- }));
328
- });
329
- }
330
- closeStick(stick) {
331
- this.logger.logEvent({ message: 'closing stick' });
332
- return new Promise((resolve, reject) => {
333
- stick.on('shutdown', () => {
334
- this.logger.logEvent({ message: 'stick shutdown' });
335
- stick.removeAllListeners('shutdown');
336
- const port = this.getUSBDeviceInfo(stick.device).port;
337
- const idx = this.sticks.findIndex(i => i.port === port);
338
- if (idx !== -1) {
339
- this.sticks[idx].connected = false;
340
- }
341
- this.sensors = {};
342
- resolve(true);
343
- });
344
- try {
345
- stick.detach_all();
346
- setTimeout(() => {
347
- try {
348
- stick.close();
349
- }
350
- catch (err) { }
351
- this.logger.logEvent({ message: 'stick closed' });
352
- stick.scanConnected = false;
353
- this.sensors = {};
354
- }, 1000);
355
- }
356
- catch (err) {
357
- reject(err);
358
- }
359
- });
360
- }
361
- stopScanOnStick(stickInfo) {
362
- const { stick, port } = stickInfo;
363
- const state = this.activeScans[port];
364
- return this.closeStick(stick)
365
- .then(() => {
366
- state.isScanning = false;
367
- this.scanning = this.stillScanning();
368
- if (state.iv) {
369
- clearInterval(state.iv);
370
- state.iv = undefined;
371
- }
372
- return true;
373
- })
374
- .catch(err => {
375
- this.logger.logEvent({ message: 'error on closing stick', error: err.message, port });
376
- return true;
377
- });
378
- }
379
- stillScanning() {
380
- const ports = Object.keys(this.activeScans);
381
- return ports.find(p => this.activeScans[p].isScanning) !== undefined;
382
- }
383
- scanOnStick(stickInfo, props = {}) {
384
- const { stick, port } = stickInfo;
385
- const timeout = props.timeout || DEFAULT_SCAN_TIMEOUT;
386
- const { onDeviceFound, onScanFinished, onUpdate, id } = props;
387
- this.logger.logEvent({ message: 'stick scan request', port, activeScans: this.activeScans });
388
- if (process.env.ANT_DEBUG) {
389
- stick.props = { debug: true };
390
- }
391
- return new Promise((resolve, reject) => {
392
- if (!port)
393
- return reject(new Error('busy'));
394
- if (this.activeScans[port] && this.activeScans[port].isScanning)
395
- return reject(new Error('busy'));
396
- if (!this.activeScans[port]) {
397
- this.activeScans[port] = { isScanning: false, stick };
398
- }
399
- const state = this.activeScans[port];
400
- if (state.isScanning)
401
- return reject(new Error('busy'));
402
- state.isScanning = true;
403
- this.logger.logEvent({ message: 'start scan', port });
404
- state.timeout = Date.now() + timeout;
405
- const onNewDevice = (profile, deviceId) => {
406
- this.logger.logEvent({ message: 'found device', profile, id: deviceId });
407
- const profileInfo = this.profiles.find(i => i.name === profile);
408
- if (profileInfo) {
409
- let device;
410
- try {
411
- device = new profileInfo.Adapter(deviceId, port, stick, this, props);
412
- const existing = this.devices.find(d => (d.getID() === deviceId && d.getProfile() === profile));
413
- if (!existing)
414
- this.devices.push(device);
415
- }
416
- catch (err) {
417
- this.logger.logEvent({ message: 'onNewDevice:ERROR', error: err.message });
418
- }
419
- if (device && onDeviceFound) {
420
- onDeviceFound(device, this);
421
- device.setDetected(true);
422
- }
423
- }
424
- };
425
- const onData = (profile, deviceId, data) => {
426
- const device = this.devices.find(d => d.getID() === deviceId);
427
- if (device) {
428
- const isHrm = device.isHrm();
429
- device.onDeviceData(data);
430
- if (device.isHrm() && !isHrm && onDeviceFound) {
431
- onDeviceFound(device, this);
432
- }
433
- if (onUpdate)
434
- onUpdate(device);
435
- }
436
- };
437
- const hrm = new AntProfile('Heartrate Monitor', this.ant.HeartRateScanner, stick, 'hbData', onNewDevice, onData);
438
- const fe = new AntProfile('Smart Trainer', this.ant.FitnessEquipmentScanner, stick, 'fitnessData', onNewDevice, onData);
439
- const power = new AntProfile('Power Meter', this.ant.BicyclePowerScanner, stick, 'powerData', onNewDevice, onData);
440
- try {
441
- hrm.getScanner().scan();
442
- hrm.getScanner().on('attached', () => {
443
- power.getScanner().scan();
444
- fe.getScanner().scan();
445
- });
446
- }
447
- catch (err) {
448
- this.logger.logEvent({ message: 'scan error', error: err.message });
449
- }
450
- state.iv = setInterval(() => {
451
- if (Date.now() > timeout) {
452
- this.logger.logEvent({ message: 'scan timeout', port });
453
- this.stopScanOnStick(stickInfo).then(() => {
454
- if (onScanFinished)
455
- onScanFinished(id);
456
- resolve(true);
457
- });
458
- }
459
- }, timeout);
460
- });
461
- }
462
- scan(props) {
463
- return __awaiter(this, void 0, void 0, function* () {
464
- this.logger.logEvent({ message: 'scan request', props });
465
- this.scanning = true;
466
- this.logStickInfo();
467
- if (this.sensors && this.sensors.stick && this.sensors.stickOpen) {
468
- try {
469
- yield this.closeStick(this.sensors.stick);
470
- this.sensors = {};
471
- this.devices = [];
472
- }
473
- catch (err) {
474
- this.logger.logEvent({ message: 'error on closing stick', error: err.message });
475
- }
476
- }
477
- try {
478
- const stick = yield this.getFirstStick();
479
- if (!stick) {
480
- this.logger.logEvent({ message: 'no stick found' });
481
- return;
482
- }
483
- this.scanOnStick(stick, props);
484
- }
485
- catch (err) {
486
- this.logger.logEvent({ message: 'scan request error', error: err.message });
487
- }
488
- });
489
- }
490
- stopScan() {
491
- return __awaiter(this, void 0, void 0, function* () {
492
- const activePorts = Object.keys(this.activeScans);
493
- for (let i = 0; i < activePorts.length; i++) {
494
- const port = activePorts[i];
495
- const scanState = this.activeScans[port];
496
- if (scanState.isScanning) {
497
- yield this.stopScanOnStick({ port, stick: scanState.stick });
498
- }
499
- }
500
- this.sensors = {};
501
- this.logger.logEvent({ message: 'scan stopped' });
502
- this.scanning = false;
503
- return true;
504
- });
505
- }
506
- waitForStickOpenedForSensor() {
507
- return new Promise((resolve, reject) => {
508
- const iv = setInterval(() => {
509
- if (!this.sensors.stickOpening && !this.scanning) {
510
- clearInterval(iv);
511
- resolve(true);
512
- }
513
- }, 100);
514
- });
515
- }
516
- attachSensors(d, SensorClass, message) {
517
- return __awaiter(this, void 0, void 0, function* () {
518
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
519
- if (d === undefined) {
520
- resolve(false);
521
- return;
522
- }
523
- const devices = Array.isArray(d) ? d : [d];
524
- if (devices.length === 0) {
525
- return resolve(false);
526
- }
527
- this.logger.logEvent({ message: 'attachSensors', names: Array.isArray(d) ? d.map(dd => dd.getName()) : d.getName(), state: this.sensors });
528
- if (this.sensors.stickOpening || this.scanning) {
529
- yield this.waitForStickOpenedForSensor();
530
- }
531
- this.sensors.stickOpening = true;
532
- let stick;
533
- if (!this.sensors.stick) {
534
- this.logger.logEvent({ message: 'openStick', device: devices[0].getName() });
535
- let retryCnt = 0;
536
- while (!stick && retryCnt < 3) {
537
- try {
538
- const stickInfo = yield this.getFirstStick();
539
- if (stickInfo && stickInfo.stick) {
540
- this.logger.logEvent({ message: 'stick opened', device: devices[0].getName() });
541
- stick = stickInfo.stick;
542
- this.sensors.stick = stick;
543
- this.sensors.stickOpen = true;
544
- this.sensors.stickStarted = true;
545
- this.sensors.stickOpening = false;
546
- }
547
- else {
548
- retryCnt++;
549
- }
550
- }
551
- catch (err) {
552
- retryCnt++;
553
- this.logger.logEvent({ message: 'stick open error', error: err.message, device: devices[0].getName() });
554
- }
555
- }
556
- if (!stick) {
557
- return reject(new Error('could not pen stick'));
558
- }
559
- }
560
- if (this.sensors.stickOpen) {
561
- if (!this.sensors.pending)
562
- this.sensors.pending = [];
563
- devices.forEach(device => {
564
- const sensor = new SensorClass(this.sensors.stick);
565
- device.setSensor(sensor);
566
- device.setStick(stick);
567
- sensor.on(message, (data) => { device.onDeviceData(data); });
568
- sensor.on('eventData', (data) => { device.onDeviceEvent(data); });
569
- sensor.once('attached', () => { device.onAttached(); });
570
- this.sensors.pending.push({ device, sensor, message });
571
- });
572
- }
573
- const attachFromPending = () => {
574
- if (!this.sensors.attached)
575
- this.sensors.attached = [];
576
- const channelsUsed = this.sensors.attached.length;
577
- try {
578
- this.sensors.pending.forEach((i, idx) => {
579
- const channel = channelsUsed + idx;
580
- const { sensor } = i;
581
- i.device.setChannel(channel);
582
- sensor.attach(channel, i.device.getID());
583
- this.sensors.attached.push(i);
584
- });
585
- this.sensors.pending = [];
586
- resolve(true);
587
- }
588
- catch (err) {
589
- this.logger.logEvent({ message: 'error', fn: 'attachFromPending()', error: err.message || err });
590
- reject(err.message ? err : new Error(err));
591
- }
592
- };
593
- if (this.sensors.stickStarted) {
594
- attachFromPending();
595
- }
596
- else {
597
- const sensors = this.sensors;
598
- const stick = this.sensors.stick;
599
- stick.on('error', (err) => {
600
- this.logger.logEvent({ message: 'stick error', error: err.message });
601
- });
602
- stick.once('startup', () => {
603
- sensors.stickStarted = true;
604
- setTimeout(attachFromPending, 1000);
605
- });
606
- }
607
- }));
608
- });
609
- }
610
- detachSensor(adapter) {
611
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
612
- const idx = (this.sensors && this.sensors.attached) ?
613
- this.sensors.attached.findIndex(i => (i.device.getID() === adapter.getID() && i.device.getName() === adapter.getName())) : -1;
614
- if (idx === -1)
615
- return resolve(true);
616
- this.sensors.attached.splice(idx, 1);
617
- if (this.sensors.attached.length > 0)
618
- return resolve(true);
619
- const stick = this.sensors.stick;
620
- if (stick === undefined)
621
- return resolve(false);
622
- try {
623
- yield this.closeStick(stick);
624
- resolve(true);
625
- }
626
- catch (err) {
627
- reject(err);
628
- }
629
- }));
630
- }
631
- closeSensor(device) {
632
- return __awaiter(this, void 0, void 0, function* () {
633
- const stick = this.findStickByPort(device.getPort());
634
- if (stick.inUse) {
635
- try {
636
- stick.close();
637
- }
638
- catch (err) {
639
- this.logger.logEvent({ message: 'closeSensor error', error: err.message, device: device ? device.getName() : 'unknown' });
640
- }
641
- }
642
- });
643
- }
644
- }
645
- exports.AntProtocol = AntProtocol;
646
- let _scanner = undefined;
647
- function AntScanner(antClass) {
648
- const scanner = _scanner || new AntProtocol(antClass);
649
- return scanner;
650
- }
651
- exports.AntScanner = AntScanner;