disable periodic ping for stopped machines

master
Tudor Stanciu 2021-04-18 18:37:48 +03:00
parent 18d05c5dcb
commit 9780b42cd1
2 changed files with 30 additions and 16 deletions

6
.env
View File

@ -6,4 +6,8 @@ REACT_APP_NETWORK_RESURRECTOR_API_URL=http://localhost:5064
REACT_APP_NETWORK_RESURRECTOR_SERVER_URL=https://toodle.ddns.net/network-resurrector-api REACT_APP_NETWORK_RESURRECTOR_SERVER_URL=https://toodle.ddns.net/network-resurrector-api
REACT_APP_MACHINE_PING_INTERVAL=300000 #600000 milliseconds = 10 minutes
REACT_APP_MACHINE_PING_INTERVAL=600000
#300000 milliseconds = 5 minutes
REACT_APP_MACHINE_STARTING_TIME=300000

View File

@ -8,15 +8,19 @@ import * as api from "../api";
const initialState = { on: false }; const initialState = { on: false };
const defaultPingInterval = 1200000; //20 minutes const defaultPingInterval = 1200000; //20 minutes
const defaultStartingTime = 300000; //5 minutes
const WakeComponent = ({ machine, addLog }) => { const WakeComponent = ({ machine, addLog }) => {
const [state, setState] = useState(initialState); const [state, setState] = useState(initialState);
const [trigger, setTrigger] = useState(false); const [trigger, setTrigger] = useState(false);
const { t } = useTranslation(); const { t } = useTranslation();
const { success, error } = useToast(); const { success, error } = useToast();
const pingInterval = const pingInterval =
process.env.REACT_APP_MACHINE_PING_INTERVAL || defaultPingInterval; process.env.REACT_APP_MACHINE_PING_INTERVAL || defaultPingInterval;
const startingTime =
process.env.REACT_APP_MACHINE_STARTING_TIME || defaultStartingTime;
const getCurrentDateTime = useCallback(() => { const getCurrentDateTime = useCallback(() => {
const currentDateTime = Date.now(); const currentDateTime = Date.now();
@ -26,34 +30,40 @@ const WakeComponent = ({ machine, addLog }) => {
return result; return result;
}, [t]); }, [t]);
const log = useCallback(
message => addLog(`[${getCurrentDateTime()}] ${message}`),
[addLog, getCurrentDateTime]
);
const wakeMachine = useCallback(async () => { const wakeMachine = useCallback(async () => {
const result = await api.wakeMachine(machine.macAddress); const result = await api.wakeMachine(machine.macAddress);
setState(prev => ({ ...prev, on: result.success })); setState(prev => ({ ...prev, on: result.success }));
addLog( log(`[Wake]: Success: ${result.success}. Status: ${result.status}`);
`[${getCurrentDateTime()}] [Wake]: Success: ${result.success}. Status: ${
result.status
}`
);
if (result.success) { if (result.success) {
success(result.status); success(result.status);
//retrigger
log(`Periodic ping will be re-triggered in ${startingTime} ms.`);
setTimeout(() => {
setTrigger(prev => !prev);
}, startingTime);
} else { } else {
error(result.status); error(result.status);
} }
}, [addLog, getCurrentDateTime, success, error, machine.macAddress]); }, [log, success, error, startingTime, machine.macAddress]);
useEffect(() => { useEffect(() => {
api.pingMachine(machine.iPv4Address || machine.machineName).then(result => { api.pingMachine(machine.iPv4Address || machine.machineName).then(result => {
setState(prev => ({ ...prev, on: result.success })); setState(prev => ({ ...prev, on: result.success }));
addLog( log(`[Ping]: Success: ${result.success}. Status: ${result.status}`);
`[${getCurrentDateTime()}] [Ping]: Success: ${
result.success if (result.success) {
}. Status: ${result.status}`
);
});
setTimeout(() => { setTimeout(() => {
setTrigger(prev => !prev); setTrigger(prev => !prev);
}, pingInterval); }, pingInterval);
}, [machine, addLog, getCurrentDateTime, pingInterval, trigger]); }
});
}, [machine, log, pingInterval, trigger]);
return ( return (
<Tooltip title={t(state.on ? "Machine.PoweredOn" : "Machine.Actions.Wake")}> <Tooltip title={t(state.on ? "Machine.PoweredOn" : "Machine.Actions.Wake")}>