|
1 | 1 | import {Col, Row} from 'react-bootstrap'; |
2 | 2 | import {useTranslation} from 'react-i18next'; |
3 | | -import {grafana} from '../config.ts'; |
| 3 | +import {sensors} from '../config.ts'; |
| 4 | +import {useMqttHistory} from "../hooks/useEndpoints.ts"; |
| 5 | +import EChartsReact from "echarts-for-react"; |
4 | 6 |
|
5 | 7 | const Sensors = () => { |
6 | 8 | const {t} = useTranslation(); |
| 9 | + const {data} = useMqttHistory(); |
7 | 10 |
|
8 | | - return (<> |
| 11 | + if(!data) return <></>; |
| 12 | + |
| 13 | + return <> |
9 | 14 | <Row> |
10 | 15 | <Col> |
11 | 16 | <h2>{t('views.sensors.title')}</h2> |
12 | 17 | </Col> |
13 | 18 | </Row> |
14 | | - <Row className="row-cols row-cols-1 row-cols-xxl-3"> |
15 | | - {grafana.urls.map(url => <Col key={url}> |
16 | | - <iframe src={url} className="w-100" height={300}/> |
17 | | - </Col>)} |
| 19 | + <Row className="row-cols row-cols-1 row-cols-xxl-2"> |
| 20 | + |
| 21 | + {Object.entries(data).map(([sensor, metrics]) => { |
| 22 | + |
| 23 | + const temperature = metrics['sensor_temperature']; |
| 24 | + const humidity = metrics['sensor_humidity']; |
| 25 | + |
| 26 | + const matchedSensor = Object.keys(sensors).find(n => n.includes(sensor)); |
| 27 | + const label = sensors[matchedSensor??''].label ?? sensor; |
| 28 | + |
| 29 | + const yAxis = []; |
| 30 | + const series = []; |
| 31 | + |
| 32 | + const parse = (d: [number, number]): [number, number] => [d[0]*1000, d[1]]; |
| 33 | + |
| 34 | + if(temperature) { |
| 35 | + yAxis.push({ |
| 36 | + name: t('views.sensors.temperature'), |
| 37 | + type: 'value', |
| 38 | + interval: 2.5, |
| 39 | + axisLabel: {formatter: '{value} °C'}, |
| 40 | + min: (v: any) => Math.floor(v.min / 5) * 5, |
| 41 | + max: (v: any) => Math.ceil(v.max / 5) * 5, |
| 42 | + }); |
| 43 | + series.push({ |
| 44 | + name: t('views.sensors.temperature'), |
| 45 | + type: 'line', |
| 46 | + smooth: true, |
| 47 | + symbol: 'none', |
| 48 | + data: temperature.map(parse), |
| 49 | + yAxisIndex: yAxis.length - 1, |
| 50 | + tooltip: {valueFormatter: (v: any) => `${v} °C`} |
| 51 | + }); |
| 52 | + } |
| 53 | + if(humidity) { |
| 54 | + yAxis.push({ |
| 55 | + name: t('views.sensors.humidity'), |
| 56 | + type: 'value', |
| 57 | + interval: 2.5, |
| 58 | + axisLabel: {formatter: '{value} %'}, |
| 59 | + min: (v: any) => Math.floor(v.min / 5) * 5, |
| 60 | + max: (v: any) => Math.ceil(v.max / 5) * 5, |
| 61 | + }); |
| 62 | + series.push({ |
| 63 | + name: t('views.sensors.humidity'), |
| 64 | + type: 'line', |
| 65 | + smooth: true, |
| 66 | + symbol: 'none', |
| 67 | + data: humidity.map(parse), |
| 68 | + yAxisIndex: yAxis.length - 1, |
| 69 | + tooltip: {valueFormatter: (v: any) => `${v} %`} |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + return <Col key={sensor}><EChartsReact option={{ |
| 75 | + title: {text: t(`rooms.${label}`), left: 'left'}, |
| 76 | + tooltip: { |
| 77 | + trigger: 'axis' |
| 78 | + }, |
| 79 | + legend: {type: 'plain', left: 'left'}, |
| 80 | + xAxis: {type: 'time',max:Date.now()}, |
| 81 | + grid: { left: 0, right: 0, top: 70, bottom: 70 }, |
| 82 | + yAxis: yAxis, |
| 83 | + series: series, |
| 84 | + animation: false, |
| 85 | + }}/></Col>; |
| 86 | + } |
| 87 | + )} |
18 | 88 | </Row> |
19 | | - </>); |
| 89 | + </>; |
20 | 90 | }; |
21 | 91 |
|
22 | 92 | export default Sensors; |
0 commit comments