#ifndef CCHALETDEVICE_H
#define CCHALETDEVICE_H
#include <QByteArray>
#include <QObject>
#include <QSerialPort>
#include "NetworkProtocol.h"
#include "NetworkDevice.h"
#include <QTimer>
#include "ChaletData.h"
#include "ChaletNetworkMessage.h"
#include <QList>
#include "BlynkCloudClient.h"
#include "ChaletDataLogger.h"
#include "ChaletUbidotsInterface.h"
#include "ThingsBoardInterface.h"
#include "ChaletModbusServer.h"
#include "ModbusRepository.h"
#include "ChaletHomeServerInterface.h"


#define LORA_MAGIC_WORD 0xBAADCAFE
#define LORA_NORMAL_REQUEST_TIMEOUT 3000
#define LORA_IMMEDIATE_REQUEST_TIMEOUT 300
#define LORA_RETRY_REQUEST_TIMEOUT 2000
#define LORA_RESPONSE_TIME_TIMEOUT 3000

#define LORA_CHALET_STATUS_POWER_RELAY_MASK 0x01
#define LORA_CHALET_STATUS_CUR_SENSOR_MASK 0x02

class CChaletDevice : public QObject, public CNetworkDevice
{
    Q_OBJECT

public:
    CChaletDevice(int Address,CAbstractNetworkCommIF *NetworkInterface);
    ~CChaletDevice();

    virtual int NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data);

    QString mChaletName;
    QTimer *mChaletStatusTimer;
    int Start();

    CChaletMainStatus GetChaletMainStatus() {return mChaletMainStatus;}
    QByteArray GetDeviceWiFiStoredParams() {return mDeviceWiFiParameters;}
    QByteArray GetDeviceFirmwareVersion() {return mDeviceFirmwareVersion;}
    CBlynkCloudClient mBlynkInterface;
    CThingsBoardInterface mThingsBoardInterface;
    CChaletModbusServer *mChaletModbusServer;
    CModbusRepository mChaletModbusRepo;

    CChaletMainStatus mChaletMainStatus;
    QList<CChaletNetworkMessage> mPendingNetworkMsgList;
    CChaletDataLogger mChaletDataLogger;
    CChaletJFUbidotsInterface *mJFUbidotsInterface;
    CChaletHomeServerInterface *mHomeServerInterface;

    unsigned long long int mNbLostRequest, mTotalNbRequest;
    float mLostRequestPercentage;

    CChaletDataLogger *GetDataLoggerHandle() {return &mChaletDataLogger;}   //for Interface use...

    QByteArray mDeviceWiFiParameters;
    QByteArray mDeviceFirmwareVersion;



    int SendWiFiModuleSetState(bool State);
    int SendInverterPowerRelayState(bool State);
    int SendDOHarakiri();
    int SendRebootCmd();
    int SendChaletCommand(int CmdID,int DataSize, QByteArray Data);
    int ResetCommStats();
    int ComputeCommStats();
    int SendGetWifiParams();
    int SendSetWifiParams(QByteArray Data);
    int SendGetFirmwareVersion();
    int SendClearCommStatsRequest();
    int SendGetWifiStatusRequest();

    int CmdResponseReceived(int CmdID);
    int ScheduleChaletStatusRequest();
    int ScheduleChaletCommand(int CmdID, int DataSize, QByteArray Data);
    int ScheduleChaletCommand(int CmdID,QByteArray Data);

    bool IsInverterON(){return mChaletMainStatus.mInverterRelayStatus == 1;}

signals:
    void DeviceWifiStoredParamsReceived();
    void DeviceWifiSetParamsResult(char);
    void DeviceFirmwareVersionReceived();
    void DeviceWifiStatusReceived(QByteArray);

public slots:
    void CommTimerExpired();

};

#endif // CCHALETDEVICE_H
