incyclist-devices 1.5.38 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/README.MD +238 -0
  2. package/lib/adapters.d.ts +7 -0
  3. package/lib/adapters.js +49 -0
  4. package/lib/antv2/adapter-factory.d.ts +21 -10
  5. package/lib/antv2/adapter-factory.js +51 -33
  6. package/lib/antv2/adapter.d.ts +65 -0
  7. package/lib/antv2/adapter.js +310 -0
  8. package/lib/antv2/ant-interface.d.ts +17 -11
  9. package/lib/antv2/ant-interface.js +41 -31
  10. package/lib/antv2/{ant-binding.d.ts → binding.d.ts} +1 -1
  11. package/lib/antv2/{ant-binding.js → binding.js} +1 -1
  12. package/lib/antv2/fe/adapter.d.ts +28 -0
  13. package/lib/antv2/{fe.js → fe/adapter.js} +106 -90
  14. package/lib/antv2/fe/index.d.ts +2 -0
  15. package/lib/antv2/fe/index.js +7 -0
  16. package/lib/antv2/hr/adapter.d.ts +15 -0
  17. package/lib/antv2/hr/adapter.js +76 -0
  18. package/lib/antv2/hr/index.d.ts +2 -0
  19. package/lib/antv2/hr/index.js +7 -0
  20. package/lib/antv2/index.d.ts +7 -0
  21. package/lib/antv2/index.js +20 -0
  22. package/lib/antv2/modes/ant-fe-adv-st-mode.d.ts +9 -0
  23. package/lib/{ant/antfe → antv2/modes}/ant-fe-adv-st-mode.js +1 -1
  24. package/lib/antv2/modes/ant-fe-erg-mode.d.ts +6 -0
  25. package/lib/{ant/antfe → antv2/modes}/ant-fe-erg-mode.js +1 -1
  26. package/lib/antv2/modes/ant-fe-st-mode.d.ts +5 -0
  27. package/lib/{ant/antfe → antv2/modes}/ant-fe-st-mode.js +1 -1
  28. package/lib/antv2/pwr/adapter.d.ts +24 -0
  29. package/lib/antv2/pwr/adapter.js +141 -0
  30. package/lib/antv2/pwr/index.d.ts +2 -0
  31. package/lib/antv2/pwr/index.js +7 -0
  32. package/lib/antv2/sensor-factory.d.ts +2 -2
  33. package/lib/antv2/types.d.ts +22 -0
  34. package/lib/antv2/types.js +5 -0
  35. package/lib/antv2/utils.d.ts +4 -0
  36. package/lib/antv2/utils.js +192 -0
  37. package/lib/base/adpater.d.ts +68 -0
  38. package/lib/base/adpater.js +183 -0
  39. package/lib/ble/adapter-factory.d.ts +33 -0
  40. package/lib/ble/adapter-factory.js +105 -0
  41. package/lib/ble/base/adapter.d.ts +53 -0
  42. package/lib/ble/base/adapter.js +290 -0
  43. package/lib/ble/base/comms-utils.d.ts +7 -0
  44. package/lib/ble/base/comms-utils.js +91 -0
  45. package/lib/ble/{ble-device.d.ts → base/comms.d.ts} +31 -16
  46. package/lib/ble/{ble-device.js → base/comms.js} +193 -55
  47. package/lib/ble/bindings/index.d.ts +2 -0
  48. package/lib/ble/bindings/index.js +8 -0
  49. package/lib/ble/bindings/linux.d.ts +15 -0
  50. package/lib/ble/bindings/linux.js +39 -0
  51. package/lib/ble/bindings/mock.d.ts +9 -0
  52. package/lib/ble/bindings/mock.js +108 -0
  53. package/lib/ble/bindings/types.d.ts +57 -0
  54. package/lib/ble/bindings/types.js +96 -0
  55. package/lib/ble/ble-interface.d.ts +38 -59
  56. package/lib/ble/ble-interface.js +254 -462
  57. package/lib/ble/ble-peripheral.d.ts +7 -5
  58. package/lib/ble/ble-peripheral.js +52 -21
  59. package/lib/ble/consts.d.ts +3 -0
  60. package/lib/ble/consts.js +4 -1
  61. package/lib/ble/cp/adapter.d.ts +22 -0
  62. package/lib/ble/cp/adapter.js +111 -0
  63. package/lib/ble/cp/comm.d.ts +31 -0
  64. package/lib/ble/cp/comm.js +125 -0
  65. package/lib/ble/cp/index.d.ts +4 -0
  66. package/lib/ble/cp/index.js +10 -0
  67. package/lib/ble/cp/types.d.ts +13 -0
  68. package/lib/ble/cp/types.js +2 -0
  69. package/lib/ble/elite/adapter.d.ts +19 -0
  70. package/lib/ble/elite/adapter.js +120 -0
  71. package/lib/ble/elite/comms.d.ts +32 -0
  72. package/lib/ble/elite/comms.js +126 -0
  73. package/lib/ble/elite/index.d.ts +3 -0
  74. package/lib/ble/elite/index.js +10 -0
  75. package/lib/ble/fm/adapter.d.ts +22 -0
  76. package/lib/ble/fm/adapter.js +249 -0
  77. package/lib/ble/fm/comms.d.ts +50 -0
  78. package/lib/ble/{fm.js → fm/comms.js} +34 -277
  79. package/lib/ble/fm/consts.d.ts +6 -0
  80. package/lib/ble/fm/consts.js +9 -0
  81. package/lib/ble/fm/index.d.ts +5 -0
  82. package/lib/ble/fm/index.js +13 -0
  83. package/lib/ble/fm/types.d.ts +30 -0
  84. package/lib/ble/fm/types.js +2 -0
  85. package/lib/ble/hr/adapter.d.ts +14 -0
  86. package/lib/ble/hr/adapter.js +45 -0
  87. package/lib/ble/hr/comm.d.ts +19 -0
  88. package/lib/ble/hr/comm.js +56 -0
  89. package/lib/ble/hr/index.d.ts +4 -0
  90. package/lib/ble/hr/index.js +10 -0
  91. package/lib/ble/hr/mock.d.ts +7 -0
  92. package/lib/ble/hr/mock.js +47 -0
  93. package/lib/ble/hr/types.d.ts +5 -0
  94. package/lib/ble/hr/types.js +2 -0
  95. package/lib/ble/index.d.ts +9 -0
  96. package/lib/ble/index.js +26 -0
  97. package/lib/ble/peripheral-cache.d.ts +43 -0
  98. package/lib/ble/peripheral-cache.js +107 -0
  99. package/lib/ble/tacx/adapter.d.ts +10 -0
  100. package/lib/ble/tacx/adapter.js +99 -0
  101. package/lib/ble/{tacx.d.ts → tacx/comms.d.ts} +11 -43
  102. package/lib/ble/{tacx.js → tacx/comms.js} +23 -155
  103. package/lib/ble/tacx/index.d.ts +4 -0
  104. package/lib/ble/tacx/index.js +10 -0
  105. package/lib/ble/tacx/types.d.ts +25 -0
  106. package/lib/ble/tacx/types.js +2 -0
  107. package/lib/ble/types.d.ts +128 -0
  108. package/lib/ble/types.js +2 -0
  109. package/lib/ble/utils.d.ts +17 -0
  110. package/lib/ble/utils.js +54 -0
  111. package/lib/ble/wahoo/adapter.d.ts +9 -0
  112. package/lib/ble/wahoo/adapter.js +95 -0
  113. package/lib/ble/{wahoo-kickr.d.ts → wahoo/comms.d.ts} +11 -44
  114. package/lib/ble/{wahoo-kickr.js → wahoo/comms.js} +29 -129
  115. package/lib/ble/wahoo/index.d.ts +4 -0
  116. package/lib/ble/wahoo/index.js +10 -0
  117. package/lib/ble/wahoo/types.d.ts +19 -0
  118. package/lib/ble/wahoo/types.js +2 -0
  119. package/lib/index.d.ts +15 -0
  120. package/lib/index.js +34 -0
  121. package/lib/interfaces.d.ts +7 -0
  122. package/lib/interfaces.js +27 -0
  123. package/lib/{ble → modes}/ble-erg-mode.d.ts +4 -4
  124. package/lib/{ble → modes}/ble-erg-mode.js +2 -2
  125. package/lib/{ble → modes}/ble-st-mode.d.ts +3 -3
  126. package/lib/{ble → modes}/ble-st-mode.js +1 -1
  127. package/lib/{cycling-mode.d.ts → modes/cycling-mode.d.ts} +8 -8
  128. package/lib/modes/power-base.d.ts +3 -3
  129. package/lib/modes/power-base.js +8 -4
  130. package/lib/modes/power-meter.d.ts +3 -3
  131. package/lib/modes/simulator.d.ts +2 -2
  132. package/lib/modes/simulator.js +1 -1
  133. package/lib/serial/adapter-factory.d.ts +12 -0
  134. package/lib/serial/adapter-factory.js +30 -0
  135. package/lib/serial/adapter.d.ts +21 -0
  136. package/lib/serial/adapter.js +61 -0
  137. package/lib/serial/bindings/tcp.d.ts +45 -0
  138. package/lib/serial/bindings/tcp.js +284 -0
  139. package/lib/serial/comm.d.ts +7 -0
  140. package/lib/serial/comm.js +2 -0
  141. package/lib/{daum → serial/daum}/DaumAdapter.d.ts +14 -23
  142. package/lib/{daum → serial/daum}/DaumAdapter.js +57 -97
  143. package/lib/{daum → serial/daum}/DaumPowerMeterCyclingMode.d.ts +2 -2
  144. package/lib/{daum → serial/daum}/DaumPowerMeterCyclingMode.js +1 -1
  145. package/lib/{daum → serial/daum}/ERGCyclingMode.d.ts +3 -3
  146. package/lib/{daum → serial/daum}/ERGCyclingMode.js +3 -3
  147. package/lib/{daum → serial/daum}/SmartTrainerCyclingMode.d.ts +3 -3
  148. package/lib/{daum → serial/daum}/SmartTrainerCyclingMode.js +4 -3
  149. package/lib/serial/daum/classic/adapter.d.ts +29 -0
  150. package/lib/{daum/classic/DaumClassicAdapter.js → serial/daum/classic/adapter.js} +77 -50
  151. package/lib/{daum/classic/bike.d.ts → serial/daum/classic/comms.d.ts} +14 -16
  152. package/lib/{daum/classic/bike.js → serial/daum/classic/comms.js} +68 -152
  153. package/lib/serial/daum/classic/mock.d.ts +96 -0
  154. package/lib/serial/daum/classic/mock.js +365 -0
  155. package/lib/{daum/classic/DaumClassicCyclingMode.d.ts → serial/daum/classic/modes/daum-classic.d.ts} +3 -3
  156. package/lib/{daum/classic/DaumClassicCyclingMode.js → serial/daum/classic/modes/daum-classic.js} +2 -2
  157. package/lib/{daum → serial/daum}/classic/utils.d.ts +1 -1
  158. package/lib/serial/daum/premium/adapter.d.ts +25 -0
  159. package/lib/{daum/premium/DaumPremiumAdapter.js → serial/daum/premium/adapter.js} +107 -47
  160. package/lib/{daum/premium/bike.d.ts → serial/daum/premium/comms.d.ts} +26 -54
  161. package/lib/{daum/premium/bike.js → serial/daum/premium/comms.js} +215 -420
  162. package/lib/serial/daum/premium/mock.d.ts +75 -0
  163. package/lib/serial/daum/premium/mock.js +289 -0
  164. package/lib/{daum/premium/DaumClassicCyclingMode.d.ts → serial/daum/premium/modes/daum-classic.d.ts} +3 -3
  165. package/lib/{daum/premium/DaumClassicCyclingMode.js → serial/daum/premium/modes/daum-classic.js} +2 -2
  166. package/lib/serial/daum/premium/types.d.ts +12 -0
  167. package/lib/serial/daum/premium/types.js +2 -0
  168. package/lib/{daum → serial/daum}/premium/utils.d.ts +8 -11
  169. package/lib/{daum → serial/daum}/premium/utils.js +22 -63
  170. package/lib/serial/index.d.ts +9 -0
  171. package/lib/serial/index.js +49 -0
  172. package/lib/{kettler → serial/kettler}/comms.d.ts +8 -6
  173. package/lib/{kettler → serial/kettler}/comms.js +71 -32
  174. package/lib/{kettler → serial/kettler}/ergo-racer/adapter.d.ts +14 -28
  175. package/lib/{kettler → serial/kettler}/ergo-racer/adapter.js +102 -169
  176. package/lib/serial/kettler/ergo-racer/mock.d.ts +66 -0
  177. package/lib/serial/kettler/ergo-racer/mock.js +216 -0
  178. package/lib/{kettler/ergo-racer/ERGCyclingMode.d.ts → serial/kettler/ergo-racer/modes/erg.d.ts} +4 -4
  179. package/lib/{kettler/ergo-racer/ERGCyclingMode.js → serial/kettler/ergo-racer/modes/erg.js} +2 -2
  180. package/lib/serial/serial-interface.d.ts +60 -0
  181. package/lib/serial/serial-interface.js +309 -0
  182. package/lib/simulator/Simulator.d.ts +12 -25
  183. package/lib/simulator/Simulator.js +26 -87
  184. package/lib/types/adapter.d.ts +39 -0
  185. package/lib/types/adapter.js +2 -0
  186. package/lib/types/capabilities.d.ts +8 -0
  187. package/lib/types/capabilities.js +12 -0
  188. package/lib/types/data.d.ts +12 -0
  189. package/lib/types/data.js +2 -0
  190. package/lib/types/device.d.ts +32 -0
  191. package/lib/types/device.js +11 -0
  192. package/lib/types/interface.d.ts +18 -0
  193. package/lib/types/interface.js +2 -0
  194. package/lib/types/route.d.ts +2 -2
  195. package/lib/types/user.d.ts +1 -1
  196. package/lib/{utils.d.ts → utils/utils.d.ts} +1 -0
  197. package/lib/{utils.js → utils/utils.js} +5 -1
  198. package/package.json +16 -14
  199. package/lib/DeviceSupport.d.ts +0 -36
  200. package/lib/DeviceSupport.js +0 -82
  201. package/lib/ant/AntAdapter.d.ts +0 -50
  202. package/lib/ant/AntAdapter.js +0 -109
  203. package/lib/ant/AntScanner.d.ts +0 -60
  204. package/lib/ant/AntScanner.js +0 -651
  205. package/lib/ant/antfe/AntFEAdapter.d.ts +0 -83
  206. package/lib/ant/antfe/AntFEAdapter.js +0 -652
  207. package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +0 -9
  208. package/lib/ant/antfe/ant-fe-erg-mode.d.ts +0 -6
  209. package/lib/ant/antfe/ant-fe-st-mode.d.ts +0 -5
  210. package/lib/ant/anthrm/AntHrmAdapter.d.ts +0 -16
  211. package/lib/ant/anthrm/AntHrmAdapter.js +0 -130
  212. package/lib/ant/antpwr/pwr-adapter.d.ts +0 -49
  213. package/lib/ant/antpwr/pwr-adapter.js +0 -251
  214. package/lib/ant/utils.d.ts +0 -1
  215. package/lib/ant/utils.js +0 -23
  216. package/lib/antv2/AntAdapter.d.ts +0 -48
  217. package/lib/antv2/AntAdapter.js +0 -104
  218. package/lib/antv2/ant-device.d.ts +0 -59
  219. package/lib/antv2/ant-device.js +0 -161
  220. package/lib/antv2/fe.d.ts +0 -32
  221. package/lib/antv2/hr.d.ts +0 -18
  222. package/lib/antv2/hr.js +0 -102
  223. package/lib/antv2/incyclist-protocol.d.ts +0 -37
  224. package/lib/antv2/incyclist-protocol.js +0 -126
  225. package/lib/antv2/pwr.d.ts +0 -28
  226. package/lib/antv2/pwr.js +0 -171
  227. package/lib/ble/ble.d.ts +0 -129
  228. package/lib/ble/ble.js +0 -86
  229. package/lib/ble/elite.d.ts +0 -90
  230. package/lib/ble/elite.js +0 -322
  231. package/lib/ble/fm.d.ts +0 -125
  232. package/lib/ble/hrm.d.ts +0 -48
  233. package/lib/ble/hrm.js +0 -134
  234. package/lib/ble/incyclist-protocol.d.ts +0 -31
  235. package/lib/ble/incyclist-protocol.js +0 -153
  236. package/lib/ble/pwr.d.ts +0 -89
  237. package/lib/ble/pwr.js +0 -322
  238. package/lib/daum/classic/DaumClassicAdapter.d.ts +0 -28
  239. package/lib/daum/classic/DaumClassicProtocol.d.ts +0 -27
  240. package/lib/daum/classic/DaumClassicProtocol.js +0 -185
  241. package/lib/daum/premium/DaumPremiumAdapter.d.ts +0 -16
  242. package/lib/daum/premium/DaumPremiumProtocol.d.ts +0 -32
  243. package/lib/daum/premium/DaumPremiumProtocol.js +0 -207
  244. package/lib/daum/premium/tcpserial.d.ts +0 -33
  245. package/lib/daum/premium/tcpserial.js +0 -123
  246. package/lib/device.d.ts +0 -94
  247. package/lib/device.js +0 -76
  248. package/lib/kettler/ergo-racer/protocol.d.ts +0 -41
  249. package/lib/kettler/ergo-racer/protocol.js +0 -203
  250. package/lib/protocol.d.ts +0 -74
  251. package/lib/protocol.js +0 -41
  252. package/lib/registry.d.ts +0 -8
  253. package/lib/registry.js +0 -33
  254. package/lib/serialport/bindings/tcp.d.ts +0 -20
  255. package/lib/serialport/bindings/tcp.js +0 -33
  256. package/lib/serialport/index.d.ts +0 -2
  257. package/lib/serialport/index.js +0 -29
  258. package/lib/serialport/serialport.d.ts +0 -29
  259. package/lib/serialport/serialport.js +0 -87
  260. /package/lib/{cycling-mode.js → modes/cycling-mode.js} +0 -0
  261. /package/lib/{daum → serial/daum}/classic/utils.js +0 -0
  262. /package/lib/{daum → serial/daum}/constants.d.ts +0 -0
  263. /package/lib/{daum → serial/daum}/constants.js +0 -0
  264. /package/lib/{serialport.d.ts → serial/serialport.d.ts} +0 -0
  265. /package/lib/{serialport.js → serial/serialport.js} +0 -0
  266. /package/lib/{calculations.d.ts → utils/calculations.d.ts} +0 -0
  267. /package/lib/{calculations.js → utils/calculations.js} +0 -0
