Skip to content

Commit

Permalink
Create photonstructdecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Sep 12, 2024
1 parent 7bbca48 commit 792dbfa
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch PhotonStruct",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/shared/log/PhotonStructDecoder.ts"
]
}],
}
7 changes: 6 additions & 1 deletion src/hub/dataSources/nt4/NT4Source.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Log from "../../../shared/log/Log";
import { PROTO_PREFIX, STRUCT_PREFIX, getEnabledKey } from "../../../shared/log/LogUtil";
import { PHOTON_PREFIX, PROTO_PREFIX, STRUCT_PREFIX, getEnabledKey } from "../../../shared/log/LogUtil";
import LoggableType from "../../../shared/log/LoggableType";
import ProtoDecoder from "../../../shared/log/ProtoDecoder";
import { checkArrayType } from "../../../shared/util";
Expand Down Expand Up @@ -185,6 +185,8 @@ export default class NT4Source extends LiveDataSource {
}
} else if (topic.type.startsWith(PROTO_PREFIX)) {
structuredType = ProtoDecoder.getFriendlySchemaType(topic.type.split(PROTO_PREFIX)[1]);
} else if (topic.type.startsWith(PHOTON_PREFIX)) {
structuredType = topic.type.split(PHOTON_PREFIX)[1];
} else if (topic.type === "msgpack") {
structuredType = "MessagePack";
} else if (topic.type === "json") {
Expand Down Expand Up @@ -284,6 +286,9 @@ export default class NT4Source extends LiveDataSource {
} else {
this.log?.putStruct(key, timestamp, value, schemaType, false);
}
} else if (topic.type.startsWith(PHOTON_PREFIX)) {
let schemaType = topic.type.split(PHOTON_PREFIX)[1];
this.log?.putPhotonStruct(key, timestamp, value, schemaType);
} else if (topic.type.startsWith(PROTO_PREFIX)) {
let schemaType = topic.type.split(PROTO_PREFIX)[1];
this.log?.putProto(key, timestamp, value, schemaType);
Expand Down
41 changes: 40 additions & 1 deletion src/shared/log/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Pose2d, Translation2d } from "../geometry";
import { arraysEqual, checkArrayType } from "../util";
import LogField from "./LogField";
import LogFieldTree from "./LogFieldTree";
import { MERGE_PREFIX, STRUCT_PREFIX, TYPE_KEY, getEnabledData, splitLogKey } from "./LogUtil";
import { MERGE_PREFIX, PHOTON_PREFIX, STRUCT_PREFIX, TYPE_KEY, getEnabledData, splitLogKey } from "./LogUtil";
import {
LogValueSetAny,
LogValueSetBoolean,
Expand All @@ -24,6 +24,7 @@ export default class Log {
private msgpackDecoder = new Decoder();
private structDecoder = new StructDecoder();
private protoDecoder = new ProtoDecoder();
private photonDecoder = new PhotonStructDecoder();

private fields: { [id: string]: LogField } = {};
private generatedParents: Set<string> = new Set(); // Children of these fields are generated
Expand Down Expand Up @@ -335,6 +336,11 @@ export default class Log {
// Check for struct schema
if (key.includes("/.schema/" + STRUCT_PREFIX)) {
this.structDecoder.addSchema(key.split(STRUCT_PREFIX)[1], value);
this.photonDecoder.addSchema(key.split(STRUCT_PREFIX)[1], value);
this.attemptQueuedStructures();
}
if (key.includes("/.schema/" + PHOTON_PREFIX)) {
this.photonDecoder.addSchema(key.split(PHOTON_PREFIX)[1], value);
this.attemptQueuedStructures();
}
}
Expand Down Expand Up @@ -535,6 +541,39 @@ export default class Log {
}
}

/** Writes a photonstruct-encoded raw value to the field.
*
* The schema type should not include "photonstruct:"
*/
putPhotonStruct(key: string, timestamp: number, value: Uint8Array, schemaType: string) {
this.putRaw(key, timestamp, value);
if (this.fields[key].getType() === LoggableType.Raw) {
this.setGeneratedParent(key);
this.setStructuredType(key, schemaType);
let decodedData: { data: unknown; schemaTypes: { [key: string]: string } } | null = null;
try {
decodedData = this.photonDecoder.decode(schemaType, value);
} catch {}
if (decodedData !== null) {
this.putUnknownStruct(key, timestamp, decodedData.data);
Object.entries(decodedData.schemaTypes).forEach(([childKey, schemaType]) => {
// Create the key so it can be dragged even though it doesn't have data
let fullChildKey = key + "/" + childKey;
this.createBlankField(fullChildKey, LoggableType.Empty);
this.processTimestamp(fullChildKey, timestamp);
this.setStructuredType(fullChildKey, schemaType);
});
} else {
this.queuedStructs.push({
key: key,
timestamp: timestamp,
value: value,
schemaType: schemaType
});
}
}
}

/** Writes a struct-encoded raw value to the field.
*
* The schema type should not include "struct:" or "[]"
Expand Down
1 change: 1 addition & 0 deletions src/shared/log/LogUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LogValueSetBoolean } from "./LogValueSets";
export const TYPE_KEY = ".type";
export const STRUCT_PREFIX = "struct:";
export const PROTO_PREFIX = "proto:";
export const PHOTON_PREFIX = "photonstruct:";
export const MAX_SEARCH_RESULTS = 128;
export const MERGE_PREFIX = "Log";
export const MERGE_MAX_FILES = 10;
Expand Down
Loading

0 comments on commit 792dbfa

Please sign in to comment.