#include "ytj_barrier.h"

#include "common/utils/date_util.h"
#include "common/utils/json_util.h"

namespace zsy
{
    YtjBarrier::YtjBarrier(const std::string &devSn, const uint32_t io, const std::shared_ptr<MQTTCli> &mqttCli)
        : mqttCli(mqttCli), devSn(devSn), io(io)
    {
        mqttCli->subscribe("device/" + devSn + "/message/down/gpio_out/reply", [this](const MQTTMsg &msg)
        {
            gpioOutReply(msg);
        });
    }

    void YtjBarrier::gpioOut(const std::string &id, const uint32_t ioStatus) const
    {
        const long timestamp = static_cast<long>(DateUtil::timestamp<std::chrono::seconds>());
        const auto topic = "device/" + devSn + "/message/down/gpio_out";
        const auto data = JSON::toJsonStr(BarrierBcd{
            .id = id,
            .sn = devSn,
            .timestamp = timestamp,
            .payload = {
                .body = {
                    .value = ioStatus,
                    .io = io
                }
            },
        });

        mqttCli->publish(topic, data);
    }

    void YtjBarrier::gpioOutReply(const MQTTMsg &msg)
    {
        LOGGER_INFO("IO 输出回复：{}", msg.payload);
    }
} // zsy