11import { Col , Row } from 'react-bootstrap' ;
22import { useTranslation } from 'react-i18next' ;
3- import { sensors } from '../config.ts' ;
43import { useMqttHistory } from "../hooks/useEndpoints.ts" ;
5- import EChartsReact from "echarts-for-react " ;
4+ import SensorGraph from "../widgets/SensorReadings/SensorGraph.tsx " ;
65
76const Sensors = ( ) => {
87 const { t} = useTranslation ( ) ;
98 const { data} = useMqttHistory ( ) ;
109
11- if ( ! data ) return < > </ > ;
10+ if ( ! data ) return < > </ > ;
1211
1312 return < >
1413 < Row >
@@ -17,73 +16,8 @@ const Sensors = () => {
1716 </ Col >
1817 </ Row >
1918 < 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- }
19+ { Object . entries ( data ) . map ( ( [ sensor , metrics ] ) =>
20+ < Col key = { sensor } > < SensorGraph sensor = { sensor } metrics = { metrics } /> </ Col >
8721 ) }
8822 </ Row >
8923 </ > ;
0 commit comments