//
// Created by Ht on 2021/9/26.
//

#ifndef INC_0_LIBDEVCPP_CPAD_MGBUSDEBUGSERVICE_H
#define INC_0_LIBDEVCPP_CPAD_MGBUSDEBUGSERVICE_H

#include <src/service/MgbusServerService.h>
#include <mgbus/MgbusNode.h>

namespace jedge {

    class MgbusDebugService :
            public MgbusServerService {

    protected:
        qlibc::QSharedObjectHolder<MgNode>  dbg_nodes_;
        qlibc::QSharedObjectHolder<QStringList> watch_points_;   //记录特定Key的clientKey

        bool                           monitoring_:1;
        bool                           debug_log_:1;

    public:
        explicit MgbusDebugService(MgBusServer &opr, const std::string &name, const qlibc::QData& config):
                MgbusServerService(opr, name, config), monitoring_(true), debug_log_(false) {
        }

        ~MgbusDebugService() override = default;

        void onServiceStop() override;

        void onServiceStart(const qlibc::QData &config) override;

        inline void iterateNode(const qlibc::QSharedObjectHolder<MgNode>::SharedObjAccessor& accessor) {
            dbg_nodes_.invokeOnAllObject(accessor, true);
        }

        void postMessageDebugInfo(qlibc::QData &msg, bool ok = true);

        bool postDebugLogMessage(qlibc::QData &msg);

        bool addDebugLogWatchPoint(const std::string &target_module, const std::string &dbg_client, qlibc::QData& r);

        void clearDebugLogWatchPoint(const std::string& modKey, const std::string& watchTarget = "");

        void preparePattern() override;

        void releaseNodes() override;

        inline bool isDebugLogging () {
            return debug_log_;
        }

        std::shared_ptr<MgNode> findNode(const std::string &nodeKey) override;

        void removeNode(const std::string &nodeKey) override;

        void addNode(const std::string &nodeKey, MgNodeRef& node) override;

        bool reIndexNode(const string &nodeKey, const string &newKey) override;

        void startMonitoring();

        void stopMonitoring();

        bool existsNode(const std::string &nodeKey) override;

        //service命令响应接口
        bool mgkick(qlibc::QData &msg, qlibc::QData *rsp);
        bool mgls(qlibc::QData &m, qlibc::QData *r);
        bool mgListSn(qlibc::QData &m, qlibc::QData *r);
        bool mgReboot(qlibc::QData &m, qlibc::QData *r);
    };
}



#endif //INC_0_LIBDEVCPP_CPAD_MGBUSDEBUGSERVICE_H
