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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.Enum.EquipStatusEnum;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.scada.base.gather.job.GatherJob;
import net.vtstar.zhongtong.avi.gateway.parts.domain.SteelCncCuttingMachine;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 型钢下料数据采集JOB
 */
@Slf4j
@Component
public class SteelCncCuttingMachineJob extends GatherJob {

    private static final String NAME = "STEEL_CNC_CUTTING";

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        log.info("JobName: {}", context.getJobDetail().getKey().getName());
        super.gatherByModbus(NAME, SteelCncCuttingMachine.class);
    }

    @Override
    public boolean handleObject(EquipmentData equipmentData) {

        SteelCncCuttingMachine data = (SteelCncCuttingMachine) equipmentData;
        Integer state = data.getState();
        if (state == null) {
            return false;
        }
        switch (state) {
            case 1:
                equipmentData.setEquipStatus(EquipStatusEnum.RUN);
                data.setStateString("运行");
                break;
            case 2:
                equipmentData.setEquipStatus(EquipStatusEnum.SHUTDOWN);
                data.setStateString("关机");
                break;
            case 4:
                equipmentData.setEquipStatus(EquipStatusEnum.ALARM);
                data.setStateString("报警");
                break;
            case 3:
            case 5:
            case 6:
                equipmentData.setEquipStatus(EquipStatusEnum.IDLE);
                data.setStateString("待机");
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        return value;
    }

    @Override
    protected void postHandle(EquipmentData equipmentData) {
        ((SteelCncCuttingMachine) equipmentData).setCreateTime(new Date());
    }
}
