@hangtime/grip-connect 0.3.6 → 0.3.8

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
@@ -47,7 +47,7 @@ Simply importing the utilities you need from `@hangtime/grip-connect`.
47
47
  ```
48
48
 
49
49
  ```js
50
- import { Motherboard, battery, connect, disconnect, info, notify, stream } from "@hangtime/grip-connect"
50
+ import { Motherboard, active, battery, connect, disconnect, info, notify, stream } from "@hangtime/grip-connect"
51
51
 
52
52
  const motherboardButton = document.querySelector("#motherboard")
53
53
 
@@ -59,10 +59,18 @@ motherboardButton.addEventListener("click", () => {
59
59
  console.log(data)
60
60
  })
61
61
 
62
+ // Check if device is being used
63
+ active((value) => {
64
+ console.log(value)
65
+ })
66
+
62
67
  // Read battery + device info
63
68
  await battery(Motherboard)
64
69
  await info(Motherboard)
65
70
 
71
+ // trigger LEDs
72
+ // await led(device)
73
+
66
74
  // Start weight streaming (for a minute) remove parameter for a continues stream
67
75
  await stream(Motherboard, 60000)
68
76
 
@@ -106,6 +114,7 @@ available services with us.
106
114
  - ✅ Read calibration
107
115
  - ✅ Device info: firmware / serial etc.
108
116
  - ✅ Check if device is connected
117
+ - ✅ Check if device is being used
109
118
  - ✅ Peak / Average load
110
119
  - ✅️ Tare / unladen weight
111
120
  - ✅️ Download data to CVS
@@ -1,4 +1,5 @@
1
1
  import { notifyCallback } from "./../notify";
2
+ import { checkActivity } from "./../is-active";
2
3
  import { applyTare } from "./../tare";
3
4
  // Constants
4
5
  let MASS_MAX = "0";
@@ -21,6 +22,8 @@ export const handleEntralpiData = (receivedData) => {
21
22
  DATAPOINT_COUNT++;
22
23
  // Calculate the average dynamically
23
24
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1);
25
+ // Check if device is being used
26
+ checkActivity(numericData);
24
27
  // Notify with weight data
