// SPDX-License-Identifier: LGPL-3.0-or-later

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of libnymea-sunspec.
*
* libnymea-sunspec is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libnymea-sunspec is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libnymea-sunspec. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef SUNSPECMETERSINGLEPHASEMODEL_H
#define SUNSPECMETERSINGLEPHASEMODEL_H

#include <QObject>

#include "sunspecmodel.h"

class SunSpecConnection;

class SunSpecMeterSinglePhaseModel : public SunSpecModel
{
    Q_OBJECT
public:

    enum Evt {
        EvtPowerFailure = 0x4,
        EvtUnderVoltage = 0x8,
        EvtLowPf = 0x10,
        EvtOverCurrent = 0x20,
        EvtOverVoltage = 0x40,
        EvtMissingSensor = 0x80,
        EvtOem01 = 0x10000,
        EvtOem02 = 0x20000,
        EvtOem03 = 0x40000,
        EvtOem04 = 0x80000,
        EvtOem05 = 0x100000,
        EvtOem06 = 0x200000,
        EvtOem07 = 0x400000,
        EvtOem08 = 0x800000,
        EvtOem09 = 0x1000000,
        EvtOem10 = 0x2000000,
        EvtOem11 = 0x4000000,
        EvtOem12 = 0x8000000,
        EvtOem13 = 0x10000000,
        EvtOem14 = 0x20000000,
        EvtOem15 = 0x40000000
    };
    Q_DECLARE_FLAGS(EvtFlags, Evt)
    Q_FLAG(Evt)

    explicit SunSpecMeterSinglePhaseModel(SunSpecConnection *connection, quint16 modbusStartRegister, quint16 modelLength, SunSpecDataPoint::ByteOrder byteOrder, QObject *parent = nullptr);
    ~SunSpecMeterSinglePhaseModel() override; 

    QString name() const override;
    QString description() const override;
    QString label() const override;

