brilliantsole 0.0.27 → 0.0.29

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 (202) hide show
  1. package/assets/3d/anchor.glb +0 -0
  2. package/assets/3d/coin.glb +0 -0
  3. package/assets/3d/glasses.glb +0 -0
  4. package/assets/audio/bounceMedium.wav +0 -0
  5. package/assets/audio/bounceStrong.wav +0 -0
  6. package/assets/audio/bounceWeak.wav +0 -0
  7. package/assets/audio/coin.wav +0 -0
  8. package/assets/audio/getUp.wav +0 -0
  9. package/assets/audio/grab.wav +0 -0
  10. package/assets/audio/kick.wav +0 -0
  11. package/assets/audio/platterFadeIn old.wav +0 -0
  12. package/assets/audio/platterFadeIn.wav +0 -0
  13. package/assets/audio/platterFadeOut.wav +0 -0
  14. package/assets/audio/punch.wav +0 -0
  15. package/assets/audio/punchSqueak.wav +0 -0
  16. package/assets/audio/purr.wav +0 -0
  17. package/assets/audio/purrFadeOut.wav +0 -0
  18. package/assets/audio/release.wav +0 -0
  19. package/assets/audio/splat.wav +0 -0
  20. package/assets/audio/stomp.wav +0 -0
  21. package/build/brilliantsole.cjs +3091 -741
  22. package/build/brilliantsole.cjs.map +1 -1
  23. package/build/brilliantsole.js +2759 -709
  24. package/build/brilliantsole.js.map +1 -1
  25. package/build/brilliantsole.ls.js +2602 -543
  26. package/build/brilliantsole.ls.js.map +1 -1
  27. package/build/brilliantsole.min.js +1 -1
  28. package/build/brilliantsole.min.js.map +1 -1
  29. package/build/brilliantsole.module.d.ts +295 -65
  30. package/build/brilliantsole.module.js +2749 -710
  31. package/build/brilliantsole.module.js.map +1 -1
  32. package/build/brilliantsole.module.min.d.ts +295 -65
  33. package/build/brilliantsole.module.min.js +1 -1
  34. package/build/brilliantsole.module.min.js.map +1 -1
  35. package/build/brilliantsole.node.module.d.ts +289 -62
  36. package/build/brilliantsole.node.module.js +3080 -742
  37. package/build/brilliantsole.node.module.js.map +1 -1
  38. package/build/dts/BS-output.d.ts +10 -0
  39. package/build/dts/BS.d.ts +21 -8
  40. package/build/dts/CameraManager.d.ts +72 -0
  41. package/build/dts/Device.d.ts +64 -13
  42. package/build/dts/DeviceInformationManager.d.ts +4 -4
  43. package/build/dts/DeviceManager.d.ts +2 -0
  44. package/build/dts/FileTransferManager.d.ts +18 -8
  45. package/build/dts/InformationManager.d.ts +2 -0
  46. package/build/dts/MicrophoneManager.d.ts +88 -0
  47. package/build/dts/TfliteManager.d.ts +22 -2
  48. package/build/dts/WifiManager.d.ts +61 -0
  49. package/build/dts/connection/BaseConnectionManager.d.ts +35 -3
  50. package/build/dts/connection/ClientConnectionManager.d.ts +7 -2
  51. package/build/dts/connection/bluetooth/NobleConnectionManager.d.ts +2 -1
  52. package/build/dts/connection/bluetooth/WebBluetoothConnectionManager.d.ts +1 -0
  53. package/build/dts/connection/bluetooth/bluetoothUUIDs.d.ts +2 -2
  54. package/build/dts/connection/udp/UDPConnectionManager.d.ts +28 -0
  55. package/build/dts/connection/webSocket/WebSocketConnectionManager.d.ts +25 -0
  56. package/build/dts/devicePair/DevicePair.d.ts +5 -5
  57. package/build/dts/scanner/BaseScanner.d.ts +4 -1
  58. package/build/dts/scanner/NobleScanner.d.ts +2 -1
  59. package/build/dts/sensor/MotionSensorDataManager.d.ts +5 -2
  60. package/build/dts/sensor/SensorDataManager.d.ts +5 -4
  61. package/build/dts/server/BaseClient.d.ts +5 -3
  62. package/build/dts/server/ServerUtils.d.ts +1 -1
  63. package/build/dts/server/websocket/WebSocketUtils.d.ts +1 -1
  64. package/build/dts/utils/AudioUtils.d.ts +2 -0
  65. package/build/dts/utils/Console.d.ts +2 -0
  66. package/build/dts/utils/ThrottleUtils.d.ts +2 -0
  67. package/build/dts/vibration/VibrationManager.d.ts +19 -2
  68. package/build/index.d.ts +292 -62
  69. package/build/index.node.d.ts +286 -59
  70. package/examples/3d/scene.html +19 -5
  71. package/examples/3d-generic/index.html +144 -0
  72. package/examples/3d-generic/script.js +266 -0
  73. package/examples/basic/index.html +267 -17
  74. package/examples/basic/script.js +958 -105
  75. package/examples/camera/barcode-detector.js +109 -0
  76. package/examples/camera/depth-estimation.js +71 -0
  77. package/examples/camera/face-detector.js +119 -0
  78. package/examples/camera/face-landmark.js +111 -0
  79. package/examples/camera/gesture-recognition.js +97 -0
  80. package/examples/camera/hand-landmark.js +74 -0
  81. package/examples/camera/image-segmentation.js +98 -0
  82. package/examples/camera/image-to-text.js +43 -0
  83. package/examples/camera/image-upscale.js +75 -0
  84. package/examples/camera/index.html +129 -0
  85. package/examples/camera/object-detection.js +98 -0
  86. package/examples/camera/pose-landmark.js +60 -0
  87. package/examples/camera/script.js +316 -0
  88. package/examples/camera/utils.js +165 -0
  89. package/examples/camera/yolo-tiny.js +54 -0
  90. package/examples/camera/yolo.js +119 -0
  91. package/examples/edge-impulse/script.js +157 -48
  92. package/examples/edge-impulse-test/README.md +11 -0
  93. package/examples/edge-impulse-test/edge-impulse-standalone.js +7228 -0
  94. package/examples/edge-impulse-test/edge-impulse-standalone.wasm +0 -0
  95. package/examples/edge-impulse-test/index.html +75 -0
  96. package/examples/edge-impulse-test/run-impulse.js +135 -0
  97. package/examples/edge-impulse-test/script.js +200 -0
  98. package/examples/glasses-gestures/README.md +11 -0
  99. package/examples/glasses-gestures/edge-impulse-standalone.js +7228 -0
  100. package/examples/glasses-gestures/edge-impulse-standalone.wasm +0 -0
  101. package/examples/glasses-gestures/index.html +69 -0
  102. package/examples/glasses-gestures/run-impulse.js +135 -0
  103. package/examples/glasses-gestures/script.js +226 -0
  104. package/examples/gloves/edge-impulse-standalone.js +7228 -0
  105. package/examples/gloves/edge-impulse-standalone.wasm +0 -0
  106. package/examples/gloves/index.html +4 -1
  107. package/examples/gloves/run-impulse.js +135 -0
  108. package/examples/gloves/script.js +367 -51
  109. package/examples/graph/script.js +94 -37
  110. package/examples/microphone/gender.js +54 -0
  111. package/examples/microphone/index.html +102 -0
  112. package/examples/microphone/script.js +394 -0
  113. package/examples/microphone/utils.js +45 -0
  114. package/examples/microphone/whisper-realtime.js +166 -0
  115. package/examples/microphone/whisper.js +132 -0
  116. package/examples/punch/index.html +135 -0
  117. package/examples/punch/punch.tflite +0 -0
  118. package/examples/punch/script.js +169 -0
  119. package/examples/server/index.html +98 -22
  120. package/examples/server/script.js +317 -109
  121. package/examples/ukaton-firmware-update/merged-firmware.bin +0 -0
  122. package/examples/utils/aframe/aframe-master.min.js +2 -0
  123. package/examples/utils/aframe/bs-vibration.js +150 -0
  124. package/examples/utils/aframe/force-pushable.js +80 -0
  125. package/examples/utils/aframe/grabbable-anchor.js +46 -0
  126. package/examples/utils/aframe/grabbable-listener.js +31 -0
  127. package/examples/utils/aframe/grabbable-physics-body.js +190 -0
  128. package/examples/utils/aframe/grow-shrink.js +25 -0
  129. package/examples/utils/aframe/hand-punch.js +119 -0
  130. package/examples/utils/aframe/my-obb-collider.js +293 -0
  131. package/examples/utils/aframe/occlude-hand-tracking-controls.js +47 -0
  132. package/examples/utils/aframe/occlude-mesh.js +42 -0
  133. package/examples/utils/aframe/palm-up-detector.js +47 -0
  134. package/examples/utils/aframe/shadow-material.js +20 -0
  135. package/examples/utils/aframe/soft-shadow-light.js +9 -0
  136. package/examples/webxr-2/assets/3d/soccerBall.glb +0 -0
  137. package/examples/webxr-2/assets/audio/shellBounce.wav +0 -0
  138. package/examples/webxr-2/assets/audio/shellHit.wav +0 -0
  139. package/examples/webxr-2/assets/audio/shellKick.wav +0 -0
  140. package/examples/webxr-2/assets/audio/soccerBounce.wav +0 -0
  141. package/examples/webxr-2/assets/audio/soccerKick.mp3 +0 -0
  142. package/examples/webxr-2/assets/images/shellTexture.png +0 -0
  143. package/examples/webxr-2/components/bs-ankle.js +337 -0
  144. package/examples/webxr-2/components/coin.js +84 -0
  145. package/examples/webxr-2/components/custom-wrap.js +17 -0
  146. package/examples/webxr-2/components/goomba.js +3250 -0
  147. package/examples/webxr-2/components/init-shell-material.js +215 -0
  148. package/examples/webxr-2/components/platter.js +172 -0
  149. package/examples/webxr-2/components/shell.js +374 -0
  150. package/examples/webxr-2/components/soccer-ball.js +250 -0
  151. package/examples/webxr-2/components/squashed-goomba.js +249 -0
  152. package/examples/webxr-2/edge-impulse-standalone.js +7228 -0
  153. package/examples/webxr-2/edge-impulse-standalone.wasm +0 -0
  154. package/examples/webxr-2/index.html +996 -0
  155. package/examples/webxr-2/kick.tflite +0 -0
  156. package/examples/webxr-2/kick2.tflite +0 -0
  157. package/examples/webxr-2/run-impulse.js +135 -0
  158. package/examples/webxr-2/script.js +384 -0
  159. package/examples/webxr-3/components/bs-camera.js +65 -0
  160. package/examples/webxr-3/index.html +134 -0
  161. package/examples/webxr-3/script.js +432 -0
  162. package/package.json +2 -1
  163. package/src/.prettierrc +4 -0
  164. package/src/BS.ts +79 -8
  165. package/src/CameraManager.ts +497 -0
  166. package/src/Device.ts +691 -86
  167. package/src/DeviceInformationManager.ts +19 -10
  168. package/src/DeviceManager.ts +85 -25
  169. package/src/FileTransferManager.ts +145 -20
  170. package/src/InformationManager.ts +40 -15
  171. package/src/MicrophoneManager.ts +599 -0
  172. package/src/TfliteManager.ts +171 -25
  173. package/src/WifiManager.ts +323 -0
  174. package/src/connection/BaseConnectionManager.ts +130 -30
  175. package/src/connection/ClientConnectionManager.ts +34 -10
  176. package/src/connection/bluetooth/BluetoothConnectionManager.ts +8 -2
  177. package/src/connection/bluetooth/NobleConnectionManager.ts +147 -41
  178. package/src/connection/bluetooth/WebBluetoothConnectionManager.ts +99 -34
  179. package/src/connection/bluetooth/bluetoothUUIDs.ts +40 -13
  180. package/src/connection/udp/UDPConnectionManager.ts +356 -0
  181. package/src/connection/websocket/WebSocketConnectionManager.ts +282 -0
  182. package/src/devicePair/DevicePair.ts +95 -25
  183. package/src/devicePair/DevicePairPressureSensorDataManager.ts +27 -7
  184. package/src/scanner/BaseScanner.ts +49 -11
  185. package/src/scanner/NobleScanner.ts +76 -14
  186. package/src/sensor/MotionSensorDataManager.ts +21 -6
  187. package/src/sensor/PressureSensorDataManager.ts +37 -8
  188. package/src/sensor/SensorConfigurationManager.ts +73 -22
  189. package/src/sensor/SensorDataManager.ts +109 -23
  190. package/src/server/BaseClient.ts +150 -36
  191. package/src/server/BaseServer.ts +50 -2
  192. package/src/server/ServerUtils.ts +39 -9
  193. package/src/server/udp/UDPServer.ts +73 -22
  194. package/src/server/udp/UDPUtils.ts +9 -2
  195. package/src/server/websocket/WebSocketClient.ts +27 -7
  196. package/src/server/websocket/WebSocketUtils.ts +4 -2
  197. package/src/utils/AudioUtils.ts +65 -0
  198. package/src/utils/Console.ts +62 -9
  199. package/src/utils/ParseUtils.ts +24 -5
  200. package/src/utils/ThrottleUtils.ts +62 -0
  201. package/src/utils/Timer.ts +1 -1
  202. package/src/vibration/VibrationManager.ts +166 -40
