mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
refactor web file structure and seperate settings from status
This commit is contained in:
58
interface/src/app/main/deviceValue.ts
Normal file
58
interface/src/app/main/deviceValue.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { TranslationFunctions } from 'i18n/i18n-types';
|
||||
|
||||
import { DeviceValueUOM, DeviceValueUOM_s } from './types';
|
||||
|
||||
const formatDurationMin = (LL: TranslationFunctions, duration_min: number) => {
|
||||
const days = Math.trunc((duration_min * 60000) / 86400000);
|
||||
const hours = Math.trunc((duration_min * 60000) / 3600000) % 24;
|
||||
const minutes = Math.trunc((duration_min * 60000) / 60000) % 60;
|
||||
|
||||
let formatted = '';
|
||||
if (days) {
|
||||
formatted += LL.NUM_DAYS({ num: days });
|
||||
}
|
||||
|
||||
if (hours) {
|
||||
if (formatted) formatted += ' ';
|
||||
formatted += LL.NUM_HOURS({ num: hours });
|
||||
}
|
||||
|
||||
if (minutes) {
|
||||
if (formatted) formatted += ' ';
|
||||
formatted += LL.NUM_MINUTES({ num: minutes });
|
||||
}
|
||||
|
||||
return formatted;
|
||||
};
|
||||
|
||||
export function formatValue(
|
||||
LL: TranslationFunctions,
|
||||
value: unknown,
|
||||
uom: DeviceValueUOM
|
||||
) {
|
||||
if (typeof value !== 'number') {
|
||||
return (value === undefined ? '' : value) as string;
|
||||
}
|
||||
switch (uom) {
|
||||
case DeviceValueUOM.HOURS:
|
||||
return value ? formatDurationMin(LL, value * 60) : LL.NUM_HOURS({ num: 0 });
|
||||
case DeviceValueUOM.MINUTES:
|
||||
return value ? formatDurationMin(LL, value) : LL.NUM_MINUTES({ num: 0 });
|
||||
case DeviceValueUOM.SECONDS:
|
||||
return LL.NUM_SECONDS({ num: value });
|
||||
case DeviceValueUOM.NONE:
|
||||
return new Intl.NumberFormat().format(value);
|
||||
case DeviceValueUOM.DEGREES:
|
||||
case DeviceValueUOM.DEGREES_R:
|
||||
case DeviceValueUOM.FAHRENHEIT:
|
||||
return (
|
||||
new Intl.NumberFormat(undefined, {
|
||||
minimumFractionDigits: 1
|
||||
}).format(value) +
|
||||
' ' +
|
||||
DeviceValueUOM_s[uom]
|
||||
);
|
||||
default:
|
||||
return new Intl.NumberFormat().format(value) + ' ' + DeviceValueUOM_s[uom];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user