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

#ifndef INC_1_LIBDEVCPP_QGACYPHERPLUGINENTRY_H
#define INC_1_LIBDEVCPP_QGACYPHERPLUGINENTRY_H

#include <jebase/config.h>
#include "../QGADataPool.h"

namespace jedge {

    /**
     * 处理加密函数的插件：
     *      获得 mqtt加密、 mqtt解密、 登录加密等函数的入口，并可获得一些额外的标准函数接口。
     */

    class JE_API QGaCypherPluginEntry :
        public qlibc::QPluginEntry {
    protected:
        QGADataPool *data_pool_ = nullptr;
        std::string sn_;

    public:
        explicit QGaCypherPluginEntry(std::string name, std::string type = "cypher", int ver = 0) :
                qlibc::QPluginEntry(std::move(name), std::move(type), ver) {
        }

        virtual QGaCypherPluginEntry* duplication(const std::string& name) {
            auto* dup = new QGaCypherPluginEntry(name, typeKey_, ver_);
            dup->sn_ = sn_;
            return dup;
        }

        ~QGaCypherPluginEntry() override {}

        virtual void setDataPool(QGADataPool &pool);

        virtual void prepareSiteMqttTopics(QStringList &topics) {  };

        virtual void resyncMqttStatus() {}

        virtual void handleMqttOfflineEvent() {}

        virtual bool decodeMqttMessage(const string &topic, void *payload, int payloadLen, char *buffer, int *len) {
            return false;   //未加密消息
        }

        virtual bool encryptMqttMessage(const std::string &payload, std::string &encryptOut) {
            return false;
        }

        virtual void makeMqttHeartBeat();

        virtual void prepareMqttParams(qlibc::QData &config, string &username, string &password, string &clientId);

        virtual bool handleMqttMessage(const string &topic, qlibc::QData &msg);

        bool handleMgbusMessage(qlibc::QData &req, qlibc::QData *rsp);

        virtual void buildSiteData(qlibc::QData &data) {}

        virtual void clearSiteData(qlibc::QData &data) {}
    };

#define bindDeliverGaoPluginHandler(_handler)  data_pool_->addUserMgbusDeliverGAO(#_handler, \
            [this](QGAChannelHolder& pool, qlibc::QData *req, qlibc::QData *resp)->bool { \
                return this->_handler(pool,req,resp);\
            })

#define bindDeliverGaoPluginHandlerFunc(_handler)  data_pool_->addUserMgbusDeliverGAO(#_handler, \
            [](QGAChannelHolder& pool, qlibc::QData *req, qlibc::QData *resp)->bool { \
                return _handler(pool,req,resp);\
            })

#define bindUserDefPluginHandler(area, _handler)  data_pool_->addUserDefineGAO(area, #_handler, \
            [this](QGAChannelHolder& pool, qlibc::QData *req, qlibc::QData *resp)->bool { \
                return this->_handler(pool,req,resp);\
            })

#define bindUserDefPluginHandlerFunc(area, _handler)  data_pool_->addUserDefineGAO(area, #_handler, \
            [](QGAChannelPool& pool, qlibc::QData *req, qlibc::QData *resp)->bool { \
                return _handler(pool,req,resp);\
            })
}

#endif //INC_1_LIBDEVCPP_QGACYPHERPLUGINENTRY_H
