@hangtime/grip-connect 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -25
- package/package.json +2 -2
- package/src/battery.ts +21 -0
- package/src/calibration.ts +4 -4
- package/src/commands/index.ts +2 -2
- package/src/commands/{tindeq.ts → progressor.ts} +7 -3
- package/src/commands/types.ts +5 -5
- package/src/connect.ts +74 -9
- package/src/data.ts +133 -0
- package/src/devices/entralpi.ts +0 -17
- package/src/devices/index.ts +1 -1
- package/src/devices/motherboard.ts +0 -134
- package/src/devices/{tindeq.ts → progressor.ts} +17 -5
- package/src/disconnect.ts +3 -3
- package/src/index.ts +10 -2
- package/src/info.ts +25 -0
- package/src/is-connected.ts +11 -0
- package/src/notify.ts +2 -2
- package/src/read.ts +2 -1
- package/src/stop.ts +7 -7
- package/src/stream.ts +16 -11
- package/src/write.ts +10 -5
- package/tsconfig.json +2 -2
- package/src/calibration.d.ts +0 -6
- package/src/calibration.js +0 -16
- package/src/characteristic.d.ts +0 -9
- package/src/characteristic.js +0 -15
- package/src/commands/climbro.d.ts +0 -6
- package/src/commands/climbro.js +0 -5
- package/src/commands/entralpi.d.ts +0 -6
- package/src/commands/entralpi.js +0 -5
- package/src/commands/index.d.ts +0 -5
- package/src/commands/index.js +0 -5
- package/src/commands/motherboard.d.ts +0 -6
- package/src/commands/motherboard.js +0 -13
- package/src/commands/smartboard.d.ts +0 -6
- package/src/commands/smartboard.js +0 -5
- package/src/commands/tindeq.d.ts +0 -11
- package/src/commands/tindeq.js +0 -23
- package/src/commands/types.d.ts +0 -18
- package/src/commands/types.js +0 -1
- package/src/connect.d.ts +0 -7
- package/src/connect.js +0 -150
- package/src/devices/climbro.d.ts +0 -2
- package/src/devices/climbro.js +0 -4
- package/src/devices/entralpi.d.ts +0 -8
- package/src/devices/entralpi.js +0 -68
- package/src/devices/index.d.ts +0 -5
- package/src/devices/index.js +0 -5
- package/src/devices/motherboard.d.ts +0 -8
- package/src/devices/motherboard.js +0 -189
- package/src/devices/smartboard.d.ts +0 -2
- package/src/devices/smartboard.js +0 -4
- package/src/devices/tindeq.d.ts +0 -2
- package/src/devices/tindeq.js +0 -22
- package/src/devices/types.d.ts +0 -20
- package/src/devices/types.js +0 -1
- package/src/disconnect.d.ts +0 -6
- package/src/disconnect.js +0 -11
- package/src/index.d.ts +0 -8
- package/src/index.js +0 -8
- package/src/notify.d.ts +0 -4
- package/src/notify.js +0 -6
- package/src/read.d.ts +0 -6
- package/src/read.js +0 -44
- package/src/stop.d.ts +0 -6
- package/src/stop.js +0 -19
- package/src/stream.d.ts +0 -6
- package/src/stream.js +0 -31
- package/src/write.d.ts +0 -7
- package/src/write.js +0 -34
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import { Device } from "./types"
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
name: "
|
|
3
|
+
export const Progressor: Device = {
|
|
4
|
+
name: "Progressor",
|
|
5
5
|
services: [
|
|
6
6
|
{
|
|
7
7
|
name: "Progressor Service",
|
|
8
8
|
id: "progressor",
|
|
9
9
|
uuid: "7e4e1701-1ea6-40c9-9dcc-13d34ffead57",
|
|
10
10
|
characteristics: [
|
|
11
|
+
{
|
|
12
|
+
name: "Notify",
|
|
13
|
+
id: "rx",
|
|
14
|
+
uuid: "7e4e1702-1ea6-40c9-9dcc-13d34ffead57",
|
|
15
|
+
},
|
|
11
16
|
{
|
|
12
17
|
name: "Write",
|
|
13
18
|
id: "tx",
|
|
14
19
|
uuid: "7e4e1703-1ea6-40c9-9dcc-13d34ffead57",
|
|
15
20
|
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "Secure DFU Service",
|
|
25
|
+
id: "dfu",
|
|
26
|
+
uuid: "0000fe59-0000-1000-8000-00805f9b34fb",
|
|
27
|
+
characteristics: [
|
|
16
28
|
{
|
|
17
|
-
name: "
|
|
18
|
-
id: "
|
|
19
|
-
uuid: "
|
|
29
|
+
name: "Buttonless DFU",
|
|
30
|
+
id: "dfu",
|
|
31
|
+
uuid: "8ec90003-f315-4f60-9fb8-838830daea50",
|
|
20
32
|
},
|
|
21
33
|
],
|
|
22
34
|
},
|
package/src/disconnect.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
|
+
import { isConnected } from "./is-connected"
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* disconnect
|
|
5
6
|
* @param board
|
|
6
7
|
*/
|
|
7
8
|
export const disconnect = (board: Device): void => {
|
|
8
|
-
if (
|
|
9
|
-
|
|
10
|
-
board.device.gatt?.disconnect()
|
|
9
|
+
if (isConnected(board)) {
|
|
10
|
+
board.device?.gatt?.disconnect()
|
|
11
11
|
}
|
|
12
12
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { Climbro, Entralpi, Motherboard, SmartBoard,
|
|
1
|
+
export { Climbro, Entralpi, Motherboard, SmartBoard, Progressor } from "./devices/index"
|
|
2
|
+
|
|
3
|
+
export { battery } from "./battery"
|
|
2
4
|
|
|
3
5
|
export { calibration } from "./calibration"
|
|
4
6
|
|
|
@@ -6,10 +8,16 @@ export { connect } from "./connect"
|
|
|
6
8
|
|
|
7
9
|
export { disconnect } from "./disconnect"
|
|
8
10
|
|
|
11
|
+
export { isConnected } from "./is-connected"
|
|
12
|
+
|
|
13
|
+
export { info } from "./info"
|
|
14
|
+
|
|
9
15
|
export { notify } from "./notify"
|
|
10
16
|
|
|
11
|
-
export { read } from "./read"
|
|
17
|
+
// export { read } from "./read"
|
|
12
18
|
|
|
13
19
|
export { stop } from "./stop"
|
|
14
20
|
|
|
15
21
|
export { stream } from "./stream"
|
|
22
|
+
|
|
23
|
+
// export { write } from "./write"
|
package/src/info.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Device } from "./devices/types"
|
|
2
|
+
import { write } from "./write"
|
|
3
|
+
import { read } from "./read"
|
|
4
|
+
import { isConnected } from "./is-connected"
|
|
5
|
+
import { Motherboard, Progressor } from "./devices"
|
|
6
|
+
import { MotherboardCommands, ProgressorCommands } from "./commands"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get device information
|
|
10
|
+
* @param board
|
|
11
|
+
*/
|
|
12
|
+
export const info = async (board: Device): Promise<void> => {
|
|
13
|
+
if (isConnected(board)) {
|
|
14
|
+
if (board.name === "Motherboard") {
|
|
15
|
+
await read(Motherboard, "device", "manufacturer", 250)
|
|
16
|
+
await read(Motherboard, "device", "hardware", 250)
|
|
17
|
+
await read(Motherboard, "device", "firmware", 250)
|
|
18
|
+
await write(Motherboard, "uart", "tx", MotherboardCommands.GET_TEXT, 250)
|
|
19
|
+
await write(Motherboard, "uart", "tx", MotherboardCommands.GET_SERIAL, 250)
|
|
20
|
+
}
|
|
21
|
+
if (board.name && board.name.startsWith("Progressor")) {
|
|
22
|
+
await write(Progressor, "progressor", "tx", ProgressorCommands.GET_FW_VERSION, 250)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Device } from "./devices/types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a Bluetooth device is connected
|
|
5
|
+
* @param board
|
|
6
|
+
* @return boolean
|
|
7
|
+
*/
|
|
8
|
+
export const isConnected = (board: Device): boolean => {
|
|
9
|
+
if (!board.device) return false
|
|
10
|
+
return !!board.device.gatt?.connected
|
|
11
|
+
}
|
package/src/notify.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// Define the
|
|
1
|
+
// Define the callbacks type
|
|
2
2
|
type NotifyCallback = (data: object) => void
|
|
3
3
|
|
|
4
4
|
// Initialize the callback variable
|
|
5
5
|
export let notifyCallback: NotifyCallback
|
|
6
6
|
|
|
7
|
-
// Export a
|
|
7
|
+
// Export a cost to set the callback
|
|
8
8
|
export const notify = (callback: NotifyCallback) => {
|
|
9
9
|
notifyCallback = callback
|
|
10
10
|
}
|
package/src/read.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
2
|
import { notifyCallback } from "./notify"
|
|
3
3
|
import { getCharacteristic } from "./characteristic"
|
|
4
|
+
import { isConnected } from "./is-connected"
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* read
|
|
@@ -13,7 +14,7 @@ export const read = (
|
|
|
13
14
|
duration: number = 0,
|
|
14
15
|
): Promise<void> => {
|
|
15
16
|
return new Promise((resolve, reject) => {
|
|
16
|
-
if (board
|
|
17
|
+
if (isConnected(board)) {
|
|
17
18
|
const characteristic = getCharacteristic(board, serviceId, characteristicId)
|
|
18
19
|
|
|
19
20
|
if (characteristic) {
|
package/src/stop.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
|
-
import { Motherboard, Tindeq } from "./devices"
|
|
3
|
-
import { MotherboardCommands, TindeqCommands } from "./commands"
|
|
4
2
|
import { write } from "./write"
|
|
3
|
+
import { isConnected } from "./is-connected"
|
|
4
|
+
import { Motherboard, Progressor } from "./devices"
|
|
5
|
+
import { MotherboardCommands, ProgressorCommands } from "./commands"
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* read calibration
|
|
8
9
|
* @param board
|
|
9
10
|
*/
|
|
10
11
|
export const stop = async (board: Device): Promise<void> => {
|
|
11
|
-
if (
|
|
12
|
-
if (board.device.gatt?.connected) {
|
|
12
|
+
if (isConnected(board)) {
|
|
13
13
|
if (board.name === "Motherboard") {
|
|
14
|
-
await write(Motherboard, "uart", "tx",
|
|
14
|
+
await write(Motherboard, "uart", "tx", MotherboardCommands.STOP_WEIGHT_MEAS, 0)
|
|
15
15
|
}
|
|
16
|
-
if (board.name
|
|
17
|
-
await write(
|
|
16
|
+
if (board.name && board.name.startsWith("Progressor")) {
|
|
17
|
+
await write(Progressor, "progressor", "tx", ProgressorCommands.STOP_WEIGHT_MEAS, 0)
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
}
|
package/src/stream.ts
CHANGED
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
|
-
import {
|
|
3
|
-
import { MotherboardCommands, TindeqCommands } from "./commands"
|
|
2
|
+
import { isConnected } from "./is-connected"
|
|
4
3
|
import { write } from "./write"
|
|
5
4
|
import { stop } from "./stop"
|
|
5
|
+
import { Motherboard, Progressor } from "./devices"
|
|
6
|
+
import { MotherboardCommands, ProgressorCommands } from "./commands"
|
|
7
|
+
import { CALIBRATION } from "./data"
|
|
8
|
+
import { calibration } from "./calibration"
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
11
|
* stream output
|
|
9
12
|
* @param board
|
|
10
13
|
*/
|
|
11
14
|
export const stream = async (board: Device, duration: number = 0): Promise<void> => {
|
|
12
|
-
if (
|
|
13
|
-
if (board.device.gatt?.connected) {
|
|
15
|
+
if (isConnected(board)) {
|
|
14
16
|
if (board.name === "Motherboard") {
|
|
15
|
-
//
|
|
17
|
+
// read calibration (required before reading data)
|
|
18
|
+
if (!CALIBRATION[0].length) {
|
|
19
|
+
await calibration(Motherboard)
|
|
20
|
+
}
|
|
16
21
|
// start stream
|
|
17
|
-
await write(Motherboard, "uart", "tx",
|
|
18
|
-
// end stream
|
|
22
|
+
await write(Motherboard, "uart", "tx", MotherboardCommands.START_WEIGHT_MEAS, duration)
|
|
23
|
+
// end stream if duration is set
|
|
19
24
|
if (duration !== 0) {
|
|
20
25
|
await stop(Motherboard)
|
|
21
26
|
}
|
|
22
27
|
}
|
|
23
|
-
if (board.name
|
|
28
|
+
if (board.name && board.name.startsWith("Progressor")) {
|
|
24
29
|
// start stream
|
|
25
|
-
await write(
|
|
26
|
-
// end stream
|
|
30
|
+
await write(Progressor, "progressor", "tx", ProgressorCommands.START_WEIGHT_MEAS, duration)
|
|
31
|
+
// end stream if duration is set
|
|
27
32
|
if (duration !== 0) {
|
|
28
|
-
await stop(
|
|
33
|
+
await stop(Progressor)
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
}
|
package/src/write.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
|
+
import { isConnected } from "./is-connected"
|
|
2
3
|
import { getCharacteristic } from "./characteristic"
|
|
4
|
+
|
|
5
|
+
export let lastWrite: string | null = null
|
|
3
6
|
/**
|
|
4
7
|
* write
|
|
5
8
|
* @param characteristic
|
|
@@ -9,19 +12,21 @@ export const write = (
|
|
|
9
12
|
board: Device,
|
|
10
13
|
serviceId: string,
|
|
11
14
|
characteristicId: string,
|
|
12
|
-
message: string,
|
|
15
|
+
message: string | undefined,
|
|
13
16
|
duration: number = 0,
|
|
14
17
|
): Promise<void> => {
|
|
15
18
|
return new Promise((resolve, reject) => {
|
|
16
|
-
if (board
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
if (isConnected(board)) {
|
|
20
|
+
if (!message) return
|
|
19
21
|
const characteristic = getCharacteristic(board, serviceId, characteristicId)
|
|
20
|
-
|
|
21
22
|
if (characteristic) {
|
|
23
|
+
const encoder = new TextEncoder()
|
|
22
24
|
characteristic
|
|
23
25
|
.writeValue(encoder.encode(message))
|
|
24
26
|
.then(() => {
|
|
27
|
+
// update last written message
|
|
28
|
+
lastWrite = message
|
|
29
|
+
// handle timeout
|
|
25
30
|
if (duration !== 0) {
|
|
26
31
|
setTimeout(() => {
|
|
27
32
|
resolve()
|
package/tsconfig.json
CHANGED
package/src/calibration.d.ts
DELETED
package/src/calibration.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Motherboard } from "./devices";
|
|
2
|
-
import { MotherboardCommands } from "./commands";
|
|
3
|
-
import { write } from "./write";
|
|
4
|
-
/**
|
|
5
|
-
* write command to get calibration
|
|
6
|
-
* @param board
|
|
7
|
-
*/
|
|
8
|
-
export const calibration = async (board) => {
|
|
9
|
-
if (!board.device)
|
|
10
|
-
return;
|
|
11
|
-
if (board.device.gatt?.connected) {
|
|
12
|
-
if (board.name === "Motherboard") {
|
|
13
|
-
await write(Motherboard, "uart", "tx", String(MotherboardCommands.GET_CALIBRATION), 2500);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
};
|
package/src/characteristic.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/// <reference types="web-bluetooth" />
|
|
2
|
-
import { Device } from "./devices/types";
|
|
3
|
-
/**
|
|
4
|
-
* getCharacteristic
|
|
5
|
-
* @param board
|
|
6
|
-
* @param serviceId
|
|
7
|
-
* @param characteristicId
|
|
8
|
-
*/
|
|
9
|
-
export declare const getCharacteristic: (board: Device, serviceId: string, characteristicId: string) => BluetoothRemoteGATTCharacteristic | undefined;
|
package/src/characteristic.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* getCharacteristic
|
|
3
|
-
* @param board
|
|
4
|
-
* @param serviceId
|
|
5
|
-
* @param characteristicId
|
|
6
|
-
*/
|
|
7
|
-
export const getCharacteristic = (board, serviceId, characteristicId) => {
|
|
8
|
-
const boardService = board.services.find((service) => service.id === serviceId);
|
|
9
|
-
if (boardService) {
|
|
10
|
-
const boardCharacteristic = boardService.characteristics.find((characteristic) => characteristic.id === characteristicId);
|
|
11
|
-
if (boardCharacteristic) {
|
|
12
|
-
return boardCharacteristic.characteristic;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
};
|
package/src/commands/climbro.js
DELETED
package/src/commands/entralpi.js
DELETED
package/src/commands/index.d.ts
DELETED
package/src/commands/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Warning:
|
|
3
|
-
* Using other commands can seriously harm your device
|
|
4
|
-
*/
|
|
5
|
-
export const MotherboardCommands = {
|
|
6
|
-
GET_SERIAL: "#",
|
|
7
|
-
START_WEIGHT_MEAS: "S30",
|
|
8
|
-
STOP_WEIGHT_MEAS: "", // All commands will stop the data stream.
|
|
9
|
-
GET_CALIBRATION: "C",
|
|
10
|
-
SLEEP: 0,
|
|
11
|
-
GET_TEXT: "T",
|
|
12
|
-
DEBUG_STREAM: "D",
|
|
13
|
-
};
|
package/src/commands/tindeq.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Commands } from "../commands/types";
|
|
2
|
-
/**
|
|
3
|
-
* Warning:
|
|
4
|
-
* Using other commands can seriously harm your device
|
|
5
|
-
*/
|
|
6
|
-
export declare const TindeqCommands: Commands;
|
|
7
|
-
export declare const NotificationTypes: {
|
|
8
|
-
COMMAND_RESPONSE: number;
|
|
9
|
-
WEIGHT_MEASURE: number;
|
|
10
|
-
LOW_BATTERY_WARNING: number;
|
|
11
|
-
};
|
package/src/commands/tindeq.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Warning:
|
|
3
|
-
* Using other commands can seriously harm your device
|
|
4
|
-
*/
|
|
5
|
-
export const TindeqCommands = {
|
|
6
|
-
TARE_SCALE: "d", // 0x64,
|
|
7
|
-
START_WEIGHT_MEAS: "e", // 0x65,
|
|
8
|
-
STOP_WEIGHT_MEAS: "f", // 0x66,
|
|
9
|
-
START_PEAK_RFD_MEAS: "g", // 0x67,
|
|
10
|
-
START_PEAK_RFD_MEAS_SERIES: "h", // 0x68,
|
|
11
|
-
ADD_CALIB_POINT: "i", // 0x69,
|
|
12
|
-
SAVE_CALIB: "j", // 0x6a,
|
|
13
|
-
GET_APP_VERSION: "k", // 0x6b,
|
|
14
|
-
GET_ERR_INFO: "l", // 0x6c,
|
|
15
|
-
CLR_ERR_INFO: "m", // 0x6d,
|
|
16
|
-
SLEEP: "n", // 0x6e,
|
|
17
|
-
GET_BATT_VLTG: "o", // 0x6f,
|
|
18
|
-
};
|
|
19
|
-
export const NotificationTypes = {
|
|
20
|
-
COMMAND_RESPONSE: 0,
|
|
21
|
-
WEIGHT_MEASURE: 1,
|
|
22
|
-
LOW_BATTERY_WARNING: 2,
|
|
23
|
-
};
|
package/src/commands/types.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export interface Commands {
|
|
2
|
-
START_WEIGHT_MEAS?: string;
|
|
3
|
-
STOP_WEIGHT_MEAS?: string;
|
|
4
|
-
SLEEP?: number | string;
|
|
5
|
-
GET_TEXT?: string;
|
|
6
|
-
GET_SERIAL?: string;
|
|
7
|
-
DEBUG_STREAM?: string;
|
|
8
|
-
GET_CALIBRATION?: string;
|
|
9
|
-
TARE_SCALE?: string;
|
|
10
|
-
START_PEAK_RFD_MEAS?: string;
|
|
11
|
-
START_PEAK_RFD_MEAS_SERIES?: string;
|
|
12
|
-
ADD_CALIB_POINT?: string;
|
|
13
|
-
SAVE_CALIB?: string;
|
|
14
|
-
GET_APP_VERSION?: string;
|
|
15
|
-
GET_ERR_INFO?: string;
|
|
16
|
-
CLR_ERR_INFO?: string;
|
|
17
|
-
GET_BATT_VLTG?: string;
|
|
18
|
-
}
|
package/src/commands/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/connect.d.ts
DELETED
package/src/connect.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { notifyCallback } from "./notify";
|
|
2
|
-
import { handleMotherboardData } from "./devices/motherboard";
|
|
3
|
-
import { handleEntralpiData } from "./devices/entralpi";
|
|
4
|
-
let server;
|
|
5
|
-
const receiveBuffer = [];
|
|
6
|
-
/**
|
|
7
|
-
* onDisconnected
|
|
8
|
-
* @param board
|
|
9
|
-
* @param event
|
|
10
|
-
*/
|
|
11
|
-
const onDisconnected = (event, board) => {
|
|
12
|
-
board.device = undefined;
|
|
13
|
-
const device = event.target;
|
|
14
|
-
console.log(`Device ${device.name} is disconnected.`);
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* handleNotifications
|
|
18
|
-
* @param event
|
|
19
|
-
* @param onNotify
|
|
20
|
-
*/
|
|
21
|
-
const handleNotifications = (event, board) => {
|
|
22
|
-
const characteristic = event.target;
|
|
23
|
-
const value = characteristic.value;
|
|
24
|
-
if (value) {
|
|
25
|
-
if (board.name === "Motherboard") {
|
|
26
|
-
for (let i = 0; i < value.byteLength; i++) {
|
|
27
|
-
receiveBuffer.push(value.getUint8(i));
|
|
28
|
-
}
|
|
29
|
-
let idx;
|
|
30
|
-
while ((idx = receiveBuffer.indexOf(10)) >= 0) {
|
|
31
|
-
const line = receiveBuffer.splice(0, idx + 1).slice(0, -1); // Combine and remove LF
|
|
32
|
-
if (line.length > 0 && line[line.length - 1] === 13)
|
|
33
|
-
line.pop(); // Remove CR
|
|
34
|
-
const decoder = new TextDecoder("utf-8");
|
|
35
|
-
const receivedData = decoder.decode(new Uint8Array(line));
|
|
36
|
-
handleMotherboardData(characteristic.uuid, receivedData);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else if (board.name === "ENTRALPI") {
|
|
40
|
-
if (value.buffer) {
|
|
41
|
-
const buffer = value.buffer;
|
|
42
|
-
const rawData = new DataView(buffer);
|
|
43
|
-
const receivedData = rawData.getUint16(0) / 100;
|
|
44
|
-
handleEntralpiData(characteristic.uuid, receivedData);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else if (board.name === "Tindeq") {
|
|
48
|
-
// TODO: handle Tindeq notify
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
if (notifyCallback) {
|
|
52
|
-
notifyCallback({ uuid: characteristic.uuid, value: value });
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* onConnected
|
|
59
|
-
* @param event
|
|
60
|
-
* @param board
|
|
61
|
-
*/
|
|
62
|
-
const onConnected = async (board, onSuccess) => {
|
|
63
|
-
try {
|
|
64
|
-
const services = await server?.getPrimaryServices();
|
|
65
|
-
if (!services || services.length === 0) {
|
|
66
|
-
console.error("No services found");
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
for (const service of services) {
|
|
70
|
-
const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid);
|
|
71
|
-
if (matchingService) {
|
|
72
|
-
// Android bug: Introduce a delay before getting characteristics
|
|
73
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
74
|
-
const characteristics = await service.getCharacteristics();
|
|
75
|
-
for (const characteristic of matchingService.characteristics) {
|
|
76
|
-
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
|
|
77
|
-
if (matchingCharacteristic) {
|
|
78
|
-
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
|
|
79
|
-
if (element) {
|
|
80
|
-
element.characteristic = matchingCharacteristic;
|
|
81
|
-
// notify
|
|
82
|
-
if (element.id === "rx") {
|
|
83
|
-
matchingCharacteristic.startNotifications();
|
|
84
|
-
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// Call the onSuccess callback after successful connection and setup
|
|
95
|
-
onSuccess();
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
console.error(error);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Return all service UUIDs
|
|
103
|
-
* @param device
|
|
104
|
-
*/
|
|
105
|
-
function getAllServiceUUIDs(device) {
|
|
106
|
-
return device.services.map((service) => service.uuid);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Connect to the BluetoothDevice
|
|
110
|
-
* @param device
|
|
111
|
-
* @param onSuccess
|
|
112
|
-
*/
|
|
113
|
-
export const connect = async (board, onSuccess) => {
|
|
114
|
-
try {
|
|
115
|
-
const deviceServices = getAllServiceUUIDs(board);
|
|
116
|
-
// setup filter list
|
|
117
|
-
const filters = [];
|
|
118
|
-
if (board.name) {
|
|
119
|
-
filters.push({
|
|
120
|
-
name: board.name,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
if (board.companyId) {
|
|
124
|
-
filters.push({
|
|
125
|
-
manufacturerData: [
|
|
126
|
-
{
|
|
127
|
-
companyIdentifier: board.companyId,
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
const device = await navigator.bluetooth.requestDevice({
|
|
133
|
-
filters: filters,
|
|
134
|
-
optionalServices: deviceServices,
|
|
135
|
-
});
|
|
136
|
-
board.device = device;
|
|
137
|
-
if (!board.device.gatt) {
|
|
138
|
-
console.error("GATT is not available on this device");
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
server = await board.device?.gatt?.connect();
|
|
142
|
-
board.device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board));
|
|
143
|
-
if (server.connected) {
|
|
144
|
-
await onConnected(board, onSuccess);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
console.error(error);
|
|
149
|
-
}
|
|
150
|
-
};
|
package/src/devices/climbro.d.ts
DELETED
package/src/devices/climbro.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Device } from "./types";
|
|
2
|
-
export declare const Entralpi: Device;
|
|
3
|
-
/**
|
|
4
|
-
* handleEntralpiData
|
|
5
|
-
* @param uuid - Unique identifier
|
|
6
|
-
* @param receivedData - Received data string
|
|
7
|
-
*/
|
|
8
|
-
export declare function handleEntralpiData(uuid: string, receivedData: number): void;
|