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