From 0c41a857332d9d4e67b644ad56adde994467ee8c Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Fri, 31 Dec 2021 23:11:00 +0100 Subject: [PATCH] Hook up interface to serial connection --- Dashboard/src/Components/KnobSection.tsx | 77 +++++++------------ .../src/redux/middlewares/serialConnection.ts | 4 +- .../src/redux/slices/serialConnectionSlice.ts | 5 +- Dashboard/src/serial/PortController.ts | 4 + 4 files changed, 39 insertions(+), 51 deletions(-) diff --git a/Dashboard/src/Components/KnobSection.tsx b/Dashboard/src/Components/KnobSection.tsx index af33a81..eb6d9e4 100644 --- a/Dashboard/src/Components/KnobSection.tsx +++ b/Dashboard/src/Components/KnobSection.tsx @@ -1,6 +1,8 @@ import React from "react"; import styled from "styled-components"; -// import { RootState } from "typesafe-actions"; +import { useAppDispatch, useAppSelector } from "../redux/hooks"; +import { sendMessage } from "../redux/slices/serialConnectionSlice"; +import Knob from "./Knob"; const KnobContainer = styled.div` display: flex; @@ -8,58 +10,35 @@ justify-content: space-between; margin: 1rem; `; -const KnobSection = () => -// const [status, setStatus] = useState([{ id: 0, value: 0 }, { id: 1, value: 0 }, { id: 2, value: 0 }, { id: 3, value: 0 }]); -// const serialConnection = useSelector((state) => state.serialConnection); +const ids: number[] = [0, 1, 2, 3]; -// const SerialDataListener = (data: string) => { -// const parsedData = data.split(","); -// console.log(parsedData); -// setStatus(status.map((element) => ({ ...element, value: parseInt(parsedData[element.id], 10) }))); -// }; - -// useEffect(() => { -// if (serialConnection.portController !== null) { -// serialConnection.portController.parser.on("data", SerialDataListener); -// } - -// return () => { -// if (serialConnection.portController !== null) { -// serialConnection.portController.parser.removeListener("data", SerialDataListener); -// } -// }; -// }, [serialConnection]); - -// const sendIncreaseHandler = (index: number) => { -// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) { -// // serialConnection.portController.port.write(`${index}i`); -// // } -// console.log(index); -// }; -// const sendDecreaseHandler = (index: number) => { -// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) { -// // serialConnection.portController.port.write(`${index}d`); -// // } -// console.log(index); -// }; -// const sendToggleHandler = (index: number) => { -// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) { -// // serialConnection.portController.port.write(`${index}t`); -// // } -// console.log(index); -// }; - // eslint-disable-next-line implicit-arrow-linebreak - ( +const KnobSection = () => { + const serialConnection = useAppSelector((state) => state.serialConnection); + const dispatch = useAppDispatch(); + const sendIncreaseHandler = (index: number) => { + dispatch(sendMessage(`${index}i`)); + console.log(index); + }; + const sendDecreaseHandler = (index: number) => { + dispatch(sendMessage(`${index}d`)); + console.log(index); + }; + const sendToggleHandler = (index: number) => { + dispatch(sendMessage(`${index}t`)); + console.log(index); + }; + return ( - {/* {status.map((element) => ( + {ids.map((id) => ( { sendIncreaseHandler(element.id); }} - decrease={() => { sendDecreaseHandler(element.id); }} - toggle={() => { sendToggleHandler(element.id); }} - status={Math.floor((element.value / 255) * 100)} + key={id} + increase={() => { sendIncreaseHandler(id); }} + decrease={() => { sendDecreaseHandler(id); }} + toggle={() => { sendToggleHandler(id); }} + status={Math.floor((parseInt(serialConnection.message.split(",")[id], 10) / 255) * 100)} /> - ))} */} + ))} ); +}; export default KnobSection; diff --git a/Dashboard/src/redux/middlewares/serialConnection.ts b/Dashboard/src/redux/middlewares/serialConnection.ts index ce918c6..65ae43a 100644 --- a/Dashboard/src/redux/middlewares/serialConnection.ts +++ b/Dashboard/src/redux/middlewares/serialConnection.ts @@ -1,7 +1,7 @@ import { Middleware, Action } from "@reduxjs/toolkit"; import { RootState } from "../store/types.d"; import { - connect, disconnect, setSerialPort, setMessage, + connect, disconnect, setSerialPort, setMessage, sendMessage, } from "../slices/serialConnectionSlice"; import PortController from "../../serial/PortController"; import store from "../store"; @@ -22,6 +22,8 @@ const serialConnection: Middleware<{}, RootState> = () => (next) => (action: Act } else if (setSerialPort.match(action)) { serialPort = new PortController(action.payload); console.log("setSerialPort"); + } else if (sendMessage.match(action)) { + serialPort?.write(action.payload); } return next(action); }; diff --git a/Dashboard/src/redux/slices/serialConnectionSlice.ts b/Dashboard/src/redux/slices/serialConnectionSlice.ts index 3edee30..bb6ddbe 100644 --- a/Dashboard/src/redux/slices/serialConnectionSlice.ts +++ b/Dashboard/src/redux/slices/serialConnectionSlice.ts @@ -45,12 +45,15 @@ export const serialConnectionSlice = createSlice({ setMessage: (state, action: PayloadAction) => { state.message = action.payload; }, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + sendMessage: (_state, _action: PayloadAction) => { + }, }, }); export const { setSerialPort, connectionStart, connectionSuccess, connectionFailure, connectionEnd, - connect, disconnect, setMessage, + connect, disconnect, setMessage, sendMessage, } = serialConnectionSlice.actions; // Other code such as selectors can use the imported `RootState` type diff --git a/Dashboard/src/serial/PortController.ts b/Dashboard/src/serial/PortController.ts index 700117e..cf0c976 100644 --- a/Dashboard/src/serial/PortController.ts +++ b/Dashboard/src/serial/PortController.ts @@ -46,6 +46,10 @@ class PortController { this.port.removeListener("data", this.onCloseCallback); } } + + write(message: string) { + this.port.write(message); + } } export default PortController;