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

import java.util.Date;

/**
 * 底盘车间智能扭力扳手数据采集JOB
 */
@Slf4j
@Component
public class ChassisTorqueMachineJob extends GatherJob {

    private static final String NAME = "CHASSIS_TORQUE_WRENCH";

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

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

    @Override
    protected boolean handleObject(EquipmentData equipmentData) {
        ChassisTorqueWrench data = (ChassisTorqueWrench) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if(null == ((ChassisTorqueWrench) equipmentData).getShaftTorque1()
                && null == ((ChassisTorqueWrench) equipmentData).getShaftTorque2()
                && null == ((ChassisTorqueWrench) equipmentData).getShaftTorque3()){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        return value;
    }

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