@hangtime/grip-connect 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,9 @@
1
- # Force-Sensing Climbing Training
1
+ # Grip Connect
2
2
 
3
- The objective of this project is to create a client that can establish connections with various Force-Sensing
4
- Hangboards/Plates used by climbers for strength measurement. Examples of such hangboards include the
3
+ **Force-Sensing Climbing Training**
4
+
5
+ The objective of this project is to create a client that can establish connections with various Force-Sensing Hangboards
6
+ / Plates used by climbers for strength measurement. Examples of such hangboards include the
5
7
  [Motherboard](https://griptonite.io/shop/motherboard/), [Climbro](https://climbro.com/),
6
8
  [SmartBoard](https://www.smartboard-climbing.com/), [Entralpi](https://entralpi.com/) or
7
9
  [Tindeq Progressor](https://tindeq.com/)
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1,15 @@
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
+ };
@@ -0,0 +1,7 @@
1
+ import { Device } from "./devices/types";
2
+ /**
3
+ * connect
4
+ * @param device
5
+ * @param onSuccess
6
+ */
7
+ export declare const connect: (board: Device, onSuccess: () => void) => Promise<void>;
@@ -0,0 +1,149 @@
1
+ import { notifyCallback } from "./notify";
2
+ /**
3
+ * onDisconnected
4
+ * @param board
5
+ * @param event
6
+ */
7
+ const onDisconnected = (event, board) => {
8
+ board.device = undefined;
9
+ const device = event.target;
10
+ console.log(`Device ${device.name} is disconnected.`);
11
+ };
12
+ /**
13
+ * handleNotifications
14
+ * @param event
15
+ * @param onNotify
16
+ */
17
+ const handleNotifications = (event, board) => {
18
+ const characteristic = event.target;
19
+ const receivedData = new Uint8Array(characteristic.value.buffer);
20
+ // Create an array to store the parsed decimal values
21
+ const decimalArray = [];
22
+ // Iterate through each byte and convert to decimal
23
+ for (let i = 0; i < receivedData.length; i++) {
24
+ decimalArray.push(receivedData[i]);
25
+ }
26
+ // Convert the decimal array to a string representation
27
+ const receivedString = String.fromCharCode(...decimalArray);
28
+ if (board.name === "Motherboard") {
29
+ // Split the string into pairs of characters
30
+ const hexPairs = receivedString.match(/.{1,2}/g);
31
+ // Convert each hexadecimal pair to decimal
32
+ const parsedDecimalArray = hexPairs?.map((hexPair) => parseInt(hexPair, 16));
33
+ // Handle different types of data
34
+ if (characteristic.value.byteLength === 20) {
35
+ const elementKeys = [
36
+ "frames",
37
+ "cycle",
38
+ "unknown",
39
+ "eleven",
40
+ "dynamic1",
41
+ "pressure1",
42
+ "left",
43
+ "dynamic2",
44
+ "pressure2",
45
+ "right",
46
+ ];
47
+ const dataObject = {};
48
+ if (parsedDecimalArray) {
49
+ elementKeys.forEach((key, index) => {
50
+ dataObject[key] = parsedDecimalArray[index];
51
+ });
52
+ }
53
+ if (notifyCallback) {
54
+ notifyCallback({ uuid: characteristic.uuid, value: dataObject });
55
+ }
56
+ }
57
+ else if (characteristic.value.byteLength === 14) {
58
+ // TODO: handle 14 byte data
59
+ // notifyCallback({ uuid: characteristic.uuid, value: characteristic.value!.getInt8(0) / 100 })
60
+ }
61
+ }
62
+ else if (board.name === "ENTRALPI") {
63
+ // TODO: handle Entralpi notify
64
+ // characteristic.value!.getInt16(0) / 100;
65
+ if (notifyCallback) {
66
+ notifyCallback({ uuid: characteristic.uuid, value: receivedString });
67
+ }
68
+ }
69
+ else if (board.name === "Tindeq") {
70
+ // TODO: handle Tindeq notify
71
+ }
72
+ else {
73
+ if (notifyCallback) {
74
+ notifyCallback({ uuid: characteristic.uuid, value: receivedString });
75
+ }
76
+ }
77
+ };
78
+ /**
79
+ * Return all service UUIDs
80
+ * @param device
81
+ */
82
+ function getAllServiceUUIDs(device) {
83
+ return device.services.map((service) => service.uuid);
84
+ }
85
+ /**
86
+ * connect
87
+ * @param device
88
+ * @param onSuccess
89
+ */
90
+ export const connect = async (board, onSuccess) => {
91
+ try {
92
+ const deviceServices = getAllServiceUUIDs(board);
93
+ // setup filter list
94
+ const filters = [];
95
+ if (board.name) {
96
+ filters.push({
97
+ name: board.name,
98
+ });
99
+ }
100
+ if (board.companyId) {
101
+ filters.push({
102
+ manufacturerData: [
103
+ {
104
+ companyIdentifier: board.companyId,
105
+ },
106
+ ],
107
+ });
108
+ }
109
+ const device = await navigator.bluetooth.requestDevice({
110
+ filters: filters,
111
+ optionalServices: deviceServices,
112
+ });
113
+ board.device = device;
114
+ device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board));
115
+ const server = await device.gatt?.connect();
116
+ const services = await server?.getPrimaryServices();
117
+ if (!services || services.length === 0) {
118
+ console.error("No services found");
119
+ return;
120
+ }
121
+ for (const service of services) {
122
+ const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid);
123
+ if (matchingService) {
124
+ const characteristics = await service.getCharacteristics();
125
+ for (const characteristic of matchingService.characteristics) {
126
+ const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
127
+ if (matchingCharacteristic) {
128
+ const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
129
+ if (element) {
130
+ element.characteristic = matchingCharacteristic;
131
+ // notify
132
+ if (element.id === "rx") {
133
+ matchingCharacteristic.startNotifications();
134
+ matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
135
+ }
136
+ }
137
+ }
138
+ else {
139
+ console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
140
+ }
141
+ }
142
+ }
143
+ }
144
+ onSuccess();
145
+ }
146
+ catch (error) {
147
+ console.error(error);
148
+ }
149
+ };
@@ -0,0 +1,2 @@
1
+ import { Device } from "./types";
2
+ export declare const Entralpi: Device;
@@ -0,0 +1,52 @@
1
+ export const Entralpi = {
2
+ name: "ENTRALPI",
3
+ services: [
4
+ {
5
+ name: "Device Information",
6
+ id: "device",
7
+ uuid: "0000180a-0000-1000-8000-00805f9b34fb",
8
+ characteristics: [],
9
+ },
10
+ {
11
+ name: "Battery Service",
12
+ id: "battery",
13
+ uuid: "0000180f-0000-1000-8000-00805f9b34fb",
14
+ characteristics: [],
15
+ },
16
+ {
17
+ name: "Generic Attribute",
18
+ id: "attribute",
19
+ uuid: "00001801-0000-1000-8000-00805f9b34fb",
20
+ characteristics: [],
21
+ },
22
+ {
23
+ name: "UART ISSC Transparent Service",
24
+ id: "uart",
25
+ uuid: "0000fff0-0000-1000-8000-00805f9b34fb",
26
+ characteristics: [
27
+ {
28
+ name: "TX",
29
+ id: "tx",
30
+ uuid: "0000fff5-0000-1000-8000-00805f9b34fb",
31
+ },
32
+ {
33
+ name: "RX",
34
+ id: "rx",
35
+ uuid: "0000fff4-0000-1000-8000-00805f9b34fb",
36
+ },
37
+ ],
38
+ },
39
+ {
40
+ name: "Weight Scale",
41
+ id: "weight",
42
+ uuid: "0000181d-0000-1000-8000-00805f9b34fb",
43
+ characteristics: [],
44
+ },
45
+ {
46
+ name: "Generic Access",
47
+ id: "access",
48
+ uuid: "00001800-0000-1000-8000-00805f9b34fb",
49
+ characteristics: [],
50
+ },
51
+ ],
52
+ };
@@ -0,0 +1,3 @@
1
+ export { Motherboard } from "./moterboard";
2
+ export { Entralpi } from "./entralpi";
3
+ export { Tindeq } from "./tindeq";
@@ -0,0 +1,3 @@
1
+ export { Motherboard } from "./moterboard";
2
+ export { Entralpi } from "./entralpi";
3
+ export { Tindeq } from "./tindeq";
@@ -0,0 +1,2 @@
1
+ import { Device } from "./types";
2
+ export declare const Motherboard: Device;
@@ -0,0 +1,79 @@
1
+ export const Motherboard = {
2
+ name: "Motherboard",
3
+ companyId: 0x2a29,
4
+ services: [
5
+ {
6
+ name: "Device Information",
7
+ id: "device",
8
+ uuid: "0000180a-0000-1000-8000-00805f9b34fb",
9
+ characteristics: [
10
+ // {
11
+ // name: 'Serial Number (Blocked)',
12
+ // id: 'serial'
13
+ // uuid: '00002a25-0000-1000-8000-00805f9b34fb'
14
+ // },
15
+ {
16
+ name: "Firmware Revision",
17
+ id: "firmware",
18
+ uuid: "00002a26-0000-1000-8000-00805f9b34fb",
19
+ },
20
+ {
21
+ name: "Hardware Revision",
22
+ id: "hardware",
23
+ uuid: "00002a27-0000-1000-8000-00805f9b34fb",
24
+ },
25
+ {
26
+ name: "Manufacturer Name",
27
+ id: "manufacturer",
28
+ uuid: "00002a29-0000-1000-8000-00805f9b34fb",
29
+ },
30
+ ],
31
+ },
32
+ {
33
+ name: "Battery Service",
34
+ id: "battery",
35
+ uuid: "0000180f-0000-1000-8000-00805f9b34fb",
36
+ characteristics: [
37
+ {
38
+ name: "Battery Level",
39
+ id: "level",
40
+ uuid: "00002a19-0000-1000-8000-00805f9b34fb",
41
+ },
42
+ ],
43
+ },
44
+ {
45
+ name: "Unknown Service",
46
+ id: "unknown",
47
+ uuid: "10ababcd-15e1-28ff-de13-725bea03b127",
48
+ characteristics: [
49
+ {
50
+ name: "Unknown 01",
51
+ id: "01",
52
+ uuid: "10ab1524-15e1-28ff-de13-725bea03b127",
53
+ },
54
+ {
55
+ name: "Unknown 02",
56
+ id: "02",
57
+ uuid: "10ab1525-15e1-28ff-de13-725bea03b127",
58
+ },
59
+ ],
60
+ },
61
+ {
62
+ name: "UART Nordic Service",
63
+ id: "uart",
64
+ uuid: "6e400001-b5a3-f393-e0a9-e50e24dcca9e",
65
+ characteristics: [
66
+ {
67
+ name: "TX",
68
+ id: "tx",
69
+ uuid: "6e400002-b5a3-f393-e0a9-e50e24dcca9e",
70
+ },
71
+ {
72
+ name: "RX",
73
+ id: "rx",
74
+ uuid: "6e400003-b5a3-f393-e0a9-e50e24dcca9e",
75
+ },
76
+ ],
77
+ },
78
+ ],
79
+ };
@@ -0,0 +1,17 @@
1
+ import { Device } from "./types";
2
+ export declare const Tindeq: Device;
3
+ export declare const Commands: {
4
+ TARE_SCALE: number;
5
+ START_MEASURING: number;
6
+ STOP_MEASURING: number;
7
+ GET_APP_VERSION: number;
8
+ GET_ERROR_INFO: number;
9
+ CLEAR_ERR_INFO: number;
10
+ GET_BATTERY_LEVEL: number;
11
+ SLEEP: number;
12
+ };
13
+ export declare const NotificationTypes: {
14
+ COMMAND_RESPONSE: number;
15
+ WEIGHT_MEASURE: number;
16
+ LOW_BATTERY_WARNING: number;
17
+ };
@@ -0,0 +1,37 @@
1
+ export const Tindeq = {
2
+ name: "Tindeq",
3
+ services: [
4
+ {
5
+ name: "Progressor Service",
6
+ id: "progressor",
7
+ uuid: "7e4e1701-1ea6-40c9-9dcc-13d34ffead57",
8
+ characteristics: [
9
+ {
10
+ name: "Write",
11
+ id: "tx",
12
+ uuid: "7e4e1703-1ea6-40c9-9dcc-13d34ffead57",
13
+ },
14
+ {
15
+ name: "Notify",
16
+ id: "rx",
17
+ uuid: "7e4e1702-1ea6-40c9-9dcc-13d34ffead57",
18
+ },
19
+ ],
20
+ },
21
+ ],
22
+ };
23
+ export const Commands = {
24
+ TARE_SCALE: 0x64,
25
+ START_MEASURING: 0x65,
26
+ STOP_MEASURING: 0x66,
27
+ GET_APP_VERSION: 0x6b,
28
+ GET_ERROR_INFO: 0x6c,
29
+ CLEAR_ERR_INFO: 0x6d,
30
+ GET_BATTERY_LEVEL: 0x6f,
31
+ SLEEP: 0x6e,
32
+ };
33
+ export const NotificationTypes = {
34
+ COMMAND_RESPONSE: 0,
35
+ WEIGHT_MEASURE: 1,
36
+ LOW_BATTERY_WARNING: 2,
37
+ };
@@ -0,0 +1,20 @@
1
+ /// <reference types="web-bluetooth" />
2
+ interface Characteristic {
3
+ name: string;
4
+ id: string;
5
+ uuid: string;
6
+ characteristic?: BluetoothRemoteGATTCharacteristic;
7
+ }
8
+ interface Service {
9
+ name: string;
10
+ id: string;
11
+ uuid: string;
12
+ characteristics: Characteristic[];
13
+ }
14
+ export interface Device {
15
+ name: string;
16
+ companyId?: number;
17
+ services: Service[];
18
+ device?: BluetoothDevice;
19
+ }
20
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Device } from "./devices/types";
2
+ /**
3
+ * disconnect
4
+ * @param board
5
+ */
6
+ export declare const disconnect: (board: Device) => void;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * disconnect
3
+ * @param board
4
+ */
5
+ export const disconnect = (board) => {
6
+ if (!board.device)
7
+ return;
8
+ if (board.device.gatt?.connected) {
9
+ board.device.gatt?.disconnect();
10
+ }
11
+ };
package/build/index.d.ts CHANGED
@@ -1,18 +1,6 @@
1
- /// <reference types="web-bluetooth" />
2
- interface Motherboard {
3
- device?: BluetoothDevice
4
- devSn?: BluetoothRemoteGATTCharacteristic
5
- devFr?: BluetoothRemoteGATTCharacteristic
6
- devHr?: BluetoothRemoteGATTCharacteristic
7
- devMn?: BluetoothRemoteGATTCharacteristic
8
- bat?: BluetoothRemoteGATTCharacteristic
9
- led01?: BluetoothRemoteGATTCharacteristic
10
- led02?: BluetoothRemoteGATTCharacteristic
11
- uartTx?: BluetoothRemoteGATTCharacteristic
12
- uartRx?: BluetoothRemoteGATTCharacteristic
13
- }
14
- declare const motherboard: Motherboard
15
- declare const connect: () => void
16
- declare const disconnect: () => void
17
- export default motherboard
18
- export { disconnect, connect }
1
+ export { Motherboard, Entralpi, Tindeq } from "./devices/index";
2
+ export { connect } from "./connect";
3
+ export { disconnect } from "./disconnect";
4
+ export { notify } from "./notify";
5
+ export { read } from "./read";
6
+ export { write } from "./write";
package/build/index.js CHANGED
@@ -1,175 +1,6 @@
1
- // Device service
2
- const DEVICE_SERVICE_UUID = "0000180a-0000-1000-8000-00805f9b34fb"
3
- const DEVICE_SN_CHARACTERISTIC_UUID = "00002a25-0000-1000-8000-00805f9b34fb"
4
- const DEVICE_FR_CHARACTERISTIC_UUID = "00002a26-0000-1000-8000-00805f9b34fb"
5
- const DEVICE_HR_CHARACTERISTIC_UUID = "00002a27-0000-1000-8000-00805f9b34fb"
6
- const DEVICE_MN_CHARACTERISTIC_UUID = "00002a29-0000-1000-8000-00805f9b34fb"
7
- // Battery service (Read / Notify)
8
- const BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb"
9
- const BATTERY_CHARACTERISTIC_UUID = "00002a19-0000-1000-8000-00805f9b34fb"
10
- // Led service
11
- const LED_SERVICE_UUID = "10ababcd-15e1-28ff-de13-725bea03b127"
12
- const LED_01_CHARACTERISTIC_UUID = "10ab1524-15e1-28ff-de13-725bea03b127"
13
- const LED_02_CHARACTERISTIC_UUID = "10ab1525-15e1-28ff-de13-725bea03b127"
14
- // An implementation of Nordic Semicondutor's UART/Serial Port Emulation over Bluetooth low energy
15
- const UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
16
- const UART_TX_CHARACTERISTIC_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
17
- const UART_RX_CHARACTERISTIC_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
18
- // https://github.com/sepp89117/GoPro_Web_RC/blob/main/GoPro_Web_RC.html
19
- const motherboard = {}
20
- const connect = () => {
21
- navigator.bluetooth
22
- .requestDevice({
23
- filters: [
24
- {
25
- name: "Motherboard",
26
- },
27
- {
28
- manufacturerData: [
29
- {
30
- companyIdentifier: 0x2a29,
31
- },
32
- ],
33
- },
34
- ],
35
- optionalServices: [DEVICE_SERVICE_UUID, BATTERY_SERVICE_UUID, UART_SERVICE_UUID, LED_SERVICE_UUID],
36
- })
37
- .then(async (device) => {
38
- motherboard.device = device
39
- device.addEventListener("gattserverdisconnected", onDisconnected)
40
- return device.gatt?.connect()
41
- })
42
- .then((server) => {
43
- return server?.getPrimaryServices()
44
- })
45
- .then((services) => {
46
- // console.log(services)
47
- if (services === null) {
48
- console.error("getPrimaryServices is 'null'")
49
- } else {
50
- if (services) {
51
- for (const service of services) {
52
- switch (service.uuid) {
53
- case DEVICE_SERVICE_UUID:
54
- // getCharacteristic(service, DEVICE_SN_CHARACTERISTIC_UUID) getCharacteristic(s) called with blocklisted UUID
55
- getCharacteristic(service, DEVICE_FR_CHARACTERISTIC_UUID)
56
- getCharacteristic(service, DEVICE_HR_CHARACTERISTIC_UUID)
57
- getCharacteristic(service, DEVICE_MN_CHARACTERISTIC_UUID)
58
- break
59
- case BATTERY_SERVICE_UUID:
60
- getCharacteristic(service, BATTERY_CHARACTERISTIC_UUID)
61
- break
62
- case LED_SERVICE_UUID:
63
- getCharacteristic(service, LED_01_CHARACTERISTIC_UUID)
64
- getCharacteristic(service, LED_02_CHARACTERISTIC_UUID)
65
- break
66
- case UART_SERVICE_UUID:
67
- getCharacteristic(service, UART_TX_CHARACTERISTIC_UUID)
68
- getCharacteristic(service, UART_RX_CHARACTERISTIC_UUID)
69
- break
70
- default:
71
- break
72
- }
73
- }
74
- }
75
- }
76
- })
77
- .catch((error) => {
78
- console.log(error)
79
- })
80
- }
81
- const disconnect = () => {
82
- if (!motherboard.device) return
83
- if (motherboard.device.gatt?.connected) {
84
- motherboard.device.gatt?.disconnect()
85
- }
86
- }
87
- const onDisconnected = (event) => {
88
- motherboard.device = undefined
89
- const device = event.target
90
- console.log(`Device ${device.name} is disconnected.`)
91
- }
92
- const handleNotifications = (event) => {
93
- const characteristic = event.target
94
- const receivedData = new Uint8Array(characteristic.value.buffer)
95
- // Create an array to store the parsed decimal values
96
- const decimalArray = []
97
- // Iterate through each byte and convert to decimal
98
- for (let i = 0; i < receivedData.length; i++) {
99
- decimalArray.push(receivedData[i])
100
- }
101
- // Convert the decimal array to a string representation
102
- const receivedString = String.fromCharCode(...decimalArray)
103
- // Split the string into pairs of characters
104
- const hexPairs = receivedString.match(/.{1,2}/g)
105
- // Convert each hexadecimal pair to decimal
106
- const parsedDecimalArray = hexPairs?.map((hexPair) => parseInt(hexPair, 16))
107
- // Handle different types of data
108
- if (characteristic.value.byteLength === 20) {
109
- // Define keys for the elements
110
- const elementKeys = [
111
- "frames",
112
- "cycle",
113
- "unknown",
114
- "eleven",
115
- "trippin1",
116
- "pressure1",
117
- "left",
118
- "trippin2",
119
- "pressure2",
120
- "right",
121
- ]
122
- // Create a single object with keys and values
123
- const dataObject = {}
124
- if (parsedDecimalArray) {
125
- elementKeys.forEach((key, index) => {
126
- dataObject[key] = parsedDecimalArray[index]
127
- })
128
- }
129
- // Print the formatted string on the screen
130
- console.log(dataObject)
131
- } else if (characteristic.value.byteLength === 14) {
132
- console.log(characteristic.value.byteLength, parsedDecimalArray)
133
- } else {
134
- console.log(characteristic.value.byteLength, parsedDecimalArray)
135
- }
136
- }
137
- const getCharacteristic = (service, uuid) => {
138
- service.getCharacteristic(uuid).then((characteristic) => {
139
- switch (characteristic.uuid) {
140
- case DEVICE_SN_CHARACTERISTIC_UUID:
141
- motherboard.devSn = characteristic
142
- break
143
- case DEVICE_FR_CHARACTERISTIC_UUID:
144
- motherboard.devFr = characteristic
145
- break
146
- case DEVICE_HR_CHARACTERISTIC_UUID:
147
- motherboard.devHr = characteristic
148
- break
149
- case DEVICE_MN_CHARACTERISTIC_UUID:
150
- motherboard.devMn = characteristic
151
- break
152
- case BATTERY_CHARACTERISTIC_UUID:
153
- motherboard.bat = characteristic
154
- break
155
- case LED_01_CHARACTERISTIC_UUID:
156
- motherboard.led01 = characteristic
157
- break
158
- case LED_02_CHARACTERISTIC_UUID:
159
- motherboard.led02 = characteristic
160
- break
161
- case UART_TX_CHARACTERISTIC_UUID:
162
- motherboard.uartTx = characteristic
163
- break
164
- case UART_RX_CHARACTERISTIC_UUID:
165
- motherboard.uartRx = characteristic
166
- motherboard.uartRx.startNotifications()
167
- motherboard.uartRx.addEventListener("characteristicvaluechanged", handleNotifications)
168
- break
169
- default:
170
- break
171
- }
172
- })
173
- }
174
- export default motherboard
175
- export { disconnect, connect }
1
+ export { Motherboard, Entralpi, Tindeq } from "./devices/index";
2
+ export { connect } from "./connect";
3
+ export { disconnect } from "./disconnect";
4
+ export { notify } from "./notify";
5
+ export { read } from "./read";
6
+ export { write } from "./write";
@@ -0,0 +1,4 @@
1
+ type NotifyCallback = (data: object) => void;
2
+ export declare let notifyCallback: NotifyCallback;
3
+ export declare const notify: (callback: NotifyCallback) => void;
4
+ export {};
@@ -0,0 +1,6 @@
1
+ // Initialize the callback variable
2
+ export let notifyCallback;
3
+ // Export a function to set the callback
4
+ export const notify = (callback) => {
5
+ notifyCallback = callback;
6
+ };
@@ -0,0 +1,6 @@
1
+ import { Device } from "./devices/types";
2
+ /**
3
+ * read
4
+ * @param characteristic
5
+ */
6
+ export declare const read: (board: Device, serviceId: string, characteristicId: string) => Promise<void>;
package/build/read.js ADDED
@@ -0,0 +1,40 @@
1
+ import { notifyCallback } from "./notify";
2
+ import { getCharacteristic } from "./characteristic";
3
+ /**
4
+ * read
5
+ * @param characteristic
6
+ */
7
+ export const read = (board, serviceId, characteristicId) => {
8
+ return new Promise((resolve, reject) => {
9
+ if (board.device?.gatt?.connected) {
10
+ const characteristic = getCharacteristic(board, serviceId, characteristicId);
11
+ if (characteristic) {
12
+ characteristic
13
+ .readValue()
14
+ .then((value) => {
15
+ let decodedValue;
16
+ const decoder = new TextDecoder("utf-8");
17
+ switch (characteristicId) {
18
+ case "level":
19
+ decodedValue = value.getUint8(0);
20
+ break;
21
+ default:
22
+ decodedValue = decoder.decode(value);
23
+ break;
24
+ }
25
+ notifyCallback({ uuid: characteristic.uuid, value: decodedValue });
26
+ resolve();
27
+ })
28
+ .catch((error) => {
29
+ reject(error);
30
+ });
31
+ }
32
+ else {
33
+ reject(new Error("Characteristic is undefined"));
34
+ }
35
+ }
36
+ else {
37
+ reject(new Error("Device is not connected"));
38
+ }
39
+ });
40
+ };
@@ -0,0 +1,7 @@
1
+ import { Device } from "./devices/types";
2
+ /**
3
+ * write
4
+ * @param characteristic
5
+ * @param message
6
+ */
7
+ export declare const write: (board: Device, serviceId: string, characteristicId: string, message: string, duration?: number) => Promise<void>;
package/build/write.js ADDED
@@ -0,0 +1,32 @@
1
+ import { getCharacteristic } from "./characteristic";
2
+ /**
3
+ * write
4
+ * @param characteristic
5
+ * @param message
6
+ */
7
+ export const write = (board, serviceId, characteristicId, message, duration = 0) => {
8
+ return new Promise((resolve, reject) => {
9
+ if (board.device?.gatt?.connected) {
10
+ const encoder = new TextEncoder();
11
+ const characteristic = getCharacteristic(board, serviceId, characteristicId);
12
+ if (characteristic) {
13
+ characteristic
14
+ .writeValue(encoder.encode(message))
15
+ .then(() => {
16
+ setTimeout(() => {
17
+ resolve();
18
+ }, duration);
19
+ })
20
+ .catch((error) => {
21
+ reject(error);
22
+ });
23
+ }
24
+ else {
25
+ reject(new Error("Characteristics is undefined"));
26
+ }
27
+ }
28
+ else {
29
+ reject(new Error("Device is not connected"));
30
+ }
31
+ });
32
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hangtime/grip-connect",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
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 Motherboard, Climbro, SmartBoard, Entralpi or Tindeq Progressor",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
@@ -0,0 +1,7 @@
1
+ import { Device } from "./devices/types";
2
+ /**
3
+ * connect
4
+ * @param device
5
+ * @param onSuccess
6
+ */
7
+ export declare const connect: (board: Device, onSuccess: () => void) => Promise<void>;
package/src/connect.js ADDED
@@ -0,0 +1,149 @@
1
+ import { notifyCallback } from "./notify";
2
+ /**
3
+ * onDisconnected
4
+ * @param board
5
+ * @param event
6
+ */
7
+ const onDisconnected = (event, board) => {
8
+ board.device = undefined;
9
+ const device = event.target;
10
+ console.log(`Device ${device.name} is disconnected.`);
11
+ };
12
+ /**
13
+ * handleNotifications
14
+ * @param event
15
+ * @param onNotify
16
+ */
17
+ const handleNotifications = (event, board) => {
18
+ const characteristic = event.target;
19
+ const receivedData = new Uint8Array(characteristic.value.buffer);
20
+ // Create an array to store the parsed decimal values
21
+ const decimalArray = [];
22
+ // Iterate through each byte and convert to decimal
23
+ for (let i = 0; i < receivedData.length; i++) {
24
+ decimalArray.push(receivedData[i]);
25
+ }
26
+ // Convert the decimal array to a string representation
27
+ const receivedString = String.fromCharCode(...decimalArray);
28
+ if (board.name === "Motherboard") {
29
+ // Split the string into pairs of characters
30
+ const hexPairs = receivedString.match(/.{1,2}/g);
31
+ // Convert each hexadecimal pair to decimal
32
+ const parsedDecimalArray = hexPairs?.map((hexPair) => parseInt(hexPair, 16));
33
+ // Handle different types of data
34
+ if (characteristic.value.byteLength === 20) {
35
+ const elementKeys = [
36
+ "frames",
37
+ "cycle",
38
+ "unknown",
39
+ "eleven",
40
+ "dynamic1",
41
+ "pressure1",
42
+ "left",
43
+ "dynamic2",
44
+ "pressure2",
45
+ "right",
46
+ ];
47
+ const dataObject = {};
48
+ if (parsedDecimalArray) {
49
+ elementKeys.forEach((key, index) => {
50
+ dataObject[key] = parsedDecimalArray[index];
51
+ });
52
+ }
53
+ if (notifyCallback) {
54
+ notifyCallback({ uuid: characteristic.uuid, value: dataObject });
55
+ }
56
+ }
57
+ else if (characteristic.value.byteLength === 14) {
58
+ // TODO: handle 14 byte data
59
+ // notifyCallback({ uuid: characteristic.uuid, value: characteristic.value!.getInt8(0) / 100 })
60
+ }
61
+ }
62
+ else if (board.name === "ENTRALPI") {
63
+ // TODO: handle Entralpi notify
64
+ // characteristic.value!.getInt16(0) / 100;
65
+ if (notifyCallback) {
66
+ notifyCallback({ uuid: characteristic.uuid, value: receivedString });
67
+ }
68
+ }
69
+ else if (board.name === "Tindeq") {
70
+ // TODO: handle Tindeq notify
71
+ }
72
+ else {
73
+ if (notifyCallback) {
74
+ notifyCallback({ uuid: characteristic.uuid, value: receivedString });
75
+ }
76
+ }
77
+ };
78
+ /**
79
+ * Return all service UUIDs
80
+ * @param device
81
+ */
82
+ function getAllServiceUUIDs(device) {
83
+ return device.services.map((service) => service.uuid);
84
+ }
85
+ /**
86
+ * connect
87
+ * @param device
88
+ * @param onSuccess
89
+ */
90
+ export const connect = async (board, onSuccess) => {
91
+ try {
92
+ const deviceServices = getAllServiceUUIDs(board);
93
+ // setup filter list
94
+ const filters = [];
95
+ if (board.name) {
96
+ filters.push({
97
+ name: board.name,
98
+ });
99
+ }
100
+ if (board.companyId) {
101
+ filters.push({
102
+ manufacturerData: [
103
+ {
104
+ companyIdentifier: board.companyId,
105
+ },
106
+ ],
107
+ });
108
+ }
109
+ const device = await navigator.bluetooth.requestDevice({
110
+ filters: filters,
111
+ optionalServices: deviceServices,
112
+ });
113
+ board.device = device;
114
+ device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board));
115
+ const server = await device.gatt?.connect();
116
+ const services = await server?.getPrimaryServices();
117
+ if (!services || services.length === 0) {
118
+ console.error("No services found");
119
+ return;
120
+ }
121
+ for (const service of services) {
122
+ const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid);
123
+ if (matchingService) {
124
+ const characteristics = await service.getCharacteristics();
125
+ for (const characteristic of matchingService.characteristics) {
126
+ const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
127
+ if (matchingCharacteristic) {
128
+ const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
129
+ if (element) {
130
+ element.characteristic = matchingCharacteristic;
131
+ // notify
132
+ if (element.id === "rx") {
133
+ matchingCharacteristic.startNotifications();
134
+ matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
135
+ }
136
+ }
137
+ }
138
+ else {
139
+ console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
140
+ }
141
+ }
142
+ }
143
+ }
144
+ onSuccess();
145
+ }
146
+ catch (error) {
147
+ console.error(error);
148
+ }
149
+ };
package/src/connect.ts CHANGED
@@ -48,12 +48,12 @@ const handleNotifications = (event: Event, board: Device): void => {
48
48
  "pressure2",
49
49
  "right",
50
50
  ]
51
- const dataObject = {}
51
+ const dataObject: { [key: string]: number } = {};
52
52
 
53
53
  if (parsedDecimalArray) {
54
- elementKeys.forEach((key, index) => {
55
- dataObject[key] = parsedDecimalArray[index]
56
- })
54
+ elementKeys.forEach((key: string, index: number) => {
55
+ dataObject[key] = parsedDecimalArray[index];
56
+ });
57
57
  }
58
58
  if (notifyCallback) {
59
59
  notifyCallback({ uuid: characteristic.uuid, value: dataObject })