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.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_2000P)
public class WifiCO2TempHumidityProcessor2000P extends AbsRenkeMonitorDataProcessor {
    public WifiCO2TempHumidityProcessor2000P(ApplicationEventPublisher applicationEventPublisher) {
        super(applicationEventPublisher);
    }

    @Override
    List<SensorData> transRealTimeData2SensorDatas(@NotNull RealTimeData data) {
        List<NodeData> nodeList = data.getNodeList();
        List<SensorData> resultList = Lists.newArrayList();
        if (nodeList == null || nodeList.isEmpty()) {
            log.warn("节点数据为空，无法转换为传感器数据");
            return resultList;
        }
        for (NodeData nodeData : nodeList) {
            SensorData sensorData = createSensorData(data, nodeData, null, null);
            resultList.add(sensorData);
        }
        return resultList;
    }

    private SensorData createSensorData(RealTimeData data, NodeData nodeData, Float battery, String plugInStatus) {
        SensorData sensorData = defaultSensorData(data);
        sensorData.setTemperature(floatValue(nodeData.getTem()) != null ? floatValue(nodeData.getTem()) * 10 : null);
        sensorData.setHumidity(null);
        sensorData.setRoads(nodeData.getNodeId());
        sensorData.setBattery(battery);
        sensorData.setPlugInStatus(plugInStatus);
        return sensorData;
    }


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