25
28
  notifyCallback({
26
29
  massMax: MASS_MAX,
@@ -1,6 +1,7 @@
1
1
  import { notifyCallback } from "./../notify";
2
2
  import { applyTare } from "./../tare";
3
3
  import { MotherboardCommands } from "./../commands";
4
+ import { checkActivity } from "./../is-active";
4
5
  import { lastWrite } from "./../write";
5
6
  import { DownloadPackets } from "./../download";
6
7
  // Constants
@@ -108,6 +109,8 @@ export const handleMotherboardData = (receivedData) => {
108
109
  DATAPOINT_COUNT++;
109
110
  // Calculate the average dynamically
110
111
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1);
112
+ // Check if device is being used
113
+ checkActivity(center);
111
114
  // Notify with weight data
112
115
  notifyCallback({
113
116
  massTotal: Math.max(-1000, left + center + right).toFixed(1),
@@ -1,5 +1,6 @@
1
1
  import { notifyCallback } from "./../notify";
2
2
  import { applyTare } from "./../tare";
3
+ import { checkActivity } from "./../is-active";
3
4
  import { ProgressorCommands, ProgressorResponses } from "./../commands/progressor";
4
5
  import { lastWrite } from "./../write";
5
6
  import struct from "./../struct";
@@ -39,6 +40,8 @@ export const handleProgressorData = (data) => {
39
40
  DATAPOINT_COUNT++;
40
41
  // Calculate the average dynamically
41
42
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1);
43
+ // Check if device is being used
44
+ checkActivity(weight);
42
45
  notifyCallback({
43
46
  massMax: MASS_MAX,
44
47
  massAverage: MASS_AVERAGE,
@@ -1,3 +1,4 @@
1
+ import { checkActivity } from "./../is-active";
1
2
  import { notifyCallback } from "./../notify";
2
3
  import { applyTare } from "./../tare";
3
4
  // Constants
@@ -26,6 +27,8 @@ export const handleWHC06Data = (data) => {
26
27
  DATAPOINT_COUNT++;
27
28
  // Calculate the average dynamically
28
29
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1);
30
+ // Check if device is being used
31
+ checkActivity(numericData);
29
32
  // Notify with weight data
30
33
  notifyCallback({
31
34
  massMax: MASS_MAX,
@@ -44,18 +44,18 @@ export const Motherboard = {
44
44
  ],
45
45
  },
46
46
  {
47
- name: "Unknown Service",
48
- id: "unknown",
47
+ name: "LED Service",
48
+ id: "led",
49
49
  uuid: "10ababcd-15e1-28ff-de13-725bea03b127",
50
50
  characteristics: [
51
51
  {
52
- name: "Unknown 01",
53
- id: "01",
52
+ name: "Red LED",
53
+ id: "red",
54
54
  uuid: "10ab1524-15e1-28ff-de13-725bea03b127",
55
55
  },
56
56
  {
57
- name: "Unknown 02",
58
- id: "02",
57
+ name: "Green LED",
58
+ id: "green",
59
59
  uuid: "10ab1525-15e1-28ff-de13-725bea03b127",
60
60
  },
61
61
  ],
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { calibration } from "./calibration";
4
4
  export { download } from "./download";
5
5
  export { connect } from "./connect";
6
6
  export { disconnect } from "./disconnect";
7
+ export { active, isActive } from "./is-active";
7
8
  export { isConnected } from "./is-connected";
8
9
  export { info } from "./info";
9
10
  export { led } from "./led";
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ export { download } from "./download";
9
9
  // Export connection related functions
10
10
  export { connect } from "./connect";
11
11
  export { disconnect } from "./disconnect";
12
+ export { active, isActive } from "./is-active";
12
13
  export { isConnected } from "./is-connected";
13
14
  // Export information retrieval function
14
15
  export { info } from "./info";
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Type definition for the callback function that is called when the activity status changes.
3
+ * @param {boolean} value - The new activity status (true if active, false if not).
4
+ */
5
+ type IsActiveCallback = (value: boolean) => void;
6
+ /**
7
+ * Indicates whether the device is currently active.
8
+ * @type {boolean}
9
+ */
10
+ export declare let isActive: boolean;
11
+ /**
12
+ * Sets the callback function to be called when the activity status changes.
13
+ *
14
+ * This function allows you to specify a callback that will be invoked whenever
15
+ * the activity status changes, indicating whether the device is currently active.
16
+ *
17
+ * @param {IsActiveCallback} callback - The callback function to be set. This function
18
+ * receives a boolean value indicating the new activity status.
19
+ * @returns {void}
20
+ */
21
+ export declare const active: (callback: IsActiveCallback) => void;
22
+ /**
23
+ * Checks if a dynamic value is active based on a threshold and duration.
24
+ *
25
+ * This function assesses whether a given dynamic value surpasses a specified threshold
26
+ * and remains active for a specified duration. If the activity status changes from
27
+ * the previous state, the callback function is called with the updated activity status.
28
+ *
29
+ * @param {number} input - The dynamic value to check for activity status.
30
+ * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
31
+ * Defaults to 2.5 if not provided.
32
+ * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
33
+ * Defaults to 1000 milliseconds if not provided.
34
+ * @returns {Promise<void>} A promise that resolves once the activity check is complete.
35
+ */
36
+ export declare const checkActivity: (input: number, threshold?: number, duration?: number) => Promise<void>;
37
+ export {};
@@ -0,0 +1,49 @@
1
+ let activeCallback;
2
+ /**
3
+ * Indicates whether the device is currently active.
4
+ * @type {boolean}
5
+ */
6
+ export let isActive = false;
7
+ /**
8
+ * Sets the callback function to be called when the activity status changes.
9
+ *
10
+ * This function allows you to specify a callback that will be invoked whenever
11
+ * the activity status changes, indicating whether the device is currently active.
12
+ *
13
+ * @param {IsActiveCallback} callback - The callback function to be set. This function
14
+ * receives a boolean value indicating the new activity status.
15
+ * @returns {void}
16
+ */
17
+ export const active = (callback) => {
18
+ activeCallback = callback;
19
+ };
20
+ /**
21
+ * Checks if a dynamic value is active based on a threshold and duration.
22
+ *
23
+ * This function assesses whether a given dynamic value surpasses a specified threshold
24
+ * and remains active for a specified duration. If the activity status changes from
25
+ * the previous state, the callback function is called with the updated activity status.
26
+ *
27
+ * @param {number} input - The dynamic value to check for activity status.
28
+ * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
29
+ * Defaults to 2.5 if not provided.
30
+ * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
31
+ * Defaults to 1000 milliseconds if not provided.
32
+ * @returns {Promise<void>} A promise that resolves once the activity check is complete.
33
+ */
34
+ export const checkActivity = (input, threshold = 2.5, duration = 1000) => {
35
+ return new Promise((resolve) => {
36
+ // Check the activity status after the specified duration
37
+ setTimeout(() => {
38
+ // Determine the activity status based on the threshold
39
+ const activeNow = input > threshold;
40
+ if (isActive !== activeNow) {
41
+ isActive = activeNow;
42
+ if (activeCallback) {
43
+ activeCallback(activeNow);
44
+ }
45
+ }
46
+ resolve();
47
+ }, duration);
48
+ });
49
+ };
@@ -8,6 +8,6 @@ export const isConnected = (board) => {
8
8
  if (!board?.device) {
9
9
  return false;
10
10
  }
11
- // Check if the device is connected using optional chaining
11
+ // Check if the device is connected
12
12
  return !!board.device.gatt?.connected;
13
13
  };
package/dist/led.d.ts CHANGED
@@ -11,9 +11,10 @@ declare class ClimbPlacement {
11
11
  */
12
12
  export declare function prepBytesV3(climbPlacementList: ClimbPlacement[]): number[];
13
13
  /**
14
- * Set device leds.
15
- * @param {Device} board - The device to retrieve information from.
16
- * @returns {Promise<void>} A promise that resolves when the information retrieval is completed.
14
+ * Sets the LEDs on the specified device.
15
+ * @param {Device} board - The device on which to set the LEDs.
16
+ * @param {ClimbPlacement[]} [placement] - An optional array of climb placements for LED positioning.
17
+ * @returns {Promise<number[] | undefined>} A promise that resolves with the payload array if LED settings were applied, or `undefined` if no action was taken.
17
18
  */
18
19
  export declare const led: (board: Device, placement?: ClimbPlacement[]) => Promise<number[] | undefined>;
19
20
  export {};
package/dist/led.js CHANGED
@@ -155,9 +155,10 @@ async function writeMessageSeries(messages) {
155
155
  }
156
156
  }
157
157
  /**
158
- * Set device leds.
159
- * @param {Device} board - The device to retrieve information from.
160
- * @returns {Promise<void>} A promise that resolves when the information retrieval is completed.
158
+ * Sets the LEDs on the specified device.
159
+ * @param {Device} board - The device on which to set the LEDs.
160
+ * @param {ClimbPlacement[]} [placement] - An optional array of climb placements for LED positioning.
161
+ * @returns {Promise<number[] | undefined>} A promise that resolves with the payload array if LED settings were applied, or `undefined` if no action was taken.
161
162
  */
162
163
  export const led = async (board, placement) => {
163
164
  // Check if the filter contains the Aurora Climbing Advertising service
@@ -175,9 +176,17 @@ export const led = async (board, placement) => {
175
176
  }
176
177
  }
177
178
  if (board.filters.some((filter) => filter.name === "Motherboard")) {
178
- // Orange
179
- await write(Motherboard, "led", "01", "0", 1000);
180
- // Yellow
181
- await write(Motherboard, "led", "02", "0", 1000);
179
+ console.log("Green");
180
+ await write(Motherboard, "led", "red", new Uint8Array([0x00]));
181
+ await write(Motherboard, "led", "green", new Uint8Array([0x01]), 2500);
182
+ console.log("Red");
183
+ await write(Motherboard, "led", "red", new Uint8Array([0x01]));
184
+ await write(Motherboard, "led", "green", new Uint8Array([0x00]), 2500);
185
+ console.log("Orage");
186
+ await write(Motherboard, "led", "red", new Uint8Array([0x01]));
187
+ await write(Motherboard, "led", "green", new Uint8Array([0x01]), 2500);
188
+ console.log("Off");
189
+ await write(Motherboard, "led", "red", new Uint8Array([0x00]));
190
+ await write(Motherboard, "led", "green", new Uint8Array([0x00]), 2500);
182
191
  }
183
192
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hangtime/grip-connect",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "A client that can establish connections with various Force-Sensing Hangboards/Plates used by climbers for strength measurement. Examples of such hangboards include the Griptonite Motherboard, Climbro, SmartBoard, Entralpi or Tindeq Progressor",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,4 +1,5 @@
1
1
  import { notifyCallback } from "./../notify"
2
+ import { checkActivity } from "./../is-active"
2
3
  import { applyTare } from "./../tare"
3
4
 
4
5
  // Constants
@@ -28,6 +29,9 @@ export const handleEntralpiData = (receivedData: string): void => {
28
29
  // Calculate the average dynamically
29
30
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1)
30
31
 
32
+ // Check if device is being used
33
+ checkActivity(numericData)
34
+
31
35
  // Notify with weight data
32
36
  notifyCallback({
33
37
  massMax: MASS_MAX,
@@ -1,6 +1,7 @@
1
1
  import { notifyCallback } from "./../notify"
2
2
  import { applyTare } from "./../tare"
3
3
  import { MotherboardCommands } from "./../commands"
4
+ import { checkActivity } from "./../is-active"
4
5
  import { lastWrite } from "./../write"
5
6
  import { DownloadPackets } from "./../download"
6
7
  import type { DownloadPacket } from "./../types/download"
@@ -131,6 +132,9 @@ export const handleMotherboardData = (receivedData: string): void => {
131
132
  // Calculate the average dynamically
132
133
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1)
133
134
 
135
+ // Check if device is being used
136
+ checkActivity(center)
137
+
134
138
  // Notify with weight data
135
139
  notifyCallback({
136
140
  massTotal: Math.max(-1000, left + center + right).toFixed(1),
@@ -1,5 +1,6 @@
1
1
  import { notifyCallback } from "./../notify"
2
2
  import { applyTare } from "./../tare"
3
+ import { checkActivity } from "./../is-active"
3
4
  import { ProgressorCommands, ProgressorResponses } from "./../commands/progressor"
4
5
  import { lastWrite } from "./../write"
5
6
  import struct from "./../struct"
@@ -43,6 +44,9 @@ export const handleProgressorData = (data: DataView): void => {
43
44
  // Calculate the average dynamically
44
45
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1)
45
46
 
47
+ // Check if device is being used
48
+ checkActivity(weight)
49
+
46
50
  notifyCallback({
47
51
  massMax: MASS_MAX,
48
52
  massAverage: MASS_AVERAGE,
@@ -1,3 +1,4 @@
1
+ import { checkActivity } from "./../is-active"
1
2
  import { notifyCallback } from "./../notify"
2
3
  import { applyTare } from "./../tare"
3
4
 
@@ -34,6 +35,9 @@ export const handleWHC06Data = (data: DataView): void => {
34
35
  // Calculate the average dynamically
35
36
  MASS_AVERAGE = (MASS_TOTAL_SUM / DATAPOINT_COUNT).toFixed(1)
36
37
 
38
+ // Check if device is being used
39
+ checkActivity(numericData)
40
+
37
41
  // Notify with weight data
38
42
  notifyCallback({
39
43
  massMax: MASS_MAX,
@@ -46,18 +46,18 @@ export const Motherboard: Device = {
46
46
  ],
47
47
  },
48
48
  {
49
- name: "Unknown Service",
50
- id: "unknown",
49
+ name: "LED Service",
50
+ id: "led",
51
51
  uuid: "10ababcd-15e1-28ff-de13-725bea03b127",
52
52
  characteristics: [
53
53
  {
54
- name: "Unknown 01",
55
- id: "01",
54
+ name: "Red LED",
55
+ id: "red",
56
56
  uuid: "10ab1524-15e1-28ff-de13-725bea03b127",
57
57
  },
58
58
  {
59
- name: "Unknown 02",
60
- id: "02",
59
+ name: "Green LED",
60
+ id: "green",
61
61
  uuid: "10ab1525-15e1-28ff-de13-725bea03b127",
62
62
  },
63
63
  ],
package/src/index.ts CHANGED
@@ -13,6 +13,7 @@ export { download } from "./download"
13
13
  // Export connection related functions
14
14
  export { connect } from "./connect"
15
15
  export { disconnect } from "./disconnect"
16
+ export { active, isActive } from "./is-active"
16
17
  export { isConnected } from "./is-connected"
17
18
 
18
19
  // Export information retrieval function
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Type definition for the callback function that is called when the activity status changes.
3
+ * @param {boolean} value - The new activity status (true if active, false if not).
4
+ */
5
+ type IsActiveCallback = (value: boolean) => void
6
+
7
+ let activeCallback: IsActiveCallback | undefined
8
+
9
+ /**
10
+ * Indicates whether the device is currently active.
11
+ * @type {boolean}
12
+ */
13
+ export let isActive = false
14
+
15
+ /**
16
+ * Sets the callback function to be called when the activity status changes.
17
+ *
18
+ * This function allows you to specify a callback that will be invoked whenever
19
+ * the activity status changes, indicating whether the device is currently active.
20
+ *
21
+ * @param {IsActiveCallback} callback - The callback function to be set. This function
22
+ * receives a boolean value indicating the new activity status.
23
+ * @returns {void}
24
+ */
25
+ export const active = (callback: IsActiveCallback): void => {
26
+ activeCallback = callback
27
+ }
28
+
29
+ /**
30
+ * Checks if a dynamic value is active based on a threshold and duration.
31
+ *
32
+ * This function assesses whether a given dynamic value surpasses a specified threshold
33
+ * and remains active for a specified duration. If the activity status changes from
34
+ * the previous state, the callback function is called with the updated activity status.
35
+ *
36
+ * @param {number} input - The dynamic value to check for activity status.
37
+ * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
38
+ * Defaults to 2.5 if not provided.
39
+ * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
40
+ * Defaults to 1000 milliseconds if not provided.
41
+ * @returns {Promise<void>} A promise that resolves once the activity check is complete.
42
+ */
43
+ export const checkActivity = (input: number, threshold = 2.5, duration = 1000): Promise<void> => {
44
+ return new Promise((resolve) => {
45
+ // Check the activity status after the specified duration
46
+ setTimeout(() => {
47
+ // Determine the activity status based on the threshold
48
+ const activeNow = input > threshold
49
+ if (isActive !== activeNow) {
50
+ isActive = activeNow
51
+ if (activeCallback) {
52
+ activeCallback(activeNow)
53
+ }
54
+ }
55
+ resolve()
56
+ }, duration)
57
+ })
58
+ }
@@ -10,6 +10,6 @@ export const isConnected = (board?: Device): boolean => {
10
10
  if (!board?.device) {
11
11
  return false
12
12
  }
13
- // Check if the device is connected using optional chaining
13
+ // Check if the device is connected
14
14
  return !!board.device.gatt?.connected
15
15
  }
package/src/led.ts CHANGED
@@ -168,9 +168,10 @@ async function writeMessageSeries(messages: Uint8Array[]) {
168
168
  }
169
169
  }
170
170
  /**
171
- * Set device leds.
172
- * @param {Device} board - The device to retrieve information from.
173
- * @returns {Promise<void>} A promise that resolves when the information retrieval is completed.
171
+ * Sets the LEDs on the specified device.
172
+ * @param {Device} board - The device on which to set the LEDs.
173
+ * @param {ClimbPlacement[]} [placement] - An optional array of climb placements for LED positioning.
174
+ * @returns {Promise<number[] | undefined>} A promise that resolves with the payload array if LED settings were applied, or `undefined` if no action was taken.
174
175
  */
175
176
  export const led = async (board: Device, placement?: ClimbPlacement[]): Promise<number[] | undefined> => {
176
177
  // Check if the filter contains the Aurora Climbing Advertising service
@@ -188,9 +189,17 @@ export const led = async (board: Device, placement?: ClimbPlacement[]): Promise<
188
189
  }
189
190
  }
190
191
  if (board.filters.some((filter) => filter.name === "Motherboard")) {
191
- // Orange
192
- await write(Motherboard, "led", "01", "0", 1000)
193
- // Yellow
194
- await write(Motherboard, "led", "02", "0", 1000)
192
+ console.log("Green")
193
+ await write(Motherboard, "led", "red", new Uint8Array([0x00]))
194
+ await write(Motherboard, "led", "green", new Uint8Array([0x01]), 2500)
195
+ console.log("Red")
196
+ await write(Motherboard, "led", "red", new Uint8Array([0x01]))
197
+ await write(Motherboard, "led", "green", new Uint8Array([0x00]), 2500)
198
+ console.log("Orage")
199
+ await write(Motherboard, "led", "red", new Uint8Array([0x01]))
200
+ await write(Motherboard, "led", "green", new Uint8Array([0x01]), 2500)
201
+ console.log("Off")
202
+ await write(Motherboard, "led", "red", new Uint8Array([0x00]))
203
+ await write(Motherboard, "led", "green", new Uint8Array([0x00]), 2500)
195
204
  }
196
205
  }