@@ -9,16 +9,36 @@ import {
9
9
  ServerMessageType,
10
10
  } from "./ServerUtils.ts";
11
11
  import { parseMessage, parseStringFromDataView } from "../utils/ParseUtils.ts";
12
- import EventDispatcher, { BoundEventListeners, Event } from "../utils/EventDispatcher.ts";
12
+ import EventDispatcher, {
13
+ BoundEventListeners,
14
+ Event,
15
+ } from "../utils/EventDispatcher.ts";
13
16
  import Device from "../Device.ts";
14
- import { sliceDataView } from "../utils/ArrayBufferUtils.ts";
15
- import { DiscoveredDevice, DiscoveredDevicesMap, ScannerEventMessages } from "../scanner/BaseScanner.ts";
17
+ import {
18
+ concatenateArrayBuffers,
19
+ sliceDataView,
20
+ stringToArrayBuffer,
21
+ } from "../utils/ArrayBufferUtils.ts";
22
+ import {
23
+ DiscoveredDevice,
24
+ DiscoveredDevicesMap,
25
+ ScannerEventMessages,
26
+ } from "../scanner/BaseScanner.ts";
16
27
  import ClientConnectionManager from "../connection/ClientConnectionManager.ts";
17
28
  import { DeviceManager } from "../BS.ts";
