import { CheckCircle as OKIcon, Error as AlarmingIcon, Help as UnknownIcon, Warning as WarningIcon } from "@mui/icons-material"; import { pond } from "protobuf-ts/pond"; import { getTextSecondary } from "theme/text"; export interface DeviceStateHelper { description: string; icon: any; colour: string; } const Unknown: DeviceStateHelper = { description: "Unknown", icon: , colour: "var(--status-unknown)" }; const OK: DeviceStateHelper = { description: "OK", icon: , colour: "var(--status-ok)" }; const LowPower: DeviceStateHelper = { description: "Low Power", icon: , colour: "var(--status-warning)" }; const Missing: DeviceStateHelper = { description: "Missing", icon: , colour: getTextSecondary() }; const DEVICE_STATE_MAP = new Map([ [pond.DeviceState.DEVICE_STATE_UNKNOWN, Unknown], [pond.DeviceState.DEVICE_STATE_OK, OK], [pond.DeviceState.DEVICE_STATE_LOW_POWER, LowPower], [pond.DeviceState.DEVICE_STATE_MISSING, Missing] ]); export function compareEnum(value: T[keyof T], e: T, enumMember: T[keyof T]): boolean { // let has = false // if (typeof value === 'string') { // Object.values(e).forEach((val: keyof T) => { if (e[value as keyof T] === enumMember) { // has = true return true } return false // }) // } else if (typeof value === 'number') { // Object.keys(e as any).forEach(val => { // if (val === enumMember) { // has = true // return // } // }) // } // return has } export function getDeviceStateHelper(state: pond.DeviceState): DeviceStateHelper { if (compareEnum(state, pond.DeviceState, pond.DeviceState.DEVICE_STATE_OK)) { return OK; } const deviceStates = Array.from(DEVICE_STATE_MAP.keys()); for (var i = 0; i < deviceStates.length; i++) { let key = deviceStates[i]; if (state === key) { return DEVICE_STATE_MAP.get(key) as DeviceStateHelper; } } return Unknown; } export function getDeviceStateDescription(state: pond.DeviceState): string { return getDeviceStateHelper(state).description; } export function getDeviceStateIcon(state: pond.DeviceState): any { return getDeviceStateHelper(state).icon; } export function getDeviceStateColour(state: pond.DeviceState): any { return getDeviceStateHelper(state).colour; } export function filterByDeviceState(searchValue: string, state: pond.DeviceState) { let matches: string[] = []; switch (state) { case pond.DeviceState.DEVICE_STATE_OK: matches = ["okay", "good", "safe", "stable"]; break; case pond.DeviceState.DEVICE_STATE_MISSING: matches = ["alarming", "alarmed", "bad", "error", "alert"]; break; case pond.DeviceState.DEVICE_STATE_LOW_POWER: matches = ["warning", "caution", "low power"]; break; default: matches = ["unknown", "?"]; break; } return matches.some(match => match.indexOf(searchValue.replace("state:", "").trim()) > -1); }