<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Deye Inverter Monitor</title>
    <style>
        :root {
            --bg-color: #1a1a1a;
            --card-bg: #2d2d2d;
            --text-color: #e0e0e0;
            --accent-color: #00d2ff;
            --secondary-accent: #3a7bd5;
            --font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: var(--bg-color);
            color: var(--text-color);
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(90deg, var(--accent-color), var(--secondary-accent));
        }

        .card h2 {
            margin: 0 0 15px 0;
            font-size: 1.2rem;
            color: #aaa;
            font-weight: 400;
        }

        .value {
            font-size: 2.5rem;
            font-weight: 700;
            color: #fff;
        }

        .unit {
            font-size: 1rem;
            color: #888;
            margin-left: 5px;
        }

        .status-bar {
            margin-top: 40px;
            padding: 10px 20px;
            border-radius: 20px;
            background-color: var(--card-bg);
            font-size: 0.9rem;
            color: #888;
        }

        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: #555;
            margin-right: 8px;
        }

        .status-indicator.active {
            background-color: #00ff88;
            box-shadow: 0 0 10px #00ff88;
        }

        .status-indicator.error {
            background-color: #ff4444;
            box-shadow: 0 0 10px #ff4444;
        }

        @keyframes pulse {
            0% {
                opacity: 1;
            }

            50% {
                opacity: 0.5;
            }

            100% {
                opacity: 1;
            }
        }

        .loading .value {
            animation: pulse 1.5s infinite;
            color: #666;
        }

        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            width: 100%;
            max-width: 1000px;
        }
    </style>
</head>

