added frontend for receiving+displaying messages and exporting as csv

This commit is contained in:
Maximilian Eissler 2021-11-15 15:42:40 +01:00
parent f741bdf838
commit 92a2cec272
9 changed files with 11931 additions and 1 deletions

7
.gitignore vendored
View File

@ -4,4 +4,9 @@
.vscode/launch.json
.vscode/ipch
/build
**/build
*.zip
**/node_modules
**/.idea
**/.DS_Store

1
frontend/.env Normal file
View File

@ -0,0 +1 @@
REACT_APP_BACKEND_URL="ws://localhost:8080"

70
frontend/README.md Normal file
View File

@ -0,0 +1,70 @@
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `yarn start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `yarn build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `yarn build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

43
frontend/package.json Normal file
View File

@ -0,0 +1,43 @@
{
"name": "testbench-frontent",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"styled-components": "^5.3.3",
"web-vitals": "^1.0.1",
"websocket": "^1.0.34"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "^2.4.1"
}
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Testbench Reader</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

129
frontend/src/App.js Normal file
View File

@ -0,0 +1,129 @@
import { useState } from "react";
import { w3cwebsocket as W3CWebSocket } from "websocket";
import styled from "styled-components";
const client = new W3CWebSocket(process.env.REACT_APP_BACKEND_URL);
const App = function App() {
const [isOpen, setIsOpen] = useState(false);
const [testFinished, setTestFinished] = useState(false);
const [messages, setMesages] = useState([]);
const addMessage = (message) => setMesages([...messages, message]);
client.onopen = () => {
setIsOpen(true);
client.send(JSON.stringify({ topic: "start_test" }));
console.log("OPEN");
};
client.onmessage = (message) => {
const { topic, data } = JSON.parse(message.data);
switch (topic) {
case "data":
console.log("MESSAGE", data);
addMessage(data);
break;
case "end_test":
console.log("ENDED TESTING");
setTestFinished(true);
break;
default:
console.error("Unknown Message Type");
}
const el = document.getElementById("log");
el.scrollTop = el.scrollHeight;
};
const handleDownload = () => {
console.log("Making CSV...");
const header = Object.keys(messages[0]);
let rows = [header, ...messages.map((message) => Object.values(message))];
let csvContent =
"data:text/csv;charset=utf-8," + rows.map((e) => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
console.log("Downloading CSV...");
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "radiator_data.csv");
document.body.appendChild(link);
link.click(); // This will
};
return (
<AppWrap>
<Column>
<Log id={"log"}>
{isOpen && <Message key={"open"}>> Connection Open</Message>}
{messages.map((message, i) => (
<Message id={i + "msg"}>
{"New Message: "} <br /> {JSON.stringify(message)}
</Message>
))}
{testFinished && <Message key={"finished"}>> Test Finished</Message>}
</Log>
<Buttons>
{testFinished && messages.length > 0 && (
<Button onClick={handleDownload}>download csv</Button>
)}
</Buttons>
</Column>
</AppWrap>
);
};
const Message = styled.p`
margin-top: 0;
margin-bottom: 20px;
width: 100%;
word-wrap: anywhere;
word-break: break-all;
`;
const Log = styled.div`
overflow-y: scroll;
background-color: black;
color: #00ff66;
font-size: 12px;
padding: 12px;
border-radius: 4px;
width: 450px;
flex-grow: 1;
min-height: 200px;
max-height: 60vh;
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
`;
const Button = styled.div`
margin-right: 5px;
border: 1px solid lightgrey;
border-radius: 2px;
font-size: 13px;
height: 18px;
font-weight: 500;
padding: 5px;
cursor: pointer;
user-select: none;
`;
const Buttons = styled.div`
height: 50px;
margin-top: 5px;
display: flex;
`;
const Column = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
`;
const AppWrap = styled.div`
width: 100vw;
height: 100vh;
background-color: whitesmoke;
display: flex;
justify-content: center;
align-content: center;
`;
export default App;

13
frontend/src/index.css Normal file
View File

@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

11
frontend/src/index.js Normal file
View File

@ -0,0 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

11647
frontend/yarn.lock Normal file

File diff suppressed because it is too large Load Diff