import React, { Component } from 'react'; import { Card, CardHeader, CardContent, Divider, Grid, Box, Typography } from '@mui/material'; import EnvChart from './EnvChart'; import LevelChart from './LevelChart'; import { withI18n } from './I18nContext'; class ControllerCard extends Component { constructor(props) { super(props); this.state = { envData: [], portData: {} }; this.interval = null; } componentDidMount() { this.fetchData(); this.interval = setInterval(() => this.fetchData(), 60000); } componentDidUpdate(prevProps) { if (prevProps.controllerName !== this.props.controllerName || prevProps.range !== this.props.range) { this.fetchData(); } } componentWillUnmount() { if (this.interval) { clearInterval(this.interval); } } fetchData = async () => { const { controllerName, ports, range } = this.props; try { if (ports.length === 0) return; // Fetch all ports concurrently const promises = ports.map(port => fetch(`api/history?devName=${encodeURIComponent(controllerName)}&port=${port.port}&range=${range}`) .then(res => res.json()) .then(data => ({ port: port.port, data })) ); const results = await Promise.all(promises); const newPortData = {}; results.forEach(item => { newPortData[item.port] = item.data; }); this.setState({ portData: newPortData }); // Use the data from the first port for the Environment Chart // This avoids a redundant network request if (results.length > 0) { this.setState({ envData: results[0].data }); } } catch (err) { console.error("Fetch error", err); } }; render() { const { controllerName, ports, range, i18n: { t } } = this.props; const { envData, portData } = this.state; return ( {/* Environment Chart */} {t('controller.environment')} {/* Port Grid */} {ports.map((port) => { const isLight = port.port_name && port.port_name.toLowerCase().includes('light'); const isCO2 = port.port_name && port.port_name.toLowerCase().includes('co2'); const pData = portData[port.port] || []; return ( {port.port_name || `${t('controller.port')} ${port.port}`} ); })} ); } } export default withI18n(ControllerCard);