Add ability to restart

This commit is contained in:
ElMoribond 2021-06-21 17:07:14 +02:00 committed by Blake Blackshear
parent 3d28d8cee2
commit a2d5754e4b

View File

@ -72,13 +72,14 @@ export function MqttProvider({
return <Mqtt.Provider value={{ state, ws: wsRef.current }}>{children}</Mqtt.Provider>; return <Mqtt.Provider value={{ state, ws: wsRef.current }}>{children}</Mqtt.Provider>;
} }
export function useMqtt(watchTopic, publishTopic) { export function useMqtt(watchTopic, publishTopic, defaultValue = null) {
const { state, ws } = useContext(Mqtt); const { state, ws } = useContext(Mqtt);
const value = state[watchTopic] || { payload: null }; const value = state[watchTopic] || { payload: defaultValue };
const send = useCallback( const send = useCallback(
(payload) => { (payload) => {
payload = payload || defaultValue;
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
topic: publishTopic || watchTopic, topic: publishTopic || watchTopic,
@ -118,3 +119,12 @@ export function useSnapshotsState(camera) {
} = useMqtt(`${camera}/snapshots/state`, `${camera}/snapshots/set`); } = useMqtt(`${camera}/snapshots/state`, `${camera}/snapshots/set`);
return { payload, send, connected }; return { payload, send, connected };
} }
export function useRestart() {
const {
value: { payload },
send,
connected,
} = useMqtt(``, `restart`, "container");
return { send, connected };
}