42 lines
1.6 KiB
TypeScript
42 lines
1.6 KiB
TypeScript
import { fanNumberFromDeviceKey, isFanHistoryMetric } from './fanHistoryUtils.ts';
|
|
import type { IotFanHistoryMetric, IotHistoryMetric } from './types.ts';
|
|
|
|
export const HVAC_HISTORY_METRICS = ['heater1', 'heater2', 'cooler'] as const;
|
|
export type IotHvacHistoryMetric = (typeof HVAC_HISTORY_METRICS)[number];
|
|
|
|
export type IotDeviceHistoryMetric = IotFanHistoryMetric | IotHvacHistoryMetric;
|
|
|
|
const HVAC_METRIC_RE = /^(heater[12]|cooler)$/;
|
|
|
|
export function isHvacHistoryMetric(metric: IotHistoryMetric): metric is IotHvacHistoryMetric {
|
|
return HVAC_METRIC_RE.test(metric);
|
|
}
|
|
|
|
export function isDeviceHistoryMetric(metric: IotHistoryMetric): metric is IotDeviceHistoryMetric {
|
|
return isFanHistoryMetric(metric) || isHvacHistoryMetric(metric);
|
|
}
|
|
|
|
export function deviceKeyFromHistoryMetric(metric: IotDeviceHistoryMetric): string {
|
|
if (isFanHistoryMetric(metric)) {
|
|
const fanNumber = Number(metric.replace('fan', ''));
|
|
return `B${fanNumber}`;
|
|
}
|
|
if (metric === 'heater1') return 'H';
|
|
if (metric === 'heater2') return 'H2';
|
|
return 'C';
|
|
}
|
|
|
|
export function historyMetricFromHvacDeviceKey(deviceKey: string): IotHvacHistoryMetric | null {
|
|
const key = String(deviceKey || '').trim();
|
|
if (key === 'H') return 'heater1';
|
|
if (key === 'H2') return 'heater2';
|
|
if (key === 'C') return 'cooler';
|
|
return null;
|
|
}
|
|
|
|
export function historyMetricFromDeviceKey(deviceKey: string): IotDeviceHistoryMetric | null {
|
|
const fanNumber = fanNumberFromDeviceKey(deviceKey);
|
|
if (fanNumber !== null) return `fan${fanNumber}` as IotFanHistoryMetric;
|
|
return historyMetricFromHvacDeviceKey(deviceKey);
|
|
}
|