Skip to content

Commit df06e58

Browse files
committed
add demo project implementation
1 parent aeea0d9 commit df06e58

File tree

12 files changed

+180
-32
lines changed

12 files changed

+180
-32
lines changed

data/config/demoSettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"blink_speed": 100
3+
}

interface/.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
REACT_APP_ENDPOINT_ROOT=http://192.168.0.19/rest/
1+
REACT_APP_ENDPOINT_ROOT=http://192.168.0.20/rest/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import { makeStyles } from '@material-ui/core/styles';
5+
import Button from '@material-ui/core/Button';
6+
import LinearProgress from '@material-ui/core/LinearProgress';
7+
import Typography from '@material-ui/core/Typography';
8+
9+
const useStyles = makeStyles(theme => ({
10+
loadingSettings: {
11+
margin: theme.spacing(0.5),
12+
},
13+
loadingSettingsDetails: {
14+
margin: theme.spacing(4),
15+
textAlign: "center"
16+
},
17+
button: {
18+
marginRight: theme.spacing(2),
19+
marginTop: theme.spacing(2),
20+
}
21+
}));
22+
23+
export default function LoadingNotification(props) {
24+
const classes = useStyles();
25+
const { fetched, errorMessage, onReset, children } = props;
26+
return (
27+
<div>
28+
{
29+
fetched ?
30+
errorMessage ?
31+
<div className={classes.loadingSettings}>
32+
<Typography variant="h4" className={classes.loadingSettingsDetails}>
33+
{errorMessage}
34+
</Typography>
35+
<Button variant="contained" color="secondary" className={classes.button} onClick={onReset}>
36+
Reset
37+
</Button>
38+
</div>
39+
:
40+
children
41+
:
42+
<div className={classes.loadingSettings}>
43+
<LinearProgress className={classes.loadingSettingsDetails} />
44+
<Typography variant="h4" className={classes.loadingSettingsDetails}>
45+
Loading...
46+
</Typography>
47+
</div>
48+
}
49+
</div>
50+
);
51+
}
52+
53+
LoadingNotification.propTypes = {
54+
fetched: PropTypes.bool.isRequired,
55+
onReset: PropTypes.func.isRequired,
56+
errorMessage: PropTypes.string
57+
};

