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

import io.netty.util.internal.StringUtil;
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.DustingLineMachine;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 喷粉线数据采集JOB
 */
@Slf4j
@Component
public class DustingLineMachineJob extends GatherJob {

    private static final String NAME = "DUSTING_LINE";

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

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

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        //0待机，1是喷粉
        DustingLineMachine data = (DustingLineMachine) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (StringUtil.isNullOrEmpty(data.getState1()) && StringUtil.isNullOrEmpty(data.getState2())){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        if (fieldName.startsWith("state")){
            if (Integer.valueOf(value.toString()) == 0){
                return "待机";
            }
            if (Integer.valueOf(value.toString()) == 1){
                return "喷粉";
            }
        }
        return value;
    }

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