@@ -8,29 +8,36 @@ 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
- exports.BleDevice = void 0;
15
+ exports.BleComms = void 0;
16
+ const events_1 = __importDefault(require("events"));
13
17
  const gd_eventlog_1 = require("gd-eventlog");
14
- const ble_1 = require("./ble");
15
- const ble_2 = require("./ble");
18
+ const utils_1 = require("../../utils/utils");
19
+ const ble_interface_1 = __importDefault(require("../ble-interface"));
20
+ const utils_2 = require("../utils");
16
21
  const CONNECT_WAIT_TIMEOUT = 10000;
17
22
  const BLE_TIMEOUT = 1000;
18
- class BleDevice extends ble_1.BleDeviceClass {
23
+ class BleComms extends events_1.default {
19
24
  constructor(props) {
20
25
  super();
21
26
  this.characteristics = [];
22
27
  this.deviceInfo = {};
28
+ this.connectState = { isConnecting: false, isConnected: false, isDisconnecting: false };
23
29
  this.id = props.id;
24
30
  this.address = props.address;
25
31
  this.name = props.name;
26
32
  this.services = props.services;
27
- this.ble = props.ble;
33
+ this.ble = ble_interface_1.default.getInstance();
28
34
  this.characteristics = [];
29
35
  this.subscribedCharacteristics = [];
30
36
  this.isInitialized = false;
31
37
  this.writeQueue = [];
32
38
  this.workerIv = null;
33
39
  this.prevMessages = [];
40
+ this.paused = false;
34
41
  if (props.peripheral) {
35
42
  const { id, address, advertisement, state } = props.peripheral;
36
43
  this.peripheral = props.peripheral;
@@ -47,10 +54,37 @@ class BleDevice extends ble_1.BleDeviceClass {
47
54
  this.logger = new gd_eventlog_1.EventLogger('BleDevice');
48
55
  }
49
56
  }
57
+ getConnectState() {
58
+ return this.connectState;
59
+ }
60
+ isConnected() {
61
+ return this.connectState.isConnected;
62
+ }
63
+ pause() {
64
+ this.paused = true;
65
+ }
66
+ resume() {
67
+ this.paused = false;
68
+ }
69
+ getServiceUUids() {
70
+ throw new Error("Method not implemented.");
71
+ }
72
+ getProfile() {
73
+ throw new Error("Method not implemented.");
74
+ }
75
+ getProtocol() {
76
+ throw new Error("Method not implemented.");
77
+ }
78
+ getSettings() {
79
+ const { id, address, name } = this;
80
+ return { id, name, address, interface: 'ble', protocol: this.getProtocol() };
81
+ }
50
82
  getServices() {
51
83
  return this.services;
52
84
  }
53
85
  logEvent(event) {
86
+ if (this.paused)
87
+ return;
54
88
  if (this.logger) {
55
89
  this.logger.logEvent(event);
56
90
  }
@@ -64,33 +98,49 @@ class BleDevice extends ble_1.BleDeviceClass {
64
98
  setInterface(ble) {
65
99
  this.ble = ble;
66
100
  }
67
- isMatching(characteristics) {
101
+ static isMatching(characteristics) {
68
102
  return true;
69
103
  }
104
+ reset() {
105
+ if (this.connectState.isConnecting)
106
+ this.ble.stopConnectSensor();
107
+ }
70
108
  cleanupListeners() {
71
109
  if (this.characteristics === undefined) {
72
110
  this.characteristics = [];
73
111
  }
74
112
  else {
75
- const connector = this.ble.getConnector(this.peripheral);
76
- this.characteristics.forEach(c => {
77
- connector.removeAllListeners((0, ble_1.uuid)(c.uuid));
78
- });
113
+ if (this.peripheral) {
114
+ const connector = this.ble.peripheralCache.getConnector(this.peripheral);
115
+ this.characteristics.forEach(c => {
116
+ connector.removeAllListeners((0, utils_2.uuid)(c.uuid));
117
+ });
118
+ }
119
+ else {
120
+ this.characteristics = [];
121
+ }
79
122
  }
80
123
  }
81
124
  onDisconnect() {
82
- this.logEvent({ message: 'device disconnected', address: this.address, profile: this.getProfile() });
83
- this.state = "disconnected";
84
- if (!this.connectState.isDisconnecting) {
85
- this.peripheral.state = 'disconnected';
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ this.logEvent({ message: 'device disconnected', address: this.address, profile: this.getProfile() });
127
+ this.state = "disconnected";
128
+ const wasConnecting = this.connectState.isConnecting;
86
129
  this.connectState.isConnecting = false;
87
130
  this.connectState.isConnected = false;
88
- this.cleanupListeners();
89
- this.subscribedCharacteristics = [];
90
- this.ble.onDisconnect(this.peripheral);
91
- this.connect({ reconnect: true });
92
- }
93
- this.emit('disconnected');
131
+ if (!this.connectState.isDisconnecting) {
132
+ this.peripheral.state = 'disconnected';
133
+ this.cleanupListeners();
134
+ this.subscribedCharacteristics = [];
135
+ this.ble.onDisconnect(this.peripheral);
136
+ if (wasConnecting) {
137
+ this.emit('connection-failed');
138
+ }
139
+ else {
140
+ this.connect({ reconnect: true });
141
+ }
142
+ }
143
+ });
94
144
  }
95
145
  waitForConnectFinished(timeout) {
96
146
  const waitStart = Date.now();
@@ -108,13 +158,13 @@ class BleDevice extends ble_1.BleDeviceClass {
108
158
  }
109
159
  }
110
160
  catch (err) {
111
- console.log('~~~ error', err);
161
+ this.logEvent({ message: 'error', fn: '', error: err.message, stack: err.stack });
112
162
  }
113
163
  }, 100);
114
164
  });
