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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.protocol.modbus.tcp.netty.utils.NettyUtils;
import net.vtstar.protocol.modbus.tcp.netty.utils.PoolKey;
import net.vtstar.scada.base.global.service.ModbusService;
import net.vtstar.zhongtong.avi.gateway.parts.domain.WeldingRobotMachine;
import net.vtstar.zhongtong.avi.gateway.parts.mapper.WeldingRobotMapper;
import net.vtstar.zhongtong.avi.utils.PingIpUtils;
import org.quartz.DisallowConcurrentExecution;
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.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 制件焊接机器人数据采集JOB
 */
@Slf4j
@Component
@DisallowConcurrentExecution
public class WeldingRobotMachineJob extends QuartzJobBean {

    @Value("${zt.ip.weldingrobot1}")
    private String ipAddress1;
    @Value("${zt.ip.weldingrobot2}")
    private String ipAddress2;
    @Value("${zt.ip.weldingrobot3}")
    private String ipAddress3;
    @Value("${zt.ip.weldingrobot4}")
    private String ipAddress4;
    @Value("${zt.ip.pingtimes}")
    private int pingTimes;
    @Value("${zt.ip.timeout}")
    private int timeOut;

    @Autowired
    private ModbusService service;

    @Autowired
    private WeldingRobotMapper mapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<String> ipAddressList = Arrays.asList(ipAddress1, ipAddress2, ipAddress3, ipAddress4);
        for (String ipAddress : ipAddressList) {
            if (!PingIpUtils.ping(ipAddress, pingTimes, timeOut)) {
                log.error("ping: {}失败, 读取焊接机器人数据操作结束!", ipAddress);
                continue;
            }
            //1.获得当天日期,2.通过日期去查询当天的开机时间和焊接时间
            Calendar calendar = Calendar.getInstance();
            int date = calendar.get(Calendar.DATE);
            PoolKey poolKey = NettyUtils.wrapPoolKey(ipAddress, 502);
            try {
                Object second = service.getSingleValue(poolKey, 1, date * 4 - 4, "Integer");
                Object minute = service.getSingleValue(poolKey, 1, date * 4 - 3, "Integer");
                Object hour = service.getSingleValue(poolKey, 1, date * 4 - 2, "Integer");
                if (null == second || null == minute || null == hour){
                    log.error("读取焊接机器人数据失败! 时, 分, 秒中有空值!");
                    continue;
                }
                String turnOnTime = Integer.valueOf(hour.toString()) + "小时" + Integer.valueOf(minute.toString()) + "分" +
                        Integer.valueOf(second.toString()) + "秒";
                Object secondWelding = service.getSingleValue(poolKey, 1, date * 4 - 4 + 300, "Integer");
                Object minuteWelding = service.getSingleValue(poolKey, 1, date * 4 - 3 + 300, "Integer");
                Object hourWelding = service.getSingleValue(poolKey, 1, date * 4 - 2 + 300, "Integer");
                if (null == minuteWelding || null == secondWelding || null == hourWelding){
                    log.error("读取焊接机器人数据失败! 时, 分, 秒中有空值!");
                    continue;
                }
                String weldingTime = Integer.valueOf(hourWelding.toString()) + "小时" + Integer.valueOf(minuteWelding.toString()) + "分" +
                        Integer.valueOf(secondWelding.toString()) + "秒";
                WeldingRobotMachine machine = new WeldingRobotMachine();
                machine.setEquipCode("焊接机器人" + ipAddress.split("\\.")[3]);
                machine.setTurnOnTime(turnOnTime);
                machine.setWeldingTime(weldingTime);
                machine.setCreateTime(new Date());
                mapper.insert(machine);
            } catch (Exception e) {
                log.error("读取焊接机器人数据失败! e : {}", e.getMessage());
                return;
            }
        }

    }
}
