@hangtime/grip-connect 0.5.6 → 0.5.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.
Files changed (41) hide show
  1. package/README.md +25 -31
  2. package/dist/index.d.ts +0 -4
  3. package/dist/index.js +0 -5
  4. package/dist/interfaces/callback.interface.d.ts +6 -0
  5. package/dist/interfaces/device/motherboard.interface.d.ts +0 -7
  6. package/dist/interfaces/device.interface.d.ts +32 -27
  7. package/dist/models/device/entralpi.model.js +7 -7
  8. package/dist/models/device/forceboard.model.js +4 -7
  9. package/dist/models/device/kilterboard.model.d.ts +10 -6
  10. package/dist/models/device/kilterboard.model.js +12 -8
  11. package/dist/models/device/motherboard.model.d.ts +12 -8
  12. package/dist/models/device/motherboard.model.js +22 -21
  13. package/dist/models/device/progressor.model.js +4 -7
  14. package/dist/models/device/wh-c06.model.d.ts +11 -6
  15. package/dist/models/device/wh-c06.model.js +30 -16
  16. package/dist/models/device.model.d.ts +161 -14
  17. package/dist/models/device.model.js +263 -5
  18. package/package.json +1 -1
  19. package/src/index.ts +0 -9
  20. package/src/interfaces/callback.interface.ts +7 -0
  21. package/src/interfaces/device/motherboard.interface.ts +0 -8
  22. package/src/interfaces/device.interface.ts +33 -31
  23. package/src/models/device/entralpi.model.ts +7 -7
  24. package/src/models/device/forceboard.model.ts +4 -7
  25. package/src/models/device/kilterboard.model.ts +23 -9
  26. package/src/models/device/motherboard.model.ts +23 -22
  27. package/src/models/device/progressor.model.ts +4 -7
  28. package/src/models/device/wh-c06.model.ts +38 -17
  29. package/src/models/device.model.ts +316 -15
  30. package/dist/helpers/download.d.ts +0 -16
  31. package/dist/helpers/download.js +0 -106
  32. package/dist/helpers/is-active.d.ts +0 -41
  33. package/dist/helpers/is-active.js +0 -59
  34. package/dist/helpers/is-device.d.ts +0 -37
  35. package/dist/helpers/is-device.js +0 -39
  36. package/dist/helpers/tare.d.ts +0 -12
  37. package/dist/helpers/tare.js +0 -70
  38. package/src/helpers/download.ts +0 -123
  39. package/src/helpers/is-active.ts +0 -70
  40. package/src/helpers/is-device.ts +0 -50
  41. package/src/helpers/tare.ts +0 -76
