First implementation of connection in middleware
This commit is contained in:
parent
e9364c73bf
commit
a7af6cc8de
3 changed files with 36 additions and 5 deletions
|
@ -4,6 +4,8 @@ import { createGlobalStyle } from "styled-components";
|
||||||
import Select from "react-select";
|
import Select from "react-select";
|
||||||
import SerialPort from "serialport";
|
import SerialPort from "serialport";
|
||||||
import KnobSection from "./Components/KnobSection";
|
import KnobSection from "./Components/KnobSection";
|
||||||
|
import { useAppDispatch } from "./redux/hooks";
|
||||||
|
import { connect, setSerialPort } from "./redux/slices/serialConnectionSlice";
|
||||||
|
|
||||||
const GlobalStyle = createGlobalStyle`
|
const GlobalStyle = createGlobalStyle`
|
||||||
html {
|
html {
|
||||||
|
@ -26,14 +28,14 @@ const fetchPorts = async () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
// const dispatch = useDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
fetchPorts();
|
fetchPorts();
|
||||||
|
|
||||||
const [portSelectionValue, setPortSelectionValue] = useState(options[0]);
|
const [portSelectionValue, setPortSelectionValue] = useState(options[0]);
|
||||||
const handlePortChange = async (selectedOption: any) => {
|
const handlePortChange = async (selectedOption: any) => {
|
||||||
setPortSelectionValue(selectedOption);
|
setPortSelectionValue(selectedOption);
|
||||||
// dispatch(setSerialPort(selectedOption.value));
|
dispatch(setSerialPort(selectedOption.value));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -43,7 +45,7 @@ const App = () => {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
// dispatch(connect());
|
dispatch(connect());
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
connect
|
connect
|
||||||
|
|
29
Dashboard/src/redux/middlewares/serialConnection.ts
Normal file
29
Dashboard/src/redux/middlewares/serialConnection.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { Middleware, Action } from "@reduxjs/toolkit";
|
||||||
|
import { RootState } from "../store/types.d";
|
||||||
|
import {
|
||||||
|
connect, disconnect, setSerialPort, setMessage,
|
||||||
|
} from "../slices/serialConnectionSlice";
|
||||||
|
import PortController from "../../serial/PortController";
|
||||||
|
import store from "../store";
|
||||||
|
|
||||||
|
let serialPort: PortController|null = null;
|
||||||
|
const errorCalbackHandler = () => {};
|
||||||
|
const closeCalbackHandler = () => {};
|
||||||
|
const serialDataListener = (msg: string) => { store.dispatch(setMessage(msg)); };
|
||||||
|
|
||||||
|
const serialConnection: Middleware<{}, RootState> = () => (next) => (action: Action) => {
|
||||||
|
if (connect.match(action)) {
|
||||||
|
serialPort?.open(errorCalbackHandler, closeCalbackHandler);
|
||||||
|
serialPort?.parser.on("data", serialDataListener);
|
||||||
|
console.log("connect");
|
||||||
|
} else if (disconnect.match(action)) {
|
||||||
|
serialPort?.close();
|
||||||
|
console.log("disconnect");
|
||||||
|
} else if (setSerialPort.match(action)) {
|
||||||
|
serialPort = new PortController(action.payload);
|
||||||
|
console.log("setSerialPort");
|
||||||
|
}
|
||||||
|
return next(action);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default serialConnection;
|
|
@ -1,9 +1,9 @@
|
||||||
import thunk from "redux-thunk";
|
import thunk from "redux-thunk";
|
||||||
import { configureStore } from "@reduxjs/toolkit";
|
import { configureStore } from "@reduxjs/toolkit";
|
||||||
import rootReducer from "./root-reducer";
|
import rootReducer from "./root-reducer";
|
||||||
import logger from "../middlewares/logger";
|
import serialConnection from "../middlewares/serialConnection";
|
||||||
|
|
||||||
const middlewares = [thunk, logger];
|
const middlewares = [thunk, serialConnection];
|
||||||
|
|
||||||
const store = configureStore(
|
const store = configureStore(
|
||||||
{
|
{
|
||||||
|
|
Reference in a new issue