package vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.impl;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import rk.netDevice.sdk.p2.NodeData;
import rk.netDevice.sdk.p2.RealTimeData;
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
import vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.MonitorDataProcessor;
import vip.xiaonuo.coldchain.core.service.dataprocess.model.PowerEnum;
import vip.xiaonuo.coldchain.modular.monitordevice.enums.DeviceModelEnum;

import java.util.List;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/20 21:24:17
 */
@Slf4j
@Component(MonitorDataProcessor.RS_CO2WS_WIFI_2_5000P)
public class WifiCO2TempHumidityProcessor extends AbsRenkeMonitorDataProcessor {
    public WifiCO2TempHumidityProcessor(ApplicationEventPublisher applicationEventPublisher) {
        super(applicationEventPublisher);
    }

    @Override
    List<SensorData> transRealTimeData2SensorDatas(@NotNull RealTimeData data) {
        List<NodeData> nodeList = data.getNodeList();
        final int numNodes = nodeList.size();
        if (numNodes < 2) {
            log.error("数据节点数量不够，需要2个节点，但只有{}个", nodeList.size());
            return null;
        }
        // 最后一路的温度的值0=电源供电  100 电池供电
        final String plugInStatus = nodeList.get(numNodes - 1).getTem() < 1 ? PowerEnum.AC.getCode() : PowerEnum.DC.getCode();
        // 最后一路的湿度的值 * 10 表示CO2的含量
        Float hum = floatValue(nodeList.get(numNodes - 1).getHum());
        Float CO2 = hum == null ? null : hum * 10;
        NodeData nodeData = nodeList.get(0);
        SensorData sensorData = defaultSensorData(data);
        sensorData.setTemperature(floatValue(nodeData.getTem()));
        sensorData.setHumidity(floatValue(nodeData.getHum()));
        sensorData.setRoads(1);
        sensorData.setPlugInStatus(plugInStatus);
        // c02含量
        sensorData.setCo2(CO2);
        return Lists.newArrayList(sensorData);
    }

    @Override
    DeviceModelEnum deviceModel() {
        return DeviceModelEnum.WIFI_CO2_TEMP_HUMIDITY_TRANSCEIVER;
    }
}
