mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
Merge branch 'dev' into dev2
This commit is contained in:
@@ -63,7 +63,7 @@ const en: Translation = {
|
||||
FREQ: 'Frequency',
|
||||
DUTY_CYCLE: 'Duty Cycle',
|
||||
UNIT: 'UoM',
|
||||
STARTVALUE: 'Start value',
|
||||
STARTVALUE: 'Start Value',
|
||||
WARN_GPIO: 'Warning: be careful when assigning a GPIO!',
|
||||
EDIT: 'Edit',
|
||||
SENSOR: 'Sensor',
|
||||
|
||||
@@ -103,7 +103,7 @@ const DashboardDevicesDialog = ({
|
||||
return (
|
||||
<Dialog sx={dialogStyle} open={open} onClose={close}>
|
||||
<DialogTitle>
|
||||
{selectedItem.v === '' && selectedItem.c ? LL.RUN_COMMAND() : writeable ? LL.CHANGE_VALUE() : LL.VALUE(0)}
|
||||
{selectedItem.v === '' && selectedItem.c ? LL.RUN_COMMAND() : writeable ? LL.CHANGE_VALUE() : LL.VALUE(1)}
|
||||
</DialogTitle>
|
||||
<DialogContent dividers>
|
||||
<Box color="warning.main" p={0} pl={0} pr={0} mt={0} mb={2}>
|
||||
|
||||
@@ -182,7 +182,7 @@ const DashboardSensorsAnalogDialog = ({
|
||||
<Grid item xs={4}>
|
||||
<TextField
|
||||
name="o"
|
||||
label={LL.VALUE(0)}
|
||||
label={LL.VALUE(1)}
|
||||
value={numberValue(editItem.o)}
|
||||
fullWidth
|
||||
type="number"
|
||||
@@ -197,7 +197,7 @@ const DashboardSensorsAnalogDialog = ({
|
||||
<Grid item xs={4}>
|
||||
<TextField
|
||||
name="o"
|
||||
label={LL.VALUE(0)}
|
||||
label={LL.VALUE(1)}
|
||||
value={numberValue(editItem.o)}
|
||||
fullWidth
|
||||
select
|
||||
|
||||
@@ -210,7 +210,7 @@ const SettingsSchedulerDialog = ({
|
||||
/>
|
||||
<TextField
|
||||
name="value"
|
||||
label={LL.VALUE(0)}
|
||||
label={LL.VALUE(1)}
|
||||
multiline
|
||||
margin="normal"
|
||||
fullWidth
|
||||
|
||||
@@ -223,12 +223,12 @@ export enum AnalogType {
|
||||
|
||||
export const AnalogTypeNames = [
|
||||
'(disabled)',
|
||||
'Digital in',
|
||||
'Digital In',
|
||||
'Counter',
|
||||
'ADC',
|
||||
'Timer',
|
||||
'Rate',
|
||||
'Digital out',
|
||||
'Digital Out',
|
||||
'PWM 0',
|
||||
'PWM 1',
|
||||
'PWM 2'
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
import { debounce } from 'lodash-es';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import Sockette from 'sockette';
|
||||
|
||||
import { addAccessTokenParameter } from 'api/authentication';
|
||||
|
||||
interface WebSocketIdMessage {
|
||||
type: 'id';
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface WebSocketPayloadMessage<D> {
|
||||
type: 'payload';
|
||||
origin_id: string;
|
||||
payload: D;
|
||||
}
|
||||
|
||||
export type WebSocketMessage<D> = WebSocketIdMessage | WebSocketPayloadMessage<D>;
|
||||
|
||||
export const useWs = <D>(wsUrl: string, wsThrottle = 100) => {
|
||||
const ws = useRef<Sockette>();
|
||||
const clientId = useRef<string>();
|
||||
|
||||
const [connected, setConnected] = useState<boolean>(false);
|
||||
const [data, setData] = useState<D>();
|
||||
const [transmit, setTransmit] = useState<boolean>();
|
||||
const [clear, setClear] = useState<boolean>();
|
||||
|
||||
const onMessage = useCallback((event: MessageEvent) => {
|
||||
const rawData = event.data;
|
||||
if (typeof rawData === 'string' || rawData instanceof String) {
|
||||
const message = JSON.parse(rawData as string) as WebSocketMessage<D>;
|
||||
switch (message.type) {
|
||||
case 'id':
|
||||
clientId.current = message.id;
|
||||
break;
|
||||
case 'payload':
|
||||
if (clientId.current) {
|
||||
setData((existingData) => (clientId.current === message.origin_id && existingData) || message.payload);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const doSaveData = useCallback((newData: D, clearData = false) => {
|
||||
if (!ws.current) {
|
||||
return;
|
||||
}
|
||||
if (clearData) {
|
||||
setData(undefined);
|
||||
}
|
||||
ws.current.json(newData);
|
||||
}, []);
|
||||
|
||||
const saveData = useRef(debounce(doSaveData, wsThrottle));
|
||||
|
||||
const updateData = (newData: React.SetStateAction<D | undefined>, transmitData: true, clearData: false) => {
|
||||
setData(newData);
|
||||
setTransmit(transmitData);
|
||||
setClear(clearData);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!transmit) {
|
||||
return;
|
||||
}
|
||||
data && saveData.current(data, clear);
|
||||
setTransmit(false);
|
||||
setClear(false);
|
||||
}, [doSaveData, data, transmit, clear]);
|
||||
|
||||
useEffect(() => {
|
||||
const instance = new Sockette(addAccessTokenParameter(wsUrl), {
|
||||
onmessage: onMessage,
|
||||
onopen: () => {
|
||||
setConnected(true);
|
||||
},
|
||||
onclose: () => {
|
||||
clientId.current = undefined;
|
||||
setConnected(false);
|
||||
setData(undefined);
|
||||
}
|
||||
});
|
||||
ws.current = instance;
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
return instance.close;
|
||||
}, [wsUrl, onMessage]);
|
||||
|
||||
return { connected, data, updateData } as const;
|
||||
};
|
||||
Reference in New Issue
Block a user