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

import java.util.Date;

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

    private static final String NAME = "PLC_CONTROL_TIRE_2";

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

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

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        PlcControlTireMachine2 data = (PlcControlTireMachine2) equipmentData;
//        Integer state = data.getStatus();
        Integer state = 1;
        if (state != null && state == 0) {
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

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

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

}
