Skip to content

Commit

Permalink
try fixing state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeechey committed Jul 1, 2024
1 parent dfc2393 commit 2056ab1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
12 changes: 6 additions & 6 deletions telemetry/packages/constants/src/pods/states.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type PodStateType = keyof typeof ALL_POD_STATES;
export type PodStateType = (typeof ALL_POD_STATES)[keyof typeof ALL_POD_STATES];

export const FAILURE_STATES = {
FAILURE_BRAKING: 'kFailureBrake',
Expand Down Expand Up @@ -47,11 +47,11 @@ export const ALL_POD_STATE_TYPES = [
export type PodStateCategoryType = (typeof ALL_POD_STATE_TYPES)[number];

export const getStateType = (
state: string,
state: any,
): (typeof ALL_POD_STATE_TYPES)[number] => {
if (FAILURE_STATES[state as keyof typeof FAILURE_STATES]) return 'FAILURE';
if (PASSIVE_STATES[state as keyof typeof PASSIVE_STATES]) return 'PASSIVE';
if (ACTIVE_STATES[state as keyof typeof ACTIVE_STATES]) return 'ACTIVE';
if (NULL_STATES[state as keyof typeof NULL_STATES]) return 'NULL';
if (Object.values(FAILURE_STATES).includes(state)) return 'FAILURE';
if (Object.values(PASSIVE_STATES).includes(state)) return 'PASSIVE';
if (Object.values(ACTIVE_STATES).includes(state)) return 'ACTIVE';
if (Object.values(NULL_STATES).includes(state)) return 'NULL';
throw new Error(`Unknown state: ${state}`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export function zodEnumFromObjKeys<K extends string>(
const [firstKey, ...otherKeys] = Object.keys(obj) as K[];
return z.enum([firstKey, ...otherKeys]);
}

export function zodEnumFromObjValues<V extends string>(
obj: Record<string, V>,
): z.ZodEnum<[V, ...V[]]> {
const [firstValue, ...otherValues] = Object.values(obj);
return z.enum([firstValue, ...otherValues]);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ALL_POD_STATES, pods } from '@hyped/telemetry-constants';
import { zodEnumFromObjKeys } from '@/modules/common/utils/zodEnumFromObjKeys';
import {
zodEnumFromObjKeys,
zodEnumFromObjValues,
} from '@/modules/common/utils/zodEnumFromObjKeys';
import { z } from 'zod';

export const StateUpdateSchema = z.object({
podId: zodEnumFromObjKeys(pods),
timestamp: z.string(), // to handle nanoseconds timestamp
value: zodEnumFromObjKeys(ALL_POD_STATES),
value: zodEnumFromObjValues(ALL_POD_STATES),
});

export type StateUpdate = z.infer<typeof StateUpdateSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,11 @@ export function StateMachine() {
nodes.filter((node) =>
isEnabledState(
currentMode,
node.id.replace(/-/g, '_').toUpperCase() as PodStateType,
ALL_POD_STATES[
node.id
.replace(/-/g, '_')
.toUpperCase() as keyof typeof ALL_POD_STATES
],
),
),
);
Expand Down
7 changes: 4 additions & 3 deletions telemetry/packages/ui/app/context/pods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ export const PodsProvider = ({ children }: { children: React.ReactNode }) => {
function subscribeToMqttAndLatency() {
if (!client) return;
const processMessage = (podId: PodId, topic: string, message: Buffer) => {
if (topic === getTopic('state', podId)) {
const newPodState = message.toString();
if (topic === getTopic('state/state', podId)) {
const newPodState = JSON.parse(message.toString())
.state as PodStateType;
const allowedStates = Object.values(ALL_POD_STATES);
if ((allowedStates as string[]).includes(newPodState)) {
setPodsState((prevState) => ({
Expand Down Expand Up @@ -275,7 +276,7 @@ export const PodsProvider = ({ children }: { children: React.ReactNode }) => {
// subscribe to latency messages and add MQTT message callback for each pod
POD_IDS.map((podId) => {
subscribe('latency/response', podId);
subscribe('state', podId);
subscribe('state/state', podId);
client.on('message', (topic, message) =>
processMessage(podId, topic, message),
);
Expand Down
2 changes: 1 addition & 1 deletion telemetry/packages/ui/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit 2056ab1

Please sign in to comment.