Files
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

38 lines
1.3 KiB
TypeScript

import type { NumericSensor } from './types.ts';
export function oneDecimal(value: number | string | null | undefined): string {
if (value === null || value === undefined || Number.isNaN(Number(value))) return '--';
const num = Number(value);
// For values > 999, add thousands separator (Indonesian format)
if (Math.abs(num) > 999) {
return num.toLocaleString('id-ID', {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
}
return num.toFixed(1).replace('.', ',');
}
export function sensorToCelsius(sensor?: NumericSensor): number | null {
if (!sensor || sensor.value === null || sensor.value === undefined) return null;
return (Number(sensor.value) + Number(sensor.calibration || 0)) / 10;
}
/**
* Flocks payload uses {@link NumericSensor} (raw tenths + calibration). The sensors/range API
* often returns inside temperatures as plain °C numbers on `sensors.temperature1` etc.
*/
export function celsiusFromHistoryTempField(
raw: NumericSensor | number | null | undefined
): number | null {
if (raw === null || raw === undefined) return null;
if (typeof raw === 'number' && Number.isFinite(raw)) return raw;
return sensorToCelsius(raw as NumericSensor);
}
export function getStatusText(status: number | null | undefined): string {
if (status === 1) return 'Aktif';
if (status === 0) return 'Off';
return '--';
}