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.utils.CollecUtils;
import net.vtstar.zhongtong.avi.gateway.painting.domain.SprayBoothMachine;
import net.vtstar.zhongtong.avi.gateway.painting.mapper.SprayBoothMapper;
import net.vtstar.zhongtong.avi.utils.EquipDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
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;

    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;

    @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<>();
                int deviceId = data.getDeviceId();
                String name = "";
                switch (deviceId) {
                    case 30019199:
                        name = "喷漆室-01";
                        break;
                    case 30019212:
                        name = "喷漆室-02";
                        break;
                    case 30019220:
                        name = "喷漆室-03";
                        break;
                    case 30019158:
                        name = "喷漆室-04";
                        break;
                    case 30019217:
                        name = "喷漆室-05";
                        break;
                    case 30019414:
                        name = "喷漆室-06";
                        break;
                    case 30019151:
                        name = "喷漆室-07";
                        break;
                    case 30019183:
                        name = "喷漆室-08";
                        break;
                    case 30019370:
                        name = "喷漆室-09";
                        break;
                    case 30018326:
                        name = "喷漆室-10";
                        break;
                    case 30019186:
                        name = "喷漆室-11";
                        break;
                    case 30019193:
                        name = "喷漆室-12";
                        break;
                    case 30019136:
                        name = "喷漆室-13";
                        break;
                    default:
                        break;

                }
                for (NodeData nd : data.getNodeList()) {
                    if (nd.getTem() == 0f) {
                        continue;
                    }
                    SprayBoothMachine sprayBoothMachine = new SprayBoothMachine();
                    sprayBoothMachine.setEquipCode(name);
                    sprayBoothMachine.setTemperature(nd.getTem());
                    sprayBoothMachine.setHumidity(nd.getHum());
                    sprayBoothMachine.setCreateTime(new Date());
                    list.add(sprayBoothMachine);
                }
                if(CollecUtils.isEmpty(list)){
                   return;
                }
                SprayBoothMachine sprayBoothMachine = list.get(0);
                String key = EquipDataUtil.getSprayValueKey(sprayBoothMachine.getEquipCode());
                Object value = valueOperations.get(key);
                if (value == null){
                    valueOperations.set(key, sprayBoothMachine.getCreateTime().getTime());
                }else{
                    long time = sprayBoothMachine.getCreateTime().getTime() - Long.valueOf(value.toString());
                    if (time > 5 * 60 * 1000){
                        mapper.insertList(list);
                        valueOperations.set(key, sprayBoothMachine.getCreateTime().getTime());
                    }
                }
            }
        });
        rsServer.start();
    }
}
