2022-01-10 07:55:39 +00:00
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import { ComposedChart, Bar, Label, LineChart, ReferenceLine, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
2022-01-11 07:27:04 +00:00
|
|
|
|
import Datetime from 'react-datetime';
|
|
|
|
|
import 'react-datetime/css/react-datetime.css';
|
2022-01-10 07:55:39 +00:00
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import moment from 'moment';
|
|
|
|
|
import './App.css';
|
|
|
|
|
|
|
|
|
|
const durations = [
|
2022-01-11 07:27:04 +00:00
|
|
|
|
{id: 0, len: 'Day', win: '10m', full: '10 min', delta: [1, 'days']},
|
|
|
|
|
{id: 1, len: 'Day', win: '1h', full: '1 hour', delta: [1, 'days']},
|
|
|
|
|
{id: 2, len: 'Week', win: '1h', full: '1 hour', delta: [7, 'days']},
|
|
|
|
|
{id: 3, len: 'Week', win: '1d', full: '1 day', delta: [7, 'days']},
|
|
|
|
|
{id: 4, len: 'Month', win: '1d', full: '1 day', delta: [1, 'months']},
|
|
|
|
|
{id: 5, len: 'Month', win: '7d', full: '7 day', delta: [1, 'months']},
|
|
|
|
|
{id: 6, len: 'Year', win: '1d', full: '1 day', delta: [1, 'years']},
|
|
|
|
|
{id: 7, len: 'Year', win: '30d', full: '30 day', delta: [1, 'years']},
|
2022-01-10 07:55:39 +00:00
|
|
|
|
];
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
function useSensor(measurement, name, end, duration) {
|
|
|
|
|
if (!data) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<h2>Water Usage</h2>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
|
|
|
|
|
<p>Loading...</p>
|
2022-01-11 07:27:04 +00:00
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-10 07:55:39 +00:00
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
return (
|
|
|
|
|
<>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
<h2>Water Usage {loading ? 'Loading...' : ''}</h2>
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
<ResponsiveContainer width='100%' height={300}>
|
|
|
|
|
<ComposedChart syncId={1} data={data} margin={{ top: 5, left: 0, right: 30, bottom: 0 }}>
|
|
|
|
|
<XAxis
|
|
|
|
|
dataKey='time'
|
|
|
|
|
minTickGap={10}
|
|
|
|
|
tickFormatter={timeStr => moment(timeStr).format('HH:mm')}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<YAxis
|
|
|
|
|
yAxisId='right'
|
|
|
|
|
domain={data.length ? [data[0].consumption_data, data.slice(-1)[0].consumption_data] : [100, 0]}
|
|
|
|
|
orientation='right'
|
|
|
|
|
hide={true}
|
|
|
|
|
/>
|
|
|
|
|
<YAxis
|
|
|
|
|
yAxisId='left'
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<CartesianGrid strokeDasharray='3 3'/>
|
|
|
|
|
<Tooltip
|
|
|
|
|
formatter={v => v + ' L'}
|
|
|
|
|
labelFormatter={timeStr => moment(timeStr).format('ddd MMM DD h:mm A')}
|
|
|
|
|
separator=': '
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<ReferenceLine yAxisId='right' x={moment().startOf('day').toISOString().replace('.000', '')} stroke='blue' />
|
|
|
|
|
|
|
|
|
|
<Bar
|
|
|
|
|
yAxisId='left'
|
|
|
|
|
type='monotone'
|
|
|
|
|
dataKey='delta'
|
|
|
|
|
name='Delta'
|
|
|
|
|
fill='green'
|
|
|
|
|
isAnimationActive={false}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Line
|
|
|
|
|
yAxisId='right'
|
|
|
|
|
type='monotone'
|
|
|
|
|
dataKey='max'
|
|
|
|
|
name='Total'
|
|
|
|
|
stroke='black'
|
|
|
|
|
strokeWidth={2}
|
|
|
|
|
dot={false}
|
|
|
|
|
isAnimationActive={false}
|
|
|
|
|
/>
|
|
|
|
|
</ComposedChart>
|
|
|
|
|
</ResponsiveContainer>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-10 07:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
function Graphs({end, duration}) {
|
|
|
|
|
return (
|
|
|
|
|
<div className='container'>
|
|
|
|
|
<OutsideTemperature end={end} duration={duration} />
|
|
|
|
|
<BedroomTemperature end={end} duration={duration} />
|
|
|
|
|
<Thermostat end={end} duration={duration} />
|
|
|
|
|
<Gas end={end} duration={duration} />
|
|
|
|
|
<Water end={end} duration={duration} />
|
2022-01-10 07:55:39 +00:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
function Menu({duration, setDuration, end, setEnd}) {
|
2022-01-10 07:55:39 +00:00
|
|
|
|
const [submenu, setSubmenu] = useState(false);
|
|
|
|
|
|
|
|
|
|
const chooseDuration = (x) => {
|
|
|
|
|
setSubmenu(false);
|
|
|
|
|
setDuration(x);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
const chooseEnd = (x) => {
|
|
|
|
|
setSubmenu(false);
|
|
|
|
|
const newEnd = x.add(...duration.delta);
|
|
|
|
|
setEnd(newEnd);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const next = () => {
|
|
|
|
|
setSubmenu(false);
|
|
|
|
|
setEnd(prevEnd => moment(prevEnd).add(...duration.delta));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prev = () => {
|
|
|
|
|
setSubmenu(false);
|
|
|
|
|
setEnd(prevEnd => moment(prevEnd).subtract(duration.delta[0], duration.delta[1]));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 07:55:39 +00:00
|
|
|
|
return (
|
|
|
|
|
<div className='menu'>
|
|
|
|
|
{!!submenu &&<div className='submenu'>
|
|
|
|
|
{submenu === 'end' &&
|
|
|
|
|
<>
|
2022-01-11 07:27:04 +00:00
|
|
|
|
<div className='submenu-header'>
|
|
|
|
|
<h2>Choose start date:</h2>
|
|
|
|
|
<button onClick={() => setSubmenu(false)}>×</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className='datepicker'>
|
|
|
|
|
<Datetime
|
|
|
|
|
input={false}
|
|
|
|
|
timeFormat={false}
|
|
|
|
|
onChange={(x) => chooseEnd(x)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{submenu === 'duration' &&
|
2022-01-11 07:27:04 +00:00
|
|
|
|
<>
|
|
|
|
|
<div className='submenu-header'>
|
|
|
|
|
<h2>Choose duration:</h2>
|
|
|
|
|
<button onClick={() => setSubmenu(false)}>×</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{durations.map(x =>
|
|
|
|
|
<button key={x.id} onClick={() => chooseDuration(x)}>Last {x.len} / {x.full} data</button>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
</div>}
|
|
|
|
|
|
|
|
|
|
<div className='menu-container'>
|
2022-01-11 07:27:04 +00:00
|
|
|
|
<button onClick={() => prev()}><</button>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setSubmenu('end')}
|
|
|
|
|
className={submenu === 'end' ? 'active' : ''}
|
|
|
|
|
>
|
2022-01-11 07:27:04 +00:00
|
|
|
|
{moment(end).subtract(duration.delta[0], duration.delta[1]).format('ddd MMM DD')}
|
2022-01-10 07:55:39 +00:00
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setSubmenu('duration')}
|
|
|
|
|
className={submenu === 'duration' ? 'active' : ''}
|
|
|
|
|
>
|
|
|
|
|
{duration.len} / {duration.win}
|
|
|
|
|
</button>
|
|
|
|
|
|
2022-01-11 07:27:04 +00:00
|
|
|
|
<button onClick={() => next()}>></button>
|
2022-01-10 07:55:39 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
const [duration, setDuration] = useState(durations[0]);
|
|
|
|
|
const [end, setEnd] = useState(moment());
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<Menu
|
|
|
|
|
duration={duration}
|
|
|
|
|
setDuration={setDuration}
|
|
|
|
|
end={end}
|
|
|
|
|
setEnd={setEnd}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Graphs
|
2022-01-11 07:27:04 +00:00
|
|
|
|
end={end}
|
|
|
|
|
duration={duration}
|
2022-01-10 07:55:39 +00:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|