#ifndef DEVICE_CTRL_YTJ_BARRIER_H
#define DEVICE_CTRL_YTJ_BARRIER_H
#include <nlohmann/json.hpp>

#include "mqtt/mqtt_cli.h"

namespace zsy
{
    struct Body
    {
        uint32_t delay = 500;
        uint32_t value;
        uint32_t io;
    };

    struct Payload
    {
        std::string type = "gpio_out";
        Body body;
    };

    struct BarrierBcd
    {
        std::string id;
        std::string sn;
        std::string name = "gpio_out";
        std::string version = "1.0";
        long timestamp;
        Payload payload;
    };

    inline void to_json(nlohmann::json &j, const Body &b)
    {
        j = nlohmann::json{{"delay", b.delay}, {"value", b.value}, {"io", b.io}};
    }

    inline void to_json(nlohmann::json &j, const Payload &p)
    {
        j = nlohmann::json{{"type", p.type}, {"body", p.body}};
    }

    inline void to_json(nlohmann::json &j, const BarrierBcd &bb)
    {
        j = nlohmann::json{
            {"id", bb.id}, {"sn", bb.sn}, {"name", bb.name},
            {"version", bb.version}, {"timestamp", bb.timestamp},
            {"payload", bb.payload}
        };
    }

    class YtjBarrier
    {
        std::shared_ptr<MQTTCli> mqttCli;
        std::string devSn;
        uint32_t io;
        // 端口状态，0-->断，1-->通，2-->先通后断
        // uint32_t currentIoStatus;
    public:
        explicit YtjBarrier(const std::string &devSn, uint32_t io, const std::shared_ptr<MQTTCli> &mqttCli);

        void gpioOut(const std::string &id, uint32_t ioStatus) const;

        void gpioOutReply(const MQTTMsg &msg);
    };
} // zsy

#endif //DEVICE_CTRL_YTJ_BARRIER_H