package net.vtstar.zhongtong.avi.gateway.painting.listener;

import com.jnrsmcu.sdk.netdevice.NodeData;
import com.jnrsmcu.sdk.netdevice.RSServer;
import com.jnrsmcu.sdk.netdevice.RealTimeData;
import net.vtstar.zhongtong.avi.gateway.painting.domain.SprayBoothMachine;
import net.vtstar.zhongtong.avi.gateway.painting.mapper.SprayBoothMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 温度传感器监听
 * @Author: xiadongwei
 * @Date: 2019/11/4 18:55
 */
//@Component
//@Order(99)
public class SprayBoothListener implements CommandLineRunner {

    @Autowired
    private SprayBoothMapper mapper;

    @Override
    public void run(String... args) throws Exception {
        // 初始化
        RSServer rsServer = RSServer.Initiate(2404);
        rsServer.addDataListener(new MyIDataListener(){
            @Override
            public void receiveRealtimeData(RealTimeData data){
                List<SprayBoothMachine> list = new ArrayList<>();
                for (NodeData nd : data.getNodeList()) {
                    //TODO: 根据设备地址设置设备编号
                    SprayBoothMachine sprayBoothMachine = new SprayBoothMachine();
                    sprayBoothMachine.setTemperature(nd.getTem());
                    sprayBoothMachine.setHumidity(nd.getHum());
                    sprayBoothMachine.setCreateTime(new Date());
                    list.add(sprayBoothMachine);
                }
                mapper.insertList(list);
            }
        });
        rsServer.start();
    }
}
