diff --git a/frontend/testing-view/src/App.tsx b/frontend/testing-view/src/App.tsx index ebe5212d6..9785243cd 100644 --- a/frontend/testing-view/src/App.tsx +++ b/frontend/testing-view/src/App.tsx @@ -14,6 +14,7 @@ import { Testing } from "./pages/Testing"; import { useStore } from "./store/store"; import type { Connection } from "./types/common/connection"; import type { MessagePacket } from "./types/data/message"; +import type { ProtectionUpdate } from "./types/data/protection"; import type { TelemetryData } from "./types/telemetry/telemetry"; function App() { @@ -63,6 +64,14 @@ function App() { }); }); + // Callback executed when a protection triggers + const addProtection = useStore((s) => s.addProtection); + + // Subscribe to protections, to highlight the measurement that triggered them + useTopic("protection/update", (protection) => { + addProtection(protection); + }); + return ( diff --git a/frontend/testing-view/src/features/workspace/components/rightSidebar/sections/MessagesSection.tsx b/frontend/testing-view/src/features/workspace/components/rightSidebar/sections/MessagesSection.tsx index 533c592c8..f8c39c1a1 100644 --- a/frontend/testing-view/src/features/workspace/components/rightSidebar/sections/MessagesSection.tsx +++ b/frontend/testing-view/src/features/workspace/components/rightSidebar/sections/MessagesSection.tsx @@ -24,6 +24,8 @@ const MessagesSection = () => { variant="secondary" size="icon" className="text-muted-foreground hover:text-destructive h-6 w-6" + title="Clear messages and protections" + aria-label="Clear messages and protections" onClick={clearMessages} > diff --git a/frontend/testing-view/src/features/workspace/components/rightSidebar/tabs/telemetry/VariableItem.tsx b/frontend/testing-view/src/features/workspace/components/rightSidebar/tabs/telemetry/VariableItem.tsx index 6ea4396dc..08ea6c402 100644 --- a/frontend/testing-view/src/features/workspace/components/rightSidebar/tabs/telemetry/VariableItem.tsx +++ b/frontend/testing-view/src/features/workspace/components/rightSidebar/tabs/telemetry/VariableItem.tsx @@ -1,6 +1,7 @@ import { useDraggable } from "@dnd-kit/core"; import { Badge } from "@hyperloopupv/frontend-kit"; import { cn } from "@hyperloopupv/frontend-kit/lib"; +import { Shield } from "lucide-react"; import { useShallow } from "zustand/shallow"; import { canAddSeriesToChart, @@ -34,6 +35,13 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => { })), ); + // A protection triggered on this measurement and the messages weren't cleared since + const isProtected = useStore( + (s) => + variable.adjId !== undefined && + s.activeProtections[variable.adjId] !== undefined, + ); + const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({ id: `draggable-${packetId}-${variable.id}`, @@ -89,6 +97,7 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => { {...listeners} className={cn( "hover:bg-accent/30 group flex items-center justify-between gap-3 border-t py-2 pl-6 pr-3", + isProtected && "bg-red-500/15 hover:bg-red-500/25", opacityStyle, cursorStyle, )} @@ -105,9 +114,21 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => { {variable.type} + {/* Protection Icon */} + {isProtected && ( + + + + )} + {/* Variable Name */}
- + {variable.name} diff --git a/frontend/testing-view/src/store/slices/__tests__/messagesSlice.test.ts b/frontend/testing-view/src/store/slices/__tests__/messagesSlice.test.ts new file mode 100644 index 000000000..0c512a4be --- /dev/null +++ b/frontend/testing-view/src/store/slices/__tests__/messagesSlice.test.ts @@ -0,0 +1,106 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { create } from "zustand"; +import { createChartsSlice } from "../../../features/charts/store/chartsSlice"; +import { createFilteringSlice } from "../../../features/filtering/store/filteringSlice"; +import { createRightSidebarSlice } from "../../../features/workspace/store/rightSidebarSlice"; +import { createWorkspacesSlice } from "../../../features/workspace/store/workspacesSlice"; +import type { Message } from "../../../types/data/message"; +import type { ProtectionUpdate } from "../../../types/data/protection"; +import type { Store } from "../../store"; +import { createAppSlice } from "../appSlice"; +import { createCatalogSlice } from "../catalogSlice"; +import { createConnectionsSlice } from "../connectionsSlice"; +import { createMessagesSlice } from "../messagesSlice"; +import { createTelemetrySlice } from "../telemetrySlice"; + +const createTestStore = () => + create()((...a) => ({ + ...createAppSlice(...a), + ...createCatalogSlice(...a), + ...createWorkspacesSlice(...a), + ...createTelemetrySlice(...a), + ...createRightSidebarSlice(...a), + ...createConnectionsSlice(...a), + ...createMessagesSlice(...a), + ...createChartsSlice(...a), + ...createFilteringSlice(...a), + })); + +/** Protection number `position` (1-7) of the measurement `measurementId`. */ +const protection = ( + position: number, + measurementId: number, + value: ProtectionUpdate["value"] = 105.5, +): ProtectionUpdate => ({ + id: (position << 13) | measurementId, + value, + timestamp: { hour: 12, minute: 30, second: 6, subsecond: 0 }, +}); + +const MESSAGE: Message = { + id: "message-1", + kind: "info", + board: "LCU", + name: "Example", + payload: "Example message", + timestamp: { + counter: 0, + second: 0, + minute: 0, + hour: 12, + day: 1, + month: 1, + year: 2026, + }, +}; + +let store: ReturnType; + +beforeEach(() => { + store = createTestStore(); +}); + +describe("addProtection", () => { + it("activates the measurement in the low 13 bits of the protection id", () => { + store.getState().addProtection(protection(1, 3000)); + + expect(Object.keys(store.getState().activeProtections)).toEqual(["3000"]); + expect(store.getState().activeProtections[3000].id).toBe(11192); + }); + + it("keeps the last protection when several protections of one measurement trigger", () => { + store.getState().addProtection(protection(1, 3000, 105.5)); + store.getState().addProtection(protection(2, 3000, 5)); + + expect(Object.keys(store.getState().activeProtections)).toEqual(["3000"]); + expect(store.getState().activeProtections[3000].value).toBe(5); + }); + + it("keeps the protections of different measurements apart", () => { + store.getState().addProtection(protection(1, 3000)); + store.getState().addProtection(protection(1, 3001)); + + expect(Object.keys(store.getState().activeProtections)).toEqual([ + "3000", + "3001", + ]); + }); + + it("does not add messages", () => { + store.getState().addProtection(protection(1, 3000)); + + expect(store.getState().messages).toEqual([]); + }); +}); + +describe("clearMessages", () => { + it("clears the messages and the active protections", () => { + store.getState().addMessage(MESSAGE); + store.getState().addProtection(protection(1, 3000)); + + store.getState().clearMessages(); + + expect(store.getState().messages).toEqual([]); + expect(store.getState().activeProtections).toEqual({}); + }); +}); diff --git a/frontend/testing-view/src/store/slices/messagesSlice.ts b/frontend/testing-view/src/store/slices/messagesSlice.ts index 0a9b2d898..87278f7ee 100644 --- a/frontend/testing-view/src/store/slices/messagesSlice.ts +++ b/frontend/testing-view/src/store/slices/messagesSlice.ts @@ -1,11 +1,22 @@ import type { StateCreator } from "zustand"; import { config } from "../../../config"; import type { Message } from "../../types/data/message"; +import type { ProtectionUpdate } from "../../types/data/protection"; import type { Store } from "../store"; +/** The low 13 bits of a protection id are the id of the measurement it watches. */ +const MEASUREMENT_ID_MASK = 0x1fff; + export interface MessagesSlice { messages: Message[]; addMessage: (message: Message) => void; + /** + * Last protection received for each measurement, by the measurement's numeric ADJ id.\ + * Those measurements are highlighted until the messages are cleared. + */ + activeProtections: Record; + addProtection: (protection: ProtectionUpdate) => void; + /** Clears the messages and the active protections */ clearMessages: () => void; } @@ -20,5 +31,13 @@ export const createMessagesSlice: StateCreator = ( config.MAX_MESSAGES_COUNT, ), })), - clearMessages: () => set({ messages: [] }), + activeProtections: {}, + addProtection: (protection) => + set((state) => ({ + activeProtections: { + ...state.activeProtections, + [protection.id & MEASUREMENT_ID_MASK]: protection, + }, + })), + clearMessages: () => set({ messages: [], activeProtections: {} }), }); diff --git a/frontend/testing-view/src/types/data/protection.ts b/frontend/testing-view/src/types/data/protection.ts new file mode 100644 index 000000000..c7acd27d5 --- /dev/null +++ b/frontend/testing-view/src/types/data/protection.ts @@ -0,0 +1,25 @@ +/** + * Time of day at which the board detected the protection.\ + * It carries no date: it is the day the protection was received. + */ +export type ProtectionTimestamp = { + hour: number; + minute: number; + second: number; + /** Raw sub-second byte from the board (its unit is not settled yet) */ + subsecond: number; +}; + +/** + * Protection event as it arrives from the backend on `protection/update`. + */ +export type ProtectionUpdate = { + /** + * Compound id: `(position << 13) | measurementId`.\ + * The low 13 bits are the numeric ADJ id of the measurement that triggered it. + */ + id: number; + /** Value of the measurement that triggered the protection */ + value: number | boolean | string; + timestamp: ProtectionTimestamp; +}; diff --git a/frontend/testing-view/src/types/data/telemetryCatalogItem.ts b/frontend/testing-view/src/types/data/telemetryCatalogItem.ts index 3d8532a11..f0653db79 100644 --- a/frontend/testing-view/src/types/data/telemetryCatalogItem.ts +++ b/frontend/testing-view/src/types/data/telemetryCatalogItem.ts @@ -8,6 +8,11 @@ export type Variable = { name: string; type: string; units?: string; + /** + * Numeric id of the measurement in the ADJ.\ + * Protections point to the measurement they watch by this id. + */ + adjId?: number; }; export interface NumericVariable extends Variable {