29
+ import {
30
+ ClientConnectionType,
31
+ ConnectionTypes,
32
+ } from "../connection/BaseConnectionManager.ts";
18
33
 
19
34
  const _console = createConsole("BaseClient", { log: false });
20
35
 
21
- export const ClientConnectionStatuses = ["notConnected", "connecting", "connected", "disconnecting"] as const;
36
+ export const ClientConnectionStatuses = [
37
+ "notConnected",
38
+ "connecting",
39
+ "connected",
40
+ "disconnecting",
41
+ ] as const;
22
42
  export type ClientConnectionStatus = (typeof ClientConnectionStatuses)[number];
23
43
 
24
44
  export const ClientEventTypes = [
@@ -37,11 +57,24 @@ interface ClientConnectionEventMessages {
37
57
  isConnected: { isConnected: boolean };
38
58
  }
39
59
 
40
- export type ClientEventMessages = ClientConnectionEventMessages & ScannerEventMessages;
41
-
42
- export type ClientEventDispatcher = EventDispatcher<BaseClient, ClientEventType, ClientEventMessages>;
43
- export type ClientEvent = Event<BaseClient, ClientEventType, ClientEventMessages>;
44
- export type BoundClientEventListeners = BoundEventListeners<BaseClient, ClientEventType, ClientEventMessages>;
60
+ export type ClientEventMessages = ClientConnectionEventMessages &
61
+ ScannerEventMessages;
62
+
63
+ export type ClientEventDispatcher = EventDispatcher<
64
+ BaseClient,
65
+ ClientEventType,
66
+ ClientEventMessages
67
+ >;
68
+ export type ClientEvent = Event<
69
+ BaseClient,
70
+ ClientEventType,
71
+ ClientEventMessages
72
+ >;
73
+ export type BoundClientEventListeners = BoundEventListeners<
74
+ BaseClient,
75
+ ClientEventType,
76
+ ClientEventMessages
77
+ >;
45
78
 
46
79
  export type ServerURL = string | URL;
47
80
 
@@ -57,7 +90,8 @@ abstract class BaseClient {
57
90
  this.#isScanning = false;
58
91
  for (const id in this.#devices) {
59
92
  const device = this.#devices[id];
60
- const connectionManager = device.connectionManager! as ClientConnectionManager;
93
+ const connectionManager =
94
+ device.connectionManager! as ClientConnectionManager;
61
95
  connectionManager.isConnected = false;
62
96
  //device.removeAllEventListeners();
63
97
  }
@@ -71,7 +105,10 @@ abstract class BaseClient {
71
105
  return this.#devices;
72
106
  }
73
107
 
74
- #eventDispatcher: ClientEventDispatcher = new EventDispatcher(this as BaseClient, ClientEventTypes);
108
+ #eventDispatcher: ClientEventDispatcher = new EventDispatcher(
109
+ this as BaseClient,
110
+ ClientEventTypes
111
+ );
75
112
  get addEventListener() {
76
113
  return this.#eventDispatcher.addEventListener;
77
114
  }
@@ -109,7 +146,8 @@ abstract class BaseClient {
109
146
  this._reconnectOnDisconnection = newReconnectOnDisconnection;
110
147
  }
111
148
 
112
- protected _reconnectOnDisconnection = this.baseConstructor.ReconnectOnDisconnection;
149
+ protected _reconnectOnDisconnection =
150
+ this.baseConstructor.ReconnectOnDisconnection;
113
151
  get reconnectOnDisconnection() {
114
152
  return this._reconnectOnDisconnection;
115
153
  }
@@ -130,7 +168,9 @@ abstract class BaseClient {
130
168
  _console.log({ newConnectionStatus });
131
169
  this.#_connectionStatus = newConnectionStatus;
132
170
 
133
- this.dispatchEvent("connectionStatus", { connectionStatus: this.connectionStatus });
171
+ this.dispatchEvent("connectionStatus", {
172
+ connectionStatus: this.connectionStatus,
173
+ });
134
174
  this.dispatchEvent(this.connectionStatus, {});
135
175
 
136
176
  switch (newConnectionStatus) {
@@ -149,7 +189,11 @@ abstract class BaseClient {
149
189
  return this._connectionStatus;
150
190
  }
151
191
 
152
- static #RequiredMessageTypes: ServerMessage[] = ["isScanningAvailable", "discoveredDevices", "connectedDevices"];
192
+ static #RequiredMessageTypes: ServerMessage[] = [
193
+ "isScanningAvailable",
194
+ "discoveredDevices",
195
+ "connectedDevices",
196
+ ];
153
197
  get #requiredMessageTypes(): ServerMessage[] {
154
198
  return BaseClient.#RequiredMessageTypes;
155
199
  }
@@ -183,7 +227,13 @@ abstract class BaseClient {
183
227
 
184
228
  protected parseMessage(dataView: DataView) {
185
229
  _console.log("parseMessage", { dataView });
186
- parseMessage(dataView, ServerMessageTypes, this.#parseMessageCallback.bind(this), null, true);
230
+ parseMessage(
231
+ dataView,
232
+ ServerMessageTypes,
233
+ this.#parseMessageCallback.bind(this),
234
+ null,
235
+ true
236
+ );
187
237
  this.#checkIfFullyConnected();
188
238
  }
189
239
 
@@ -209,10 +259,15 @@ abstract class BaseClient {
209
259
  break;
210
260
  case "discoveredDevice":
211
261
  {
212
- const { string: discoveredDeviceString } = parseStringFromDataView(dataView, byteOffset);
262
+ const { string: discoveredDeviceString } = parseStringFromDataView(
263
+ dataView,
264
+ byteOffset
265
+ );
213
266
  _console.log({ discoveredDeviceString });
214
267
 
215
- const discoveredDevice: DiscoveredDevice = JSON.parse(discoveredDeviceString);
268
+ const discoveredDevice: DiscoveredDevice = JSON.parse(
269
+ discoveredDeviceString
270
+ );
216
271
  _console.log({ discoveredDevice });
217
272
 
218
273
  this.onDiscoveredDevice(discoveredDevice);
@@ -220,7 +275,10 @@ abstract class BaseClient {
220
275
  break;
221
276
  case "expiredDiscoveredDevice":
222
277
  {
223
- const { string: bluetoothId } = parseStringFromDataView(dataView, byteOffset);
278
+ const { string: bluetoothId } = parseStringFromDataView(
279
+ dataView,
280
+ byteOffset
281
+ );
224
282
  this.#onExpiredDiscoveredDevice(bluetoothId);
225
283
  }
226
284
  break;
@@ -229,20 +287,28 @@ abstract class BaseClient {
229
287
  if (dataView.byteLength == 0) {
230
288
  break;
231
289
  }
232
- const { string: connectedBluetoothDeviceIdStrings } = parseStringFromDataView(dataView, byteOffset);
290
+ const { string: connectedBluetoothDeviceIdStrings } =
291
+ parseStringFromDataView(dataView, byteOffset);
233
292
  _console.log({ connectedBluetoothDeviceIdStrings });
234
- const connectedBluetoothDeviceIds = JSON.parse(connectedBluetoothDeviceIdStrings).connectedDevices;
293
+ const connectedBluetoothDeviceIds = JSON.parse(
294
+ connectedBluetoothDeviceIdStrings
295
+ ).connectedDevices;
235
296
  _console.log({ connectedBluetoothDeviceIds });
236
297
  this.onConnectedBluetoothDeviceIds(connectedBluetoothDeviceIds);
237
298
  }
238
299
  break;
239
300
  case "deviceMessage":
240
301
  {
241
- const { string: bluetoothId, byteOffset: _byteOffset } = parseStringFromDataView(dataView, byteOffset);
302
+ const { string: bluetoothId, byteOffset: _byteOffset } =
303
+ parseStringFromDataView(dataView, byteOffset);
242
304
  byteOffset = _byteOffset;
243
305
  const device = this.#devices[bluetoothId];
244
- _console.assertWithError(device, `no device found for id ${bluetoothId}`);
245
- const connectionManager = device.connectionManager! as ClientConnectionManager;
306
+ _console.assertWithError(
307
+ device,
308
+ `no device found for id ${bluetoothId}`
309
+ );
310
+ const connectionManager =
311
+ device.connectionManager! as ClientConnectionManager;
246
312
  const _dataView = sliceDataView(dataView, byteOffset);
247
313
  connectionManager.onClientMessage(_dataView);
248
314
  }
@@ -265,7 +331,9 @@ abstract class BaseClient {
265
331
  set #isScanningAvailable(newIsAvailable) {
266
332
  _console.assertTypeWithError(newIsAvailable, "boolean");
267
333
  this.#_isScanningAvailable = newIsAvailable;
268
- this.dispatchEvent("isScanningAvailable", { isScanningAvailable: this.isScanningAvailable });
334
+ this.dispatchEvent("isScanningAvailable", {
335
+ isScanningAvailable: this.isScanningAvailable,
336
+ });
269
337
  if (this.isScanningAvailable) {
270
338
  this.#requestIsScanning();
271
339
  }
@@ -275,7 +343,10 @@ abstract class BaseClient {
275
343
  }
276
344
  #assertIsScanningAvailable() {
277
345
  this.assertConnection();
278
- _console.assertWithError(this.isScanningAvailable, "scanning is not available");
346
+ _console.assertWithError(
347
+ this.isScanningAvailable,
348
+ "scanning is not available"
349
+ );
279
350
  }
280
351
  protected requestIsScanningAvailable() {
281
352
  this.sendServerMessage("isScanningAvailable");
@@ -349,29 +420,61 @@ abstract class BaseClient {
349
420
  }
350
421
 
351
422
  // DEVICE CONNECTION
352
- connectToDevice(bluetoothId: string) {
353
- return this.requestConnectionToDevice(bluetoothId);
423
+ connectToDevice(bluetoothId: string, connectionType?: ClientConnectionType) {
424
+ return this.requestConnectionToDevice(bluetoothId, connectionType);
354
425
  }
355
- protected requestConnectionToDevice(bluetoothId: string) {
426
+ protected requestConnectionToDevice(
427
+ bluetoothId: string,
428
+ connectionType?: ClientConnectionType
429
+ ) {
356
430
  this.assertConnection();
357
431
  _console.assertTypeWithError(bluetoothId, "string");
358
432
  const device = this.#getOrCreateDevice(bluetoothId);
359
- device.connect();
433
+ if (connectionType) {
434
+ device.connect({ type: "client", subType: connectionType });
435
+ } else {
436
+ device.connect();
437
+ }
360
438
  return device;
361
439
  }
362
- protected sendConnectToDeviceMessage(bluetoothId: string) {
363
- this.sendServerMessage({ type: "connectToDevice", data: bluetoothId });
440
+ protected sendConnectToDeviceMessage(
441
+ bluetoothId: string,
442
+ connectionType?: ClientConnectionType
443
+ ) {
444
+ if (connectionType) {
445
+ this.sendServerMessage({
446
+ type: "connectToDevice",
447
+ data: concatenateArrayBuffers(
448
+ stringToArrayBuffer(bluetoothId),
449
+ ConnectionTypes.indexOf(connectionType)
450
+ ),
451
+ });
452
+ } else {
453
+ this.sendServerMessage({ type: "connectToDevice", data: bluetoothId });
454
+ }
364
455
  }
365
456
 
366
457
  // DEVICE CONNECTION
367
458
  createDevice(bluetoothId: string) {
368
459
  const device = new Device();
460
+ const discoveredDevice = this.#discoveredDevices[bluetoothId];
369
461
  const clientConnectionManager = new ClientConnectionManager();
462
+ clientConnectionManager.discoveredDevice = Object.assign(
463
+ {},
464
+ discoveredDevice
465
+ );
370
466
  clientConnectionManager.client = this;
371
467
  clientConnectionManager.bluetoothId = bluetoothId;
372
- clientConnectionManager.sendClientMessage = this.sendDeviceMessage.bind(this, bluetoothId);
373
- clientConnectionManager.sendClientConnectMessage = this.sendConnectToDeviceMessage.bind(this, bluetoothId);
374
- clientConnectionManager.sendClientDisconnectMessage = this.sendDisconnectFromDeviceMessage.bind(this, bluetoothId);
468
+ clientConnectionManager.sendClientMessage = this.sendDeviceMessage.bind(
469
+ this,
470
+ bluetoothId
471
+ );
472
+ clientConnectionManager.sendRequiredDeviceInformationMessage =
473
+ this.sendRequiredDeviceInformationMessage.bind(this, bluetoothId);
474
+ clientConnectionManager.sendClientConnectMessage =
475
+ this.sendConnectToDeviceMessage.bind(this, bluetoothId);
476
+ clientConnectionManager.sendClientDisconnectMessage =
477
+ this.sendDisconnectFromDeviceMessage.bind(this, bluetoothId);
375
478
  device.connectionManager = clientConnectionManager;
376
479
  return device;
377
480
  }
@@ -388,7 +491,8 @@ abstract class BaseClient {
388
491
  _console.log({ bluetoothIds });
389
492
  bluetoothIds.forEach((bluetoothId) => {
390
493
  const device = this.#getOrCreateDevice(bluetoothId);
391
- const connectionManager = device.connectionManager! as ClientConnectionManager;
494
+ const connectionManager =
495
+ device.connectionManager! as ClientConnectionManager;
392
496
  connectionManager.isConnected = true;
393
497
  DeviceManager._CheckDeviceAvailability(device);
394
498
  });
@@ -409,12 +513,22 @@ abstract class BaseClient {
409
513
  this.sendServerMessage({ type: "disconnectFromDevice", data: bluetoothId });
410
514
  }
411
515
 
412
- protected sendDeviceMessage(bluetoothId: string, ...messages: ClientDeviceMessage[]) {
516
+ protected sendDeviceMessage(
517
+ bluetoothId: string,
518
+ ...messages: ClientDeviceMessage[]
519
+ ) {
413
520
  this.sendServerMessage({
414
521
  type: "deviceMessage",
415
522
  data: [bluetoothId, createClientDeviceMessage(...messages)],
416
523
  });
417
524
  }
525
+
526
+ protected sendRequiredDeviceInformationMessage(bluetoothId: string) {
527
+ this.sendServerMessage({
528
+ type: "requiredDeviceInformation",
529
+ data: [bluetoothId],
530
+ });
531
+ }
418
532
  }
419
533
 
420
534
  export default BaseClient;
@@ -16,6 +16,7 @@ import Device, {
16
16
  BoundDeviceEventListeners,
17
17
  DeviceEventMap,
18
18
  DeviceEventType,
19
+ RequiredInformationConnectionMessages,
19
20
  } from "../Device.ts";
20
21
  import {
21
22
  addEventListeners,
@@ -26,6 +27,8 @@ import { parseMessage, parseStringFromDataView } from "../utils/ParseUtils.ts";
26
27
  import {
27
28
  ConnectionMessageType,
28
29
  ConnectionMessageTypes,
30
+ ConnectionType,
31
+ ConnectionTypes,
29
32
  } from "../connection/BaseConnectionManager.ts";
30
33
  import {
31
34
  BoundScannerEventListeners,
@@ -37,6 +40,14 @@ import DeviceManager, {
37
40
  DeviceManagerEventMap,
38
41
  BoundDeviceManagerEventListeners,
39
42
  } from "../DeviceManager.ts";
43
+ import { RequiredWifiMessageTypes } from "../WifiManager.ts";
44
+ import { DeviceInformationTypes } from "../DeviceInformationManager.ts";
45
+
46
+ const RequiredDeviceInformationMessageTypes: ConnectionMessageType[] = [
47
+ ...DeviceInformationTypes,
48
+ "batteryLevel",
49
+ ...RequiredInformationConnectionMessages,
50
+ ];
40
51
 
41
52
  const _console = createConsole("BaseServer", { log: false });
42
53
 
@@ -340,6 +351,9 @@ abstract class BaseServer {
340
351
  dataView: DataView,
341
352
  context: { responseMessages: ArrayBuffer[] }
342
353
  ) {
354
+ _console.log(
355
+ `onClientMessage "${messageType}" (${dataView.byteLength} bytes)`
356
+ );
343
357
  const { responseMessages } = context;
344
358
  switch (messageType) {
345
359
  case "isScanningAvailable":
@@ -359,8 +373,14 @@ abstract class BaseServer {
359
373
  break;
360
374
  case "connectToDevice":
361
375
  {
362
- const { string: deviceId } = parseStringFromDataView(dataView);
363
- scanner!.connectToDevice(deviceId);
376
+ const { string: deviceId, byteOffset } =
377
+ parseStringFromDataView(dataView);
378
+ let connectionType = undefined;
379
+ if (byteOffset < dataView.byteLength) {
380
+ connectionType = ConnectionTypes[dataView.getUint8(byteOffset)];
381
+ _console.log(`connectToDevice via ${connectionType}`);
382
+ }
383
+ scanner!.connectToDevice(deviceId, connectionType);
364
384
  }
365
385
  break;
366
386
  case "disconnectFromDevice":
@@ -403,6 +423,34 @@ abstract class BaseServer {
403
423
  }
404
424
  }
405
425
  break;
426
+ case "requiredDeviceInformation":
427
+ {
428
+ const { string: deviceId } = parseStringFromDataView(dataView);
429
+ const device = DeviceManager.ConnectedDevices.find(
430
+ (device) => device.bluetoothId == deviceId
431
+ );
432
+ if (!device) {
433
+ _console.error(`no device found with id ${deviceId}`);
434
+ break;
435
+ }
436
+
437
+ const messages = RequiredDeviceInformationMessageTypes.map(
438
+ (messageType) => this.#createDeviceMessage(device, messageType)
439
+ );
440
+ if (device.isWifiAvailable) {
441
+ RequiredWifiMessageTypes.forEach((messageType) => {
442
+ messages.push(this.#createDeviceMessage(device, messageType));
443
+ });
444
+ }
445
+ const responseMessage = this.#createDeviceServerMessage(
446
+ device,
447
+ ...messages
448
+ );
449
+ if (responseMessage) {
450
+ responseMessages.push(responseMessage);
451
+ }
452
+ }
453
+ break;
406
454
  default:
407
455
  _console.error(`uncaught messageType "${messageType}"`);
408
456
  break;
@@ -1,5 +1,8 @@
1
1
  import { DeviceEventTypes } from "../Device.ts";
2
- import { ConnectionMessageType, ConnectionMessageTypes } from "../connection/BaseConnectionManager.ts";
2
+ import {
3
+ ConnectionMessageType,
4
+ ConnectionMessageTypes,
5
+ } from "../connection/BaseConnectionManager.ts";
3
6
  import { concatenateArrayBuffers } from "../utils/ArrayBufferUtils.ts";
4
7
  import { createConsole } from "../utils/Console.ts";
5
8
  import { DeviceEventType } from "../Device.ts";
@@ -18,15 +21,29 @@ export const ServerMessageTypes = [
18
21
  "disconnectFromDevice",
19
22
  "connectedDevices",
20
23
  "deviceMessage",
24
+ "requiredDeviceInformation",
21
25
  ] as const;
22
26
  export type ServerMessageType = (typeof ServerMessageTypes)[number];
23
27
 
24
- export const DeviceMessageTypes = ["connectionStatus", "batteryLevel", "deviceInformation", "rx", "smp"] as const;
28
+ export const DeviceMessageTypes = [
29
+ "connectionStatus",
30
+ "batteryLevel",
31
+ "deviceInformation",
32
+ "rx",
33
+ "smp",
34
+ ] as const;
25
35
  export type DeviceMessageType = (typeof DeviceMessageTypes)[number];
26
36
 
27
37
  // MESSAGING
28
38
 
29
- export type MessageLike = number | number[] | ArrayBufferLike | DataView | boolean | string | any;
39
+ export type MessageLike =
40
+ | number
41
+ | number[]
42
+ | ArrayBufferLike
43
+ | DataView
44
+ | boolean
45
+ | string
46
+ | any;
30
47
 
31
48
  export interface Message<MessageType extends string> {
32
49
  type: MessageType;
@@ -59,9 +76,17 @@ export function createMessage<MessageType extends string>(
59
76
  const messageTypeEnum = enumeration.indexOf(message.type);
60
77
 
61
78
  const messageDataLengthDataView = new DataView(new ArrayBuffer(2));
62
- messageDataLengthDataView.setUint16(0, messageDataArrayBufferByteLength, true);
63
-
64
- return concatenateArrayBuffers(messageTypeEnum, messageDataLengthDataView, messageDataArrayBuffer);
79
+ messageDataLengthDataView.setUint16(
80
+ 0,
81
+ messageDataArrayBufferByteLength,
82
+ true
83
+ );
84
+
85
+ return concatenateArrayBuffers(
86
+ messageTypeEnum,
87
+ messageDataLengthDataView,
88
+ messageDataArrayBuffer
89
+ );
65
90
  });
66
91
  _console.log("messageBuffers", ...messageBuffers);
67
92
  return concatenateArrayBuffers(...messageBuffers);
@@ -79,15 +104,20 @@ export function createDeviceMessage(...messages: DeviceMessage[]) {
79
104
  return createMessage(DeviceEventTypes, ...messages);
80
105
  }
81
106
 
82
- export type ClientDeviceMessage = ConnectionMessageType | Message<ConnectionMessageType>;
107
+ export type ClientDeviceMessage =
108
+ | ConnectionMessageType
109
+ | Message<ConnectionMessageType>;
83
110
  export function createClientDeviceMessage(...messages: ClientDeviceMessage[]) {
84
111
  _console.log("createClientDeviceMessage", ...messages);
85
112
  return createMessage(ConnectionMessageTypes, ...messages);
86
113
  }
87
114
 
88
115
  // STATIC MESSAGES
89
- export const isScanningAvailableRequestMessage = createServerMessage("isScanningAvailable");
116
+ export const isScanningAvailableRequestMessage = createServerMessage(
117
+ "isScanningAvailable"
118
+ );
90
119
  export const isScanningRequestMessage = createServerMessage("isScanning");
91
120
  export const startScanRequestMessage = createServerMessage("startScan");
92
121
  export const stopScanRequestMessage = createServerMessage("stopScan");
93
- export const discoveredDevicesMessage = createServerMessage("discoveredDevices");
122
+ export const discoveredDevicesMessage =
123
+ createServerMessage("discoveredDevices");
@@ -1,6 +1,12 @@
1
- import { concatenateArrayBuffers, dataToArrayBuffer } from "../../utils/ArrayBufferUtils.ts";
1
+ import {
2
+ concatenateArrayBuffers,
3
+ dataToArrayBuffer,
4
+ } from "../../utils/ArrayBufferUtils.ts";
2
5
  import { createConsole } from "../../utils/Console.ts";
3
- import { addEventListeners, removeEventListeners } from "../../utils/EventUtils.ts";
6
+ import {
7
+ addEventListeners,
8
+ removeEventListeners,
9
+ } from "../../utils/EventUtils.ts";
4
10
  import { parseMessage } from "../../utils/ParseUtils.ts";
5
11
  import BaseServer from "../BaseServer.ts";
6
12
  import {
@@ -11,10 +17,10 @@ import {
11
17
  UDPServerMessageType,
12
18
  UDPServerMessageTypes,
13
19
  } from "./UDPUtils.ts";
20
+ import Timer from "../../utils/Timer.ts";
14
21
 
15
22
  /** NODE_START */
16
23
  import type * as dgram from "dgram";
17
- import Timer from "../../utils/Timer.ts";
18
24
  /** NODE_END */
19
25
 
20
26
  const _console = createConsole("UDPServer", { log: false });
@@ -38,14 +44,22 @@ class UDPServer extends BaseServer {
38
44
  return this.#clients.length;
39
45
  }
40
46
 
41
- #getClientByRemoteInfo(remoteInfo: dgram.RemoteInfo, createIfNotFound = false) {
47
+ #getClientByRemoteInfo(
48
+ remoteInfo: dgram.RemoteInfo,
49
+ createIfNotFound = false
50
+ ) {
42
51
  const { address, port } = remoteInfo;
43
- let client = this.#clients.find((client) => client.address == address && client.port == port);
52
+ let client = this.#clients.find(
53
+ (client) => client.address == address && client.port == port
54
+ );
44
55
  if (!client && createIfNotFound) {
45
56
  client = {
46
57
  ...remoteInfo,
47
58
  isAlive: true,
48
- removeSelfTimer: new Timer(() => this.#removeClient(client!), removeUDPClientTimeout),
59
+ removeSelfTimer: new Timer(
60
+ () => this.#removeClient(client!),
61
+ removeUDPClientTimeout
62
+ ),
49
63
  lastTimeSentData: 0,
50
64
  };
51
65
  _console.log("created new client", client);
@@ -114,7 +128,11 @@ class UDPServer extends BaseServer {
114
128
  _console.log(`socket listening on port ${address.address}:${address.port}`);
115
129
  }
116
130
  #onSocketMessage(message: Buffer, remoteInfo: dgram.RemoteInfo) {
117
- _console.log(`received ${message.length} bytes from ${this.#remoteInfoToString(remoteInfo)}`);
131
+ _console.log(
132
+ `received ${message.length} bytes from ${this.#remoteInfoToString(
133
+ remoteInfo
134
+ )}`
135
+ );
118
136
  const client = this.#getClientByRemoteInfo(remoteInfo, true);
119
137
  if (!client) {
120
138
  _console.error("no client found");
@@ -127,7 +145,12 @@ class UDPServer extends BaseServer {
127
145
 
128
146
  // PARSING
129
147
  #onClientData(client: UDPClient, dataView: DataView) {
130
- _console.log(`parsing ${dataView.byteLength} bytes from ${this.#clientToString(client)}`, dataView.buffer);
148
+ _console.log(
149
+ `parsing ${dataView.byteLength} bytes from ${this.#clientToString(
150
+ client
151
+ )}`,
152
+ dataView.buffer
153
+ );
131
154
  let responseMessages: ArrayBuffer[] = [];
132
155
  parseMessage(
133
156
  dataView,
@@ -153,9 +176,15 @@ class UDPServer extends BaseServer {
153
176
  _console.log(`responding with ${response.byteLength} bytes...`, response);
154
177
  this.#sendToClient(client, response);
155
178
  }
156
- #onClientUDPMessage(messageType: UDPServerMessageType, dataView: DataView, context: UDPClientContext) {
179
+ #onClientUDPMessage(
180
+ messageType: UDPServerMessageType,
181
+ dataView: DataView,
182
+ context: UDPClientContext
183
+ ) {
157
184
  const { client, responseMessages } = context;
158
- _console.log(`received "${messageType}" message from ${client.address}:${client.port}`);
185
+ _console.log(
186
+ `received "${messageType}" message from ${client.address}:${client.port}`
187
+ );
159
188
  switch (messageType) {
160
189
  case "ping":
161
190
  responseMessages.push(this.#createPongMessage(context));
@@ -168,7 +197,12 @@ class UDPServer extends BaseServer {
168
197
  case "serverMessage":
169
198
  const responseMessage = this.parseClientMessage(dataView);
170
199
  if (responseMessage) {
171
- responseMessages.push(createUDPServerMessage({ type: "serverMessage", data: responseMessage }));
200
+ responseMessages.push(
201
+ createUDPServerMessage({
202
+ type: "serverMessage",
203
+ data: responseMessage,
204
+ })
205
+ );
172
206
  }
173
207
  break;
174
208
 
@@ -187,24 +221,38 @@ class UDPServer extends BaseServer {
187
221
  #parseRemoteReceivePort(dataView: DataView, client: UDPClient) {
188
222
  const receivePort = dataView.getUint16(0);
189
223
  client.receivePort = receivePort;
190
- _console.log(`updated ${client.address}:${client.port} receivePort to ${receivePort}`);
224
+ _console.log(
225
+ `updated ${client.address}:${client.port} receivePort to ${receivePort}`
226
+ );
191
227
  const responseDataView = new DataView(new ArrayBuffer(2));
192
228
  responseDataView.setUint16(0, client.receivePort);
193
- return createUDPServerMessage({ type: "setRemoteReceivePort", data: responseDataView });
229
+ return createUDPServerMessage({
230
+ type: "setRemoteReceivePort",
231
+ data: responseDataView,
232
+ });
194
233
  }
195
234
 
196
235
  // CLIENT MESSAGING
197
236
  #sendToClient(client: UDPClient, message: ArrayBuffer) {
198
- _console.log(`sending ${message.byteLength} bytes to ${this.#clientToString(client)}...`);
237
+ _console.log(
238
+ `sending ${message.byteLength} bytes to ${this.#clientToString(
239
+ client
240
+ )}...`
241
+ );
199
242
  try {
200
- this.#socket!.send(new Uint8Array(message), client.receivePort, client.address, (error, bytes) => {
201
- if (error) {
202
- _console.error("error sending data", error);
203
- return;
243
+ this.#socket!.send(
244
+ new Uint8Array(message),
245
+ client.receivePort,
246
+ client.address,
247
+ (error, bytes) => {
248
+ if (error) {
249
+ _console.error("error sending data", error);
250
+ return;
251
+ }
252
+ _console.log(`sent ${bytes} bytes`);
253
+ client.lastTimeSentData = Date.now();
204
254
  }
205
- _console.log(`sent ${bytes} bytes`);
206
- client.lastTimeSentData = Date.now();
207
- });
255
+ );
208
256
  } catch (error) {
209
257
  _console.error("serious error sending data", error);
210
258
  }
@@ -212,7 +260,10 @@ class UDPServer extends BaseServer {
212
260
  broadcastMessage(message: ArrayBuffer) {
213
261
  super.broadcastMessage(message);
214
262
  this.#clients.forEach((client) => {
215
- this.#sendToClient(client, createUDPServerMessage({ type: "serverMessage", data: message }));
263
+ this.#sendToClient(
264
+ client,
265
+ createUDPServerMessage({ type: "serverMessage", data: message })
266
+ );
216
267
  });
217
268
  }
218
269