@@ -8,27 +8,32 @@ export declare class WHC06 extends Device implements IWHC06 {
8
8
  /**
9
9
  * Offset for the byte location in the manufacturer data to extract the weight.
10
10
  * @type {number}
11
+ * @static
12
+ * @readonly
11
13
  * @constant
12
14
  */
13
- private static readonly WEIGHT_OFFSET;
15
+ private static readonly weightOffset;
14
16
  /**
15
17
  * Company identifier for WH-C06, also used by 'TomTom International BV': https://www.bluetooth.com/specifications/assigned-numbers/
16
18
  * @type {number}
19
+ * @static
20
+ * @readonly
17
21
  * @constant
18
22
  */
19
- private static readonly MANUFACTURER_ID;
23
+ private static readonly manufacturerId;
20
24
  /**
21
25
  * To track disconnection timeout.
22
26
  * @type {number|null}
23
- * @constant
27
+ * @private
24
28
  */
25
29
  private advertisementTimeout;
26
30
  /**
27
31
  * The limit in seconds when timeout is triggered
28
- * @type {number|null}
29
- * @constant
32
+ * @type {number}
33
+ * @private
34
+ * @readonly
30
35
  */
31
- private advertisementTimeoutTime;
36
+ private readonly advertisementTimeoutTime;
32
37
  constructor();
33
38
  /**
34
39
  * Connects to a Bluetooth device.
@@ -1,7 +1,4 @@
1
1
  import { Device } from "../device.model";
2
- import { applyTare } from "../../helpers/tare";
3
- import { checkActivity } from "../../helpers/is-active";
4
- import { DownloadPackets } from "../../helpers/download";
5
2
  /**
6
3
  * Represents a Weiheng - WH-C06 (or MAT Muscle Meter) device
7
4
  * Enable 'Experimental Web Platform features' Chrome Flags.
@@ -10,28 +7,40 @@ export class WHC06 extends Device {
10
7
  /**
11
8
  * Offset for the byte location in the manufacturer data to extract the weight.
12
9
  * @type {number}
10
+ * @static
11
+ * @readonly
13
12
  * @constant
14
13
  */
15
- static WEIGHT_OFFSET = 10;
14
+ static weightOffset = 10;
16
15
  /**
17
16
  * Company identifier for WH-C06, also used by 'TomTom International BV': https://www.bluetooth.com/specifications/assigned-numbers/
18
17
  * @type {number}
18
+ * @static
19
+ * @readonly
19
20
  * @constant
20
21
  */
21
- static MANUFACTURER_ID = 256;
22
+ static manufacturerId = 256;
22
23
  /**
23
24
  * To track disconnection timeout.
24
25
  * @type {number|null}
25
- * @constant
26
+ * @private
26
27
  */
27
28
  advertisementTimeout = null;
28
29
  /**
29
30
  * The limit in seconds when timeout is triggered
30
- * @type {number|null}
31
- * @constant
31
+ * @type {number}
32
+ * @private
33
+ * @readonly
32
34
  */
33
35
  advertisementTimeoutTime = 10;
34
- // private static readonly STABLE_OFFSET = 14
36
+ // /**
37
+ // * Offset for the byte location in the manufacturer data to determine weight stability.
38
+ // * @type {number}
39
+ // * @static
40
+ // * @readonly
41
+ // * @constant
42
+ // */
43
+ // private static readonly stableOffset: number = 14
35
44
  constructor() {
36
45
  super({
37
46
  filters: [
@@ -66,18 +75,23 @@ export class WHC06 extends Device {
66
75
  // Device has no services / characteristics, so we directly call onSuccess
67
76
  onSuccess();
68
77
  this.bluetooth.addEventListener("advertisementreceived", (event) => {
69
- const data = event.manufacturerData.get(WHC06.MANUFACTURER_ID);
78
+ const data = event.manufacturerData.get(WHC06.manufacturerId);
70
79
  if (data) {
71
80
  // Handle recieved data
72
- const weight = (data.getUint8(WHC06.WEIGHT_OFFSET) << 8) | data.getUint8(WHC06.WEIGHT_OFFSET + 1);
81
+ const weight = (data.getUint8(WHC06.weightOffset) << 8) | data.getUint8(WHC06.weightOffset + 1);
73
82
  // const stable = (data.getUint8(STABLE_OFFSET) & 0xf0) >> 4
74
83
  // const unit = data.getUint8(STABLE_OFFSET) & 0x0f
75
84
  const receivedTime = Date.now();
76
85
  const receivedData = weight / 100;
77
86
  // Tare correction
78
- const numericData = receivedData - applyTare(receivedData);
87
+ // 0.20kg - 0.20kg = 0kg
88
+ // 0.40kg - 0.20kg = 0.20kg
89
+ const numericData = receivedData - this.applyTare(receivedData) * -1;
90
+ // what i want (if tare is available)
91
+ // 75kg - 75kg = 0
92
+ // 50kg - 75kg = -25kg * -1 = 25kg
79
93
  // Add data to downloadable Array
80
- DownloadPackets.push({
94
+ this.downloadPackets.push({
81
95
  received: receivedTime,
82
96
  sampleNum: this.dataPointCount,
83
97
  battRaw: 0,
@@ -93,7 +107,7 @@ export class WHC06 extends Device {
93
107
  // Calculate the average dynamically
94
108
  this.massAverage = (this.massTotalSum / this.dataPointCount).toFixed(1);
95
109
  // Check if device is being used
96
- checkActivity(numericData);
110
+ this.activityCheck(numericData);
97
111
  // Notify with weight data
98
112
  this.notifyCallback({
99
113
  massMax: this.massMax,
@@ -142,8 +156,8 @@ export class WHC06 extends Device {
142
156
  // Mimic a disconnect
143
157
  const disconnectedEvent = new Event("gattserverdisconnected");
144
158
  Object.defineProperty(disconnectedEvent, "target", { value: this.bluetooth, writable: false });
145
- // Also display a e
146
- throw new Error(`No advertisement received for ${this.advertisementTimeoutTime} seconds, stopping tracking..`);
159
+ // Print error to the console
160
+ console.error(`No advertisement received for ${this.advertisementTimeoutTime} seconds, stopping tracking..`);
147
161
  this.onDisconnected(disconnectedEvent);
148
162
  }, this.advertisementTimeoutTime * 1000); // 10 seconds
149
163
  };
@@ -1,32 +1,61 @@
1
1
  import { BaseModel } from "./../models/base.model";
2
2
  import type { IDevice, Service } from "../interfaces/device.interface";
3
- import type { NotifyCallback, WriteCallback } from "../interfaces/callback.interface";
3
+ import type { NotifyCallback, WriteCallback, ActiveCallback } from "../interfaces/callback.interface";
4
+ import type { DownloadPacket } from "../interfaces/download.interface";
4
5
  import type { Commands } from "../interfaces/command.interface";
5
6
  export declare abstract class Device extends BaseModel implements IDevice {
6
7
  /**
7
8
  * Filters to identify the device during Bluetooth scanning.
8
9
  * Used to match devices that meet specific criteria such as name, service UUIDs, etc.
10
+ * @type {BluetoothLEScanFilter[]}
11
+ * @public
12
+ * @readonly
9
13
  */
10
- filters: BluetoothLEScanFilter[];
14
+ readonly filters: BluetoothLEScanFilter[];
11
15
  /**
12
16
  * Array of services provided by the device.
13
17
  * Services represent functionalities that the device supports, such as weight measurement, battery information, or custom services.
18
+ * @type {Service[]}
19
+ * @public
20
+ * @readonly
14
21
  */
15
- services: Service[];
22
+ readonly services: Service[];
16
23
  /**
17
24
  * Reference to the `BluetoothDevice` object representing this device.
18
25
  * This is the actual device object obtained from the Web Bluetooth API after a successful connection.
26
+ * @type {BluetoothDevice | undefined}
27
+ * @public
19
28
  */
20
29
  bluetooth?: BluetoothDevice | undefined;
21
30
  /**
22
31
  * Object representing the set of commands available for this device.
23
32
  * These commands allow communication with the device to perform various operations such as starting measurements, retrieving data, or calibrating the device.
33
+ * @type {Commands}
34
+ * @public
35
+ * @readonly
24
36
  */
25
- commands: Commands;
37
+ readonly commands: Commands;
26
38
  /**
27
39
  * The BluetoothRemoteGATTServer interface of the Web Bluetooth API represents a GATT Server on a remote device.
40
+ * @type {BluetoothRemoteGATTServer | undefined}
41
+ * @private
28
42
  */
29
43
  private server;
44
+ /**
45
+ * The last message written to the device.
46
+ * @type {string | Uint8Array | null}
47
+ * @protected
48
+ */
49
+ protected writeLast: string | Uint8Array | null;
50
+ /**
51
+ * Indicates whether the device is currently active.
52
+ * @type {boolean}
53
+ */
54
+ protected isActive: boolean;
55
+ /**
56
+ * Configuration for threshold and duration.
57
+ */
58
+ private activeConfig;
30
59
  /**
31
60
  * Maximum mass recorded from the device, initialized to "0".
32
61
  * @type {string}
@@ -53,6 +82,38 @@ export declare abstract class Device extends BaseModel implements IDevice {
53
82
  * @protected
54
83
  */
55
84
  protected dataPointCount: number;
85
+ /**
86
+ * Array of DownloadPacket entries.
87
+ * This array holds packets that contain data downloaded from the device.
88
+ * @type {DownloadPacket[]}
89
+ * @protected
90
+ */
91
+ protected downloadPackets: DownloadPacket[];
92
+ /**
93
+ * Represents the current tare value for calibration.
94
+ * @type {number}
95
+ */
96
+ private tareCurrent;
97
+ /**
98
+ * Indicates whether the tare calibration process is active.
99
+ * @type {boolean}
100
+ */
101
+ private tareActive;
102
+ /**
103
+ * Timestamp when the tare calibration process started.
104
+ * @type {number | null}
105
+ */
106
+ private tareStartTime;
107
+ /**
108
+ * Array holding the samples collected during tare calibration.
109
+ * @type {number[]}
110
+ */
111
+ private tareSamples;
112
+ /**
113
+ * Duration time for the tare calibration process.
114
+ * @type {number}
115
+ */
116
+ private tareDuration;
56
117
  /**
57
118
  * Optional callback for handling write operations.
58
119
  * @callback NotifyCallback
@@ -70,69 +131,156 @@ export declare abstract class Device extends BaseModel implements IDevice {
70
131
  */
71
132
  protected writeCallback: WriteCallback;
72
133
  /**
73
- * The last message written to the device.
74
- * @type {string | Uint8Array | null}
134
+ * Optional callback for handling write operations.
135
+ * @callback ActiveCallback
136
+ * @param {string} data - The data passed to the callback.
137
+ * @type {ActiveCallback | undefined}
138
+ * @protected
75
139
  */
76
- protected writeLast: string | Uint8Array | null;
140
+ protected activeCallback: ActiveCallback;
77
141
  constructor(device: Partial<IDevice>);
142
+ /**
143
+ * Sets the callback function to be called when the activity status changes,
144
+ * and optionally sets the configuration for threshold and duration.
145
+ *
146
+ * This function allows you to specify a callback that will be invoked whenever
147
+ * the activity status changes, indicating whether the device is currently active.
148
+ * It also allows optionally configuring the threshold and duration used to determine activity.
149
+ *
150
+ * @param {ActiveCallback} callback - The callback function to be set. This function
151
+ * receives a boolean value indicating the new activity status.
152
+ * @param {object} [options] - Optional configuration object containing the threshold and duration.
153
+ * @param {number} [options.threshold=2.5] - The threshold value for determining activity.
154
+ * @param {number} [options.duration=1000] - The duration (in milliseconds) to monitor the input for activity.
155
+ * @returns {void}
156
+ * @public
157
+ */
158
+ active: (callback: ActiveCallback, options?: {
159
+ threshold?: number;
160
+ duration?: number;
161
+ }) => void;
162
+ /**
163
+ * Checks if a dynamic value is active based on a threshold and duration.
164
+ *
165
+ * This function assesses whether a given dynamic value surpasses a specified threshold
166
+ * and remains active for a specified duration. If the activity status changes from
167
+ * the previous state, the callback function is called with the updated activity status.
168
+ *
169
+ * @param {number} input - The dynamic value to check for activity status.
170
+ * @returns {Promise<void>} A promise that resolves once the activity check is complete.
171
+ */
172
+ protected activityCheck: (input: number) => Promise<void>;
78
173
  /**
79
174
  * Connects to a Bluetooth device.
80
175
  * @param {Function} [onSuccess] - Optional callback function to execute on successful connection. Default logs success.
81
176
  * @param {Function} [onError] - Optional callback function to execute on error. Default logs the error.
177
+ * @public
82
178
  */
83
179
  connect: (onSuccess?: () => void, onError?: (error: Error) => void) => Promise<void>;
84
180
  /**
85
181
  * Disconnects the device if it is currently connected.
86
182
  * - Checks if the device is connected via it's GATT server.
87
183
  * - If the device is connected, it attempts to gracefully disconnect.
184
+ * @public
88
185
  */
89
186
  disconnect: () => void;
187
+ /**
188
+ * Converts the `downloadPackets` array into a CSV formatted string.
189
+ * @returns {string} A CSV string representation of the `downloadPackets` data, with each packet on a new line.
190
+ * @private
191
+ */
192
+ private downloadToCSV;
193
+ /**
194
+ * Converts an array of DownloadPacket objects to a JSON string.
195
+ * @returns {string} JSON string representation of the data.
196
+ * @private
197
+ */
198
+ private downloadToJSON;
199
+ /**
200
+ * Converts an array of DownloadPacket objects to an XML string.
201
+ * @returns {string} XML string representation of the data.
202
+ * @private
203
+ */
204
+ private downloadToXML;
205
+ /**
206
+ * Exports the data in the specified format (CSV, JSON, XML) with a filename format:
207
+ * 'data-export-YYYY-MM-DD-HH-MM-SS.{format}'.
208
+ *
209
+ * @param {('csv' | 'json' | 'xml')} [format='csv'] - The format in which to download the data.
210
+ * Defaults to 'csv'. Accepted values are 'csv', 'json', and 'xml'.
211
+ *
212
+ * @returns {void} Initiates a download of the data in the specified format.
213
+ * @private
214
+ */
215
+ download: (format?: "csv" | "json" | "xml") => void;
90
216
  /**
91
217
  * Returns UUIDs of all services associated with the device.
92
218
  * @returns {string[]} Array of service UUIDs.
219
+ * @protected
93
220
  */
94
- getAllServiceUUIDs: () => string[];
221
+ protected getAllServiceUUIDs: () => string[];
95
222
  /**
96
223
  * Retrieves the characteristic from the device's service.
97
224
  * @param {string} serviceId - The UUID of the service.
98
225
  * @param {string} characteristicId - The UUID of the characteristic.
99
226
  * @returns {BluetoothRemoteGATTCharacteristic | undefined} The characteristic, if found.
227
+ * @protected
100
228
  */
101
- getCharacteristic: (serviceId: string, characteristicId: string) => BluetoothRemoteGATTCharacteristic | undefined;
229
+ protected getCharacteristic: (serviceId: string, characteristicId: string) => BluetoothRemoteGATTCharacteristic | undefined;
102
230
  /**
103
231
  * Handles notifications received from a characteristic.
104
232
  * @param {Event} event - The notification event.
233
+ * @protected
105
234
  */
106
- handleNotifications: (event: Event) => void;
235
+ protected handleNotifications: (event: Event) => void;
107
236
  /**
108
237
  * Checks if a Bluetooth device is connected.
109
238
  * @returns {boolean} A boolean indicating whether the device is connected.
239
+ * @public
110
240
  */
111
241
  isConnected: () => boolean;
112
242
  /**
113
243
  * Sets the callback function to be called when notifications are received.
114
244
  * @param {NotifyCallback} callback - The callback function to be set.
115
245
  * @returns {void}
246
+ * @public
116
247
  */
117
248
  notify: (callback: NotifyCallback) => void;
118
249
  /**
119
250
  * Handles the 'connected' event.
120
251
  * @param {Function} onSuccess - Callback function to execute on successful connection.
252
+ * @public
121
253
  */
122
- onConnected: (onSuccess: () => void) => Promise<void>;
254
+ protected onConnected: (onSuccess: () => void) => Promise<void>;
123
255
  /**
124
256
  * Handles the 'disconnected' event.
125
257
  * @param {Event} event - The 'disconnected' event.
258
+ * @public
126
259
  */
127
- onDisconnected: (event: Event) => void;
260
+ protected onDisconnected: (event: Event) => void;
128
261
  /**
129
262
  * Reads the value of the specified characteristic from the device.
130
263
  * @param {string} serviceId - The service ID where the characteristic belongs.
131
264
  * @param {string} characteristicId - The characteristic ID to read from.
132
265
  * @param {number} [duration=0] - The duration to wait before resolving the promise, in milliseconds.
133
266
  * @returns {Promise<string | undefined>} A promise that resolves when the read operation is completed.
267
+ * @public
134
268
  */
135
269
  read: (serviceId: string, characteristicId: string, duration?: number) => Promise<string | undefined>;
270
+ /**
271
+ * Initiates the tare calibration process.
272
+ * @param {number} duration - The duration time for tare calibration.
273
+ * @returns {void}
274
+ * @public
275
+ */
276
+ tare(duration?: number): void;
277
+ /**
278
+ * Apply tare calibration to the provided sample.
279
+ * @param {number} sample - The sample to calibrate.
280
+ * @returns {number} The calibrated tare value.
281
+ * @protected
282
+ */
283
+ protected applyTare(sample: number): number;
136
284
  /**
137
285
  * Writes a message to the specified characteristic of a Bluetooth device and optionally provides a callback to handle responses.
138
286
  * @param {string} serviceId - The service UUID of the Bluetooth device containing the target characteristic.
@@ -140,9 +288,8 @@ export declare abstract class Device extends BaseModel implements IDevice {
140
288
  * @param {string | Uint8Array | undefined} message - The message to be written to the characteristic. It can be a string or a Uint8Array.
141
289
  * @param {number} [duration=0] - Optional. The time in milliseconds to wait before resolving the promise. Defaults to 0 for immediate resolution.
142
290
  * @param {WriteCallback} [callback=writeCallback] - Optional. A custom callback to handle the response after the write operation is successful.
143
- *
144
291
  * @returns {Promise<void>} A promise that resolves once the write operation is complete.
145
- *
292
+ * @public
146
293
  * @throws {Error} Throws an error if the characteristic is undefined.
147
294
  *
148
295
  * @example