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

import java.util.Date;

/**
 * 前处理数据采集JOB
 */
@Slf4j
@Component
public class PretreamentMachineJob extends GatherJob {

    private static final String NAME = "PRETREATMENT";

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

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

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

    @Override
    public Object handleValue(String fieldName, Object value) {
        if (fieldName.endsWith("Temperature")){
            return String.valueOf(Long.valueOf(value.toString()) / 10f);
        }
        if (fieldName.endsWith("Time")){
            return String.valueOf(Integer.valueOf(value.toString()) / 10);
        }

        return value;
    }

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