<body>
    <h1>Deye Inverter Monitor</h1>

    <div class="dashboard">
        <div class="card" id="card-input">
            <h2>Grid Voltage (Input)</h2>
            <div class="value-container loading">
                <span class="value" id="input-voltage">--</span><span class="unit">V</span>
            </div>
        </div>

        <div class="card" id="card-output">
            <h2>Output Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="output-voltage">--</span><span class="unit">V</span>
            </div>
        </div>

        <div class="card" id="card-load">
            <h2>Load Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="load-voltage">--</span><span class="unit">V</span>
            </div>
        </div>

        <div class="card" id="card-grid-current">
            <h2>Grid Current</h2>
            <div class="value-container loading">
                <span class="value" id="grid-current">--</span><span class="unit">A</span>
            </div>
        </div>

        <div class="card" id="card-clamp-current">
            <h2>Grid Clamp Current</h2>
            <div class="value-container loading">
                <span class="value" id="grid-clamp-current">--</span><span class="unit">A</span>
            </div>
        </div>

        <div class="card" id="card-output-current">
            <h2>Output Current</h2>
            <div class="value-container loading">
                <span class="value" id="output-current">--</span><span class="unit">A</span>
            </div>
        </div>
    </div>

    <h2>Battery & PV</h2>
    <div class="dashboard">
        <div class="card">
            <h2>Battery SOC</h2>
            <div class="value-container loading">
                <span class="value" id="batt-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>Battery Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="batt-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
        <div class="card">
            <h2>Battery Current</h2>
            <div class="value-container loading">
                <span class="value" id="batt-current">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Battery Power</h2>
            <div class="value-container loading">
                <span class="value" id="batt-power">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>Battery Temp</h2>
            <div class="value-container loading">
                <span class="value" id="batt-temp">--</span><span class="unit">°C</span>
            </div>
        </div>
        <div class="card">
            <h2>PV1 Power</h2>
            <div class="value-container loading">
                <span class="value" id="pv1-power">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>PV2 Power</h2>
            <div class="value-container loading">
                <span class="value" id="pv2-power">--</span><span class="unit">W</span>
            </div>
        </div>
    </div>

    <h2>Control Status</h2>
    <div class="dashboard">
        <div class="card">
            <h2>Grid Relay</h2>
            <div class="value-container loading">
                <span class="value" id="grid-relay">--</span>
            </div>
        </div>
        <div class="card">
            <h2>Gen Relay</h2>
            <div class="value-container loading">
                <span class="value" id="gen-relay">--</span>
            </div>
        </div>
        <div class="card">
            <h2>Max Charge I</h2>
            <div class="value-container loading">
                <span class="value" id="max-charge">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Max Discharge I</h2>
            <div class="value-container loading">
                <span class="value" id="max-discharge">--</span><span class="unit">A</span>
            </div>
        </div>
    </div>

    <h2>BMS Data</h2>
    <div class="dashboard">
        <div class="card">
            <h2>BMS1 SOC</h2>
            <div class="value-container loading">
                <span class="value" id="bms1-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS1 Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="bms1-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS1 Current</h2>
            <div class="value-container loading">
                <span class="value" id="bms1-current">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS2 SOC</h2>
            <div class="value-container loading">
                <span class="value" id="bms2-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS2 Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="bms2-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS2 Current</h2>
            <div class="value-container loading">
                <span class="value" id="bms2-current">--</span><span class="unit">A</span>
            </div>
        </div>
    </div>

    <h2>Advanced Control & Limits</h2>
    <div class="dashboard">
        <div class="card">
            <h2>Max Grid Output</h2>
            <div class="value-container loading">
                <span class="value" id="max-grid-output">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>Gen Peak Shaving</h2>
            <div class="value-container loading">
                <span class="value" id="gen-peak-shaving">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>Grid Peak Shaving</h2>
            <div class="value-container loading">
                <span class="value" id="grid-peak-shaving">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>Discharge Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="discharge-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
        <div class="card">
            <h2>Charge I Limit</h2>
            <div class="value-container loading">
                <span class="value" id="charge-i-limit">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Discharge I Limit</h2>
            <div class="value-container loading">
                <span class="value" id="discharge-i-limit">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Real Time Cap</h2>
            <div class="value-container loading">
                <span class="value" id="real-time-cap">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>Real Time Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="real-time-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
        <div class="card">
            <h2>Max Charge I Limit</h2>
            <div class="value-container loading">
                <span class="value" id="max-charge-i-limit">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Max Discharge I Limit</h2>
            <div class="value-container loading">
                <span class="value" id="max-discharge-i-limit">--</span><span class="unit">A</span>
            </div>
        </div>
        <div class="card">
            <h2>Lithium Type</h2>
            <div class="value-container loading">
                <span class="value" id="lithium-type">--</span>
            </div>
        </div>
    </div>

    <div class="status-bar">
        <span class="status-indicator" id="status-dot"></span>
        <div class="card">
            <h2>Battery SOC</h2>
            <div class="value-container loading"><span class="value" id="batt-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>Battery Voltage</h2>
            <div class="value-container loading"><span class="value" id="batt-voltage">--</span><span
                    class="unit">V</span></div>
        </div>
        <div class="card">
            <h2>Battery Current</h2>
            <div class="value-container loading"><span class="value" id="batt-current">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Battery Power</h2>
            <div class="value-container loading"><span class="value" id="batt-power">--</span><span
                    class="unit">W</span></div>
        </div>
        <div class="card">
            <h2>Battery Temp</h2>
            <div class="value-container loading"><span class="value" id="batt-temp">--</span><span
                    class="unit">°C</span></div>
        </div>
        <div class="card">
            <h2>PV1 Power</h2>
            <div class="value-container loading"><span class="value" id="pv1-power">--</span><span class="unit">W</span>
            </div>
        </div>
        <div class="card">
            <h2>PV2 Power</h2>
            <div class="value-container loading"><span class="value" id="pv2-power">--</span><span class="unit">W</span>
            </div>
        </div>
    </div>
    <h2>Control Status</h2>
    <div class="dashboard">
        <div class="card">
            <h2>Grid Relay</h2>
            <div class="value-container loading"><span class="value" id="grid-relay">--</span></div>
        </div>
        <div class="card">
            <h2>Gen Relay</h2>
            <div class="value-container loading"><span class="value" id="gen-relay">--</span></div>
        </div>
        <div class="card">
            <h2>Max Charge I</h2>
            <div class="value-container loading"><span class="value" id="max-charge">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Max Discharge I</h2>
            <div class="value-container loading"><span class="value" id="max-discharge">--</span><span
                    class="unit">A</span></div>
        </div>
    </div>
    <h2>BMS Data</h2>
    <div class="dashboard">
        <div class="card">
            <h2>BMS1 SOC</h2>
            <div class="value-container loading"><span class="value" id="bms1-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS1 Voltage</h2>
            <div class="value-container loading"><span class="value" id="bms1-voltage">--</span><span
                    class="unit">V</span></div>
        </div>
        <div class="card">
            <h2>BMS1 Current</h2>
            <div class="value-container loading"><span class="value" id="bms1-current">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>BMS2 SOC</h2>
            <div class="value-container loading"><span class="value" id="bms2-soc">--</span><span class="unit">%</span>
            </div>
        </div>
        <div class="card">
            <h2>BMS2 Voltage</h2>
            <div class="value-container loading"><span class="value" id="bms2-voltage">--</span><span
                    class="unit">V</span></div>
        </div>
        <div class="card">
            <h2>BMS2 Current</h2>
            <div class="value-container loading"><span class="value" id="bms2-current">--</span><span
                    class="unit">A</span></div>
        </div>
    </div>
    <h2>Advanced Control & Limits</h2>
    <div class="dashboard">
        <div class="card">
            <h2>Max Grid Output</h2>
            <div class="value-container loading"><span class="value" id="max-grid-output">--</span><span
                    class="unit">W</span></div>
        </div>
        <div class="card">
            <h2>Gen Peak Shaving</h2>
            <div class="value-container loading"><span class="value" id="gen-peak-shaving">--</span><span
                    class="unit">W</span></div>
        </div>
        <div class="card">
            <h2>Grid Peak Shaving</h2>
            <div class="value-container loading"><span class="value" id="grid-peak-shaving">--</span><span
                    class="unit">W</span></div>
        </div>
        <div class="card">
            <h2>Discharge Voltage</h2>
            <div class="value-container loading"><span class="value" id="discharge-voltage">--</span><span
                    class="unit">V</span></div>
        </div>
        <div class="card">
            <h2>Charge I Limit</h2>
            <div class="value-container loading"><span class="value" id="charge-i-limit">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Discharge I Limit</h2>
            <div class="value-container loading"><span class="value" id="discharge-i-limit">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Real Time Cap</h2>
            <div class="value-container loading"><span class="value" id="real-time-cap">--</span><span
                    class="unit">%</span></div>
        </div>
        <div class="card">
            <h2>Real Time Voltage</h2>
            <div class="value-container loading"><span class="value" id="real-time-voltage">--</span><span
                    class="unit">V</span></div>
        </div>
        <div class="card">
            <h2>Max Charge I Limit</h2>
            <div class="value-container loading"><span class="value" id="max-charge-i-limit">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Max Discharge I Limit</h2>
            <div class="value-container loading"><span class="value" id="max-discharge-i-limit">--</span><span
                    class="unit">A</span></div>
        </div>
        <div class="card">
            <h2>Lithium Type</h2>
            <div class="value-container loading"><span class="value" id="lithium-type">--</span></div>
        </div>
    </div>
    <div class="status-bar"><span class="status-indicator" id="status-dot"></span><span
            id="status-text">Connecting...</span></div>
    <script>const inputVoltageEl = document.getElementById('input-voltage');
        const outputVoltageEl = document.getElementById('output-voltage');
        const loadVoltageEl = document.getElementById('load-voltage');
        const gridCurrentEl = document.getElementById('grid-current');
        const gridClampCurrentEl = document.getElementById('grid-clamp-current');
        const outputCurrentEl = document.getElementById('output-current');
        const statusDot = document.getElementById('status-dot');
        const statusText = document.getElementById('status-text');
        const valueContainers = document.querySelectorAll('.value-container');

        async function fetchData() {
            try {
                const response = await fetch('/api/data');
                const data = await response.json();

                if (data.status === 'success') {
                    // Basic
                    inputVoltageEl.textContent = data.input_voltage;
                    outputVoltageEl.textContent = data.output_voltage;
                    loadVoltageEl.textContent = data.load_voltage;
                    gridCurrentEl.textContent = data.grid_current;
                    gridClampCurrentEl.textContent = data.grid_clamp_current;
                    outputCurrentEl.textContent = data.output_current;

                    // Battery & PV
                    document.getElementById('batt-soc').textContent = data.batt_soc;
                    document.getElementById('batt-voltage').textContent = data.batt_voltage;
                    document.getElementById('batt-current').textContent = data.batt_current;
                    document.getElementById('batt-power').textContent = data.batt_power;
                    document.getElementById('batt-temp').textContent = data.batt_temp;
                    document.getElementById('pv1-power').textContent = data.pv1_power;
                    document.getElementById('pv2-power').textContent = data.pv2_power;

                    // Control Status
                    document.getElementById('grid-relay').textContent = data.grid_relay_status;
                    document.getElementById('gen-relay').textContent = data.gen_relay_status;
                    document.getElementById('max-charge').textContent = data.max_charge_current;
                    document.getElementById('max-discharge').textContent = data.max_discharge_current;

                    // BMS Data
                    document.getElementById('bms1-soc').textContent = data.bms1_soc;
                    document.getElementById('bms1-voltage').textContent = data.bms1_charge_voltage;
                    document.getElementById('bms1-current').textContent = data.bms1_charge_current;
                    document.getElementById('bms2-soc').textContent = data.bms2_soc;
                    document.getElementById('bms2-voltage').textContent = data.bms2_charge_voltage;
                    document.getElementById('bms2-current').textContent = data.bms2_charge_current;

                    // Advanced Control
                    document.getElementById('max-grid-output').textContent = data.max_grid_output_power;
                    document.getElementById('gen-peak-shaving').textContent = data.gen_peak_shaving_power;
                    document.getElementById('grid-peak-shaving').textContent = data.grid_peak_shaving_power;
                    document.getElementById('discharge-voltage').textContent = data.discharge_voltage;
                    document.getElementById('charge-i-limit').textContent = data.charge_current_limit;
                    document.getElementById('discharge-i-limit').textContent = data.discharge_current_limit;
                    document.getElementById('real-time-cap').textContent = data.real_time_capacity;
                    document.getElementById('real-time-voltage').textContent = data.real_time_voltage;
                    document.getElementById('max-charge-i-limit').textContent = data.max_charge_current_limit;
                    document.getElementById('max-discharge-i-limit').textContent = data.max_discharge_current_limit;
                    document.getElementById('lithium-type').textContent = data.lithium_battery_type;

                    statusDot.className = 'status-indicator active';
                    statusText.textContent = 'Connected';

                    valueContainers.forEach(el => el.classList.remove('loading'));
                }

                else {
                    throw new Error(data.message || 'Unknown error');
                }
            }

            catch (error) {
                console.error('Error fetching data:', error);
                statusDot.className = 'status-indicator error';
                statusText.textContent = 'Error: ' + error.message;
            }
        }

        // Fetch immediately, then every 5 seconds
        fetchData();
        setInterval(fetchData, 5000);
    </script>
</body>

</html>