package vip.xiaonuo.coldchain.core.renke.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import rk.netDevice.sdk.p2.*;
import vip.xiaonuo.coldchain.core.service.dataprocess.handler.impl.RenKeColdChainDataHandler;
import vip.xiaonuo.coldchain.core.service.dataprocess.model.RenKeColdChainMessageData;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;

/**
 * 默认的 RK 数据监听器，用于处理设备通信中的各种事件。
 * 包括校时应答、遥控指令应答、存储数据接收、实时数据接收等。
 * 提供统一的默认日志输出，方便调试和监控。
 *
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @date 2024/11/1 15:06:18
 */
@Slf4j
public class JfcloudColdChainRenKeDefaultDataListener implements IDataListener {
    private final RenKeColdChainDataHandler renKeColdChainDataHandler;
    private final MonitorDeviceService monitorDeviceService;

    public JfcloudColdChainRenKeDefaultDataListener(RenKeColdChainDataHandler renKeColdChainDataHandler, MonitorDeviceService monitorDeviceService) {
        this.renKeColdChainDataHandler = renKeColdChainDataHandler;
        this.monitorDeviceService = monitorDeviceService;
    }

    @Override
    public void receiveTimmingAck(TimmingAck data) {
        log.info("================================================================");
        log.info("接收到校时应答: 设备ID = {}", data.getDeviceId());
        log.info("*************************************************************************");
    }

    @Override
    public void receiveTelecontrolAck(TelecontrolAck data) {
        log.info("================================================================");
        log.info("接收到遥控指令应答: 设备ID = {}", data.getDeviceId());
        log.info("*************************************************************************");
    }

    @Override
    public void receiveStoreData(StoreData data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到存储数据: 设备ID = {}", data.getDeviceId());
        data.getNodeList().forEach(nodeData -> log.info("记录ID: {}, 记录时间: {}, 温度: {}, 湿度: {}", nodeData.getNodeId(), nodeData.getRecordTime(), nodeData.getTem(), nodeData.getHum()));
        log.info("*************************************************************************");
        RenKeColdChainMessageData renKeColdChainMessageData = new RenKeColdChainMessageData();
        renKeColdChainMessageData.setStoreData(data);
        renKeColdChainDataHandler.handleStoreData(renKeColdChainMessageData);
    }

    @Override
    public void receiveRealtimeData(RealTimeData data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到实时数据: 设备ID = {},时间: {}", data.getDeviceId(), DateUtil.now());
        data.getNodeList().forEach(nodeData -> {
            log.info("记录ID: {}, 记录时间: {}, 温度: {}°C, 湿度: {}%", nodeData.getNodeId(), nodeData.getRecordTime(), nodeData.getTem(), nodeData.getHum());
        });
        log.info("*************************************************************************");

        RenKeColdChainMessageData renKeColdChainMessageData = new RenKeColdChainMessageData();
        renKeColdChainMessageData.setRealTimeData(data);
        renKeColdChainDataHandler.handleRealTimeData(renKeColdChainMessageData);
    }

    @Override
    public void receiveLoginData(LoginData data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到设备登录信息: 设备ID = {}\n{}", data.getDeviceId(), JSONUtil.toJsonStr(data));
        log.info("*************************************************************************");
        RenKeColdChainMessageData renKeColdChainMessageData = new RenKeColdChainMessageData();
        renKeColdChainMessageData.setDeviceId(data.getDeviceId());
        renKeColdChainDataHandler.login(renKeColdChainMessageData);
    }

    @Override
    public void receiveParamIds(ParamIdsData data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到设备参数【编号】信息{}", JSONUtil.toJsonStr(data));
        log.info("*************************************************************************");
        // 更新设备编号参数
        monitorDeviceService.updateParamIds(data);
    }

    @Override
    public void receiveParam(ParamData data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到设备参数【配置项目】信息: {}", JSONUtil.toJsonStr(data));
        log.info("*************************************************************************");
        // 更新设备参数
        monitorDeviceService.updateParameters(data);
    }

    @Override
    public void receiveWriteParamAck(WriteParamAck data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到参数下载应答:{}", JSONUtil.toJsonStr(data));
        log.info("*************************************************************************");
    }

    @Override
    public void receiveTransDataAck(TransDataAck data) {
        log.info("======================== 设备ID: {} ========================================", data.getDeviceId());
        log.info("接收到透传数据应答:{}", JSONUtil.toJsonStr(data));
        log.info("*************************************************************************");
    }

    @Override
    public void receiveHeartbeatData(HeartbeatData heartbeatData) {
        log.info("======================== 设备ID: {} ========================================", heartbeatData.getDeviceId());
        log.info("接收到心跳包: 设备ID = {}", JSONUtil.toJsonStr(heartbeatData));
        log.info("*************************************************************************");
        RenKeColdChainMessageData renKeColdChainMessageData = new RenKeColdChainMessageData();
        renKeColdChainMessageData.setDeviceId(heartbeatData.getDeviceId());
        renKeColdChainDataHandler.login(renKeColdChainMessageData);
    }
}
