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

import java.util.Date;

/**
 * 离合油加注设备数据采集JOB
 */
@Slf4j
@Component
public class CentralizedFillingClutchMachineJob extends GatherJob {

    private static final String NAME = "CENTRALIZED_FILLING_CLUTCH";

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

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

    public boolean handleObject(EquipmentData equipmentData) {
        CentralizedFillingClutchMachine data = (CentralizedFillingClutchMachine) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (null == data.getFillingSettings() || null == data.getFillingAmount() || null == data.getPressureSettings()
                || null == data.getFillingPressure() || data.getFillingSettings() == 0 || data.getPressureSettings() ==0){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        if ("fillingPressure".equals(fieldName) || "pressureSettings".equals(fieldName)){
            return Integer.valueOf(value.toString()) / 10;
        }
        return value;
    }

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