mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
update libs
This commit is contained in:
@@ -10,7 +10,6 @@ import {
|
||||
DialogActions,
|
||||
DialogContent,
|
||||
DialogTitle,
|
||||
TextField,
|
||||
MenuItem,
|
||||
InputAdornment
|
||||
} from '@mui/material';
|
||||
@@ -33,36 +32,62 @@ import { extractErrorMessage, updateValue } from 'utils';
|
||||
|
||||
import { validate } from 'validators';
|
||||
import { entityItemValidation } from './validators';
|
||||
import { ValidateFieldsError } from 'async-validator';
|
||||
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
import { ValidateFieldsError } from 'async-validator';
|
||||
|
||||
import * as EMSESP from './api';
|
||||
|
||||
function makeid() {
|
||||
return Math.floor(Math.random() * (Math.floor(200) - 100) + 100);
|
||||
}
|
||||
|
||||
const SettingsEntities: FC = () => {
|
||||
const { LL, locale } = useI18nContext();
|
||||
const { LL } = useI18nContext();
|
||||
|
||||
const [numChanges, setNumChanges] = useState<number>(0);
|
||||
const blocker = useBlocker(numChanges !== 0);
|
||||
|
||||
const emptyEntity = {
|
||||
id: 0,
|
||||
name: '',
|
||||
device_id: 8,
|
||||
type_id: 2,
|
||||
type_id: 0,
|
||||
offset: 0,
|
||||
factor: 1,
|
||||
uom: 0,
|
||||
val_type: 2,
|
||||
name: 'name',
|
||||
deleted: false
|
||||
deleted: false,
|
||||
o_name: ''
|
||||
};
|
||||
const [entity, setEntity] = useState<EntityItem[]>([emptyEntity]);
|
||||
|
||||
const [entities, setEntities] = useState<EntityItem[]>([emptyEntity]);
|
||||
const [entityItem, setEntityItem] = useState<EntityItem>();
|
||||
const [errorMessage, setErrorMessage] = useState<string>();
|
||||
const [creating, setCreating] = useState<boolean>(false);
|
||||
|
||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||
|
||||
function hasEntityChanged(ei: EntityItem) {
|
||||
return (
|
||||
ei.id !== ei.o_id ||
|
||||
(ei?.name || '') !== (ei?.o_name || '') ||
|
||||
ei.device_id !== ei.o_device_id ||
|
||||
ei.type_id !== ei.o_type_id ||
|
||||
ei.offset !== ei.o_offset ||
|
||||
ei.uom !== ei.o_uom ||
|
||||
ei.factor !== ei.o_factor ||
|
||||
ei.val_type !== ei.o_val_type ||
|
||||
ei.deleted !== ei.o_deleted
|
||||
);
|
||||
}
|
||||
|
||||
const getNumChanges = () => {
|
||||
if (!entities) {
|
||||
return 0;
|
||||
}
|
||||
return entities.filter((ei) => hasEntityChanged(ei)).length;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setNumChanges(getNumChanges());
|
||||
});
|
||||
@@ -78,6 +103,9 @@ const SettingsEntities: FC = () => {
|
||||
}
|
||||
`,
|
||||
BaseCell: `
|
||||
&:nth-of-type(1) {
|
||||
padding: 8px;
|
||||
}
|
||||
&:nth-of-type(2) {
|
||||
text-align: center;
|
||||
}
|
||||
@@ -88,7 +116,7 @@ const SettingsEntities: FC = () => {
|
||||
text-align: center;
|
||||
}
|
||||
&:nth-of-type(5) {
|
||||
text-align: right;
|
||||
text-align: center;
|
||||
}
|
||||
`,
|
||||
HeaderRow: `
|
||||
@@ -118,23 +146,11 @@ const SettingsEntities: FC = () => {
|
||||
`
|
||||
});
|
||||
|
||||
const fetchEntities = useCallback(async () => {
|
||||
try {
|
||||
const response = await EMSESP.readEntities();
|
||||
setOriginalEntity(response.data.entity);
|
||||
} catch (error) {
|
||||
setErrorMessage(extractErrorMessage(error, LL.PROBLEM_LOADING()));
|
||||
}
|
||||
}, [LL]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchEntities();
|
||||
}, [fetchEntities]);
|
||||
|
||||
const setOriginalEntity = (data: EntityItem[]) => {
|
||||
setEntity(
|
||||
setEntities(
|
||||
data.map((ei) => ({
|
||||
...ei,
|
||||
o_id: ei.id,
|
||||
o_device_id: ei.device_id,
|
||||
o_type_id: ei.type_id,
|
||||
o_offset: ei.offset,
|
||||
@@ -147,41 +163,35 @@ const SettingsEntities: FC = () => {
|
||||
);
|
||||
};
|
||||
|
||||
function hasEntityChanged(ei: EntityItem) {
|
||||
return (
|
||||
ei.device_id !== ei.o_device_id ||
|
||||
ei.type_id !== ei.o_type_id ||
|
||||
ei.name !== ei.o_name ||
|
||||
ei.offset !== ei.o_offset ||
|
||||
ei.uom !== ei.o_uom ||
|
||||
ei.factor !== ei.o_factor ||
|
||||
ei.val_type !== ei.o_val_type ||
|
||||
ei.deleted !== ei.o_deleted
|
||||
);
|
||||
}
|
||||
|
||||
const getNumChanges = () => {
|
||||
if (!entity) {
|
||||
return 0;
|
||||
const fetchEntities = useCallback(async () => {
|
||||
try {
|
||||
const response = await EMSESP.readEntities();
|
||||
setOriginalEntity(response.data.entities);
|
||||
} catch (error) {
|
||||
setErrorMessage(extractErrorMessage(error, LL.PROBLEM_LOADING()));
|
||||
}
|
||||
return entity.filter((ei) => hasEntityChanged(ei)).length;
|
||||
};
|
||||
}, [LL]);
|
||||
|
||||
const saveEntity = async () => {
|
||||
if (entity) {
|
||||
useEffect(() => {
|
||||
void fetchEntities();
|
||||
}, [fetchEntities]);
|
||||
|
||||
const saveEntities = async () => {
|
||||
if (entities) {
|
||||
try {
|
||||
const response = await EMSESP.writeEntities({
|
||||
entity: entity
|
||||
entities: entities
|
||||
.filter((ei) => !ei.deleted)
|
||||
.map((condensed_ei) => {
|
||||
return {
|
||||
id: condensed_ei.id,
|
||||
name: condensed_ei.name,
|
||||
device_id: condensed_ei.device_id,
|
||||
type_id: condensed_ei.type_id,
|
||||
offset: condensed_ei.offset,
|
||||
factor: condensed_ei.factor,
|
||||
val_type: condensed_ei.val_type,
|
||||
uom: condensed_ei.uom,
|
||||
name: condensed_ei.name
|
||||
val_type: condensed_ei.val_type
|
||||
};
|
||||
})
|
||||
});
|
||||
@@ -193,7 +203,7 @@ const SettingsEntities: FC = () => {
|
||||
} catch (error) {
|
||||
toast.error(extractErrorMessage(error, LL.PROBLEM_UPDATING()));
|
||||
}
|
||||
setOriginalEntity(entity);
|
||||
setOriginalEntity(entities);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -205,20 +215,22 @@ const SettingsEntities: FC = () => {
|
||||
const addEntityItem = () => {
|
||||
setCreating(true);
|
||||
setEntityItem({
|
||||
device_id: 8,
|
||||
type_id: 2,
|
||||
id: makeid(),
|
||||
device_id: 11,
|
||||
type_id: 0,
|
||||
offset: 0,
|
||||
factor: 1,
|
||||
val_type: 2,
|
||||
uom: 0,
|
||||
name: 'name',
|
||||
name: '',
|
||||
deleted: false
|
||||
});
|
||||
};
|
||||
|
||||
const updateEntityItem = () => {
|
||||
console.log('here');
|
||||
if (entityItem) {
|
||||
setEntity([...entity.filter((ei) => creating || ei.o_name !== entityItem.o_name), entityItem]);
|
||||
setEntities([...entities.filter((ei) => creating || ei.o_id !== entityItem.o_id), entityItem]);
|
||||
}
|
||||
setEntityItem(undefined);
|
||||
};
|
||||
@@ -233,24 +245,20 @@ const SettingsEntities: FC = () => {
|
||||
return new Intl.NumberFormat().format(value) + ' ' + DeviceValueUOM_s[uom];
|
||||
}
|
||||
|
||||
function showHex(value: string, digit: number) {
|
||||
function showHex(value: number, digit: number) {
|
||||
if (digit === 4) {
|
||||
return '0x' + ('000' + value).slice(-4);
|
||||
return '0x' + ('000' + value.toString(16).toUpperCase()).slice(-4);
|
||||
}
|
||||
return '0x' + ('0' + value).slice(-2);
|
||||
return '0x' + ('0' + value.toString(16).toUpperCase()).slice(-2);
|
||||
}
|
||||
|
||||
const renderEntity = () => {
|
||||
if (!entity) {
|
||||
if (!entities) {
|
||||
return <FormLoader errorMessage={errorMessage} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Table
|
||||
data={{ nodes: entity.filter((ei) => !ei.deleted).sort((a, b) => a.name.localeCompare(b.time)) }}
|
||||
theme={entity_theme}
|
||||
layout={{ custom: true }}
|
||||
>
|
||||
<Table data={{ nodes: entities.filter((ei) => !ei.deleted) }} theme={entity_theme} layout={{ custom: true }}>
|
||||
{(tableList: any) => (
|
||||
<>
|
||||
<Header>
|
||||
@@ -259,7 +267,7 @@ const SettingsEntities: FC = () => {
|
||||
<HeaderCell stiff>Device ID</HeaderCell>
|
||||
<HeaderCell stiff>Type ID</HeaderCell>
|
||||
<HeaderCell stiff>Offset</HeaderCell>
|
||||
<HeaderCell stiff>{LL.VALUE()}</HeaderCell>
|
||||
<HeaderCell stiff>{LL.VALUE(0)}</HeaderCell>
|
||||
</HeaderRow>
|
||||
</Header>
|
||||
<Body>
|
||||
@@ -287,11 +295,16 @@ const SettingsEntities: FC = () => {
|
||||
|
||||
const validateEntityItem = async () => {
|
||||
if (entityItem) {
|
||||
console.log(1);
|
||||
try {
|
||||
setFieldErrors(undefined);
|
||||
await validate(entityItemValidation(entity, entityItem), entityItem);
|
||||
console.log(2);
|
||||
await validate(entityItemValidation(entities, entityItem), entityItem);
|
||||
console.log(3);
|
||||
updateEntityItem();
|
||||
} catch (errors: any) {
|
||||
console.log(4);
|
||||
console.log(errors);
|
||||
setFieldErrors(errors);
|
||||
}
|
||||
}
|
||||
@@ -307,7 +320,7 @@ const SettingsEntities: FC = () => {
|
||||
return (
|
||||
<Dialog open={!!entityItem} onClose={() => closeDialog()}>
|
||||
<DialogTitle>
|
||||
{creating ? LL.ADD(1) + ' ' + LL.NEW() : LL.EDIT()} {LL.CUSTOM_ENTITIES()}
|
||||
{creating ? LL.ADD(1) + ' ' + LL.NEW() : LL.EDIT()} {LL.ENTITY()}
|
||||
</DialogTitle>
|
||||
<DialogContent dividers>
|
||||
<Box display="flex" flexWrap="wrap" mb={1}>
|
||||
@@ -323,11 +336,12 @@ const SettingsEntities: FC = () => {
|
||||
value={entityItem.name}
|
||||
margin="normal"
|
||||
fullWidth
|
||||
onChange={updateValue(setEntityItem)}
|
||||
// onChange={updateValue(setEntityItem)}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={4}>
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors}
|
||||
name="device_id"
|
||||
label="Device ID"
|
||||
margin="normal"
|
||||
@@ -341,6 +355,7 @@ const SettingsEntities: FC = () => {
|
||||
</Grid>
|
||||
<Grid item xs={4}>
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors}
|
||||
name="type_id"
|
||||
label="Type ID"
|
||||
margin="normal"
|
||||
@@ -354,6 +369,7 @@ const SettingsEntities: FC = () => {
|
||||
</Grid>
|
||||
<Grid item xs={4}>
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors}
|
||||
name="offset"
|
||||
label="Offset"
|
||||
margin="normal"
|
||||
@@ -469,7 +485,7 @@ const SettingsEntities: FC = () => {
|
||||
startIcon={<WarningIcon color="warning" />}
|
||||
variant="contained"
|
||||
color="info"
|
||||
onClick={() => saveEntity()}
|
||||
onClick={() => saveEntities()}
|
||||
>
|
||||
{LL.APPLY_CHANGES(numChanges)}
|
||||
</Button>
|
||||
|
||||
@@ -72,12 +72,12 @@ const SettingsScheduler: FC = () => {
|
||||
name: '',
|
||||
o_name: ''
|
||||
};
|
||||
|
||||
const [schedule, setSchedule] = useState<ScheduleItem[]>([emptySchedule]);
|
||||
const [scheduleItem, setScheduleItem] = useState<ScheduleItem>();
|
||||
const [dow, setDow] = useState<string[]>([]);
|
||||
const [errorMessage, setErrorMessage] = useState<string>();
|
||||
const [creating, setCreating] = useState<boolean>(false);
|
||||
|
||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||
|
||||
// eslint-disable-next-line
|
||||
|
||||
@@ -97,18 +97,6 @@ export function getCustomizations(): AxiosPromise<void> {
|
||||
return AXIOS.get('/getCustomizations');
|
||||
}
|
||||
|
||||
export function getEntities(): AxiosPromise<void> {
|
||||
return AXIOS.get('/getEntities');
|
||||
}
|
||||
|
||||
export function readEntities(): AxiosPromise<void> {
|
||||
return AXIOS.get('/entity');
|
||||
}
|
||||
|
||||
export function writeEntities(entities: Entities): AxiosPromise<void> {
|
||||
return AXIOS.post('/entity', entities);
|
||||
}
|
||||
|
||||
export function getSchedule(): AxiosPromise<Schedule> {
|
||||
return AXIOS.get('/getSchedule');
|
||||
}
|
||||
@@ -120,3 +108,15 @@ export function readSchedule(): AxiosPromise<Schedule> {
|
||||
export function writeSchedule(schedule: Schedule): AxiosPromise<void> {
|
||||
return AXIOS.post('/schedule', schedule);
|
||||
}
|
||||
|
||||
export function getEntities(): AxiosPromise<Entities> {
|
||||
return AXIOS.get('/getEntities');
|
||||
}
|
||||
|
||||
export function readEntities(): AxiosPromise<Entities> {
|
||||
return AXIOS.get('/entities');
|
||||
}
|
||||
|
||||
export function writeEntities(entities: Entities): AxiosPromise<void> {
|
||||
return AXIOS.post('/entities', entities);
|
||||
}
|
||||
|
||||
@@ -339,25 +339,27 @@ export enum ScheduleFlag {
|
||||
}
|
||||
|
||||
export interface EntityItem {
|
||||
id: number; // unique number
|
||||
name: string;
|
||||
device_id: string;
|
||||
type_id: string;
|
||||
device_id: number;
|
||||
type_id: number;
|
||||
offset: number;
|
||||
factor: number;
|
||||
uom: number;
|
||||
val_type: number;
|
||||
value?: number;
|
||||
deleted?: boolean; // optional
|
||||
o_id?: number;
|
||||
o_name?: string;
|
||||
o_device_id?: string;
|
||||
o_type_id?: string;
|
||||
o_device_id?: number;
|
||||
o_type_id?: number;
|
||||
o_offset?: number;
|
||||
o_factor?: number;
|
||||
o_uom?: number;
|
||||
o_val_type?: number;
|
||||
deleted?: boolean; // optional
|
||||
o_deleted?: boolean;
|
||||
}
|
||||
|
||||
export interface Entities {
|
||||
entity: EntityItem[];
|
||||
entities: EntityItem[];
|
||||
}
|
||||
|
||||
@@ -85,6 +85,16 @@ export const createSettingsValidator = (settings: Settings) =>
|
||||
})
|
||||
});
|
||||
|
||||
export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) => ({
|
||||
validator(rule: InternalRuleItem, name: string, callback: (error?: string) => void) {
|
||||
if (name && o_name && o_name !== name && schedule.find((si) => si.name === name)) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const schedulerItemValidation = (schedule: ScheduleItem[], scheduleItem: ScheduleItem) =>
|
||||
new Schema({
|
||||
name: [
|
||||
@@ -101,9 +111,9 @@ export const schedulerItemValidation = (schedule: ScheduleItem[], scheduleItem:
|
||||
]
|
||||
});
|
||||
|
||||
export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) => ({
|
||||
export const uniqueEntityNameValidator = (entities: EntityItem[], o_name?: string) => ({
|
||||
validator(rule: InternalRuleItem, name: string, callback: (error?: string) => void) {
|
||||
if (name && o_name && o_name !== name && schedule.find((si) => si.name === name)) {
|
||||
if (name && o_name && o_name !== name && entities.find((ei) => ei.name === name)) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
@@ -111,7 +121,7 @@ export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) =
|
||||
}
|
||||
});
|
||||
|
||||
export const entityItemValidation = (entity: EntityItem[], entityItem: EntityItem) =>
|
||||
export const entityItemValidation = (entities: EntityItem[], entityItem: EntityItem) =>
|
||||
new Schema({
|
||||
name: [
|
||||
{ required: true, message: 'Name is required' },
|
||||
@@ -120,28 +130,15 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte
|
||||
pattern: /^[a-zA-Z0-9_\\.]{1,15}$/,
|
||||
message: "Must be <15 characters: alpha numeric, '_' or '.'"
|
||||
},
|
||||
...[uniqueEntityNameValidator(entity, entityItem.o_name)]
|
||||
...[uniqueEntityNameValidator(entities, entityItem.o_name)]
|
||||
],
|
||||
device_id: [
|
||||
{ required: true, message: 'Device_id is required' },
|
||||
{ type: 'string', pattern: /^[A-F0-9]{1,2}$/, message: 'Must be a hex number' }
|
||||
],
|
||||
type_id: [
|
||||
{ required: true, message: 'Type_id is required' },
|
||||
{ type: 'string', pattern: /^[A-F0-9]{1,4}$/, message: 'Must be a hex number' }
|
||||
],
|
||||
offset: [
|
||||
{ required: true, message: 'Offset is required' },
|
||||
{ type: 'number', min: 0, max: 255, message: 'Must be between 0 and 255' }
|
||||
]
|
||||
device_id: [{ type: 'hex', required: true, message: 'ID must be a hex value' }]
|
||||
// type_id: [
|
||||
// { required: true, message: 'Type_id is required' },
|
||||
// { type: 'string', pattern: /^[A-F0-9]{1,4}$/, message: 'Must be a hex number' }
|
||||
// ],
|
||||
// offset: [
|
||||
// { required: true, message: 'Offset is required' },
|
||||
// { type: 'number', min: 0, max: 255, message: 'Must be between 0 and 255' }
|
||||
// ]
|
||||
});
|
||||
|
||||
export const uniqueEntityNameValidator = (entity: EntityItem[], o_name?: string) => ({
|
||||
validator(rule: InternalRuleItem, name: string, callback: (error?: string) => void) {
|
||||
if (name && o_name && o_name !== name && entity.find((ei) => ei.name === name)) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user