Skip to content

Commit

Permalink
Fix handling of merged keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Oct 11, 2024
1 parent e9a240e commit 000f190
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions src/shared/log/LogUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,85 @@ export const STRUCT_PREFIX = "struct:";
export const PROTO_PREFIX = "proto:";
export const MAX_SEARCH_RESULTS = 128;
export const MERGE_PREFIX = "Log";
export const MERGE_MAX_FILES = 10;
export const MERGE_PREFIX_REGEX = new RegExp(/^\/?Log\d+/);
export const SEPARATOR_REGEX = new RegExp(/\/|:/);
export const SEPARATOR_REGEX_PHOENIX = new RegExp(/\/|:|_/);
export const PHOENIX_PREFIX = "Phoenix6";
export const ENABLED_KEYS = withMergedKeys([
export const ENABLED_KEYS = [
"/DriverStation/Enabled",
"NT:/AdvantageKit/DriverStation/Enabled",
"DS:enabled",
"NT:/FMSInfo/FMSControlData",
"/DSLog/Status/DSDisabled",
"RobotEnable" // Phoenix
]);
export const AUTONOMOUS_KEYS = withMergedKeys([
];
export const AUTONOMOUS_KEYS = [
"/DriverStation/Autonomous",
"NT:/AdvantageKit/DriverStation/Autonomous",
"DS:autonomous",
"NT:/FMSInfo/FMSControlData",
"/DSLog/Status/DSTeleop"
]);
export const ALLIANCE_KEYS = withMergedKeys([
];
export const ALLIANCE_KEYS = [
"/DriverStation/AllianceStation",
"NT:/AdvantageKit/DriverStation/AllianceStation",
"NT:/FMSInfo/IsRedAlliance"
]);
export const DRIVER_STATION_KEYS = withMergedKeys([
];
export const DRIVER_STATION_KEYS = [
"/DriverStation/AllianceStation",
"NT:/AdvantageKit/DriverStation/AllianceStation",
"NT:/FMSInfo/StationNumber"
]);
export const JOYSTICK_KEYS = withMergedKeys([
"/DriverStation/Joystick",
"NT:/AdvantageKit/DriverStation/Joystick",
"DS:joystick"
]);
export const SYSTEM_TIME_KEYS = withMergedKeys([
];
export const JOYSTICK_KEYS = ["/DriverStation/Joystick", "NT:/AdvantageKit/DriverStation/Joystick", "DS:joystick"];
export const SYSTEM_TIME_KEYS = [
"/SystemStats/EpochTimeMicros",
"NT:/AdvantageKit/SystemStats/EpochTimeMicros",
"systemTime"
]);
export const AKIT_TIMESTAMP_KEYS = withMergedKeys(["/Timestamp", "NT:/AdvantageKit/Timestamp"]);
export const METADATA_KEYS = withMergedKeys([
];
export const AKIT_TIMESTAMP_KEYS = ["/Timestamp", "NT:/AdvantageKit/Timestamp"];
export const METADATA_KEYS = [
"/Metadata",
"/RealMetadata",
"/ReplayMetadata",
"NT:/Metadata",
"NT:/AdvantageKit/RealMetadata",
"NT:/AdvantageKit/ReplayMetadata"
]);
export const EVENT_KEYS = withMergedKeys([
];
export const EVENT_KEYS = [
"/DriverStation/EventName",
"NT:/AdvantageKit/DriverStation/EventName",
"NT:/FMSInfo/EventName"
]);
export const MATCH_TYPE_KEYS = withMergedKeys([
];
export const MATCH_TYPE_KEYS = [
"/DriverStation/MatchType",
"NT:/AdvantageKit/DriverStation/MatchType",
"NT:/FMSInfo/MatchType"
]);
export const MATCH_NUMBER_KEYS = withMergedKeys([
];
export const MATCH_NUMBER_KEYS = [
"/DriverStation/MatchNumber",
"NT:/AdvantageKit/DriverStation/MatchNumber",
"NT:/FMSInfo/MatchNumber"
]);

/** Returns a set of keys starting with the merged log prefixes. */
function withMergedKeys(keys: string[]): string[] {
let output: string[] = [];
keys.forEach((key) => {
output.push(key);
for (let i = 0; i < MERGE_MAX_FILES; i++) {
output.push("/" + MERGE_PREFIX + i.toString() + (key.startsWith("/") ? "" : "/") + key);
];

/** Returns the version of the key without the merge prefix. */
export function removeMergePrefix(key: string): string {
let match = key.match(MERGE_PREFIX_REGEX);
if (match === null) {
return key;
} else {
return key.slice(match[0].length);
}
}

export function findKey(log: Log, search: string[]): string | undefined {
let fieldKeys = log.getFieldKeys();
for (let i = 0; i < fieldKeys.length; i++) {
let unmerged = removeMergePrefix(fieldKeys[i]);
if (search.includes(unmerged)) {
return fieldKeys[i];
}
});
return output;
}
return undefined;
}

/** Adds a prefix to a log key. */
Expand Down Expand Up @@ -153,7 +158,8 @@ export function logValuesEqual(type: LoggableType, a: any, b: any): boolean {
}

export function splitLogKey(key: string): string[] {
if (key.startsWith(PHOENIX_PREFIX)) {
let unmergedKey = removeMergePrefix(key);
if (unmergedKey.startsWith(PHOENIX_PREFIX) || unmergedKey.startsWith("/" + PHOENIX_PREFIX)) {
return key.split(SEPARATOR_REGEX_PHOENIX);
} else {
return key.split(SEPARATOR_REGEX);
Expand Down Expand Up @@ -192,7 +198,7 @@ export function getURCLKeys(log: Log): string[] {
}

export function getEnabledKey(log: Log): string | undefined {
return ENABLED_KEYS.find((key) => log.getFieldKeys().includes(key));
return findKey(log, ENABLED_KEYS);
}

export function getEnabledData(log: Log): LogValueSetBoolean | null {
Expand Down Expand Up @@ -222,7 +228,7 @@ export function getEnabledData(log: Log): LogValueSetBoolean | null {
}

export function getAutonomousKey(log: Log): string | undefined {
return AUTONOMOUS_KEYS.find((key) => log.getFieldKeys().includes(key));
return findKey(log, AUTONOMOUS_KEYS);
}

export function getAutonomousData(log: Log): LogValueSetBoolean | null {
Expand Down Expand Up @@ -300,7 +306,7 @@ export function getRobotStateRanges(log: Log): { start: number; end?: number; mo
}

export function getIsRedAlliance(log: Log, time: number): boolean {
let allianceKey = ALLIANCE_KEYS.find((key) => log.getFieldKeys().includes(key));
let allianceKey = findKey(log, ALLIANCE_KEYS);
if (!allianceKey) return false;

if (allianceKey.endsWith("AllianceStation")) {
Expand All @@ -324,7 +330,7 @@ export function getIsRedAlliance(log: Log, time: number): boolean {
}

export function getDriverStation(log: Log, time: number): number {
let dsKey = DRIVER_STATION_KEYS.find((key) => log.getFieldKeys().includes(key));
let dsKey = findKey(log, DRIVER_STATION_KEYS);
if (!dsKey) return -1;
let tempDSData = log.getNumber(dsKey, time, time);
if (tempDSData && tempDSData.values.length > 0) {
Expand Down Expand Up @@ -394,8 +400,8 @@ export function getJoystickState(log: Log, joystickId: number, time: number): Jo
if (tablePrefix !== "") return;
JOYSTICK_KEYS.forEach((joystickKey) => {
if (tablePrefix !== "") return;
if (key.startsWith(joystickKey + joystickId.toString())) {
tablePrefix = joystickKey + joystickId.toString() + "/";
if (removeMergePrefix(key).startsWith(joystickKey + joystickId.toString())) {
tablePrefix = key.slice(0, key.indexOf(joystickKey)) + joystickKey + joystickId.toString() + "/";
isAkit = joystickKey.endsWith("/DriverStation/Joystick");
}
});
Expand Down Expand Up @@ -587,10 +593,10 @@ export function getMatchInfo(log: Log): MatchInfo | null {
}

// Get match info keys
let systemTimeKey = SYSTEM_TIME_KEYS.find((key) => log.getFieldKeys().includes(key));
let eventKey = EVENT_KEYS.find((key) => log.getFieldKeys().includes(key));
let matchTypeKeys = MATCH_TYPE_KEYS.find((key) => log.getFieldKeys().includes(key));
let matchNumberKeys = MATCH_NUMBER_KEYS.find((key) => log.getFieldKeys().includes(key));
let systemTimeKey = findKey(log, SYSTEM_TIME_KEYS);
let eventKey = findKey(log, EVENT_KEYS);
let matchTypeKeys = findKey(log, MATCH_TYPE_KEYS);
let matchNumberKeys = findKey(log, MATCH_NUMBER_KEYS);
if (!eventKey || !matchTypeKeys || !matchNumberKeys) return null;

// Read match info
Expand Down

0 comments on commit 000f190

Please sign in to comment.