This repository has been archived on 2023-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
old-monorepo/Dashboard/src/App.tsx

114 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-08-26 11:39:34 +02:00
import React, { useEffect, useState } from "react";
import { createGlobalStyle } from "styled-components";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "typesafe-actions";
2021-08-27 13:03:23 +02:00
import Select from "react-select";
import SerialPort from "serialport";
2021-07-10 12:00:50 +02:00
import Knob from "./Components/Knob";
import { connect, disconnect } from "./redux/actions/asyncSerialConnectionActions";
import { setSerialPort } from "./redux/actions/serialConnectionActions";
const GlobalStyle = createGlobalStyle`
html {
border-style: solid;
border-color: #363636;
border-width: 1px;
box-sizing: border-box;
height: 100%;
border-bottom-style: hidden;
border-right-style: hidden;
}
`;
2021-07-09 13:54:25 +02:00
2021-08-27 13:03:23 +02:00
const options: [{value: string, label:string}] = [{ value: "placeholder", label: "Select Port" }];
const fetchPorts = async () => {
const data = await SerialPort.list();
options.shift();
data.forEach((comPort) => { options.push({ value: comPort.path, label: comPort.path }); });
};
const App = () => {
2021-08-26 11:39:34 +02:00
const [status, setStatus] = useState(0);
const dispatch = useDispatch();
const serialConnection = useSelector<RootState, RootState["serialConnection"]>((state) => state.serialConnection);
2021-08-26 11:39:34 +02:00
const SerialDataListener = (data: string) => {
const parsedData = data.split(",");
console.log(parsedData);
2021-08-26 11:39:34 +02:00
setStatus(parseInt(parsedData[2], 10));
};
2021-08-27 13:03:23 +02:00
fetchPorts();
const [portSelectionValue, setPortSelectionValue] = useState(options[0]);
const handlePortChange = async (selectedOption: any) => {
setPortSelectionValue(selectedOption);
dispatch(setSerialPort(selectedOption.value));
};
2021-08-26 11:39:34 +02:00
useEffect(() => {
if (serialConnection.portController !== null) {
serialConnection.portController.parser.on("data", SerialDataListener);
}
2021-08-26 11:39:34 +02:00
return () => {
if (serialConnection.portController !== null) {
serialConnection.portController.parser.removeListener("data", SerialDataListener);
}
2021-08-26 11:39:34 +02:00
};
}, [serialConnection]);
2021-07-10 16:28:20 +02:00
const sendIncreaseHandler = () => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
setStatus(status + 1);
serialConnection.portController.port.write("2i");
}
2021-07-10 16:28:20 +02:00
};
const sendDecreaseHandler = () => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
setStatus(status - 1);
serialConnection.portController.port.write("2d");
}
2021-07-10 16:28:20 +02:00
};
const sendToggleHandler = () => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
setStatus(status - 1);
serialConnection.portController.port.write("2t");
}
};
return (
<div>
<GlobalStyle />
2021-07-10 16:28:20 +02:00
<Knob
increase={sendIncreaseHandler}
decrease={sendDecreaseHandler}
toggle={sendToggleHandler}
2021-08-27 22:22:07 +02:00
status={Math.floor((status / 255) * 100)}
2021-07-10 16:28:20 +02:00
/>
<button
type="button"
onClick={() => {
dispatch(connect());
}}
>
connect
</button>
2021-06-30 09:51:39 +02:00
<button
type="button"
onClick={() => {
dispatch(disconnect());
2021-06-30 09:51:39 +02:00
}}
>
disconnect
</button>
2021-08-27 13:03:23 +02:00
<Select
value={portSelectionValue}
onChange={handlePortChange}
options={options}
/>
</div>
);
};
export default App;