@hangtime/grip-connect 0.5.1 → 0.5.2
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 +8 -17
- package/dist/{is-device.d.ts → helpers/is-device.d.ts} +1 -1
- package/dist/{is-device.js → helpers/is-device.js} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/interfaces/device.interface.d.ts +45 -3
- package/dist/models/device/entralpi.model.js +9 -10
- package/dist/models/device/forceboard.model.js +3 -4
- package/dist/models/device/kilterboard.model.js +1 -2
- package/dist/models/device/motherboard.model.js +13 -16
- package/dist/models/device/progressor.model.js +12 -13
- package/dist/models/device.model.d.ts +45 -0
- package/dist/models/device.model.js +116 -0
- package/package.json +1 -1
- package/src/{is-device.ts → helpers/is-device.ts} +2 -2
- package/src/index.ts +2 -2
- package/src/interfaces/device.interface.ts +55 -3
- package/src/models/device/entralpi.model.ts +9 -10
- package/src/models/device/forceboard.model.ts +3 -4
- package/src/models/device/kilterboard.model.ts +1 -2
- package/src/models/device/motherboard.model.ts +13 -16
- package/src/models/device/progressor.model.ts +12 -13
- package/src/models/device.model.ts +128 -0
- package/dist/characteristic.d.ts +0 -9
- package/dist/characteristic.js +0 -21
- package/dist/read.d.ts +0 -10
- package/dist/read.js +0 -43
- package/dist/write.d.ts +0 -34
- package/dist/write.js +0 -58
- package/src/characteristic.ts +0 -29
- package/src/read.ts +0 -45
- package/src/write.ts +0 -74
- /package/dist/{struct/index.d.ts → helpers/struct.d.ts} +0 -0
- /package/dist/{struct/index.js → helpers/struct.js} +0 -0
- /package/src/{struct/index.ts → helpers/struct.ts} +0 -0
|
@@ -57,6 +57,26 @@ export class Device extends BaseModel {
|
|
|
57
57
|
getAllServiceUUIDs = () => {
|
|
58
58
|
return this.services.map((service) => service.uuid);
|
|
59
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the characteristic from the device's service.
|
|
62
|
+
* @param {string} serviceId - The UUID of the service.
|
|
63
|
+
* @param {string} characteristicId - The UUID of the characteristic.
|
|
64
|
+
* @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
|
|
65
|
+
*/
|
|
66
|
+
getCharacteristic = (serviceId, characteristicId) => {
|
|
67
|
+
// Find the service with the specified serviceId
|
|
68
|
+
const boardService = this.services.find((service) => service.id === serviceId);
|
|
69
|
+
if (boardService) {
|
|
70
|
+
// If the service is found, find the characteristic with the specified characteristicId
|
|
71
|
+
const boardCharacteristic = boardService.characteristics.find((characteristic) => characteristic.id === characteristicId);
|
|
72
|
+
if (boardCharacteristic) {
|
|
73
|
+
// If the characteristic is found, return it
|
|
74
|
+
return boardCharacteristic.characteristic;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Return undefined if the service or characteristic is not found
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
60
80
|
/**
|
|
61
81
|
* Handles notifications received from a characteristic.
|
|
62
82
|
* @param {Event} event - The notification event.
|
|
@@ -151,4 +171,100 @@ export class Device extends BaseModel {
|
|
|
151
171
|
const device = event.target;
|
|
152
172
|
throw new Error(`Device ${device.name} is disconnected.`);
|
|
153
173
|
};
|
|
174
|
+
/**
|
|
175
|
+
* Reads the value of the specified characteristic from the device.
|
|
176
|
+
* @param {string} serviceId - The service ID where the characteristic belongs.
|
|
177
|
+
* @param {string} characteristicId - The characteristic ID to read from.
|
|
178
|
+
* @param {number} [duration=0] - The duration to wait before resolving the promise, in milliseconds.
|
|
179
|
+
* @returns {Promise<string>} A promise that resolves when the read operation is completed.
|
|
180
|
+
*/
|
|
181
|
+
read = (serviceId, characteristicId, duration = 0) => {
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
if (this.isConnected()) {
|
|
184
|
+
const characteristic = this.getCharacteristic(serviceId, characteristicId);
|
|
185
|
+
if (characteristic) {
|
|
186
|
+
characteristic
|
|
187
|
+
.readValue()
|
|
188
|
+
.then((value) => {
|
|
189
|
+
let decodedValue;
|
|
190
|
+
const decoder = new TextDecoder("utf-8");
|
|
191
|
+
switch (characteristicId) {
|
|
192
|
+
case "level":
|
|
193
|
+
// TODO: This is battery specific.
|
|
194
|
+
decodedValue = value.getUint8(0).toString();
|
|
195
|
+
break;
|
|
196
|
+
default:
|
|
197
|
+
decodedValue = decoder.decode(value);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
// Resolve after specified duration
|
|
201
|
+
setTimeout(() => {
|
|
202
|
+
return resolve(decodedValue);
|
|
203
|
+
}, duration);
|
|
204
|
+
})
|
|
205
|
+
.catch((error) => {
|
|
206
|
+
reject(error);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
reject(new Error("Characteristic is undefined"));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Writes a message to the specified characteristic of a Bluetooth device and optionally provides a callback to handle responses.
|
|
217
|
+
* @param {string} serviceId - The service UUID of the Bluetooth device containing the target characteristic.
|
|
218
|
+
* @param {string} characteristicId - The characteristic UUID where the message will be written.
|
|
219
|
+
* @param {string | Uint8Array | undefined} message - The message to be written to the characteristic. It can be a string or a Uint8Array.
|
|
220
|
+
* @param {number} [duration=0] - Optional. The time in milliseconds to wait before resolving the promise. Defaults to 0 for immediate resolution.
|
|
221
|
+
* @param {WriteCallback} [callback=writeCallback] - Optional. A custom callback to handle the response after the write operation is successful.
|
|
222
|
+
*
|
|
223
|
+
* @returns {Promise<void>} A promise that resolves once the write operation is complete.
|
|
224
|
+
*
|
|
225
|
+
* @throws {Error} Throws an error if the characteristic is undefined.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* // Example usage of the write function with a custom callback
|
|
229
|
+
* await Progressor.write("progressor", "tx", ProgressorCommands.GET_BATT_VLTG, 250, (data) => {
|
|
230
|
+
* console.log(`Battery voltage: ${data}`);
|
|
231
|
+
* });
|
|
232
|
+
*/
|
|
233
|
+
write = async (serviceId, characteristicId, message, duration = 0, callback = this.writeCallback) => {
|
|
234
|
+
if (this.isConnected()) {
|
|
235
|
+
// Check if message is provided
|
|
236
|
+
if (message === undefined) {
|
|
237
|
+
// If not provided, return without performing write operation
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
// Get the characteristic from the device using serviceId and characteristicId
|
|
241
|
+
const characteristic = this.getCharacteristic(serviceId, characteristicId);
|
|
242
|
+
if (!characteristic) {
|
|
243
|
+
throw new Error("Characteristic is undefined");
|
|
244
|
+
}
|
|
245
|
+
// Convert the message to Uint8Array if it's a string
|
|
246
|
+
const valueToWrite = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
247
|
+
// Write the value to the characteristic
|
|
248
|
+
await characteristic.writeValue(valueToWrite);
|
|
249
|
+
// Update the last written message
|
|
250
|
+
this.writeLast = message;
|
|
251
|
+
// Assign the provided callback to `writeCallback`
|
|
252
|
+
this.writeCallback = callback;
|
|
253
|
+
// If a duration is specified, resolve the promise after the duration
|
|
254
|
+
if (duration > 0) {
|
|
255
|
+
await new Promise((resolve) => setTimeout(resolve, duration));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* A default write callback that logs the response
|
|
261
|
+
*/
|
|
262
|
+
writeCallback = (data) => {
|
|
263
|
+
console.log(data);
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* The last message written to the device.
|
|
267
|
+
* @type {string | Uint8Array | null}
|
|
268
|
+
*/
|
|
269
|
+
writeLast = null;
|
|
154
270
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hangtime/grip-connect",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Griptonite Motherboard, Tindeq Progressor, PitchSix Force Board, WHC-06, Entralpi, Climbro, mySmartBoard: Web Bluetooth API Force-Sensing strength analysis for climbers",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Device } from "
|
|
2
|
-
import { AuroraUUID } from "
|
|
1
|
+
import type { Device } from "./../models/device.model"
|
|
2
|
+
import { AuroraUUID } from "./../models/device/kilterboard.model"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Checks if the given device is an Entralpi device.
|
package/src/index.ts
CHANGED
|
@@ -10,9 +10,9 @@ export {
|
|
|
10
10
|
} from "./models/index"
|
|
11
11
|
|
|
12
12
|
// helpers
|
|
13
|
-
export { isEntralpi, isKilterboard, isMotherboard, isWHC06, isProgressor } from "./is-device"
|
|
13
|
+
export { isEntralpi, isKilterboard, isMotherboard, isWHC06, isProgressor } from "./helpers/is-device"
|
|
14
14
|
|
|
15
|
-
// functions
|
|
15
|
+
// TODO: Make functions device specific
|
|
16
16
|
export { download } from "./download"
|
|
17
17
|
|
|
18
18
|
export { active, isActive } from "./is-active"
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { IBase } from "./base.interface"
|
|
2
2
|
import type { massObject } from "../types/notify"
|
|
3
3
|
|
|
4
|
-
type NotifyCallback = (data: massObject) => void
|
|
5
4
|
/**
|
|
6
5
|
* Represents a characteristic of a Bluetooth service.
|
|
7
6
|
*/
|
|
@@ -61,6 +60,14 @@ export interface IDevice extends IBase {
|
|
|
61
60
|
*/
|
|
62
61
|
getAllServiceUUIDs(): string[]
|
|
63
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Retrieves the characteristic from the device's service.
|
|
65
|
+
* @param {string} serviceId - The UUID of the service.
|
|
66
|
+
* @param {string} characteristicId - The UUID of the characteristic.
|
|
67
|
+
* @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
|
|
68
|
+
*/
|
|
69
|
+
getCharacteristic(serviceId: string, characteristicId: string): BluetoothRemoteGATTCharacteristic | undefined
|
|
70
|
+
|
|
64
71
|
/**
|
|
65
72
|
* Handles notifications received from a characteristic.
|
|
66
73
|
* @param {Event} event - The notification event.
|
|
@@ -78,14 +85,14 @@ export interface IDevice extends IBase {
|
|
|
78
85
|
* @param {NotifyCallback} callback - The callback function to be set.
|
|
79
86
|
* @returns {void}
|
|
80
87
|
*/
|
|
81
|
-
notify(callback:
|
|
88
|
+
notify(callback: (data: massObject) => void): void
|
|
82
89
|
|
|
83
90
|
/**
|
|
84
91
|
* Defines the type for the callback function.
|
|
85
92
|
* @callback NotifyCallback
|
|
86
93
|
* @param {massObject} data - The data passed to the callback.
|
|
87
94
|
*/
|
|
88
|
-
notifyCallback:
|
|
95
|
+
notifyCallback: (data: massObject) => void
|
|
89
96
|
|
|
90
97
|
/**
|
|
91
98
|
* Handles the 'connected' event.
|
|
@@ -98,4 +105,49 @@ export interface IDevice extends IBase {
|
|
|
98
105
|
* @param {Event} event - The 'disconnected' event.
|
|
99
106
|
*/
|
|
100
107
|
onDisconnected(event: Event): void
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Reads the value of the specified characteristic from the device.
|
|
111
|
+
* @param {string} serviceId - The service ID where the characteristic belongs.
|
|
112
|
+
* @param {string} characteristicId - The characteristic ID to read from.
|
|
113
|
+
* @param {number} [duration=0] - The duration to wait before resolving the promise, in milliseconds.
|
|
114
|
+
* @returns {Promise<string>} A promise that resolves when the read operation is completed.
|
|
115
|
+
*/
|
|
116
|
+
read(serviceId: string, characteristicId: string, duration?: number): Promise<string>
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Writes a message to the specified characteristic of a Bluetooth device and optionally provides a callback to handle responses.
|
|
120
|
+
* @param {string} serviceId - The service UUID of the Bluetooth device containing the target characteristic.
|
|
121
|
+
* @param {string} characteristicId - The characteristic UUID where the message will be written.
|
|
122
|
+
* @param {string | Uint8Array | undefined} message - The message to be written to the characteristic. It can be a string or a Uint8Array.
|
|
123
|
+
* @param {number} [duration=0] - Optional. The time in milliseconds to wait before resolving the promise. Defaults to 0 for immediate resolution.
|
|
124
|
+
* @param {WriteCallback} [callback=writeCallback] - Optional. A custom callback to handle the response after the write operation is successful.
|
|
125
|
+
*
|
|
126
|
+
* @returns {Promise<void>} A promise that resolves once the write operation is complete.
|
|
127
|
+
*
|
|
128
|
+
* @throws {Error} Throws an error if the characteristic is undefined.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* // Example usage of the write function with a custom callback
|
|
132
|
+
* await Progressor.write("progressor", "tx", ProgressorCommands.GET_BATT_VLTG, 250, (data) => {
|
|
133
|
+
* console.log(`Battery voltage: ${data}`);
|
|
134
|
+
* });
|
|
135
|
+
*/
|
|
136
|
+
write(
|
|
137
|
+
serviceId: string,
|
|
138
|
+
characteristicId: string,
|
|
139
|
+
message: string | Uint8Array | undefined,
|
|
140
|
+
duration?: number,
|
|
141
|
+
callback?: (data: string) => void,
|
|
142
|
+
): Promise<void>
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A default write callback that logs the response
|
|
146
|
+
*/
|
|
147
|
+
writeCallback: (data: string) => void
|
|
148
|
+
/**
|
|
149
|
+
* The last message written to the device.
|
|
150
|
+
* @type {string | Uint8Array | null}
|
|
151
|
+
*/
|
|
152
|
+
writeLast: string | Uint8Array | null
|
|
101
153
|
}
|
|
@@ -2,7 +2,6 @@ import { Device } from "../device.model"
|
|
|
2
2
|
import type { IEntralpi } from "../../interfaces/device/entralpi.interface"
|
|
3
3
|
import { applyTare } from "../../tare"
|
|
4
4
|
import { checkActivity } from "../../is-active"
|
|
5
|
-
import { read } from "../../read"
|
|
6
5
|
|
|
7
6
|
// Constants
|
|
8
7
|
let MASS_MAX = "0"
|
|
@@ -139,7 +138,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
139
138
|
*/
|
|
140
139
|
battery = async (): Promise<string | undefined> => {
|
|
141
140
|
if (this.isConnected()) {
|
|
142
|
-
return await read(
|
|
141
|
+
return await this.read("battery", "level", 250)
|
|
143
142
|
}
|
|
144
143
|
// If device is not found, return undefined
|
|
145
144
|
return undefined
|
|
@@ -153,7 +152,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
153
152
|
// Check if the device is connected
|
|
154
153
|
if (this.isConnected()) {
|
|
155
154
|
// Read certification from the device
|
|
156
|
-
return await read(
|
|
155
|
+
return await this.read("device", "certification", 250)
|
|
157
156
|
}
|
|
158
157
|
// If device is not found, return undefined
|
|
159
158
|
return undefined
|
|
@@ -167,7 +166,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
167
166
|
// Check if the device is connected
|
|
168
167
|
if (this.isConnected()) {
|
|
169
168
|
// Read firmware version from the Motherboard
|
|
170
|
-
return await read(
|
|
169
|
+
return await this.read("device", "firmware", 250)
|
|
171
170
|
}
|
|
172
171
|
// If device is not found, return undefined
|
|
173
172
|
return undefined
|
|
@@ -227,7 +226,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
227
226
|
// Check if the device is connected
|
|
228
227
|
if (this.isConnected()) {
|
|
229
228
|
// Read hardware version from the device
|
|
230
|
-
return await read(
|
|
229
|
+
return await this.read("device", "hardware", 250)
|
|
231
230
|
}
|
|
232
231
|
// If device is not found, return undefined
|
|
233
232
|
return undefined
|
|
@@ -241,7 +240,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
241
240
|
// Check if the device is connected
|
|
242
241
|
if (this.isConnected()) {
|
|
243
242
|
// Read manufacturer information from the device
|
|
244
|
-
return await read(
|
|
243
|
+
return await this.read("device", "manufacturer", 250)
|
|
245
244
|
}
|
|
246
245
|
// If device is not found, return undefined
|
|
247
246
|
return undefined
|
|
@@ -255,7 +254,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
255
254
|
// Check if the device is connected
|
|
256
255
|
if (this.isConnected()) {
|
|
257
256
|
// Read model number from the Entralpi
|
|
258
|
-
return await read(
|
|
257
|
+
return await this.read("device", "model", 250)
|
|
259
258
|
}
|
|
260
259
|
// If device is not found, return undefined
|
|
261
260
|
return undefined
|
|
@@ -270,7 +269,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
270
269
|
// Check if the device is connected
|
|
271
270
|
if (this.isConnected()) {
|
|
272
271
|
// Read software version from the Entralpi
|
|
273
|
-
return await read(
|
|
272
|
+
return await this.read("device", "pnp", 250)
|
|
274
273
|
}
|
|
275
274
|
// If device is not found, return undefined
|
|
276
275
|
return undefined
|
|
@@ -284,7 +283,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
284
283
|
// Check if the device is connected
|
|
285
284
|
if (this.isConnected()) {
|
|
286
285
|
// Read software version from the Entralpi
|
|
287
|
-
return await read(
|
|
286
|
+
return await this.read("device", "software", 250)
|
|
288
287
|
}
|
|
289
288
|
// If device is not found, return undefined
|
|
290
289
|
return undefined
|
|
@@ -298,7 +297,7 @@ export class Entralpi extends Device implements IEntralpi {
|
|
|
298
297
|
// Check if the device is connected
|
|
299
298
|
if (this.isConnected()) {
|
|
300
299
|
// Read system id from the device
|
|
301
|
-
return await read(
|
|
300
|
+
return await this.read("device", "system", 250)
|
|
302
301
|
}
|
|
303
302
|
// If device is not found, return undefined
|
|
304
303
|
return undefined
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Device } from "../device.model"
|
|
2
2
|
import type { IForceBoard } from "../../interfaces/device/forceboard.interface"
|
|
3
|
-
import { read } from "../../read"
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Represents a PitchSix Force Board device
|
|
@@ -171,7 +170,7 @@ export class ForceBoard extends Device implements IForceBoard {
|
|
|
171
170
|
*/
|
|
172
171
|
battery = async (): Promise<string | undefined> => {
|
|
173
172
|
if (this.isConnected()) {
|
|
174
|
-
return await read(
|
|
173
|
+
return await this.read("battery", "level", 250)
|
|
175
174
|
}
|
|
176
175
|
// If device is not found, return undefined
|
|
177
176
|
return undefined
|
|
@@ -184,7 +183,7 @@ export class ForceBoard extends Device implements IForceBoard {
|
|
|
184
183
|
humidity = async (): Promise<string | undefined> => {
|
|
185
184
|
// Check if the device is connected
|
|
186
185
|
if (this.isConnected()) {
|
|
187
|
-
return await read(
|
|
186
|
+
return await this.read("humidity", "level", 250)
|
|
188
187
|
}
|
|
189
188
|
// If device is not found, return undefined
|
|
190
189
|
return undefined
|
|
@@ -198,7 +197,7 @@ export class ForceBoard extends Device implements IForceBoard {
|
|
|
198
197
|
// Check if the device is connected
|
|
199
198
|
if (this.isConnected()) {
|
|
200
199
|
// Read manufacturer information from the device
|
|
201
|
-
return await read(
|
|
200
|
+
return await this.read("device", "manufacturer", 250)
|
|
202
201
|
}
|
|
203
202
|
// If device is not found, return undefined
|
|
204
203
|
return undefined
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Device } from "../device.model"
|
|
2
2
|
import type { IKilterBoard } from "../../interfaces/device/kilterboard.interface"
|
|
3
3
|
import { KilterBoardPacket, KilterBoardPlacementRoles } from "../../commands/kilterboard"
|
|
4
|
-
import { write } from "../../write"
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Aurora Climbing Advertising service
|
|
@@ -206,7 +205,7 @@ export class KilterBoard extends Device implements IKilterBoard {
|
|
|
206
205
|
*/
|
|
207
206
|
async writeMessageSeries(messages: Uint8Array[]) {
|
|
208
207
|
for (const message of messages) {
|
|
209
|
-
await write(
|
|
208
|
+
await this.write("uart", "tx", message)
|
|
210
209
|
}
|
|
211
210
|
}
|
|
212
211
|
/**
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { Device } from "../device.model"
|
|
2
2
|
import type { IMotherboard } from "../../interfaces/device/motherboard.interface"
|
|
3
|
-
import { write, writeCallback } from "../../write"
|
|
4
3
|
import { applyTare } from "../../tare"
|
|
5
4
|
import { MotherboardCommands } from "../../commands"
|
|
6
5
|
import { checkActivity } from "../../is-active"
|
|
7
|
-
import { lastWrite } from "../../write"
|
|
8
6
|
import { DownloadPackets, emptyDownloadPackets } from "../../download"
|
|
9
7
|
import type { DownloadPacket } from "../../types/download"
|
|
10
|
-
import { read } from "../../read"
|
|
11
8
|
|
|
12
9
|
// Constants
|
|
13
10
|
const PACKET_LENGTH = 32
|
|
@@ -154,7 +151,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
154
151
|
*/
|
|
155
152
|
battery = async (): Promise<string | undefined> => {
|
|
156
153
|
if (this.isConnected()) {
|
|
157
|
-
return await read(
|
|
154
|
+
return await this.read("battery", "level", 250)
|
|
158
155
|
}
|
|
159
156
|
// If device is not found, return undefined
|
|
160
157
|
return undefined
|
|
@@ -168,7 +165,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
168
165
|
// Check if the device is connected
|
|
169
166
|
if (this.isConnected()) {
|
|
170
167
|
// Write the command to get calibration data to the device
|
|
171
|
-
await write(
|
|
168
|
+
await this.write("uart", "tx", MotherboardCommands.GET_CALIBRATION, 2500, (data) => {
|
|
172
169
|
console.log(data)
|
|
173
170
|
})
|
|
174
171
|
}
|
|
@@ -182,7 +179,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
182
179
|
// Check if the device is connected
|
|
183
180
|
if (this.isConnected()) {
|
|
184
181
|
// Read firmware version from the Motherboard
|
|
185
|
-
return await read(
|
|
182
|
+
return await this.read("device", "firmware", 250)
|
|
186
183
|
}
|
|
187
184
|
// If device is not found, return undefined
|
|
188
185
|
return undefined
|
|
@@ -295,7 +292,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
295
292
|
massCenter: Math.max(-1000, packet.masses[1]).toFixed(1),
|
|
296
293
|
massRight: Math.max(-1000, packet.masses[2]).toFixed(1),
|
|
297
294
|
})
|
|
298
|
-
} else if (
|
|
295
|
+
} else if (this.writeLast === MotherboardCommands.GET_CALIBRATION) {
|
|
299
296
|
// check data integrity
|
|
300
297
|
if ((receivedData.match(/,/g) || []).length === 3) {
|
|
301
298
|
const parts: string[] = receivedData.split(",")
|
|
@@ -304,7 +301,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
304
301
|
}
|
|
305
302
|
} else {
|
|
306
303
|
// unhandled data
|
|
307
|
-
writeCallback(receivedData)
|
|
304
|
+
this.writeCallback(receivedData)
|
|
308
305
|
}
|
|
309
306
|
}
|
|
310
307
|
}
|
|
@@ -319,7 +316,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
319
316
|
// Check if the device is connected
|
|
320
317
|
if (this.isConnected()) {
|
|
321
318
|
// Read hardware version from the device
|
|
322
|
-
return await read(
|
|
319
|
+
return await this.read("device", "hardware", 250)
|
|
323
320
|
}
|
|
324
321
|
// If device is not found, return undefined
|
|
325
322
|
return undefined
|
|
@@ -341,8 +338,8 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
341
338
|
// Default to "off" color if config is not set or not found in colorMapping
|
|
342
339
|
const color = typeof config === "string" && colorMapping[config] ? config : "off"
|
|
343
340
|
const [redValue, greenValue] = colorMapping[color]
|
|
344
|
-
await write(
|
|
345
|
-
await write(
|
|
341
|
+
await this.write("led", "red", new Uint8Array(redValue))
|
|
342
|
+
await this.write("led", "green", new Uint8Array(greenValue), 1250)
|
|
346
343
|
}
|
|
347
344
|
return undefined
|
|
348
345
|
}
|
|
@@ -355,7 +352,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
355
352
|
// Check if the device is connected
|
|
356
353
|
if (this.isConnected()) {
|
|
357
354
|
// Read manufacturer information from the device
|
|
358
|
-
return await read(
|
|
355
|
+
return await this.read("device", "manufacturer", 250)
|
|
359
356
|
}
|
|
360
357
|
// If device is not found, return undefined
|
|
361
358
|
return undefined
|
|
@@ -370,7 +367,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
370
367
|
if (this.isConnected()) {
|
|
371
368
|
// Write serial number command to the Motherboard and read output
|
|
372
369
|
let response: string | undefined = undefined
|
|
373
|
-
await write(
|
|
370
|
+
await this.write("uart", "tx", MotherboardCommands.GET_SERIAL, 250, (data) => {
|
|
374
371
|
response = data
|
|
375
372
|
})
|
|
376
373
|
return response
|
|
@@ -386,7 +383,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
386
383
|
stop = async (): Promise<void> => {
|
|
387
384
|
if (this.isConnected()) {
|
|
388
385
|
// Stop stream of device
|
|
389
|
-
await write(
|
|
386
|
+
await this.write("uart", "tx", MotherboardCommands.STOP_WEIGHT_MEAS, 0)
|
|
390
387
|
}
|
|
391
388
|
}
|
|
392
389
|
|
|
@@ -406,7 +403,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
406
403
|
await this.calibration()
|
|
407
404
|
}
|
|
408
405
|
// Start streaming data
|
|
409
|
-
await write(
|
|
406
|
+
await this.write("uart", "tx", MotherboardCommands.START_WEIGHT_MEAS, duration)
|
|
410
407
|
// Stop streaming if duration is set
|
|
411
408
|
if (duration !== 0) {
|
|
412
409
|
await this.stop()
|
|
@@ -428,7 +425,7 @@ export class Motherboard extends Device implements IMotherboard {
|
|
|
428
425
|
if (this.isConnected()) {
|
|
429
426
|
// Write text information command to the Motherboard and read output
|
|
430
427
|
let response: string | undefined = undefined
|
|
431
|
-
await write(
|
|
428
|
+
await this.write("uart", "tx", MotherboardCommands.GET_TEXT, 250, (data) => {
|
|
432
429
|
response = data
|
|
433
430
|
})
|
|
434
431
|
return response
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Device } from "../device.model"
|
|
2
2
|
import type { IProgressor } from "../../interfaces/device/progressor.interface"
|
|
3
|
-
import { applyTare } from "../../tare"
|
|
4
|
-
import { checkActivity } from "../../is-active"
|
|
5
3
|
import { ProgressorCommands, ProgressorResponses } from "../../commands/progressor"
|
|
6
|
-
import struct from "../../struct"
|
|
4
|
+
import struct from "../../helpers/struct"
|
|
5
|
+
import { checkActivity } from "../../is-active"
|
|
7
6
|
import { DownloadPackets, emptyDownloadPackets } from "../../download"
|
|
8
|
-
import {
|
|
7
|
+
import { applyTare } from "../../tare"
|
|
9
8
|
|
|
10
9
|
// Constants
|
|
11
10
|
let MASS_MAX = "0"
|
|
@@ -61,7 +60,7 @@ export class Progressor extends Device implements IProgressor {
|
|
|
61
60
|
battery = async (): Promise<string | undefined> => {
|
|
62
61
|
if (this.isConnected()) {
|
|
63
62
|
let response: string | undefined = undefined
|
|
64
|
-
await write(
|
|
63
|
+
await this.write("progressor", "tx", ProgressorCommands.GET_BATT_VLTG, 250, (data) => {
|
|
65
64
|
response = data
|
|
66
65
|
})
|
|
67
66
|
return response
|
|
@@ -79,7 +78,7 @@ export class Progressor extends Device implements IProgressor {
|
|
|
79
78
|
if (this.isConnected()) {
|
|
80
79
|
// Read firmware version from the device
|
|
81
80
|
let response: string | undefined = undefined
|
|
82
|
-
await write(
|
|
81
|
+
await this.write("progressor", "tx", ProgressorCommands.GET_FW_VERSION, 250, (data) => {
|
|
83
82
|
response = data
|
|
84
83
|
})
|
|
85
84
|
return response
|
|
@@ -141,18 +140,18 @@ export class Progressor extends Device implements IProgressor {
|
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
} else if (kind === ProgressorResponses.COMMAND_RESPONSE) {
|
|
144
|
-
if (!
|
|
143
|
+
if (!this.writeLast) return
|
|
145
144
|
|
|
146
145
|
let value = ""
|
|
147
146
|
|
|
148
|
-
if (
|
|
147
|
+
if (this.writeLast === ProgressorCommands.GET_BATT_VLTG) {
|
|
149
148
|
value = new DataView(data.buffer, 2).getUint32(0, true).toString()
|
|
150
|
-
} else if (
|
|
149
|
+
} else if (this.writeLast === ProgressorCommands.GET_FW_VERSION) {
|
|
151
150
|
value = new TextDecoder().decode(data.buffer.slice(2))
|
|
152
|
-
} else if (
|
|
151
|
+
} else if (this.writeLast === ProgressorCommands.GET_ERR_INFO) {
|
|
153
152
|
value = new TextDecoder().decode(data.buffer.slice(2))
|
|
154
153
|
}
|
|
155
|
-
writeCallback(value)
|
|
154
|
+
this.writeCallback(value)
|
|
156
155
|
} else if (kind === ProgressorResponses.LOW_BATTERY_WARNING) {
|
|
157
156
|
console.warn("⚠️ Low power detected. Please consider connecting to a power source.")
|
|
158
157
|
} else {
|
|
@@ -169,7 +168,7 @@ export class Progressor extends Device implements IProgressor {
|
|
|
169
168
|
stop = async (): Promise<void> => {
|
|
170
169
|
if (this.isConnected()) {
|
|
171
170
|
// Stop stream of device
|
|
172
|
-
await write(
|
|
171
|
+
await this.write("progressor", "tx", ProgressorCommands.STOP_WEIGHT_MEAS, 0)
|
|
173
172
|
}
|
|
174
173
|
}
|
|
175
174
|
|
|
@@ -183,7 +182,7 @@ export class Progressor extends Device implements IProgressor {
|
|
|
183
182
|
// Reset download packets
|
|
184
183
|
emptyDownloadPackets()
|
|
185
184
|
// Start streaming data
|
|
186
|
-
await write(
|
|
185
|
+
await this.write("progressor", "tx", ProgressorCommands.START_WEIGHT_MEAS, duration)
|
|
187
186
|
// Stop streaming if duration is set
|
|
188
187
|
if (duration !== 0) {
|
|
189
188
|
await this.stop()
|