    /* Total AC Current [A] */
    float amps() const;
    /* Phase A Current [A] */
    float ampsPhaseA() const;
    /* Phase B Current [A] */
    float ampsPhaseB() const;
    /* Phase C Current [A] */
    float ampsPhaseC() const;
    /* Current scale factor */
    qint16 a_SF() const;
    /* Line to Neutral AC Voltage (average of active phases) [V] */
    float voltageLn() const;
    /* Phase Voltage AN [V] */
    float phaseVoltageAn() const;
    /* Phase Voltage BN [V] */
    float phaseVoltageBn() const;
    /* Phase Voltage CN [V] */
    float phaseVoltageCn() const;
    /* Line to Line AC Voltage (average of active phases) [V] */
    float voltageLl() const;
    /* Phase Voltage AB [V] */
    float phaseVoltageAb() const;
    /* Phase Voltage BC [V] */
    float phaseVoltageBc() const;
    /* Phase Voltage CA [V] */
    float phaseVoltageCa() const;
    /* Voltage scale factor */
    qint16 v_SF() const;
    /* Frequency [Hz] */
    float hz() const;
    /* Frequency scale factor */
    qint16 hz_SF() const;
    /* Total Real Power [W] */
    float watts() const;
    /* Watts phase A [W] */
    float wattsPhaseA() const;
    /* Watts phase B [W] */
    float wattsPhaseB() const;
    /* Watts phase C [W] */
    float wattsPhaseC() const;
    /* Real Power scale factor */
    qint16 w_SF() const;
    /* AC Apparent Power [VA] */
    float va() const;
    /* VA phase A [VA] */
    float vaPhaseA() const;
    /* VA phase B [VA] */
    float vaPhaseB() const;
    /* VA phase C [VA] */
    float vaPhaseC() const;
    /* Apparent Power scale factor */
    qint16 vA_SF() const;
    /* Reactive Power [var] */
    float var() const;
    /* VAR phase A [var] */
    float varPhaseA() const;
    /* VAR phase B [var] */
    float varPhaseB() const;
    /* VAR phase C [var] */
    float varPhaseC() const;
    /* Reactive Power scale factor */
    qint16 vAR_SF() const;
    /* Power Factor [Pct] */
    float pf() const;
    /* PF phase A [Pct] */
    float pfPhaseA() const;
    /* PF phase B [Pct] */
    float pfPhaseB() const;
    /* PF phase C [Pct] */
    float pfPhaseC() const;
    /* Power Factor scale factor */
    qint16 pF_SF() const;
    /* Total Real Energy Exported [Wh] */
    quint32 totalWattHoursExported() const;
    /* Total Watt-hours Exported phase A [Wh] */
    quint32 totalWattHoursExportedPhaseA() const;
    /* Total Watt-hours Exported phase B [Wh] */
    quint32 totalWattHoursExportedPhaseB() const;
    /* Total Watt-hours Exported phase C [Wh] */
    quint32 totalWattHoursExportedPhaseC() const;
    /* Total Real Energy Imported [Wh] */
    quint32 totalWattHoursImported() const;
    /* Total Watt-hours Imported phase A [Wh] */
    quint32 totalWattHoursImportedPhaseA() const;
    /* Total Watt-hours Imported phase B [Wh] */
    quint32 totalWattHoursImportedPhaseB() const;
    /* Total Watt-hours Imported phase C [Wh] */
    quint32 totalWattHoursImportedPhaseC() const;
    /* Real Energy scale factor */
    qint16 totWh_SF() const;
    /* Total Apparent Energy Exported [VAh] */
    quint32 totalVaHoursExported() const;
    /* Total VA-hours Exported phase A [VAh] */
    quint32 totalVaHoursExportedPhaseA() const;
    /* Total VA-hours Exported phase B [VAh] */
    quint32 totalVaHoursExportedPhaseB() const;
    /* Total VA-hours Exported phase C [VAh] */
    quint32 totalVaHoursExportedPhaseC() const;
    /* Total Apparent Energy Imported [VAh] */
    quint32 totalVaHoursImported() const;
    /* Total VA-hours Imported phase A [VAh] */
    quint32 totalVaHoursImportedPhaseA() const;
    /* Total VA-hours Imported phase B [VAh] */
    quint32 totalVaHoursImportedPhaseB() const;
    /* Total VA-hours Imported phase C [VAh] */
    quint32 totalVaHoursImportedPhaseC() const;
    /* Apparent Energy scale factor */
    qint16 totVAh_SF() const;
    /* Total Reactive Energy Imported Quadrant 1 [varh] */
    quint32 totalVarHoursImportedQ1() const;
    /* Total VAr-hours Imported Q1 phase A [varh] */
    quint32 totalVArHoursImportedQ1PhaseA() const;
    /* Total VAr-hours Imported Q1 phase B [varh] */
    quint32 totalVArHoursImportedQ1PhaseB() const;
    /* Total VAr-hours Imported Q1 phase C [varh] */
    quint32 totalVArHoursImportedQ1PhaseC() const;
    /* Total Reactive Power Imported Quadrant 2 [varh] */
    quint32 totalVArHoursImportedQ2() const;
    /* Total VAr-hours Imported Q2 phase A [varh] */
    quint32 totalVArHoursImportedQ2PhaseA() const;
    /* Total VAr-hours Imported Q2 phase B [varh] */
    quint32 totalVArHoursImportedQ2PhaseB() const;
    /* Total VAr-hours Imported Q2 phase C [varh] */
    quint32 totalVArHoursImportedQ2PhaseC() const;
    /* Total Reactive Power Exported Quadrant 3 [varh] */
    quint32 totalVArHoursExportedQ3() const;
    /* Total VAr-hours Exported Q3 phase A [varh] */
    quint32 totalVArHoursExportedQ3PhaseA() const;
    /* Total VAr-hours Exported Q3 phase B [varh] */
    quint32 totalVArHoursExportedQ3PhaseB() const;
    /* Total VAr-hours Exported Q3 phase C [varh] */
    quint32 totalVArHoursExportedQ3PhaseC() const;
    /* Total Reactive Power Exported Quadrant 4 [varh] */
    quint32 totalVArHoursExportedQ4() const;
    /* Total VAr-hours Exported Q4 Imported phase A [varh] */
    quint32 totalVArHoursExportedQ4ImportedPhaseA() const;
    /* Total VAr-hours Exported Q4 Imported phase B [varh] */
    quint32 totalVArHoursExportedQ4ImportedPhaseB() const;
    /* Total VAr-hours Exported Q4 Imported phase C [varh] */
    quint32 totalVArHoursExportedQ4ImportedPhaseC() const;
    /* Reactive Energy scale factor */
    qint16 totVArh_SF() const;
    /* Meter Event Flags */
    EvtFlags events() const;

protected:
    quint16 m_fixedBlockLength = 105;

