Skip to content

Commit

Permalink
Implement WebSocket to fetch measurement
Browse files Browse the repository at this point in the history
See also: #10
  • Loading branch information
BECATRUE committed Dec 4, 2024
1 parent 5b82423 commit 4baec80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion wlm-ui/src/MainPage/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React, { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux';

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

interface IProps extends ChannelInfo {
Expand Down Expand Up @@ -30,6 +32,20 @@ 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}/measurement/${channel}/`);

socket.onmessage = event => {
const data = JSON.parse(event.data) as MeasurementType;
dispatch(channelListActions.fetchMeasurement(
{ channel: channel, measurement: data }));
};

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

useEffect(() => {
setIsInUseButtonEnabled(true);
}, [props.inUse]);
Expand Down
8 changes: 8 additions & 0 deletions wlm-ui/src/store/slices/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ export const channelListSlice = createSlice({
info.setting.period = period;
}
},
fetchMeasurement: (
state,
action: PayloadAction<Pick<ChannelType, 'channel'> & { measurement: MeasurementType }>
) => {
const { channel, measurement } = action.payload;
const info = getChannelInfoWithException(state, channel);
info.measurements.push(measurement);
},
},
extraReducers: (builder) => {
builder
Expand Down

0 comments on commit 4baec80

Please sign in to comment.