/
index.js
152 lines (139 loc) · 5.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
import moment from 'moment-timezone';
import LastReading from './components/LastReading';
import RangeReading from './components/RangeReading';
import RangeChart from './components/RangeChart';
import TemperatureReading from './components/TemperatureReading';
import './css/bootstrap.min.css';
import './css/index.css';
class App extends Component {
constructor() {
super();
this.timeoutInMs = 1*60*1000;
this.state = {
time: moment().tz('America/Edmonton').format(' h:mma MMMM DD, YYYY'),
temperature: {},
darksky: {},
outside: {},
humidity: {},
core: {},
range_inside: [],
range_outside: [],
hours: 3,
}
}
getLastTemperature() {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings/last?name=temperature`)
.then((res) => { this.setState({temperature:res.data}); })
.catch((err) => { console.log(err); })
}
getLastOutside() {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings/last?name=outside`)
.then((res) => { this.setState({outside:res.data}); })
.catch((err) => { console.log(err); })
}
getLastDarksky() {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings/last?name=darksky`)
.then((res) => { this.setState({darksky:res.data}); })
.catch((err) => { console.log(err); })
}
getLastCore() {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings/last?name=core`)
.then((res) => { this.setState({core:res.data}); })
.catch((err) => { console.log(err); })
}
getLastHumidity() {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings/last?name=humidity`)
.then((res) => { this.setState({humidity:res.data}); })
.catch((err) => { console.log(err); })
}
getTemperatureRange(hours) {
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings?name=temperature&hours=${hours}`)
.then((res) => { this.setState({range_inside:res.data.readings}); })
.catch((err) => { console.log(err); });
axios.get(`${process.env.REACT_APP_CAMPUTER_SERVICE_URL}/sensorreadings?name=outside&hours=${hours}`)
.then((res) => { this.setState({range_outside:res.data.readings}); })
.catch((err) => { console.log(err); });
}
onTimeout() {
this.setState({time: moment().tz('America/Edmonton').format(' h:mma MMMM DD, YYYY')});
this.getLastTemperature();
this.getLastOutside();
this.getLastDarksky();
this.getTemperatureRange(this.state.hours);
this.getLastHumidity();
this.getLastCore();
setTimeout(() => { this.onTimeout() }, this.timeoutInMs);
}
componentDidMount(){
this.getLastDarksky();
this.getLastTemperature();
this.getLastOutside();
this.getTemperatureRange(this.state.hours);
this.getLastHumidity();
this.getLastCore();
setTimeout(()=>{ this.onTimeout() }, this.timeoutInMs)
}
changeHours(hours) {
this.setState({ hours });
this.getTemperatureRange(hours);
}
render() {
return (
<>
<div className="top-bar"/>
<nav className="navbar navbar-light bg-light">
<a className="navbar-brand" href="#">
<img className='d-inline align-top' src='./images/002-camping-144x144.png' alt='Camputer' width='60px' height='60px' />
{ this.state.time }
</a>
</nav>
<div className="container">
<div className="row">
<div className="col-md-3">
<TemperatureReading sensorData={this.state.temperature} sensorName="Inside" />
</div>
<div className="col-md-3">
<TemperatureReading sensorData={this.state.outside} sensorName="Outside" />
</div>
<div className="col-md-3">
<TemperatureReading sensorData={this.state.darksky} sensorName="Darksky" />
</div>
<div className="col-md-3">
<TemperatureReading sensorData={this.state.core} sensorName="Core" />
</div>
</div>
<div className="row chart-controls">
<div className="col-md-6">
<button className="btn-primary" onClick={() => this.changeHours(3)}>3 Hrs</button>
<button className="btn-primary" onClick={() => this.changeHours(12)}>12 Hrs</button>
<button className="btn-primary" onClick={() => this.changeHours(24)}>24 Hrs</button>
</div>
</div>
<div className="row">
<div className="col-md-6">
<RangeChart insideData={ this.state.range_inside } outsideData={ this.state.range_outside } />
</div>
</div>
<div className="row">
<div className="col-md-3">
<RangeReading sensorData={this.state.range_outside} range={this.state.hours} sensorName="Outside Temperature" />
</div>
<div className="col-md-3">
<RangeReading sensorData={this.state.range_inside} range={this.state.hours} sensorName="Inside Temperature" />
</div>
<div className="col-md-3">
<LastReading sensorData={this.state.humidity} sensorName="Humidity" />
</div>
</div>
</div>
</>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);