Skip to content

Commit

Permalink
Fetch lock status in real-time
Browse files Browse the repository at this point in the history
See also: #13
  • Loading branch information
BECATRUE committed Dec 7, 2024
1 parent f4711a9 commit cac7c6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion wlm-ui/src/MainPage/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Line } from '@nivo/line';

import { AppDispatch } from '../../store';
import {
channelListActions, SettingType, MeasurementType, ChannelInfo
channelListActions, SettingType, MeasurementType, LockType, ChannelInfo
} from '../../store/slices/channel/channel';
import './Channel.scss';

Expand Down Expand Up @@ -56,6 +56,19 @@ const Channel = (props: IProps) => {
return () => socket.close();
}, [dispatch, props.channel.channel]);

useEffect(() => {
const channel = props.channel.channel;
const socket = new WebSocket(`${process.env.REACT_APP_WEBSOCKET_URL}/lock/${channel}/`);

socket.onmessage = event => {
const data = JSON.parse(event.data) as LockType;
dispatch(channelListActions.fetchLock(
{ channel: channel, lock: data }));
};

return () => socket.close();
}, [dispatch, props.channel.channel]);

useEffect(() => {
const channel = props.channel.channel;

Expand Down
7 changes: 7 additions & 0 deletions wlm-ui/src/store/slices/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export const channelListSlice = createSlice({
info.measurements.push(measurements);
}
},
fetchLock: (
state, action: PayloadAction<Pick<ChannelType, 'channel'> & { lock: LockType }>
) => {
const { channel, lock } = action.payload;
const info = getChannelInfoWithException(state, channel);
info.lock = lock;
},
removeOldMeasurements: (state, action: PayloadAction<Pick<ChannelType, 'channel'>>) => {
const info = getChannelInfoWithException(state, action.payload.channel);
const cutoffTime = new Date(Date.now() - 10 * 60 * 1000);
Expand Down

0 comments on commit cac7c6a

Please sign in to comment.