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.LaserCutting3DMachine;
import net.vtstar.zhongtong.avi.gateway.stamping.mapper.LaserCutting3DMapper;
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;

/**
 * 3维激光切割机数据采集JOB
 */
@Slf4j
@Component
public class LaserCutting3DMachineJob extends QuartzJobBean {

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

    @Autowired
    private EquipStatusRealTimeService realTimeService;
    @Autowired
    private LaserCutting3DMapper laserCutting3DMapper;

    @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);
        for (String ipAddress : ipAddressList) {
            String equipCode = "";
            String equipName = "";
            String equipFactory = "";
            switch (ipAddress) {
                case "10.1.25.175":
                    equipCode = "076707-105";
                    equipName = "三维激光切割机";
                    equipFactory = "01跨东南";
                    break;
            }
            LaserCutting3DMachine machine = new LaserCutting3DMachine();
            machine.setAlias(equipName);
            machine.setEquipCode(equipCode);
            machine.setCreateTime(new Date());
            machine.setStatus("1");
            machine.setBeginTime(new Date());
            machine.setEquipFactory(equipFactory);
            machine.setEquipType("3D_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);
            laserCutting3DMapper.insert(machine);
            String key = EquipDataUtil.getStatusKey(machine.getAlias());
            listOperations.rightPush(key, machine);
            key = EquipDataUtil.getLastStatusKey(machine.getAlias());
            valueOperations.set(key, machine);
        }
    }
}