mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-07 03:35:26 +03:00
check ready state
This commit is contained in:
parent
31db8e6a6d
commit
a247571829
@ -2,7 +2,7 @@ import { createContext } from 'preact';
|
||||
import { baseUrl } from './baseUrl';
|
||||
import { produce } from 'immer';
|
||||
import { useCallback, useContext, useEffect, useReducer } from 'preact/hooks';
|
||||
import useWebSocket from 'react-use-websocket';
|
||||
import useWebSocket, { ReadyState } from 'react-use-websocket';
|
||||
|
||||
const initialState = Object.freeze({ __connected: false });
|
||||
export const WS = createContext({ state: initialState, connection: null });
|
||||
@ -35,9 +35,8 @@ export function WsProvider({
|
||||
wsUrl = `${baseUrl.replace(/^http/, 'ws')}ws`,
|
||||
}) {
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
console.log(dispatch);
|
||||
|
||||
const { sendJsonMessage } = useWebSocket(wsUrl, {
|
||||
const { sendJsonMessage, readyState } = useWebSocket(wsUrl, {
|
||||
|
||||
onMessage: (event) => {
|
||||
dispatch(event.data);
|
||||
@ -47,37 +46,37 @@ export function WsProvider({
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (readyState === ReadyState.OPEN) {
|
||||
Object.keys(config.cameras).forEach((camera) => {
|
||||
console.log("Setting up")
|
||||
const { name, record, detect, snapshots, audio } = config.cameras[camera];
|
||||
dispatch({ topic: `${name}/recordings/state`, payload: record.enabled ? 'ON' : 'OFF', retain: false });
|
||||
dispatch({ topic: `${name}/detect/state`, payload: detect.enabled ? 'ON' : 'OFF', retain: false });
|
||||
dispatch({ topic: `${name}/snapshots/state`, payload: snapshots.enabled ? 'ON' : 'OFF', retain: false });
|
||||
dispatch({ topic: `${name}/audio/state`, payload: audio.enabled ? 'ON' : 'OFF', retain: false });
|
||||
});
|
||||
}, [config]);
|
||||
}
|
||||
}, [config, readyState]);
|
||||
|
||||
|
||||
return <WS.Provider value={{ state, sendJsonMessage }}>{children}</WS.Provider>;
|
||||
return <WS.Provider value={{ state, readyState, sendJsonMessage }}>{children}</WS.Provider>;
|
||||
}
|
||||
|
||||
export function useWs(watchTopic, publishTopic) {
|
||||
const { state, sendJsonMessage } = useContext(WS);
|
||||
|
||||
console.log(state);
|
||||
const { state, readyState, sendJsonMessage } = useContext(WS);
|
||||
|
||||
const value = state[watchTopic] || { payload: null };
|
||||
|
||||
const send = useCallback(
|
||||
(payload, retain = false) => {
|
||||
console.log("sending json");
|
||||
if (readyState === ReadyState.OPEN) {
|
||||
sendJsonMessage({
|
||||
topic: publishTopic || watchTopic,
|
||||
payload: payload,
|
||||
payload,
|
||||
retain,
|
||||
});
|
||||
}
|
||||
},
|
||||
[sendJsonMessage, watchTopic, publishTopic]
|
||||
[sendJsonMessage, readyState, watchTopic, publishTopic]
|
||||
);
|
||||
|
||||
return { value, send, connected: state.__connected };
|
||||
|
||||
Loading…
Reference in New Issue
Block a user