Skip to content

Commit

Permalink
remove struct, explain code
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevie-Ray committed Oct 18, 2024
1 parent 6053c6c commit 5c4d05d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 262 deletions.
239 changes: 0 additions & 239 deletions packages/core/src/helpers/struct.ts

This file was deleted.

5 changes: 1 addition & 4 deletions packages/core/src/models/device.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,7 @@ export abstract class Device extends BaseModel implements IDevice {

if (value) {
if (value.buffer) {
const buffer: ArrayBuffer = value.buffer
console.log(new Uint8Array(buffer))
const rawData: DataView = new DataView(buffer)
console.log(rawData)
console.log(value)
} else {
console.log(value)
}
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/models/device/entralpi.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ export class Entralpi extends Device implements IEntralpi {

if (value) {
if (value.buffer) {
const buffer: ArrayBuffer = value.buffer
const rawData: DataView = new DataView(buffer)
const receivedTime: number = Date.now()
const receivedData: string = (rawData.getUint16(0) / 100).toFixed(1)
const receivedData: string = (value.getUint16(0) / 100).toFixed(1)

const convertedData = Number(receivedData)
// Adjust weight by using the tare value
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/models/device/forceboard.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ export class ForceBoard extends Device implements IForceBoard {
const value: DataView | undefined = characteristic.value
if (value) {
if (value.buffer) {
const buffer: ArrayBuffer = value.buffer
const rawData: DataView = new DataView(buffer)
const receivedTime: number = Date.now()

const receivedData = rawData.getUint8(4) // Read the value at index 4
const receivedData = value.getUint8(4) // Read the value at index 4
// Convert from LBS to KG
const convertedReceivedData = receivedData * 0.453592
// Tare correction
Expand Down
35 changes: 22 additions & 13 deletions packages/core/src/models/device/progressor.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Device } from "../device.model"
import type { IProgressor } from "../../interfaces/device/progressor.interface"
import struct from "../../helpers/struct"

/**
* Represents the possible responses of a Tindeq Progressor device.
Expand Down Expand Up @@ -129,15 +128,25 @@ export class Progressor extends Device implements IProgressor {

if (value) {
if (value.buffer) {
const buffer: ArrayBuffer = value.buffer
const rawData: DataView = new DataView(buffer)
const receivedTime: number = Date.now()
const [kind] = struct("<bb").unpack(rawData.buffer.slice(0, 2))
// Read the first byte of the buffer to determine the kind of message
const kind = value.getInt8(0)
// Check if the message is a weight measurement
if (kind === ProgressorResponses.WEIGHT_MEASURE) {
const iterable: IterableIterator<unknown[]> = struct("<fi").iter_unpack(rawData.buffer.slice(2))
// eslint-disable-next-line prefer-const
for (let [weight, seconds] of iterable) {
if (typeof weight === "number" && !isNaN(weight) && typeof seconds === "number" && !isNaN(seconds)) {
// Start parsing data from the 3rd byte (index 2)
let offset = 2
// Continue parsing while there's data left in the buffer
while (offset < value.byteLength) {
// Read a 32-bit float (4 bytes) for the weight, using little-endian
const weight = value.getFloat32(offset, true)
// Move the offset by 4 bytes
offset += 4
// Read a 32-bit integer (4 bytes) for the seconds, using little-endian
const seconds = value.getInt32(offset, true)
// Move the offset by 4 bytes
offset += 4
// Check if both weight and seconds are valid numbers
if (!isNaN(weight) && !isNaN(seconds)) {
// Tare correction
const numericData = weight - this.applyTare(weight)
// Add data to downloadable Array
Expand Down Expand Up @@ -171,16 +180,16 @@ export class Progressor extends Device implements IProgressor {
} else if (kind === ProgressorResponses.COMMAND_RESPONSE) {
if (!this.writeLast) return

let value = ""
let output = ""

if (this.writeLast === this.commands.GET_BATT_VLTG) {
value = new DataView(rawData.buffer, 2).getUint32(0, true).toString()
output = new DataView(value.buffer, 2).getUint32(0, true).toString()
} else if (this.writeLast === this.commands.GET_FW_VERSION) {
value = new TextDecoder().decode(rawData.buffer.slice(2))
output = new TextDecoder().decode(new Uint8Array(value.buffer).slice(2))
} else if (this.writeLast === this.commands.GET_ERR_INFO) {
value = new TextDecoder().decode(rawData.buffer.slice(2))
output = new TextDecoder().decode(new Uint8Array(value.buffer.slice(2)))
}
this.writeCallback(value)
this.writeCallback(output)
} else if (kind === ProgressorResponses.LOW_BATTERY_WARNING) {
console.warn("⚠️ Low power detected. Please consider connecting to a power source.")
} else {
Expand Down

0 comments on commit 5c4d05d

Please sign in to comment.