package net.vtstar.zhongtong.avi.gateway.stamping.job;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.Enum.EquipStatusEnum;
import net.vtstar.scada.base.redis.util.EquipDataUtil;
import net.vtstar.zhongtong.avi.equipment.service.EquipStatusRealTimeService;
import net.vtstar.zhongtong.avi.gateway.stamping.domain.LaserCuttingMachine;
import net.vtstar.zhongtong.avi.gateway.stamping.mapper.LaserCuttingMapper;
import net.vtstar.zhongtong.avi.gateway.stamping.utils.ClientSocketUtil;
import net.vtstar.zhongtong.avi.utils.PingIpUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 激光切管机数据采集JOB
 */
@Slf4j
@Component
public class LaserCuttingMachineJob extends QuartzJobBean {

    private String ipAddress1 = "10.1.25.177";
    private String ipAddress2 = "10.1.25.176";
    @Value("${zt.ip.pingtimes}")
    private int pingTimes;
    @Value("${zt.ip.timeout}")
    private int timeOut;

    @Autowired
    private EquipStatusRealTimeService realTimeService;
    @Autowired
    private LaserCuttingMapper mapper;
    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;
    @Resource(name = "redisTemplate")
    protected ListOperations listOperations;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<String> ipAddressList = Arrays.asList(ipAddress1, ipAddress2);
        for (String ipAddress : ipAddressList) {
            String equipCode = "";
            String equipName = "";
            String equipFactory = "";
            switch (ipAddress) {
                case "10.1.25.177":
                    equipCode = "076707-146";
                    equipName = "激光切管机01";
                    equipFactory = "03西北";
                    break;
                case "10.1.25.176":
                    equipCode = "076707-166";
                    equipName = "激光切管机02";
                    equipFactory = "03西南";
                    break;
            }
            LaserCuttingMachine machine = new LaserCuttingMachine();
            machine.setAlias(equipName);
            machine.setEquipCode(equipCode);
            machine.setCreateTime(new Date());
            machine.setStatus("1");
            machine.setBeginTime(new Date());
            machine.setEquipFactory(equipFactory);
            machine.setEquipType("LASER_CUTTING");
            if (!PingIpUtils.ping(ipAddress, pingTimes, timeOut)) {
                realTimeService.saveEquip("制件车间", equipCode, equipName, "关机");
                machine.setEquipStatus(EquipStatusEnum.SHUTDOWN);
                String key = EquipDataUtil.getStatusKey(machine.getAlias());
                listOperations.rightPush(key, machine);
                key = EquipDataUtil.getLastStatusKey(machine.getAlias());
                valueOperations.set(key, machine);
                continue;
            }
            realTimeService.saveEquip("制件车间", equipCode, equipName, "开机");
            //速度
            String cuttingSpeed = ClientSocketUtil.send(ipAddress, "P101");
            //功率
            String laserPower = ClientSocketUtil.send(ipAddress, "P102");
            //气体压力
            String gasPressure = ClientSocketUtil.send(ipAddress, "P107");
            if ("0".equals(cuttingSpeed) && "0".equals(laserPower) && "0".equals(gasPressure)){
                log.error("ip: {}能ping通, 但62944端口连接不上", ipAddress);
                continue;
            }
            machine.setCuttingSpeed(new BigDecimal(cuttingSpeed));
            machine.setGasPressure(new BigDecimal(gasPressure));
            machine.setLaserPower(new BigDecimal(laserPower));
            machine.setEquipStatus(EquipStatusEnum.RUN);
            mapper.insert(machine);
            String key = EquipDataUtil.getStatusKey(machine.getAlias());
            listOperations.rightPush(key, machine);
            key = EquipDataUtil.getLastStatusKey(machine.getAlias());
            valueOperations.set(key, machine);
        }
    }
}