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 = "底架喷漆室";
                        break;
                    case 30019212:
                        name = "中涂喷漆室349";
                        break;
                    case 30019220:
                        name = "中涂喷漆室350";
                        break;
                    case 30019158:
                        name = "彩条喷漆室365";
                        break;
                    case 30019217:
                        name = "彩条喷漆室367";
                        break;
                    case 30019414:
                        name = "彩条喷漆室369";
                        break;
                    case 30019151:
                        name = "彩条喷漆室371";
                        break;
                    case 30019183:
                        name = "罩光喷漆室374";
                        break;
                    case 30019370:
                        name = "面漆喷漆室359";
                        break;
                    case 30018326:
                        name = "面漆喷漆室363";
                        break;
                    case 30019186:
                        name = "面漆喷漆室361";
                        break;
                    case 30019193:
                        name = "喷漆室-12";
                        break;
                    case 30019136:
                        name = "喷漆室-13";
                        break;
                    default:
                        name = "彩条喷漆室369";
                        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();
    }
}
