blakeblackshear.frigate/web/src/api/mqtt.jsx

130 lines
3.4 KiB
React
Raw Normal View History

2021-02-16 05:10:20 +01:00
import { h, createContext } from 'preact';
import { baseUrl } from './baseUrl';
import produce from 'immer';
import { useCallback, useContext, useEffect, useRef, useReducer } from 'preact/hooks';
const initialState = Object.freeze({ __connected: false });
export const Mqtt = createContext({ state: initialState, connection: null });
const defaultCreateWebsocket = (url) => new WebSocket(url);
function reducer(state, { topic, payload, retain }) {
switch (topic) {
case '__CLIENT_CONNECTED':
return produce(state, (draftState) => {
draftState.__connected = true;
});
default:
return produce(state, (draftState) => {
let parsedPayload = payload;
try {
parsedPayload = payload && JSON.parse(payload);
} catch (e) {}
draftState[topic] = {
lastUpdate: Date.now(),
payload: parsedPayload,
retain,
};
});
}
}
export function MqttProvider({
config,
2021-02-16 05:10:20 +01:00
children,
createWebsocket = defaultCreateWebsocket,
2021-02-20 15:11:58 +01:00
mqttUrl = `${baseUrl.replace(/^http/, 'ws')}/ws`,
2021-02-16 05:10:20 +01:00
}) {
const [state, dispatch] = useReducer(reducer, initialState);
const wsRef = useRef();
useEffect(() => {
Object.keys(config.cameras).forEach((camera) => {
const { name, clips, detect, snapshots } = config.cameras[camera];
dispatch({ topic: `${name}/clips/state`, payload: clips.enabled ? 'ON' : 'OFF' });
dispatch({ topic: `${name}/detect/state`, payload: detect.enabled ? 'ON' : 'OFF' });
dispatch({ topic: `${name}/snapshots/state`, payload: snapshots.enabled ? 'ON' : 'OFF' });
});
}, [config]);
2021-02-16 05:10:20 +01:00
useEffect(
() => {
const ws = createWebsocket(mqttUrl);
ws.onopen = () => {
dispatch({ topic: '__CLIENT_CONNECTED' });
};
ws.onmessage = (event) => {
dispatch(JSON.parse(event.data));
};
wsRef.current = ws;
return () => {
ws.close(3000, 'Provider destroyed');
};
},
// Forces reconnecting
[state.__reconnectAttempts, mqttUrl] // eslint-disable-line react-hooks/exhaustive-deps
);
return <Mqtt.Provider value={{ state, ws: wsRef.current }}>{children}</Mqtt.Provider>;
}
2021-06-21 17:07:14 +02:00
export function useMqtt(watchTopic, publishTopic, defaultValue = null) {
2021-02-16 05:10:20 +01:00
const { state, ws } = useContext(Mqtt);
2021-06-21 17:07:14 +02:00
const value = state[watchTopic] || { payload: defaultValue };
2021-02-16 05:10:20 +01:00
const send = useCallback(
(payload) => {
2021-06-21 17:07:14 +02:00
payload = payload || defaultValue;
ws.send(
JSON.stringify({
topic: publishTopic || watchTopic,
payload: typeof payload !== 'string' ? JSON.stringify(payload) : payload,
})
);
2021-02-16 05:10:20 +01:00
},
[ws, watchTopic, publishTopic]
2021-02-16 05:10:20 +01:00
);
return { value, send, connected: state.__connected };
}
export function useDetectState(camera) {
const {
value: { payload },
send,
connected,
} = useMqtt(`${camera}/detect/state`, `${camera}/detect/set`);
return { payload, send, connected };
}
export function useClipsState(camera) {
const {
value: { payload },
send,
connected,
} = useMqtt(`${camera}/clips/state`, `${camera}/clips/set`);
return { payload, send, connected };
}
export function useSnapshotsState(camera) {
const {
value: { payload },
send,
connected,
} = useMqtt(`${camera}/snapshots/state`, `${camera}/snapshots/set`);
return { payload, send, connected };
}
2021-06-21 17:07:14 +02:00
export function useRestart() {
const {
send,
connected,
2021-06-23 14:49:49 +02:00
} = useMqtt('', 'restart', 'container');
2021-06-21 17:07:14 +02:00
return { send, connected };
}