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

import java.util.Date;

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

    private static final String NAME = "VOC";

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

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

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

    @Override
    public Object handleValue(String fieldName, Object value) {
        if (null == value){
            return null;
        }
        if (fieldName.startsWith("concentration")){
            Long aLong = Long.valueOf(value.toString());
            String longStr = Long.toHexString(aLong);
            if (longStr.startsWith("4")){
                return null;
            }else{
                return aLong / 1000;
            }
        }
        return value;
    }

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