interface/src/components/RestComponent.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { withSnackbar } from 'notistack';
33
import { redirectingAuthorizedFetch } from '../authentication/Authentication';
4+
45
/*
56
* It is unlikely this application will grow complex enough to require redux.
67
*
@@ -86,9 +87,9 @@ export const restComponent = (endpointUrl, FormComponent) => {
8687
});
8788
}
8889

89-
handleValueChange = name => event => {
90+
handleValueChange = name => (event, newValue) => {
9091
const { data } = this.state;
91-
data[name] = event.target.value;
92+
data[name] = newValue;
9293
this.setState({ data });
9394
};
9495

interface/src/containers/SecuritySettings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class SecuritySettings extends Component {
1212
}
1313

1414
render() {
15-
const { data, fetched, errorMessage } = this.props;
15+
const { data, fetched, errorMessage, saveData, loadData, handleValueChange } = this.props;
1616
return (
1717
<SectionContent title="Security Settings">
1818
<SecuritySettingsForm
1919
securitySettings={data}
2020
securitySettingsFetched={fetched}
2121
errorMessage={errorMessage}
22-
onSubmit={this.props.saveData}
23-
onReset={this.props.loadData}
24-
handleValueChange={this.props.handleValueChange}
22+
onSubmit={saveData}
23+
onReset={loadData}
24+
handleValueChange={handleValueChange}
2525
/>
2626
</SectionContent>
2727
)
Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,78 @@
11
import React, { Component } from 'react';
2-
3-
import { withStyles } from '@material-ui/core/styles';
42
import SectionContent from '../components/SectionContent';
3+
import { restComponent } from '../components/RestComponent';
4+
import LoadingNotification from '../components/LoadingNotification';
55

6-
const styles = theme => ({
6+
import Button from '@material-ui/core/Button';
7+
import Typography from '@material-ui/core/Typography';
8+
import Slider from '@material-ui/core/Slider';
9+
import { makeStyles } from '@material-ui/core/styles';
10+
import { ValidatorForm } from 'react-material-ui-form-validator';
711

8-
});
12+
export const DEMO_SETTINGS_ENDPOINT = process.env.REACT_APP_ENDPOINT_ROOT + "demoSettings";
913

10-
class DemoController extends Component {
14+
const valueToPercentage = (value) => `${Math.round(value / 255 * 100)}%`;
1115

12-
render() {
16+
class DemoController extends Component {
17+
componentDidMount() {
18+
this.props.loadData();
19+
}
20+
21+
render() {
22+
const { data, fetched, errorMessage, saveData, loadData, handleValueChange } = this.props;
1323
return (
1424
<SectionContent title="Controller" titleGutter>
15-
TODO - This will contain a form which controls the speed of the built in LED.
25+
<LoadingNotification
26+
onReset={loadData}
27+
fetched={fetched}
28+
errorMessage={errorMessage}>
29+
<DemoControllerForm
30+
demoSettings={data}
31+
onReset={loadData}
32+
onSubmit={saveData}
33+
handleValueChange={handleValueChange}
34+
/>
35+
</LoadingNotification>
1636
</SectionContent>
1737
)
1838
}
39+
}
1940

41+
const useStyles = makeStyles(theme => ({
42+
button: {
43+
marginRight: theme.spacing(2),
44+
marginTop: theme.spacing(2),
45+
},
46+
blinkSpeedLabel:{
47+
marginBottom: theme.spacing(5),
48+
}
49+
}));
50+
51+
function DemoControllerForm(props) {
52+
const { demoSettings, onSubmit, onReset, handleValueChange } = props;
53+
const classes = useStyles();
54+
return (
55+
<ValidatorForm onSubmit={onSubmit}>
56+
<Typography id="blink-speed-slider" className={classes.blinkSpeedLabel}>
57+
Blink Speed
58+
</Typography>
59+
<Slider
60+
value={demoSettings.blink_speed}
61+
valueLabelFormat={valueToPercentage}
62+
aria-labelledby="blink-speed-slider"
63+
valueLabelDisplay="on"
64+
min={0}
65+
max={255}
66+
onChange={handleValueChange('blink_speed')}
67+
/>
68+
<Button variant="contained" color="primary" className={classes.button} type="submit">
69+
Save
70+
</Button>
71+
<Button variant="contained" color="secondary" className={classes.button} onClick={onReset}>
72+
Reset
73+
</Button>
74+
</ValidatorForm>
75+
);
2076
}
2177

22-
export default withStyles(styles)(DemoController);
78+
export default restComponent(DEMO_SETTINGS_ENDPOINT, DemoController);

interface/src/project/DemoInformation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TableCell from '@material-ui/core/TableCell';
77
import TableBody from '@material-ui/core/TableBody';
88
import TableRow from '@material-ui/core/TableRow';
99
import Typography from '@material-ui/core/Typography';
10+
1011
import SectionContent from '../components/SectionContent';
1112

1213
const styles = theme => ({

interface/src/project/ProjectMenu.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import ListItem from '@material-ui/core/ListItem';
88
import ListItemIcon from '@material-ui/core/ListItemIcon';
99
import ListItemText from '@material-ui/core/ListItemText';
1010
import SettingsRemoteIcon from '@material-ui/icons/SettingsRemote';
11-
import Divider from '@material-ui/core/Divider';
1211

1312
class ProjectMenu extends Component {
1413

platformio.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
;
88
; Please visit documentation for the other options and examples
99
; http://docs.platformio.org/page/projectconf.html
10-
[env:node32s]
11-
platform = espressif32
12-
board = node32s
10+
[env:esp12e]
11+
platform = espressif8266
12+
board = esp12e
13+
board_build.f_cpu = 160000000L
1314

1415
extra_scripts = pre:timelib_fix.py
1516

src/DemoProject.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#include <DemoProject.h>
22

3-
void DemoProject::begin() {
4-
3+
void DemoProject::init(AsyncWebServer* server) {
4+
AdminSettingsService::init(server);
5+
pinMode(BLINK_LED, OUTPUT);
56
}
67

78
void DemoProject::loop() {
9+
unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed);
10+
unsigned long currentMillis = millis();
11+
if (!_lastBlink || (unsigned long)(currentMillis - _lastBlink) >= delay) {
12+
_lastBlink = currentMillis;
13+
digitalWrite(BLINK_LED, !digitalRead(BLINK_LED));
14+
}
15+
}
816

17+
void DemoProject::readFromJsonObject(JsonObject& root) {
18+
_blinkSpeed = root["blink_speed"] | DEFAULT_BLINK_SPEED;
919
}
20+
21+
void DemoProject::writeToJsonObject(JsonObject& root) {
22+
// connection settings
23+
root["blink_speed"] = _blinkSpeed;
24+
}
25+

0 commit comments

Comments
 (0)