//
// Created by Ht on 2022/8/27.
//

#ifndef INC_1_LIBDEVCPP_QJAGAFFLICSITE_H
#define INC_1_LIBDEVCPP_QJAGAFFLICSITE_H

#include <qlibc/qlibc.h>
#include <jadom2/JABaseDom.h>
#include <exlib/QJAServer.h>
#include <exlib/QJAService.h>
#include <exlib/QMqttClient.h>
#include <exlib/http/QHttpSSLClient.h>
#include "QGADataPool.h"
#include "plugin/QGaCypherPluginEntry.h"

namespace jedge {

    static const char* sval_cls_name_gaffic_site_ = "gafSite";

    class JE_API QJAGafflicSite : public QGADataPool {
    protected:
        QJAMgServer &opr_;
        QJAMgService &service_;

        QGaCypherPluginEntry* site_plugin_entry_;

    public:

        explicit QJAGafflicSite(QJAMgServer &server, QJAMgService &service, qlibc::QData& siteCfg, const std::string& name, std::string  basePath);

        ~QJAGafflicSite();

        void handleMqttOfflineEvent(QData &data) override;

        void prepareMqttParams(QData &cfg, std::string &username, std::string &password, std::string &clientId) override;

        bool handleMgbusMessage(QData &req, QData *rsp) override;

        void buildSiteData() override;
        void clearSiteData() override;

        std::string showSiteInfo();

        void getSiteInfo(qlibc::QData &data);

    protected:
        void reprepareMqttTopics(const qlibc::QData& data) override;

        void resyncMqttStatus() override;

        bool decodeMqttMessage(const std::string &topic, void *payload, int payloadLen, char *buffer, int *len) override;

        bool encryptMqttMessage(const std::string& payload, std::string& encryptOut) override;

        bool handleMqttMessage(const std::string &topic, QData &message) override;

        void mqttHeartBeat() override;

    };
}


#endif //INC_1_LIBDEVCPP_QJAGAFFLICSITE_H
