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

import java.util.Date;

/**
 * 试交车间充电桩设备数据采集JOB
 */
@Slf4j
@Component
public class ChargingPileMachineJob extends GatherJob {

    private static final String NAME = "CHARGING_PILE";

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

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

    public boolean handleObject(EquipmentData equipmentData) {
        ChargingPileMachine data = (ChargingPileMachine) equipmentData;
        Integer state = data.getStatus();
        if (state != null && state == 0) {
            return false;
        }
        if (null == ((ChargingPileMachine) equipmentData).getBusCode()
                || "".equals(((ChargingPileMachine) equipmentData).getBusCode())){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        if ("outputVoltage".equals(fieldName) || "outputCurrent".equals(fieldName)){
            return Integer.valueOf(value.toString()) / 10f;
        }
        if ("usePower".equals(fieldName)){
            return Integer.valueOf(value.toString()) / 100f;
        }
        return value;
    }

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