Last active
August 8, 2023 19:36
-
-
Save Flova/7ff186d20972f278dae68fe239c65314 to your computer and use it in GitHub Desktop.
Foxglove DSD active action user script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parses the debug json message from the DSD and publishes the currently active action | |
import { Input } from "./types"; | |
// Define message | |
type Output = { | |
name: string; | |
}; | |
interface DSDDebugDataElement { | |
type: string; | |
classname: string; | |
next?: DSDDebugDataElement; | |
debug_data?: { | |
[key: string]: any; | |
}; | |
} | |
export const inputs = ["/debug/dsd/body_behavior"]; | |
export const output = "/active_action"; | |
export default function script( | |
event: Input<"/debug/dsd/body_behavior"> | |
): Output { | |
function findDeepestElement(data: DSDDebugDataElement): DSDDebugDataElement { | |
if (data.next) { | |
return findDeepestElement(data.next); | |
} | |
return data; | |
} | |
const deepestElement = findDeepestElement(JSON.parse(event.message.data)); | |
let action_name = deepestElement.classname; | |
// Handle sequence elements | |
if ( | |
deepestElement.type === "sequence" && | |
deepestElement.debug_data && | |
deepestElement.debug_data["Active Element"] | |
) { | |
action_name = deepestElement.debug_data["Active Element"]; | |
} | |
// Check if we have an action | |
if (!["action", "sequence"].includes(deepestElement.type)) { | |
action_name = ""; | |
} | |
return { | |
name: action_name, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment