package net.vtstar.zhongtong.avi.gateway.painting.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.painting.domain.SprayBoothStateMachine;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 喷涂室状态数据采集JOB
 */
@Slf4j
@Component
public class SprayBoothStateMachineJob extends GatherJob {

    private static final String NAME = "SPRAY_BOOTH";

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

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

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        SprayBoothStateMachine data = (SprayBoothStateMachine) equipmentData;
        Integer state = data.getStatus();
        if (state != null && state == 0) {
            return false;
        }
        Integer runState = data.getState();
        if (null == runState){
            return false;
        }
        if (runState == 1){
            equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        }else {
            equipmentData.setEquipStatus(EquipStatusEnum.SHUTDOWN);
        }
        return true;
    }

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

    @Override
    protected void postHandle(EquipmentData equipmentData) {
        ((SprayBoothStateMachine)equipmentData).setCreateTime(new Date());
        if (1 == ((SprayBoothStateMachine) equipmentData).getPfjError()){
            //todo:创建报警记录,排风机故障信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getQtxlError()){
            //todo:创建报警记录,气体泄露报警信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getRsjError()){
            //todo:创建报警记录,燃烧机故障信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getSfcwError()){
            //todo:创建报警记录,送风超温信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getSfjError()){
            //todo:创建报警记录,送风机故障信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getXhsgywError()){
            //todo:创建报警记录,循环水高液位信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getXhsjError()){
            //todo:创建报警记录,循环水泵故障信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getSfcsError()){
            //todo:创建报警记录,送风超湿信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getJsError()){
            //todo:创建报警记录,加湿故障信号
        }
        if (1 == ((SprayBoothStateMachine) equipmentData).getJtError()){
            //todo:创建报警记录,急停钮
        }

    }
}