115
165
  }
116
166
  hasService(serviceUuid) {
117
- return this.services && this.services.find(s => s === serviceUuid || (0, ble_1.uuid)(serviceUuid)) !== undefined;
167
+ return this.services && this.services.find(s => s === serviceUuid || (0, utils_2.uuid)(serviceUuid)) !== undefined;
118
168
  }
119
169
  init() {
120
170
  return __awaiter(this, void 0, void 0, function* () {
@@ -133,27 +183,89 @@ class BleDevice extends ble_1.BleDeviceClass {
133
183
  connectPeripheral(peripheral) {
134
184
  return __awaiter(this, void 0, void 0, function* () {
135
185
  this.connectState.isConnecting = true;
186
+ let disconnectSignalled = false;
187
+ return new Promise((done) => __awaiter(this, void 0, void 0, function* () {
188
+ try {
189
+ const connector = this.ble.peripheralCache.getConnector(peripheral);
190
+ const disconnectHandler = () => {
191
+ this.logEvent({ message: 'device disconnected', address: this.address, profile: this.getProfile() });
192
+ this.state = "disconnected";
193
+ disconnectSignalled = true;
194
+ this.cleanupListeners();
195
+ this.subscribedCharacteristics = [];
196
+ this.connectState.isConnecting = false;
197
+ this.connectState.isConnected = false;
198
+ this.ble.onDisconnect(this.peripheral);
199
+ done(false);
200
+ };
201
+ connector.removeAllListeners('disconnect');
202
+ connector.once('disconnect', disconnectHandler);
203
+ yield connector.connect();
204
+ if (disconnectSignalled)
205
+ return;
206
+ yield connector.initialize();
207
+ if (disconnectSignalled)
208
+ return;
209
+ yield this.subscribeAll(connector);
210
+ if (disconnectSignalled)
211
+ return;
212
+ this.connectState.isConnected = true;
213
+ this.state = "connected";
214
+ this.emit('connected');
215
+ connector.removeAllListeners('disconnect');
216
+ connector.on('disconnect', this.onDisconnect.bind(this));
217
+ const success = yield this.init();
218
+ done(success);
219
+ return;
220
+ }
221
+ catch (err) {
222
+ this.logEvent({ message: 'Error', fn: 'connectPeripheral()', error: err.message, stack: err.stack });
223
+ }
224
+ this.connectState.isConnecting = false;
225
+ if (disconnectSignalled)
226
+ return;
227
+ done(true);
228
+ }));
229
+ });
230
+ }
231
+ subscribeMultiple(characteristics, conn) {
232
+ return new Promise(resolve => {
136
233
  try {
137
- const connector = this.ble.getConnector(peripheral);
138
- connector.on('disconnect', () => { this.onDisconnect(); });
139
- yield connector.connect();
140
- yield connector.initialize();
141
- yield this.subscribeAll(connector);
142
- this.connectState.isConnected = true;
143
- this.state = "connected";
144
- this.emit('connected');
145
- yield this.init();
234
+ const connector = conn || this.ble.peripheralCache.getConnector(this.peripheral);
235
+ const subscribeSingle = (c) => {
236
+ connector.removeAllListeners(c);
237
+ connector.on(c, (uuid, data) => {
238
+ this.onData(uuid, data);
239
+ });
240
+ return connector.subscribe(c).then(res => {
241
+ this.subscribedCharacteristics.push(c);
242
+ return res;
243
+ }).catch(err => {
244
+ this.logEvent({ message: 'subscription failed', characteristic: c, error: err.message });
245
+ });
246
+ };
247
+ const promises = characteristics
248
+ .filter(c => {
249
+ if (this.characteristics) {
250
+ const existing = this.characteristics.find(rc => rc.uuid === c || (0, utils_2.uuid)(rc.uuid) === c);
251
+ if (!existing)
252
+ return false;
253
+ }
254
+ const isAlreadySubscribed = connector.isSubscribed(c);
255
+ return !isAlreadySubscribed;
256
+ })
257
+ .map(c => subscribeSingle(c));
258
+ Promise.all(promises).then(() => resolve());
146
259
  }
147
260
  catch (err) {
148
- this.logEvent({ message: 'Error', fn: 'connectPeripheral()', error: err.message, stack: err.stack });
261
+ this.logEvent({ message: 'Error', fn: 'subscribeMultiple()', error: err.message, stack: err.stack });
149
262
  }
150
- this.connectState.isConnecting = false;
151
263
  });
152
264
  }
153
265
  subscribeAll(conn) {
154
266
  return __awaiter(this, void 0, void 0, function* () {
155
267
  try {
156
- const connector = conn || this.ble.getConnector(this.peripheral);
268
+ const connector = conn || this.ble.peripheralCache.getConnector(this.peripheral);
157
269
  const subscribed = yield connector.subscribeAll((uuid, data) => { this.onData(uuid, data); });
158
270
  subscribed.forEach(c => this.subscribedCharacteristics.push(c));
159
271
  }
@@ -162,11 +274,28 @@ class BleDevice extends ble_1.BleDeviceClass {
162
274
  }
163
275
  });
164
276
  }
277
+ unsubscribeAll(conn) {
278
+ const connector = conn || this.ble.peripheralCache.getConnector(this.peripheral);
279
+ connector.unsubscribeAll();
280
+ }
165
281
  connect(props) {
166
282
  return __awaiter(this, void 0, void 0, function* () {
283
+ if (!this.ble.isConnected()) {
284
+ try {
285
+ yield this.ble.connect();
286
+ if (!this.ble.isConnected())
287
+ return false;
288
+ }
289
+ catch (err) {
290
+ return false;
291
+ }
292
+ }
293
+ yield (0, utils_1.sleep)(Math.random() * 100);
294
+ yield this.ble.waitForSensorConnectionFinish();
295
+ this.ble.startConnectSensor();
167
296
  const { reconnect } = props || {};
168
297
  try {
169
- this.logEvent({ message: reconnect ? 'reconnect' : 'connect', address: this.peripheral ? this.peripheral.address : this.address, state: this.connectState });
298
+ this.logEvent({ message: reconnect ? 'reconnect' : 'connect', name: this.name, id: this.id, address: this.peripheral ? this.peripheral.address : this.address, state: this.connectState });
170
299
  if (!reconnect && this.connectState.isConnecting) {
171
300
  yield this.waitForConnectFinished(CONNECT_WAIT_TIMEOUT);
172
301
  }
@@ -177,27 +306,32 @@ class BleDevice extends ble_1.BleDeviceClass {
177
306
  }
178
307
  catch (err) {
179
308
  this.logEvent({ message: 'cannot reconnect', error: err.message || err });
309
+ this.ble.stopConnectSensor();
180
310
  return false;
181
311
  }
312
+ this.ble.stopConnectSensor();
182
313
  return true;
183
314
  }
184
315
  this.connectState.isConnecting = true;
185
316
  if (!this.peripheral) {
186
317
  const { id, name, address } = this;
187
318
  try {
188
- this.peripheral = this.ble.findPeripheral({ id, name, address });
319
+ this.peripheral = this.ble.peripheralCache.getPeripheral({ id, name, address });
189
320
  }
190
321
  catch (err) {
191
- console.log('~~~ error', err);
322
+ this.logEvent({ message: 'error', fn: 'connect()', error: err.message, stack: err.stack });
192
323
  }
193
324
  }
194
325
  if (this.peripheral) {
195
326
  const { id, address, advertisement } = this.peripheral;
196
327
  const name = advertisement === null || advertisement === void 0 ? void 0 : advertisement.localName;
197
328
  this.logEvent({ message: 'connect requested', mode: 'peripheral', device: { id, name, address: address } });
198
- yield this.connectPeripheral(this.peripheral);
199
- this.logEvent({ message: 'connect result: success', mode: 'peripheral', device: { id, name, address } });
200
- return true;
329
+ const connected = yield this.connectPeripheral(this.peripheral);
330
+ this.logEvent({ message: 'connect result: ', connected, mode: 'peripheral', device: { id, name, address } });
331
+ this.connectState.isConnecting = false;
332
+ this.connectState.isConnected = connected;
333
+ this.ble.stopConnectSensor();
334
+ return connected;
201
335
  }
202
336
  else {
203
337
  const { id, name, address } = this;
@@ -208,24 +342,26 @@ class BleDevice extends ble_1.BleDeviceClass {
208
342
  if (this.ble.isScanning()) {
209
343
  yield this.ble.stopScan();
210
344
  }
211
- const devices = yield this.ble.scan({ requested: this });
212
- if (devices && devices.length > 0) {
213
- this.peripheral = devices[0].peripheral;
214
- yield this.connectPeripheral(this.peripheral);
215
- this.logEvent({ message: 'connect result: success', mode: 'device', device: { id, name, address } });
345
+ const peripheral = yield this.ble.scanForDevice(this, {}).catch(() => null);
346
+ if (peripheral) {
347
+ this.peripheral = peripheral;
348
+ const connected = yield this.connectPeripheral(this.peripheral);
349
+ this.logEvent({ message: 'connect result: ', connected, mode: 'device', device: { id, name, address } });
216
350
  this.connectState.isConnecting = false;
217
- this.connectState.isConnected = true;
218
- return true;
351
+ this.connectState.isConnected = connected;
352
+ this.ble.stopConnectSensor();
353
+ return connected;
219
354
  }
220
355
  }
221
356
  catch (err) {
222
- console.log('~~~ error', err);
357
+ console.log('~~~ ERROR', err);
223
358
  error = err;
224
359
  }
225
360
  }
226
361
  this.logEvent({ message: 'connect result: failure', mode: 'device', device: { id, name, address }, error: error.message, stack: error.stack });
227
362
  this.connectState.isConnecting = false;
228
363
  this.connectState.isConnected = false;
364
+ this.ble.stopConnectSensor();
229
365
  return false;
230
366
  }
231
367
  }
@@ -233,6 +369,7 @@ class BleDevice extends ble_1.BleDeviceClass {
233
369
  this.connectState.isConnecting = false;
234
370
  this.connectState.isConnected = false;
235
371
  this.logEvent({ message: 'connect result: error', error: err.message });
372
+ this.ble.stopConnectSensor();
236
373
  return false;
237
374
  }
238
375
  });
@@ -261,8 +398,8 @@ class BleDevice extends ble_1.BleDeviceClass {
261
398
  return true;
262
399
  }
263
400
  if (this.connectState.isConnected) {
264
- this.ble.removeConnectedDevice(this);
265
401
  this.cleanupListeners();
402
+ this.unsubscribeAll();
266
403
  this.logEvent({ message: 'disconnect result: success', device: { id, name, address } });
267
404
  this.connectState.isDisconnecting = false;
268
405
  this.connectState.isConnecting = false;
@@ -296,7 +433,7 @@ class BleDevice extends ble_1.BleDeviceClass {
296
433
  }
297
434
  this.logEvent({ message: 'got data', characteristic, data: data.toString('hex'), writeQueue: this.writeQueue.length });
298
435
  if (this.writeQueue.length > 0) {
299
- const writeIdx = this.writeQueue.findIndex(i => (0, ble_2.matches)(i.uuid, characteristic));
436
+ const writeIdx = this.writeQueue.findIndex(i => (0, utils_2.matches)(i.uuid, characteristic));
300
437
  if (writeIdx !== -1) {
301
438
  const writeItem = this.writeQueue[writeIdx];
302
439
  this.writeQueue.splice(writeIdx, 1);
@@ -342,13 +479,13 @@ class BleDevice extends ble_1.BleDeviceClass {
342
479
  throw new Error('not connected');
343
480
  try {
344
481
  const { withoutResponse, timeout } = props || {};
345
- const connector = this.ble.getConnector(this.peripheral);
482
+ const connector = this.ble.peripheralCache.getConnector(this.peripheral);
346
483
  const isAlreadySubscribed = connector.isSubscribed(characteristicUuid);
347
484
  if (!withoutResponse && !this.workerIv) {
348
485
  this.startWorker();
349
486
  }
350
487
  if (!withoutResponse && !isAlreadySubscribed) {
351
- const connector = this.ble.getConnector(this.peripheral);
488
+ const connector = this.ble.peripheralCache.getConnector(this.peripheral);
352
489
  connector.removeAllListeners(characteristicUuid);
353
490
  connector.on(characteristicUuid, (uuid, data) => {
354
491
  this.onData(uuid, data);
@@ -358,7 +495,7 @@ class BleDevice extends ble_1.BleDeviceClass {
358
495
  this.subscribedCharacteristics.push(characteristicUuid);
359
496
  }
360
497
  return new Promise((resolve, reject) => {
361
- const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, ble_1.uuid)(c.uuid) === characteristicUuid);
498
+ const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, utils_2.uuid)(c.uuid) === characteristicUuid);
362
499
  if (!characteristic) {
363
500
  reject(new Error('Characteristic not found'));
364
501
  return;
@@ -402,7 +539,7 @@ class BleDevice extends ble_1.BleDeviceClass {
402
539
  return new Promise((resolve, reject) => {
403
540
  if (!this.isConnected())
404
541
  return reject(new Error('not connected'));
405
- const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, ble_1.uuid)(c.uuid) === characteristicUuid);
542
+ const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, utils_2.uuid)(c.uuid) === characteristicUuid);
406
543
  if (!characteristic) {
407
544
  reject(new Error('Characteristic not found'));
408
545
  return;
@@ -441,4 +578,5 @@ class BleDevice extends ble_1.BleDeviceClass {
441
578
  });
442
579
  }
443
580
  }
444
- exports.BleDevice = BleDevice;
581
+ exports.BleComms = BleComms;
582
+ BleComms.services = [];
@@ -0,0 +1,2 @@
1
+ import MockBinding from './mock';
2
+ export { MockBinding };
@@ -0,0 +1,8 @@
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
+ exports.MockBinding = void 0;
7
+ const mock_1 = __importDefault(require("./mock"));
8
+ exports.MockBinding = mock_1.default;
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { BleBinding, BleInterfaceState } from '../types';
4
+ export declare class BleLinuxBinding extends EventEmitter implements BleBinding {
5
+ static _instance: any;
6
+ _bindings: any;
7
+ state: BleInterfaceState;
8
+ constructor();
9
+ static getInstance(): any;
10
+ startScanning(serviceUUIDs?: string[] | undefined, allowDuplicates?: boolean | undefined, callback?: ((error?: Error | undefined) => void) | undefined): void;
11
+ stopScanning(callback?: (() => void) | undefined): void;
12
+ on(eventName: string | symbol, listener: (...args: any[]) => void): this;
13
+ }
14
+ declare const Binding: any;
15
+ export default Binding;
@@ -0,0 +1,39 @@
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
+ exports.BleLinuxBinding = void 0;
7
+ const events_1 = __importDefault(require("events"));
8
+ class BleLinuxBinding extends events_1.default {
9
+ constructor() {
10
+ super();
11
+ this.state = 'unknown';
12
+ this._bindings = this;
13
+ }
14
+ static getInstance() {
15
+ if (!BleLinuxBinding._instance) {
16
+ BleLinuxBinding._instance = new BleLinuxBinding();
17
+ }
18
+ return BleLinuxBinding._instance;
19
+ }
20
+ startScanning(serviceUUIDs, allowDuplicates, callback) {
21
+ throw new Error('Method not implemented.');
22
+ }
23
+ stopScanning(callback) {
24
+ throw new Error('Method not implemented.');
25
+ }
26
+ on(eventName, listener) {
27
+ super.addListener(eventName, listener);
28
+ if (eventName === 'stateChange') {
29
+ setTimeout(() => {
30
+ this.state = 'poweredOn';
31
+ this.emit('stateChange', this.state);
32
+ }, 100);
33
+ }
34
+ return this;
35
+ }
36
+ }
37
+ exports.BleLinuxBinding = BleLinuxBinding;
38
+ const Binding = BleLinuxBinding.getInstance();
39
+ exports.default = Binding;
@@ -0,0 +1,9 @@
1
+ import { PrimaryService } from './types';
2
+ export type BleMockPeripheral = {
3
+ services: PrimaryService[];
4
+ id: string;
5
+ name: string;
6
+ address: string;
7
+ };
8
+ declare const mock: any;
9
+ export default mock;
@@ -0,0 +1,108 @@
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 utils_1 = require("../../utils/utils");
17
+ class Binding extends events_1.default {
18
+ constructor() {
19
+ super();
20
+ this._bindings = this;
21
+ this.state = 'unknown';
22
+ this.channelState = 'IDLE';
23
+ this.peripherals = [];
24
+ }
25
+ static getInstance() {
26
+ if (!Binding._instance) {
27
+ Binding._instance = new Binding();
28
+ }
29
+ return Binding._instance;
30
+ }
31
+ init() { }
32
+ addMock(peripheral) {
33
+ this.peripherals.push(peripheral);
34
+ }
35
+ reset() {
36
+ this.peripherals = [];
37
+ this.state = 'unknown';
38
+ this.channelState = 'IDLE';
39
+ }
40
+ on(eventName, listener) {
41
+ super.addListener(eventName, listener);
42
+ if (eventName === 'stateChange') {
43
+ setTimeout(() => {
44
+ this.state = 'poweredOn';
45
+ this.emit('stateChange', this.state);
46
+ }, 100);
47
+ }
48
+ return this;
49
+ }
50
+ startScanning(serviceUUIDs, allowDuplicates, callback) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ this.channelState = 'SCANNING';
53
+ if (callback)
54
+ callback();
55
+ yield (0, utils_1.sleep)(50);
56
+ this.peripherals.forEach(p => {
57
+ this.emit('discover', new MockPeripheral(p));
58
+ });
59
+ });
60
+ }
61
+ stopScanning(callback) {
62
+ this.channelState = 'IDLE';
63
+ if (callback)
64
+ callback();
65
+ }
66
+ }
67
+ class MockPeripheral extends events_1.default {
68
+ constructor(p) {
69
+ super();
70
+ this.id = p.id;
71
+ this.name = p.name;
72
+ this.address = p.address;
73
+ this.advertisement = {
74
+ localName: p.name,
75
+ serviceUuids: p.services.map(s => s.uuid)
76
+ };
77
+ this.services = p.services;
78
+ }
79
+ connectAsync() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ });
82
+ }
83
+ disconnect(cb) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ this.removeAllListeners();
86
+ });
87
+ }
88
+ discoverSomeServicesAndCharacteristicsAsync(serviceUUIDs, characteristicUUIDs) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ const characteristics = [];
91
+ this.services.forEach(s => {
92
+ if (serviceUUIDs && serviceUUIDs.length > 0 && !serviceUUIDs.includes(s.uuid))
93
+ return;
94
+ s.characteristics.forEach(c => {
95
+ var _a, _b;
96
+ if (characteristicUUIDs && characteristicUUIDs.length > 0 && !characteristicUUIDs.includes(c.uuid))
97
+ return;
98
+ c._serviceUuid = s.uuid;
99
+ c.name = (_b = (_a = c.descriptors) === null || _a === void 0 ? void 0 : _a.find(d => d.uuid === '2901')) === null || _b === void 0 ? void 0 : _b.value;
100
+ characteristics.push(c);
101
+ });
102
+ });
103
+ return { services: this.services, characteristics };
104
+ });
105
+ }
106
+ }
107
+ const mock = Binding.getInstance();
108
+ exports.default = mock;