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:25:30
 */
@Slf4j
@Component(MonitorDataProcessor.RS_WS_WIFI5_C3_Y2)
public class WifiLowTempRecorderProcessorBattery2 extends AbsRenkeMonitorDataProcessor {
    public WifiLowTempRecorderProcessorBattery2(ApplicationEventPublisher applicationEventPublisher) {
        super(applicationEventPublisher);
    }

    List<SensorData> transRealTimeData2SensorDatas(@NotNull RealTimeData data) {
        List<NodeData> nodeList = data.getNodeList();
        final int numNodes = nodeList.size();
        if (numNodes < 3) {
            log.error("数据节点数量不够，需要2个节点，但只有{}个", nodeList.size());
            return null;
        }
        List<SensorData> rlt = Lists.newArrayList();
        // 倒数第二路的温度的值  是电池的电量
        final float battery = nodeList.get(numNodes - 2).getTem();
        // 最后一路的温度的值0=电源供电  100 电池供电
        final String plugInStatus = nodeList.get(numNodes - 1).getTem() < 1 ? PowerEnum.AC.getCode() : PowerEnum.DC.getCode();
        //前面两路的温湿度
        for (int i = 0; i < 2; i++) {
            NodeData nodeData = nodeList.get(i);
            SensorData sensorData = defaultSensorData(data);
            sensorData.setTemperature(floatValue(nodeData.getTem()));
            sensorData.setHumidity(floatValue(nodeData.getHum()));
            //设置路数
            sensorData.setRoads(nodeData.getNodeId());
            sensorData.setBattery(battery);//第三路温度表示电量
            sensorData.setPlugInStatus(plugInStatus);//第三路温度表示电量
            rlt.add(sensorData);
        }
        return rlt;
    }

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