|
1 | 1 | import React, { useEffect, useState } from "react"; |
2 | 2 | import styles from "./Module.module.scss"; |
| 3 | + |
3 | 4 | import { useMeasurementsStore } from "common"; |
4 | 5 |
|
5 | 6 | interface CellProps { |
6 | | - value: number; |
| 7 | + value: number; |
| 8 | + min: number; |
| 9 | + max: number; |
7 | 10 | } |
8 | 11 |
|
9 | 12 | const Module: React.FC<{ id: string | number }> = ({ id }) => { |
10 | | - const numericInfo = useMeasurementsStore( |
11 | | - (state) => state.getNumericMeasurementInfo(`module/${id}`) |
12 | | - ); |
| 13 | + const moduleMinCell = useMeasurementsStore( |
| 14 | + (state) => (state.getNumericMeasurementInfo(`module_${id}_min_cell`)?.getUpdate() ?? 0) |
| 15 | + ); |
| 16 | + const moduleMaxCell = useMeasurementsStore( |
| 17 | + (state) => (state.getNumericMeasurementInfo(`module_${id}_max_cell`)?.getUpdate() ?? 0) |
| 18 | + ); |
13 | 19 |
|
14 | | - const [cellValues, setCellValues] = useState(Array(48).fill(0)); |
| 20 | + const moduleTotalVoltage = useMeasurementsStore( |
| 21 | + (state) => (state.getNumericMeasurementInfo(`module_${id}_voltage`)?.getUpdate() ?? 0) |
| 22 | + ); |
15 | 23 |
|
16 | | - useEffect(() => { |
17 | | - const intervalId = setInterval(() => { |
18 | | - const newValue = numericInfo.getUpdate(); |
19 | | - setCellValues((prev) => prev.map(() => newValue)); |
20 | | - }, 1); |
| 24 | + // Estado para las celdas |
| 25 | + const [cellValues, setCellValues] = useState<number[]>(Array(48).fill(0)); // Define el tipo correctamente |
21 | 26 |
|
22 | | - return () => clearInterval(intervalId); |
23 | | - }, [numericInfo]); |
| 27 | + useEffect(() => { |
| 28 | + const intervalId = setInterval(() => { |
| 29 | + setCellValues(() => |
| 30 | + Array.from({ length: 48 }, (_, i) => { |
| 31 | + const variableName = `module_${id}_cell_${i + 1}_voltage`; |
| 32 | + return useMeasurementsStore.getState().getNumericMeasurementInfo(variableName)?.getUpdate() ?? 0; |
| 33 | + }) |
| 34 | + ); |
| 35 | + }, 100); |
24 | 36 |
|
25 | | - const getColorFromValue = (value: number, min: number | null, max: number | null) => { |
26 | | - if (min !== null && max !== null) { |
27 | | - if (value < min) return styles.red; |
28 | | - if (value > max) return styles.red; |
29 | | - if (value >= min && value <= max) return styles.green; |
30 | | - } |
31 | | - return styles.yellow; |
32 | | - }; |
| 37 | + return () => clearInterval(intervalId); |
| 38 | + }, [id]); |
33 | 39 |
|
34 | | - const Cell: React.FC<CellProps> = ({ value }) => { |
35 | | - const colorClass = getColorFromValue(value, numericInfo.range[0], numericInfo.range[1]); |
36 | | - return <div className={`${styles.cell} ${colorClass}`}></div>; |
37 | | - }; |
| 40 | + const getColorFromValue = (value: number, min: number, max: number) => { |
| 41 | + if (value < min) return styles.red; |
| 42 | + if (value > max) return styles.red; |
| 43 | + if (value >= min && value <= max) return styles.green; |
| 44 | + return styles.yellow; |
| 45 | + }; |
38 | 46 |
|
| 47 | + const Cell: React.FC<CellProps> = ({ value, min, max }) => { |
| 48 | + const colorClass = getColorFromValue(value, min, max); |
39 | 49 | return ( |
40 | | - <div className={styles.boxContainer1}> |
41 | | - <div className={styles.boxContainer2}> |
42 | | - <article className={styles.titleDecorationModule}> |
43 | | - <h2 className={styles.h2Module}>Module {id}</h2> |
44 | | - </article> |
45 | | - <div className={styles.boxContainer3}> |
46 | | - <div className={styles.voltajeContainer}> |
47 | | - <h3 className={styles.h3}>Voltage</h3> |
48 | | - <p className={styles.p}>max: {numericInfo.range[1]} V</p> |
49 | | - <p className={styles.p}>min: {numericInfo.range[0]} V</p> |
50 | | - <p className={styles.p}>mean: {cellValues.reduce((a, b) => a + b, 0) / cellValues.length}</p> |
51 | | - </div> |
52 | | - <div className={styles.intensityContainer}> |
53 | | - <h3 className={styles.h3}>Intensity</h3> |
54 | | - <p className={styles.p}>max: {numericInfo.range[1]} A</p> |
55 | | - <p className={styles.p}>min: {numericInfo.range[0]} A</p> |
56 | | - </div> |
57 | | - </div> |
58 | | - </div> |
59 | | - <div className={styles.flexCells}> |
60 | | - {cellValues.map((value, index) => ( |
61 | | - <Cell key={index} value={value} /> |
62 | | - ))} |
63 | | - </div> |
64 | | - </div> |
| 50 | + <div |
| 51 | + className={`${styles.cell} ${colorClass}`} |
| 52 | + title={`${value.toFixed(3)} V`} |
| 53 | + ></div> |
65 | 54 | ); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className={styles.boxContainer1}> |
| 59 | + <div className={styles.boxContainer2}> |
| 60 | + <article className={styles.titleDecorationModule}> |
| 61 | + <h2 className={styles.h2Module}>Module {id}</h2> |
| 62 | + </article> |
| 63 | + |
| 64 | + <div className={styles.voltajeContainer}> |
| 65 | + <div className={styles.dataStyle}> |
| 66 | + <p className={styles.moduleInfoLabel}>max:</p> |
| 67 | + <p className={styles.p}>{`${moduleMaxCell} V`}</p> |
| 68 | + </div> |
| 69 | + <div className={styles.dataStyle}> |
| 70 | + <p className={styles.moduleInfoLabel}>min:</p> |
| 71 | + <p className={styles.p}>{`${moduleMinCell} V`}</p> |
| 72 | + </div> |
| 73 | + <div className={styles.dataStyle}> |
| 74 | + <p className={styles.moduleInfoLabel}>total:</p> |
| 75 | + <p className={styles.p}>{`${moduleTotalVoltage} V`}</p> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + <div className={styles.flexCells}> |
| 80 | + {cellValues.map((value, index) => ( |
| 81 | + <Cell key={index} value={value} min={moduleMinCell} max={moduleMaxCell} /> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + ); |
66 | 86 | }; |
67 | 87 |
|
68 | 88 | export default Module; |
| 89 | + |
0 commit comments