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

import java.util.Date;

/**
 * 打磨室风机数据采集JOB
 */
@Slf4j
@Component
public class PolishingRoomStateJob extends GatherJob {

    private static final String NAME = "POLISHING_ROOM";

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

        super.gatherByModbus(NAME, PolishingRoomStateMachine.class);
    }

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        PolishingRoomStateMachine data = (PolishingRoomStateMachine) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (null == data.getState()){
            return false;
        }
        if(null != data.getState() && data.getState() == 1){
            equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        }else{
            equipmentData.setEquipStatus(EquipStatusEnum.ALARM);
        }

        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        Integer valueInt = Integer.valueOf(value.toString());
        return (valueInt & 0x01);
    }

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