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

import java.util.Date;

/**
 * PLC数控合装胎数据采集JOB
 */
@Slf4j
@Component
public class PlcControlTireMachine1Job extends GatherJob {

    private static final String NAME = "PLC_CONTROL_TIRE_1";

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

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

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        PlcControlTireMachine1 data = (PlcControlTireMachine1) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (null == data.getTopBusWidth() || null == data.getTopBusHeight()){
            return false;
        }
        if (data.getTopBusWidth() == 1 && data.getTopBusHeight() == 1){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        if (!"state".equals(fieldName)){
            return Float.valueOf(value.toString()) / 100f;
        }
        return value;
    }

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