    void initDataPoints();
    void processBlockData() override;

private:
    float m_amps = 0;
    float m_ampsPhaseA = 0;
    float m_ampsPhaseB = 0;
    float m_ampsPhaseC = 0;
    qint16 m_a_SF = 0;
    float m_voltageLn = 0;
    float m_phaseVoltageAn = 0;
    float m_phaseVoltageBn = 0;
    float m_phaseVoltageCn = 0;
    float m_voltageLl = 0;
    float m_phaseVoltageAb = 0;
    float m_phaseVoltageBc = 0;
    float m_phaseVoltageCa = 0;
    qint16 m_v_SF = 0;
    float m_hz = 0;
    qint16 m_hz_SF = 0;
    float m_watts = 0;
    float m_wattsPhaseA = 0;
    float m_wattsPhaseB = 0;
    float m_wattsPhaseC = 0;
    qint16 m_w_SF = 0;
    float m_va = 0;
    float m_vaPhaseA = 0;
    float m_vaPhaseB = 0;
    float m_vaPhaseC = 0;
    qint16 m_vA_SF = 0;
    float m_var = 0;
    float m_varPhaseA = 0;
    float m_varPhaseB = 0;
    float m_varPhaseC = 0;
    qint16 m_vAR_SF = 0;
    float m_pf = 0;
    float m_pfPhaseA = 0;
    float m_pfPhaseB = 0;
    float m_pfPhaseC = 0;
    qint16 m_pF_SF = 0;
    quint32 m_totalWattHoursExported = 0;
    quint32 m_totalWattHoursExportedPhaseA = 0;
    quint32 m_totalWattHoursExportedPhaseB = 0;
    quint32 m_totalWattHoursExportedPhaseC = 0;
    quint32 m_totalWattHoursImported = 0;
    quint32 m_totalWattHoursImportedPhaseA = 0;
    quint32 m_totalWattHoursImportedPhaseB = 0;
    quint32 m_totalWattHoursImportedPhaseC = 0;
    qint16 m_totWh_SF = 0;
    quint32 m_totalVaHoursExported = 0;
    quint32 m_totalVaHoursExportedPhaseA = 0;
    quint32 m_totalVaHoursExportedPhaseB = 0;
    quint32 m_totalVaHoursExportedPhaseC = 0;
    quint32 m_totalVaHoursImported = 0;
    quint32 m_totalVaHoursImportedPhaseA = 0;
    quint32 m_totalVaHoursImportedPhaseB = 0;
    quint32 m_totalVaHoursImportedPhaseC = 0;
    qint16 m_totVAh_SF = 0;
    quint32 m_totalVarHoursImportedQ1 = 0;
    quint32 m_totalVArHoursImportedQ1PhaseA = 0;
    quint32 m_totalVArHoursImportedQ1PhaseB = 0;
    quint32 m_totalVArHoursImportedQ1PhaseC = 0;
    quint32 m_totalVArHoursImportedQ2 = 0;
    quint32 m_totalVArHoursImportedQ2PhaseA = 0;
    quint32 m_totalVArHoursImportedQ2PhaseB = 0;
    quint32 m_totalVArHoursImportedQ2PhaseC = 0;
    quint32 m_totalVArHoursExportedQ3 = 0;
    quint32 m_totalVArHoursExportedQ3PhaseA = 0;
    quint32 m_totalVArHoursExportedQ3PhaseB = 0;
    quint32 m_totalVArHoursExportedQ3PhaseC = 0;
    quint32 m_totalVArHoursExportedQ4 = 0;
    quint32 m_totalVArHoursExportedQ4ImportedPhaseA = 0;
    quint32 m_totalVArHoursExportedQ4ImportedPhaseB = 0;
    quint32 m_totalVArHoursExportedQ4ImportedPhaseC = 0;
    qint16 m_totVArh_SF = 0;
    EvtFlags m_events;


};

QDebug operator<<(QDebug debug, SunSpecMeterSinglePhaseModel *model);

#endif // SUNSPECMETERSINGLEPHASEMODEL_H
