Skip to content

Commit

Permalink
very ugly UI
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 1, 2024
1 parent b416d53 commit a74a11d
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
29 changes: 29 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.3",
"bootstrap": "^5.3.2",
"react": "^18.2.0",
"react-bootstrap": "^2.9.2",
Expand Down
79 changes: 78 additions & 1 deletion ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,86 @@
import React, { useState, useEffect } from 'react';
import './App.css';
import { CWaterPumpAPI } from './api/CWaterPumpAPI.js';

const STORE_API = 'apiAddress';
const STORE_RUNTIME = 'runTime';

function App() {
const [apiAddress, setApiAddress] = useState('');
const [runTime, setRunTime] = useState(1000);

useEffect(() => {
const storedApiAddress = localStorage.getItem(STORE_API);
if (storedApiAddress) setApiAddress(storedApiAddress);

let storedRunTime = localStorage.getItem(STORE_RUNTIME);
if (storedRunTime) {
// if string then convert to int
if (typeof storedRunTime === 'string') {
storedRunTime = parseInt(storedRunTime, 10);
}
setRunTime(storedRunTime);
}
}, []); // on mount

const waterPumpAPI = React.useMemo(
() => {
// ensure apiAddress is started with http:// or https://
let url = apiAddress;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'http://' + url;
}
return new CWaterPumpAPI({ URL: url });
}, [apiAddress] // only re-create if apiAddress changes
);

const handleStart = async () => {
try {
await waterPumpAPI.start(runTime);
alert('Water pump started successfully!');
} catch (error) {
alert('Error starting water pump: ' + error.message);
}
};

const handleStop = async () => {
try {
await waterPumpAPI.stop();
alert('Water pump stopped successfully!');
} catch (error) {
alert('Error stopping water pump: ' + error.message);
}
};

const handleRunTimeChange = (event) => {
const runTime = parseInt(event.target.value, 10);
setRunTime(runTime);
localStorage.setItem(STORE_RUNTIME, runTime);
};

const handleApiAddressChange = (event) => {
const apiAddress = event.target.value;
setApiAddress(apiAddress);
localStorage.setItem(STORE_API, apiAddress);
};

return (
<div className="App">
Tea System UI
<h1>Tea System UI</h1>
<div>
<label>
API Address:
<input type="text" value={apiAddress} onChange={handleApiAddressChange} />
</label>
</div>
<div>
<label>
Run Time (ms):
<input type="number" value={runTime} onChange={handleRunTimeChange} />
</label>
</div>
<button onClick={handleStart}>Start</button>
<button onClick={handleStop}>Stop</button>
</div>
);
}
Expand Down
27 changes: 27 additions & 0 deletions ui/src/api/CWaterPumpAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from 'axios';

class CWaterPumpAPI {
constructor({ client=null, URL }) {
this._client = client || axios.create({ baseURL: URL });
}

async start(runTimeMs) {
const response = await this._client.get('/start', {
runTimeMs: runTimeMs
});
return response.data;
}

async stop() {
const response = await this._client.get('/stop');
return response.data;
}

async status() {
const response = await this._client.get('/status');
return response.data;
}
}

export default CWaterPumpAPI;
export { CWaterPumpAPI };

0 comments on commit a74a11d

Please sign in to comment.