align structure more with underlying chunk layout

This commit is contained in:
2025-01-06 21:49:08 -06:00
parent acac519e9a
commit 6580f66e91
35 changed files with 594 additions and 372 deletions
+137 -15
View File
@@ -8,16 +8,24 @@ import dataTrackerTimestampChunk from '../encoders/data/data-tracker-timestamp-c
import dataTrackerTrgtposChunk from '../encoders/data/data-tracker-trgtpos-chunk';
import infoTrackerChunk from '../encoders/info/info-tracker-chunk';
import infoTrackerNameChunk from '../encoders/info/info-tracker-name-chunk';
import { DataTrackerChunk } from './data/data-tracker-chunk';
import { InfoTrackerChunk } from './info/info-tracker-chunk';
export type XYZData = {
x: number;
y: number;
z: number;
};
export class Tracker {
id: number;
name: string;
pos?: [number, number, number];
speed?: [number, number, number];
ori?: [number, number, number];
pos?: XYZData;
speed?: XYZData;
ori?: XYZData;
validity?: number;
accel?: [number, number, number];
trgtpos?: [number, number, number];
accel?: XYZData;
trgtpos?: XYZData;
timestamp?: bigint;
constructor(id: number, name: string) {
@@ -33,15 +41,45 @@ export class Tracker {
}
setPos(x: number, y: number, z: number) {
this.pos = [x, y, z];
if (this.pos === undefined) {
this.pos = {
x,
y,
z,
};
} else {
this.pos.x = x;
this.pos.y = y;
this.pos.z = z;
}
}
setSpeed(x: number, y: number, z: number) {
this.speed = [x, y, z];
if (this.speed === undefined) {
this.speed = {
x,
y,
z,
};
} else {
this.speed.x = x;
this.speed.y = y;
this.speed.z = z;
}
}
setOri(x: number, y: number, z: number) {
this.ori = [x, y, z];
if (this.ori === undefined) {
this.ori = {
x,
y,
z,
};
} else {
this.ori.x = x;
this.ori.y = y;
this.ori.z = z;
}
}
setStatus(validity: number) {
@@ -49,11 +87,31 @@ export class Tracker {
}
setAccel(x: number, y: number, z: number) {
this.accel = [x, y, z];
if (this.accel === undefined) {
this.accel = {
x,
y,
z,
};
} else {
this.accel.x = x;
this.accel.y = y;
this.accel.z = z;
}
}
setTrgtPos(x: number, y: number, z: number) {
this.trgtpos = [x, y, z];
if (this.trgtpos === undefined) {
this.trgtpos = {
x,
y,
z,
};
} else {
this.trgtpos.x = x;
this.trgtpos.y = y;
this.trgtpos.z = z;
}
}
setTimestamp(timestamp: bigint) {
@@ -64,15 +122,15 @@ export class Tracker {
const fieldChunks: Uint8Array[] = [];
if (this.pos) {
fieldChunks.push(dataTrackerPosChunk(...this.pos));
fieldChunks.push(dataTrackerPosChunk(this.pos.x, this.pos.y, this.pos.z));
}
if (this.speed) {
fieldChunks.push(dataTrackerSpeedChunk(...this.speed));
fieldChunks.push(dataTrackerSpeedChunk(this.speed.x, this.speed.y, this.speed.z));
}
if (this.ori) {
fieldChunks.push(dataTrackerOriChunk(...this.ori));
fieldChunks.push(dataTrackerOriChunk(this.ori.x, this.ori.y, this.ori.z));
}
if (this.validity) {
@@ -80,11 +138,11 @@ export class Tracker {
}
if (this.accel) {
fieldChunks.push(dataTrackerAccelChunk(...this.accel));
fieldChunks.push(dataTrackerAccelChunk(this.accel.x, this.accel.y, this.accel.z));
}
if (this.trgtpos) {
fieldChunks.push(dataTrackerTrgtposChunk(...this.trgtpos));
fieldChunks.push(dataTrackerTrgtposChunk(this.trgtpos.x, this.trgtpos.y, this.trgtpos.z));
}
if (this.timestamp) {
@@ -97,4 +155,68 @@ export class Tracker {
getInfoChunk() {
return infoTrackerChunk(this.id, infoTrackerNameChunk(this.name));
}
updateInfo(infoTrackerChunk: InfoTrackerChunk) {
if (infoTrackerChunk.data) {
this.name = infoTrackerChunk.data.trackerName.data.trackerName;
}
}
updateData(dataTrackerChunk: DataTrackerChunk) {
if (dataTrackerChunk.data.pos) {
this.setPos(dataTrackerChunk.data.pos.data.x, dataTrackerChunk.data.pos.data.y, dataTrackerChunk.data.pos.data.z);
}
if (dataTrackerChunk.data.speed) {
this.setSpeed(
dataTrackerChunk.data.speed.data.x,
dataTrackerChunk.data.speed.data.y,
dataTrackerChunk.data.speed.data.z
);
}
if (dataTrackerChunk.data.ori) {
this.setOri(dataTrackerChunk.data.ori.data.x, dataTrackerChunk.data.ori.data.y, dataTrackerChunk.data.ori.data.z);
}
if (dataTrackerChunk.data.status) {
this.setStatus(dataTrackerChunk.data.status.data.validity);
}
if (dataTrackerChunk.data.accel) {
this.setAccel(
dataTrackerChunk.data.accel.data.x,
dataTrackerChunk.data.accel.data.y,
dataTrackerChunk.data.accel.data.z
);
}
if (dataTrackerChunk.data.trgtpos) {
this.setTrgtPos(
dataTrackerChunk.data.trgtpos.data.x,
dataTrackerChunk.data.trgtpos.data.y,
dataTrackerChunk.data.trgtpos.data.z
);
}
if (dataTrackerChunk.data.timestamp) {
this.setTimestamp(dataTrackerChunk.data.timestamp.data.timestamp);
}
}
}
export function TrackerFromInfo(infoTrackerChunk: InfoTrackerChunk) {
if (infoTrackerChunk.data) {
return new Tracker(infoTrackerChunk.chunk.header.id, infoTrackerChunk.data.trackerName.data.trackerName);
}
return undefined;
}
export function TrackerFromData(dataTrackerChunk: DataTrackerChunk) {
if (dataTrackerChunk.data) {
const tracker = new Tracker(dataTrackerChunk.chunk.header.id, '');
tracker.updateData(dataTrackerChunk);
return tracker;
}
return undefined;
}