From 9954e1fb070a41e1abe255b0e6090f9e5afb0be3 Mon Sep 17 00:00:00 2001 From: ElMoribond Date: Thu, 15 Jul 2021 17:26:48 +0200 Subject: [PATCH] Improve restart from UI --- frigate/mqtt.py | 5 ++- frigate/util.py | 9 +---- web/src/components/DialogRestart.jsx | 60 +++++++++++----------------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/frigate/mqtt.py b/frigate/mqtt.py index d606721ed..50518b386 100644 --- a/frigate/mqtt.py +++ b/frigate/mqtt.py @@ -90,7 +90,10 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): client.publish(state_topic, payload, retain=True) def on_restart_command(client, userdata, message): - restart_frigate(client, mqtt_config.topic_prefix) + payload = message.payload.decode() + + if payload == "please_restart": + restart_frigate() def on_connect(client, userdata, flags, rc): threading.current_thread().name = "mqtt" diff --git a/frigate/util.py b/frigate/util.py index de6d36b9d..3a4b703ad 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -519,13 +519,8 @@ def clipped(obj, frame_shape): return False -def restart_frigate(mqtt_client, topic_prefix, from_ui = 1): - - def on_publish(client,userdata,result): - os.kill(os.getpid(), signal.SIGKILL) - - mqtt_client.on_publish = on_publish - mqtt_client.publish(f"{topic_prefix}/restarted", int(from_ui)) +def restart_frigate(): + os.kill(os.getpid(), signal.SIGKILL) class EventsPerSecond: diff --git a/web/src/components/DialogRestart.jsx b/web/src/components/DialogRestart.jsx index 0561cca51..d8519b628 100644 --- a/web/src/components/DialogRestart.jsx +++ b/web/src/components/DialogRestart.jsx @@ -4,57 +4,43 @@ import { useCallback, useEffect, useState } from 'preact/hooks'; import { useRestart } from '../api/mqtt'; export default function DialogRestart({ show, setShow }) { - const { payload: detectRestarted = null, send: sendRestart } = useRestart(); - const [dialogTitle, setDialogTitle] = useState('Restart in progress'); useEffect(() => { - if (detectRestarted != null && Number.isInteger(detectRestarted)) { - if (!detectRestarted) { - setDialogTitle('Server-initiated startup'); - } - setShow(false); - } - }, [detectRestarted]); // eslint-disable-line react-hooks/exhaustive-deps + if (show === 2) { + sendRestart('please_restart'); + setTimeout(async () => { + const delay = (ms) => new Promise(res => setTimeout(res, ms)); - const waitPlease = async () => { - const delay = ms => new Promise(res => setTimeout(res, ms)); - await delay(3456); - /* eslint-disable no-constant-condition */ - while (true) { - try { - const response = await fetch('/api/config', { method: 'GET' }); - if (await response.status === 200) { - window.location.reload(); + while (show === 2) { + try { + const response = await fetch('/api/config', { method: 'GET' }); + if (await response.status === 200) { + setShow(0); + continue; + } + } + catch (e) {} + await delay(987); } - } - catch (e) {} - await delay(987); + window.location.reload(); + + }, 3456); } - }; - - const handleClick = useCallback(() => { - sendRestart(); - setShow(false); - waitPlease(); - }, [show]); // eslint-disable-line react-hooks/exhaustive-deps - - const handleDismiss = useCallback(() => { - setShow(false); }, [show]); // eslint-disable-line react-hooks/exhaustive-deps return ( - {show ? ( + {show === 2 ? ( + + ) : show === 1 && ( setShow(0)} title="Restart Frigate" text="Are you sure?" actions={[ - { text: 'Yes', color: 'red', onClick: handleClick }, - { text: 'Cancel', onClick: handleDismiss } ]} + { text: 'Yes', color: 'red', onClick: () => setShow(2) }, + { text: 'Cancel', onClick: () => setShow(0) } ]} /> - ) : detectRestarted != null && ( - )} );