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.ElectrophoresisRectificationMachine;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 电泳整流数据采集JOB
 */
@Slf4j
@Component
public class ElectrophoresisRectificationMachineJob extends GatherJob {

    private static final String NAME = "ELECTROPHORESIS_RECTIFICATION";

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

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

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        ElectrophoresisRectificationMachine data = (ElectrophoresisRectificationMachine) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (data.getVoltage1() == null || data.getVoltage2() == null){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

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

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