@hangtime/grip-connect 0.2.3 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/package.json +6 -6
- package/src/battery.d.ts +3 -2
- package/src/battery.js +8 -2
- package/src/battery.ts +8 -2
- package/src/calibration.d.ts +3 -2
- package/src/calibration.js +6 -2
- package/src/calibration.ts +6 -2
- package/src/characteristic.d.ts +5 -4
- package/src/characteristic.js +9 -4
- package/src/characteristic.ts +14 -5
- package/src/connect.d.ts +3 -3
- package/src/connect.js +23 -30
- package/src/connect.ts +23 -30
- package/src/data.d.ts +13 -6
- package/src/data.js +92 -50
- package/src/data.ts +117 -63
- package/src/devices/climbro.d.ts +4 -0
- package/src/devices/climbro.js +4 -0
- package/src/devices/climbro.ts +4 -0
- package/src/devices/entralpi.d.ts +3 -0
- package/src/devices/entralpi.js +3 -0
- package/src/devices/entralpi.ts +3 -0
- package/src/devices/motherboard.d.ts +3 -0
- package/src/devices/motherboard.js +3 -0
- package/src/devices/motherboard.ts +3 -0
- package/src/devices/progressor.d.ts +3 -0
- package/src/devices/progressor.js +4 -1
- package/src/devices/progressor.ts +4 -2
- package/src/devices/smartboard.d.ts +4 -0
- package/src/devices/smartboard.js +4 -0
- package/src/devices/smartboard.ts +4 -0
- package/src/devices/types.d.ts +9 -0
- package/src/devices/types.ts +21 -12
- package/src/disconnect.d.ts +2 -2
- package/src/disconnect.js +4 -2
- package/src/disconnect.ts +4 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +8 -2
- package/src/index.ts +8 -7
- package/src/info.d.ts +3 -2
- package/src/info.js +9 -2
- package/src/info.ts +9 -2
- package/src/is-connected.d.ts +3 -3
- package/src/is-connected.js +7 -4
- package/src/is-connected.ts +8 -4
- package/src/notify.d.ts +19 -1
- package/src/notify.js +10 -2
- package/src/notify.ts +21 -6
- package/src/read.d.ts +6 -2
- package/src/read.js +10 -6
- package/src/read.ts +10 -6
- package/src/stop.d.ts +3 -2
- package/src/stop.js +5 -2
- package/src/stop.ts +5 -2
- package/src/stream.d.ts +4 -2
- package/src/stream.js +9 -7
- package/src/stream.ts +9 -7
- package/src/struct/index.d.ts +9 -0
- package/src/struct/index.js +176 -0
- package/src/struct/index.ts +215 -0
- package/src/tare.d.ts +12 -0
- package/src/tare.js +70 -0
- package/src/tare.ts +76 -0
- package/src/write.d.ts +11 -3
- package/src/write.js +21 -6
- package/src/write.ts +23 -6
- package/src/declarations.d.ts +0 -1
package/README.md
CHANGED
|
@@ -13,10 +13,13 @@ Learn more: [Docs](https://stevie-ray.github.io/hangtime-grip-connect/) -
|
|
|
13
13
|
|
|
14
14
|
## Try it out
|
|
15
15
|
|
|
16
|
-
[Chart](https://grip-connect.vercel.app/) - [Flappy Bird](https://grip-connect-flappy-bird.vercel.app/)
|
|
16
|
+
[Chart](https://grip-connect.vercel.app/) - [Flappy Bird](https://grip-connect-flappy-bird.vercel.app/) -
|
|
17
|
+
[Pong](https://grip-connect-pong.vercel.app/)
|
|
17
18
|
|
|
18
19
|
## Install
|
|
19
20
|
|
|
21
|
+
This project can be found in the [NPM package registry](https://www.npmjs.com/package/@hangtime/grip-connect).
|
|
22
|
+
|
|
20
23
|
```sh [npm]
|
|
21
24
|
$ npm install @hangtime/grip-connect
|
|
22
25
|
```
|
|
@@ -38,7 +41,7 @@ motherboardButton.addEventListener("click", () => {
|
|
|
38
41
|
connect(Motherboard, async () => {
|
|
39
42
|
// Listen for stream notifications
|
|
40
43
|
notify((data) => {
|
|
41
|
-
//
|
|
44
|
+
// { massTotal: "0", massMax: "0", massAverage: "0", massLeft: "0", massCenter: "0", massRight: "0" }
|
|
42
45
|
console.log(data)
|
|
43
46
|
})
|
|
44
47
|
|
|
@@ -49,6 +52,9 @@ motherboardButton.addEventListener("click", () => {
|
|
|
49
52
|
// Start weight streaming (for a minute) remove parameter for a continues stream
|
|
50
53
|
await stream(Motherboard, 60000)
|
|
51
54
|
|
|
55
|
+
// Manualy tare the device when the stream is running
|
|
56
|
+
// await tare(5000)
|
|
57
|
+
|
|
52
58
|
// Manually call stop method if stream is continues
|
|
53
59
|
// await stop(Motherboard)
|
|
54
60
|
|
|
@@ -104,6 +110,7 @@ A special thank you to:
|
|
|
104
110
|
- [@donaldharvey](https://github.com/donaldharvey) for a valuable example on connecting to the Motherboard.
|
|
105
111
|
- [@ecstrema](https://github.com/ecstrema) for providing [examples](https://github.com/ecstrema/entralpi-games) on how
|
|
106
112
|
to play games with the Entralpi.
|
|
113
|
+
- [Tindeq](https://tindeq.com/) for providing an open [Progressor API](https://tindeq.com/progressor_api/)
|
|
107
114
|
- [@StuartLittlefair](https://github.com/StuartLittlefair) for his
|
|
108
115
|
[PyTindeq](https://github.com/StuartLittlefair/PyTindeq) implementation.
|
|
109
116
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hangtime/grip-connect",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "A client that can establish connections with various Force-Sensing Hangboards/Plates used by climbers for strength measurement. Examples of such hangboards include the Griptonite Motherboard, Climbro, SmartBoard, Entralpi or Tindeq Progressor",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"hangboard",
|
|
20
20
|
"hangboarding",
|
|
21
21
|
"beastmaker",
|
|
22
|
-
"motherboard"
|
|
22
|
+
"motherboard",
|
|
23
|
+
"tindeq",
|
|
24
|
+
"progressor",
|
|
25
|
+
"entralpi"
|
|
23
26
|
],
|
|
24
27
|
"bugs": {
|
|
25
28
|
"url": "https://github.com/Stevie-Ray/hangtime-grip-connect/issues"
|
|
26
29
|
},
|
|
27
|
-
"homepage": "https://stevie-ray.github.io/hangtime-grip-connect/"
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"@aksel/structjs": "^1.0.0"
|
|
30
|
-
}
|
|
30
|
+
"homepage": "https://stevie-ray.github.io/hangtime-grip-connect/"
|
|
31
31
|
}
|
package/src/battery.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Device } from "./devices/types";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param board
|
|
3
|
+
* Retrieves battery or voltage information from the device.
|
|
4
|
+
* @param {Device} board - The device.
|
|
5
|
+
* @returns {Promise<void>} A Promise that resolves when the information is successfully retrieved.
|
|
5
6
|
*/
|
|
6
7
|
export declare const battery: (board: Device) => Promise<void>;
|
package/src/battery.js
CHANGED
|
@@ -4,15 +4,21 @@ import { isConnected } from "./is-connected";
|
|
|
4
4
|
import { Motherboard, Progressor } from "./devices";
|
|
5
5
|
import { ProgressorCommands } from "./commands";
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param board
|
|
7
|
+
* Retrieves battery or voltage information from the device.
|
|
8
|
+
* @param {Device} board - The device.
|
|
9
|
+
* @returns {Promise<void>} A Promise that resolves when the information is successfully retrieved.
|
|
9
10
|
*/
|
|
10
11
|
export const battery = async (board) => {
|
|
12
|
+
// Check if the device is connected
|
|
11
13
|
if (isConnected(board)) {
|
|
14
|
+
// If the device is connected and it is a Motherboard device
|
|
12
15
|
if (board.name === "Motherboard") {
|
|
16
|
+
// Read battery level information from the Motherboard
|
|
13
17
|
await read(Motherboard, "battery", "level", 250);
|
|
14
18
|
}
|
|
19
|
+
// If the device is connected and its name starts with "Progressor"
|
|
15
20
|
if (board.name && board.name.startsWith("Progressor")) {
|
|
21
|
+
// Write command to get battery voltage information to the Progressor
|
|
16
22
|
await write(Progressor, "progressor", "tx", ProgressorCommands.GET_BATT_VLTG, 250);
|
|
17
23
|
}
|
|
18
24
|
}
|
package/src/battery.ts
CHANGED
|
@@ -6,15 +6,21 @@ import { Motherboard, Progressor } from "./devices"
|
|
|
6
6
|
import { ProgressorCommands } from "./commands"
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param board
|
|
9
|
+
* Retrieves battery or voltage information from the device.
|
|
10
|
+
* @param {Device} board - The device.
|
|
11
|
+
* @returns {Promise<void>} A Promise that resolves when the information is successfully retrieved.
|
|
11
12
|
*/
|
|
12
13
|
export const battery = async (board: Device): Promise<void> => {
|
|
14
|
+
// Check if the device is connected
|
|
13
15
|
if (isConnected(board)) {
|
|
16
|
+
// If the device is connected and it is a Motherboard device
|
|
14
17
|
if (board.name === "Motherboard") {
|
|
18
|
+
// Read battery level information from the Motherboard
|
|
15
19
|
await read(Motherboard, "battery", "level", 250)
|
|
16
20
|
}
|
|
21
|
+
// If the device is connected and its name starts with "Progressor"
|
|
17
22
|
if (board.name && board.name.startsWith("Progressor")) {
|
|
23
|
+
// Write command to get battery voltage information to the Progressor
|
|
18
24
|
await write(Progressor, "progressor", "tx", ProgressorCommands.GET_BATT_VLTG, 250)
|
|
19
25
|
}
|
|
20
26
|
}
|
package/src/calibration.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Device } from "./devices/types";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param board
|
|
3
|
+
* Writes a command to get calibration data from the device.
|
|
4
|
+
* @param {Device} board - The device.
|
|
5
|
+
* @returns {Promise<void>} A Promise that resolves when the command is successfully sent.
|
|
5
6
|
*/
|
|
6
7
|
export declare const calibration: (board: Device) => Promise<void>;
|
package/src/calibration.js
CHANGED
|
@@ -3,12 +3,16 @@ import { write } from "./write";
|
|
|
3
3
|
import { Motherboard } from "./devices";
|
|
4
4
|
import { MotherboardCommands } from "./commands";
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @param board
|
|
6
|
+
* Writes a command to get calibration data from the device.
|
|
7
|
+
* @param {Device} board - The device.
|
|
8
|
+
* @returns {Promise<void>} A Promise that resolves when the command is successfully sent.
|
|
8
9
|
*/
|
|
9
10
|
export const calibration = async (board) => {
|
|
11
|
+
// Check if the device is connected
|
|
10
12
|
if (isConnected(board)) {
|
|
13
|
+
// If the device is connected, and it is a Motherboard device
|
|
11
14
|
if (board.name === "Motherboard") {
|
|
15
|
+
// Write the command to get calibration data to the device
|
|
12
16
|
await write(Motherboard, "uart", "tx", MotherboardCommands.GET_CALIBRATION, 2500);
|
|
13
17
|
}
|
|
14
18
|
}
|
package/src/calibration.ts
CHANGED
|
@@ -5,12 +5,16 @@ import { Motherboard } from "./devices"
|
|
|
5
5
|
import { MotherboardCommands } from "./commands"
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param board
|
|
8
|
+
* Writes a command to get calibration data from the device.
|
|
9
|
+
* @param {Device} board - The device.
|
|
10
|
+
* @returns {Promise<void>} A Promise that resolves when the command is successfully sent.
|
|
10
11
|
*/
|
|
11
12
|
export const calibration = async (board: Device): Promise<void> => {
|
|
13
|
+
// Check if the device is connected
|
|
12
14
|
if (isConnected(board)) {
|
|
15
|
+
// If the device is connected, and it is a Motherboard device
|
|
13
16
|
if (board.name === "Motherboard") {
|
|
17
|
+
// Write the command to get calibration data to the device
|
|
14
18
|
await write(Motherboard, "uart", "tx", MotherboardCommands.GET_CALIBRATION, 2500)
|
|
15
19
|
}
|
|
16
20
|
}
|
package/src/characteristic.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="web-bluetooth" />
|
|
2
2
|
import { Device } from "./devices/types";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param board
|
|
6
|
-
* @param serviceId
|
|
7
|
-
* @param characteristicId
|
|
4
|
+
* Retrieves the characteristic from the device's service.
|
|
5
|
+
* @param {Device} board - The device.
|
|
6
|
+
* @param {string} serviceId - The UUID of the service.
|
|
7
|
+
* @param {string} characteristicId - The UUID of the characteristic.
|
|
8
|
+
* @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
|
|
8
9
|
*/
|
|
9
10
|
export declare const getCharacteristic: (board: Device, serviceId: string, characteristicId: string) => BluetoothRemoteGATTCharacteristic | undefined;
|
package/src/characteristic.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param board
|
|
4
|
-
* @param serviceId
|
|
5
|
-
* @param characteristicId
|
|
2
|
+
* Retrieves the characteristic from the device's service.
|
|
3
|
+
* @param {Device} board - The device.
|
|
4
|
+
* @param {string} serviceId - The UUID of the service.
|
|
5
|
+
* @param {string} characteristicId - The UUID of the characteristic.
|
|
6
|
+
* @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
|
|
6
7
|
*/
|
|
7
8
|
export const getCharacteristic = (board, serviceId, characteristicId) => {
|
|
9
|
+
// Find the service with the specified serviceId
|
|
8
10
|
const boardService = board.services.find((service) => service.id === serviceId);
|
|
9
11
|
if (boardService) {
|
|
12
|
+
// If the service is found, find the characteristic with the specified characteristicId
|
|
10
13
|
const boardCharacteristic = boardService.characteristics.find((characteristic) => characteristic.id === characteristicId);
|
|
11
14
|
if (boardCharacteristic) {
|
|
15
|
+
// If the characteristic is found, return it
|
|
12
16
|
return boardCharacteristic.characteristic;
|
|
13
17
|
}
|
|
14
18
|
}
|
|
19
|
+
// Return undefined if the service or characteristic is not found
|
|
15
20
|
};
|
package/src/characteristic.ts
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param board
|
|
6
|
-
* @param serviceId
|
|
7
|
-
* @param characteristicId
|
|
4
|
+
* Retrieves the characteristic from the device's service.
|
|
5
|
+
* @param {Device} board - The device.
|
|
6
|
+
* @param {string} serviceId - The UUID of the service.
|
|
7
|
+
* @param {string} characteristicId - The UUID of the characteristic.
|
|
8
|
+
* @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
|
|
8
9
|
*/
|
|
9
|
-
export const getCharacteristic = (
|
|
10
|
+
export const getCharacteristic = (
|
|
11
|
+
board: Device,
|
|
12
|
+
serviceId: string,
|
|
13
|
+
characteristicId: string,
|
|
14
|
+
): BluetoothRemoteGATTCharacteristic | undefined => {
|
|
15
|
+
// Find the service with the specified serviceId
|
|
10
16
|
const boardService = board.services.find((service) => service.id === serviceId)
|
|
11
17
|
if (boardService) {
|
|
18
|
+
// If the service is found, find the characteristic with the specified characteristicId
|
|
12
19
|
const boardCharacteristic = boardService.characteristics.find(
|
|
13
20
|
(characteristic) => characteristic.id === characteristicId,
|
|
14
21
|
)
|
|
15
22
|
if (boardCharacteristic) {
|
|
23
|
+
// If the characteristic is found, return it
|
|
16
24
|
return boardCharacteristic.characteristic
|
|
17
25
|
}
|
|
18
26
|
}
|
|
27
|
+
// Return undefined if the service or characteristic is not found
|
|
19
28
|
}
|
package/src/connect.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Device } from "./devices/types";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param device
|
|
5
|
-
* @param onSuccess
|
|
3
|
+
* Connects to a Bluetooth device.
|
|
4
|
+
* @param {Device} board - The device to connect to.
|
|
5
|
+
* @param {Function} onSuccess - Callback function to execute on successful connection.
|
|
6
6
|
*/
|
|
7
7
|
export declare const connect: (board: Device, onSuccess: () => void) => Promise<void>;
|
package/src/connect.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { handleMotherboardData, handleProgressorData } from "./data";
|
|
1
|
+
import { handleEntralpiData, handleMotherboardData, handleProgressorData } from "./data";
|
|
3
2
|
let server;
|
|
4
3
|
const receiveBuffer = [];
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
5
|
+
* Handles the 'disconnected' event.
|
|
6
|
+
* @param {Event} event - The 'disconnected' event.
|
|
7
|
+
* @param {Device} board - The device that is disconnected.
|
|
9
8
|
*/
|
|
10
9
|
const onDisconnected = (event, board) => {
|
|
11
10
|
board.device = undefined;
|
|
@@ -13,9 +12,9 @@ const onDisconnected = (event, board) => {
|
|
|
13
12
|
console.log(`Device ${device.name} is disconnected.`);
|
|
14
13
|
};
|
|
15
14
|
/**
|
|
16
|
-
*
|
|
17
|
-
* @param event
|
|
18
|
-
* @param
|
|
15
|
+
* Handles notifications received from a characteristic.
|
|
16
|
+
* @param {Event} event - The notification event.
|
|
17
|
+
* @param {Device} board - The device associated with the characteristic.
|
|
19
18
|
*/
|
|
20
19
|
const handleNotifications = (event, board) => {
|
|
21
20
|
const characteristic = event.target;
|
|
@@ -32,45 +31,37 @@ const handleNotifications = (event, board) => {
|
|
|
32
31
|
line.pop(); // Remove CR
|
|
33
32
|
const decoder = new TextDecoder("utf-8");
|
|
34
33
|
const receivedData = decoder.decode(new Uint8Array(line));
|
|
35
|
-
handleMotherboardData(
|
|
34
|
+
handleMotherboardData(receivedData);
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
else if (board.name === "ENTRALPI") {
|
|
39
38
|
if (value.buffer) {
|
|
40
39
|
const buffer = value.buffer;
|
|
41
40
|
const rawData = new DataView(buffer);
|
|
42
|
-
const receivedData = rawData.getUint16(0) / 100;
|
|
43
|
-
|
|
44
|
-
notifyCallback({
|
|
45
|
-
uuid: characteristic.uuid,
|
|
46
|
-
value: {
|
|
47
|
-
massTotal: receivedData,
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
}
|
|
41
|
+
const receivedData = (rawData.getUint16(0) / 100).toFixed(1);
|
|
42
|
+
handleEntralpiData(receivedData);
|
|
51
43
|
}
|
|
52
44
|
}
|
|
53
45
|
else if (board.name && board.name.startsWith("Progressor")) {
|
|
54
46
|
if (value.buffer) {
|
|
55
47
|
const buffer = value.buffer;
|
|
56
48
|
const rawData = new DataView(buffer);
|
|
57
|
-
handleProgressorData(
|
|
49
|
+
handleProgressorData(rawData);
|
|
58
50
|
}
|
|
59
51
|
}
|
|
60
52
|
else {
|
|
61
|
-
|
|
62
|
-
notifyCallback({ uuid: characteristic.uuid, value: value });
|
|
63
|
-
}
|
|
53
|
+
console.log(value);
|
|
64
54
|
}
|
|
65
55
|
}
|
|
66
56
|
};
|
|
67
57
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @param
|
|
70
|
-
* @param
|
|
58
|
+
* Handles the 'connected' event.
|
|
59
|
+
* @param {Device} board - The connected device.
|
|
60
|
+
* @param {Function} onSuccess - Callback function to execute on successful connection.
|
|
71
61
|
*/
|
|
72
62
|
const onConnected = async (board, onSuccess) => {
|
|
73
63
|
try {
|
|
64
|
+
// Connect to GATT server and set up characteristics
|
|
74
65
|
const services = await server?.getPrimaryServices();
|
|
75
66
|
if (!services || services.length === 0) {
|
|
76
67
|
console.error("No services found");
|
|
@@ -109,19 +100,21 @@ const onConnected = async (board, onSuccess) => {
|
|
|
109
100
|
}
|
|
110
101
|
};
|
|
111
102
|
/**
|
|
112
|
-
*
|
|
113
|
-
* @param device
|
|
103
|
+
* Returns UUIDs of all services associated with the device.
|
|
104
|
+
* @param {Device} device - The device.
|
|
105
|
+
* @returns {string[]} Array of service UUIDs.
|
|
114
106
|
*/
|
|
115
107
|
const getAllServiceUUIDs = (device) => {
|
|
116
108
|
return device.services.map((service) => service.uuid);
|
|
117
109
|
};
|
|
118
110
|
/**
|
|
119
|
-
*
|
|
120
|
-
* @param device
|
|
121
|
-
* @param onSuccess
|
|
111
|
+
* Connects to a Bluetooth device.
|
|
112
|
+
* @param {Device} board - The device to connect to.
|
|
113
|
+
* @param {Function} onSuccess - Callback function to execute on successful connection.
|
|
122
114
|
*/
|
|
123
115
|
export const connect = async (board, onSuccess) => {
|
|
124
116
|
try {
|
|
117
|
+
// Request device and set up connection
|
|
125
118
|
const deviceServices = getAllServiceUUIDs(board);
|
|
126
119
|
// setup filter list
|
|
127
120
|
const filters = [];
|
package/src/connect.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
|
-
import {
|
|
3
|
-
import { handleMotherboardData, handleProgressorData } from "./data"
|
|
2
|
+
import { handleEntralpiData, handleMotherboardData, handleProgressorData } from "./data"
|
|
4
3
|
|
|
5
4
|
let server: BluetoothRemoteGATTServer
|
|
6
5
|
const receiveBuffer: number[] = []
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
8
|
+
* Handles the 'disconnected' event.
|
|
9
|
+
* @param {Event} event - The 'disconnected' event.
|
|
10
|
+
* @param {Device} board - The device that is disconnected.
|
|
12
11
|
*/
|
|
13
12
|
const onDisconnected = (event: Event, board: Device): void => {
|
|
14
13
|
board.device = undefined
|
|
@@ -16,9 +15,9 @@ const onDisconnected = (event: Event, board: Device): void => {
|
|
|
16
15
|
console.log(`Device ${device.name} is disconnected.`)
|
|
17
16
|
}
|
|
18
17
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @param event
|
|
21
|
-
* @param
|
|
18
|
+
* Handles notifications received from a characteristic.
|
|
19
|
+
* @param {Event} event - The notification event.
|
|
20
|
+
* @param {Device} board - The device associated with the characteristic.
|
|
22
21
|
*/
|
|
23
22
|
const handleNotifications = (event: Event, board: Device): void => {
|
|
24
23
|
const characteristic: BluetoothRemoteGATTCharacteristic = event.target as BluetoothRemoteGATTCharacteristic
|
|
@@ -36,42 +35,34 @@ const handleNotifications = (event: Event, board: Device): void => {
|
|
|
36
35
|
if (line.length > 0 && line[line.length - 1] === 13) line.pop() // Remove CR
|
|
37
36
|
const decoder: TextDecoder = new TextDecoder("utf-8")
|
|
38
37
|
const receivedData: string = decoder.decode(new Uint8Array(line))
|
|
39
|
-
handleMotherboardData(
|
|
38
|
+
handleMotherboardData(receivedData)
|
|
40
39
|
}
|
|
41
40
|
} else if (board.name === "ENTRALPI") {
|
|
42
41
|
if (value.buffer) {
|
|
43
42
|
const buffer: ArrayBuffer = value.buffer
|
|
44
43
|
const rawData: DataView = new DataView(buffer)
|
|
45
|
-
const receivedData:
|
|
46
|
-
|
|
47
|
-
notifyCallback({
|
|
48
|
-
uuid: characteristic.uuid,
|
|
49
|
-
value: {
|
|
50
|
-
massTotal: receivedData,
|
|
51
|
-
},
|
|
52
|
-
})
|
|
53
|
-
}
|
|
44
|
+
const receivedData: string = (rawData.getUint16(0) / 100).toFixed(1)
|
|
45
|
+
handleEntralpiData(receivedData)
|
|
54
46
|
}
|
|
55
47
|
} else if (board.name && board.name.startsWith("Progressor")) {
|
|
56
48
|
if (value.buffer) {
|
|
57
49
|
const buffer: ArrayBuffer = value.buffer
|
|
58
50
|
const rawData: DataView = new DataView(buffer)
|
|
59
|
-
handleProgressorData(
|
|
51
|
+
handleProgressorData(rawData)
|
|
60
52
|
}
|
|
61
53
|
} else {
|
|
62
|
-
|
|
63
|
-
notifyCallback({ uuid: characteristic.uuid, value: value })
|
|
64
|
-
}
|
|
54
|
+
console.log(value)
|
|
65
55
|
}
|
|
66
56
|
}
|
|
67
57
|
}
|
|
68
58
|
/**
|
|
69
|
-
*
|
|
70
|
-
* @param
|
|
71
|
-
* @param
|
|
59
|
+
* Handles the 'connected' event.
|
|
60
|
+
* @param {Device} board - The connected device.
|
|
61
|
+
* @param {Function} onSuccess - Callback function to execute on successful connection.
|
|
72
62
|
*/
|
|
73
63
|
const onConnected = async (board: Device, onSuccess: () => void): Promise<void> => {
|
|
74
64
|
try {
|
|
65
|
+
// Connect to GATT server and set up characteristics
|
|
75
66
|
const services: BluetoothRemoteGATTService[] = await server?.getPrimaryServices()
|
|
76
67
|
|
|
77
68
|
if (!services || services.length === 0) {
|
|
@@ -118,19 +109,21 @@ const onConnected = async (board: Device, onSuccess: () => void): Promise<void>
|
|
|
118
109
|
}
|
|
119
110
|
}
|
|
120
111
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param device
|
|
112
|
+
* Returns UUIDs of all services associated with the device.
|
|
113
|
+
* @param {Device} device - The device.
|
|
114
|
+
* @returns {string[]} Array of service UUIDs.
|
|
123
115
|
*/
|
|
124
116
|
const getAllServiceUUIDs = (device: Device) => {
|
|
125
117
|
return device.services.map((service) => service.uuid)
|
|
126
118
|
}
|
|
127
119
|
/**
|
|
128
|
-
*
|
|
129
|
-
* @param device
|
|
130
|
-
* @param onSuccess
|
|
120
|
+
* Connects to a Bluetooth device.
|
|
121
|
+
* @param {Device} board - The device to connect to.
|
|
122
|
+
* @param {Function} onSuccess - Callback function to execute on successful connection.
|
|
131
123
|
*/
|
|
132
124
|
export const connect = async (board: Device, onSuccess: () => void): Promise<void> => {
|
|
133
125
|
try {
|
|
126
|
+
// Request device and set up connection
|
|
134
127
|
const deviceServices = getAllServiceUUIDs(board)
|
|
135
128
|
|
|
136
129
|
// setup filter list
|
package/src/data.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
export declare const CALIBRATION: never[][];
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @param uuid - Unique identifier
|
|
6
|
-
* @param receivedData - Received data string
|
|
3
|
+
* Handles data received from the Motherboard device.
|
|
4
|
+
* @param {string} receivedData - The received data string.
|
|
7
5
|
*/
|
|
8
|
-
export declare const handleMotherboardData: (
|
|
9
|
-
|
|
6
|
+
export declare const handleMotherboardData: (receivedData: string) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Handles data received from the Progressor device.
|
|
9
|
+
* @param {DataView} data - The received data.
|
|
10
|
+
*/
|
|
11
|
+
export declare const handleProgressorData: (data: DataView) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Handles data received from the Entralpi device.
|
|
14
|
+
* @param {string} receivedData - The received data string.
|
|
15
|
+
*/
|
|
16
|
+
export declare const handleEntralpiData: (receivedData: string) => void;
|