package org.nl.acs.device_driver.basedriver.agv.xg_agv;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.device.domain.Device;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.utils.ReadUtil;
import org.nl.acs.device_driver.DeviceDriver;
import org.nl.acs.device_driver.basedriver.standard_inspect_site.StandardInspectSiteDeviceDriver;
import org.nl.acs.device_driver.driver.AbstractOpcDeviceDriver;
import org.nl.acs.device_driver.driver.ExecutableDeviceDriver;
import org.nl.acs.ext.wms.service.AcsToWmsService;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.monitor.DeviceStageMonitor;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.route.service.RouteLineService;
import org.nl.acs.task.service.TaskService;
import org.nl.acs.task.service.dto.TaskDto;
import org.nl.config.SpringContextHolder;
import org.nl.system.service.lucene.LuceneExecuteLogService;
import org.nl.system.service.lucene.dto.LuceneLogDto;
import org.openscada.opc.lib.da.Server;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 仙工agv
 */
@Slf4j
@Data
@RequiredArgsConstructor
public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, DeviceStageMonitor {
    @Autowired
    RouteLineService routelineserver = SpringContextHolder.getBean("routeLineServiceImpl");
    @Autowired
    TaskService taskserver = SpringContextHolder.getBean("taskServiceImpl");
    @Autowired
    InstructionService instructionService = SpringContextHolder.getBean("instructionServiceImpl");
    @Autowired
    DeviceAppService deviceAppService = SpringContextHolder.getBean("deviceAppServiceImpl");
    @Autowired
    LuceneExecuteLogService luceneExecuteLogService = SpringContextHolder.getBean("luceneExecuteLogServiceImpl");
    ;
    @Autowired
    AcsToWmsService acsToWmsService = SpringContextHolder.getBean("acsToWmsServiceImpl");
    //车辆ip 0-未知；1-需充电；2-可执行任务；3-充满电
    String agv_mode = "0";

    String last_mode = "0";
    private Date instruction_require_time = new Date();
    private int instruction_require_time_out = 3000;
    int is_charging = 0;
    int stop = 0;
    int DI0 = 0;
    int DI1 = 0;
    int DI2 = 0;
    int DI3 = 0;
    int DI4 = 0;
    int DI11 = 0;
    int DI12 = 0;
    int DO0 = 0;
    int DO1 = 0;
    int DO2 = 0;
    Float x = 0f;
    Float y = 0f;
    Float angle = 0f;
    int navigation_address = 0;
    int positioning_status = 0;
    int navigation_status = 0;
    int navigation_type = 0;
    int electricity = 0;
    int address = 0;
    int next_address = 0;
    int home_relocation = 0;
    int locate_correct = 0;
    int pause_navigation = 0;
    int continue_navigation = 0;
    int cancle_navigation = 0;
    int drum_run_status = 0;

    int last_is_charging = 0;
    int last_stop = 0;
    int last_DI0 = 0;
    int last_DI1 = 0;
    int last_DI2 = 0;
    int last_DI3 = 0;
    int last_DI4 = 0;
    int last_DI11 = 0;
    int last_DI12 = 0;
    int last_DO0 = 0;
    int last_DO1 = 0;
    int last_DO2 = 0;
    Float last_x = 0f;
    Float last_y = 0f;
    Float last_angle = 0f;
    int last_navigation_address = 0;
    int last_positioning_status = 0;
    int last_navigation_status = 0;
    int last_navigation_type = 0;
    int last_electricity = 0;
    int last_address = 0;
    int last_next_address = 0;
    int last_home_relocation = 0;
    int last_locate_correct = 0;
    int last_pause_navigation = 0;
    int last_continue_navigation = 0;
    int last_cancle_navigation = 0;
    int last_drum_run_status = 0;
    String message = null;
    String device_code;
    Instruction inst = null;
    protected ItemProtocol itemProtocol = new ItemProtocol(this);

    //最小电量
    double min_electric = 0.0;
    //最大电量
    double cancle_electric = 0.0;
    //执行任务电量
    double task_electric = 0.0;
    //休息点
    int relax_point = 0;
    //充电点
    int charge_point =0;
    @Override
    public Device getDevice() {
        return this.device;
    }

    @Override
    public void execute() throws Exception {

        device_code = this.getDeviceCode();
        is_charging = this.itemProtocol.getIs_charging();
        DI0 = this.itemProtocol.getDI0();
        DI1 = this.itemProtocol.getDI1();
        DI2 = this.itemProtocol.getDI2();
        DI3 = this.itemProtocol.getDI3();
        DI4 = this.itemProtocol.getDI4();
        DI11 = this.itemProtocol.getDI11();
        DI12 = this.itemProtocol.getDI12();
        DO0 = this.itemProtocol.getDO0();
        DO1 = this.itemProtocol.getDO1();
        DO2 = this.itemProtocol.getDO2();
        x = this.itemProtocol.getX();
        y = this.itemProtocol.getY();
        navigation_address = this.itemProtocol.getNavigation_address();
        positioning_status = this.itemProtocol.getPositioning_status();
        navigation_status = this.itemProtocol.getNavigation_status();
        navigation_type = this.itemProtocol.getNavigation_type();
        electricity = this.itemProtocol.getElectricity();
        address = this.itemProtocol.getAddress();
        next_address = this.itemProtocol.getNext_address();
        home_relocation = this.itemProtocol.getHome_relocation();
        locate_correct = this.itemProtocol.getLocate_correct();
        pause_navigation = this.itemProtocol.getPause_navigation();
        continue_navigation = this.itemProtocol.getContinue_navigation();
        cancle_navigation = this.itemProtocol.getCancle_navigation();
        drum_run_status = this.itemProtocol.getDrum_run_status();
        stop = this.itemProtocol.getStop();

        if (home_relocation != last_home_relocation) {
//            LuceneLogDto logDto = LuceneLogDto.builder()
//                    .device_code(device_code)
//                    .content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.to_home_relocation + "变更从" + this.last_home_relocation + "->" + this.home_relocation)
//                    .build();
//           luceneExecuteLogService.deviceExecuteLog(logDto);
//            luceneExecuteLogService.deviceExecuteLog(
//                    new LuceneLogDto(this.getDevice().getOpc_server_code(),this.getDevice().getOpc_plc_code(),this.device_code,ItemProtocol.to_home_relocation,String.valueOf(this.last_home_relocation),String.valueOf(this.home_relocation)));
        }

        if (stop != last_stop) {
            LuceneLogDto logDto = LuceneLogDto.builder()
                    .device_code(device_code)
                    .content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.stop + "变更从" + this.last_stop + "->" + this.stop)
                    .build();
            luceneExecuteLogService.deviceExecuteLog(logDto);
        }

        if (address != last_address) {
            LuceneLogDto logDto = LuceneLogDto.builder()
                    .device_code(device_code)
                    .content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.address +  this.last_address + "->" + this.address)
                    .build();
            luceneExecuteLogService.deviceExecuteLog(logDto);
        }

        if (electricity != last_electricity) {
            LuceneLogDto logDto = LuceneLogDto.builder()
                    .device_code(device_code)
                    .content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.electricity +  this.last_electricity + "->" + this.electricity)
                    .build();
            luceneExecuteLogService.deviceExecuteLog(logDto);
        }

        //获取最小电量
        if (ObjectUtil.isEmpty(this.getDevice().getExtraValue().get("min_electric"))) {
            message = "未配置最小电量";
            return;
        }
        min_electric = Double.parseDouble((String) this.getDevice().getExtraValue().get("min_electric"));
        //获取最大电量
        if (ObjectUtil.isEmpty(this.getDevice().getExtraValue().get("cancle_electric"))) {
            message = "未配置最大电量";
            return;
        }
        cancle_electric = Double.parseDouble((String) this.getDevice().getExtraValue().get("cancle_electric"));
        //可执行任务电量
        if (ObjectUtil.isEmpty(this.getDevice().getExtraValue().get("task_electric"))) {
            message = "未配置可执行任务电量";
            return;
        }
        task_electric = Double.parseDouble((String) this.getDevice().getExtraValue().get("task_electric"));
        //休息点
        if (ObjectUtil.isEmpty(this.getDevice().getExtraValue().get("relax_point"))) {
            message = "未配置休息点";
            return;
        }
        Device relax_point_device = deviceAppService.findDeviceByCode((String) this.getDevice().getExtraValue().get("relax_point"));
        relax_point = Integer.parseInt(relax_point_device.getAddress());
        //充电点
        if (ObjectUtil.isEmpty(this.getDevice().getExtraValue().get("charge_point"))) {
            message = "未配置充电点";
            return;
        }
        Device charge_point_device = deviceAppService.findDeviceByCode((String) this.getDevice().getExtraValue().get("charge_point"));
        charge_point = Integer.parseInt(charge_point_device.getAddress());
        List<Instruction> instList = instructionService.findAllInstFromCache();

        if (instList.size() > 0) {
            for (int i = 0; i < instList.size(); i++) {
                inst = instList.get(i);
                if (ObjectUtil.equal(inst.getInstruction_type(), "1")) {
                    continue;
                }
            }
            if (ObjectUtil.isEmpty(inst)) {
                message = "未找到对应指令";
                return;
            }
        }
        //先判断当前有无指令
        if (ObjectUtil.isEmpty(inst)) {
            if (electricity != 0 && electricity < min_electric && address != charge_point && (navigation_status == 0 || navigation_status == 4)) {
                log.info("下发充电:{}", charge_point);
                feedAgvTaskStatus(inst, "5");
            }
            if (electricity != 0 && electricity > cancle_electric && address == charge_point && (navigation_status == 0 || navigation_status == 4)) {
                log.info("下发回休息点:{}", relax_point);

            }
        } else {
            TaskDto task = taskserver.findByCodeFromCache(inst.getTask_code());
            Device start_device = deviceAppService.findDeviceByCode(inst.getStart_device_code());
            Device next_device = deviceAppService.findDeviceByCode(inst.getNext_device_code());
            int start_device_code = Integer.parseInt(start_device.getAddress());
            int next_device_code = Integer.parseInt(next_device.getAddress());


            StandardInspectSiteDeviceDriver standardInspectSiteDeviceDriver;
            //指令状态就绪
            if (StrUtil.equals("0", inst.getInstruction_status())) {
                log.info("下发指令号{}，起点{}", inst.getInstruction_code(), start_device);
                //判断指令起点与任务起点是否相同
                feedAgvTaskStatus(inst, "1");
            }

            //取货完成请求离开
            if (StrUtil.equals("1", inst.getInstruction_status()) && address == 5 && stop == 1 && StrUtil.equals(inst.getExecute_status(),"1")) {
                //请求wms是否允许从取货点离开
                feedAgvTaskStatus(inst, "2");
            }

            //请求放货
            if (StrUtil.equals("1", inst.getInstruction_status()) && address == 2 && stop == 1 && StrUtil.equals(inst.getExecute_status(),"2")) {
                //请求wms是否允许进入放货
                feedAgvTaskStatus(inst, "3");

            }
            //放货完成  完成任务回休息点
            if (StrUtil.equals("1", inst.getInstruction_status()) && address == 1 && stop == 1 && StrUtil.equals(inst.getExecute_status(),"3")) {
                feedAgvTaskStatus(inst, "4");
            }

        }

        //请求开门
        if ( (address == 3 || address == 11 ) && stop == 1 ) {
            feedAgvAction("5");
        }

        //请求关门
        if ( (address == 9 || address == 10 ) && stop == 1 ) {
            feedAgvAction("6");
        }

        //请求充电
        if ( address == 4 && stop == 1 ) {
            feedAgvAction( "7");
        }
        last_home_relocation = home_relocation;
        last_address = address;
        last_next_address = next_address;
        last_electricity = electricity;
        last_stop = stop;
    }


    public void writing(String key, int value) {
        String to_command = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code()
                + "." + key;
        String opcservcerid = this.getDevice().getOpc_server_id();
        Server server = ReadUtil.getServer(opcservcerid);
        Map<String, Object> itemMap = new HashMap<String, Object>();
        itemMap.put(to_command, value);
        LuceneLogDto resplogDto = LuceneLogDto.builder()
                .device_code(device_code)
                .content("下发信号"+ this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + key +  "value:"+ value )
                .build();

        ReadUtil.write(itemMap, server);
    }

    @Override
    public JSONObject getDeviceStatusName() {
        JSONObject jo = new JSONObject();
        String mode = "";
        jo.put("device_name", this.getDevice().getDevice_name());
        jo.put("message", this.getMessage());
        jo.put("is_click", true);
        jo.put("address", address);
        jo.put("stop", stop);
        jo.put("electricity", electricity);
        jo.put("is_charging", is_charging);

        return jo;
    }

    @Override
    public void setDeviceStatus(JSONObject data) {


    }

    /**
     * 反馈wms动作状态
     *
     * @param type
     * @return
     */
    public boolean feedAgvTaskStatus(Instruction inst, String type) throws Exception {
        Date date = new Date();
        if (date.getTime() - this.instruction_require_time.getTime() < (long) this.instruction_require_time_out) {
            log.trace("触发时间因为小于{}毫秒，而被无视", this.instruction_require_time_out);
            return false;
        } else {

            //请求WMS允许取货
            if (StrUtil.equals(type, "1")) {
                Device start_device = deviceAppService.findDeviceByCode(inst.getStart_device_code());
                Device next_device = deviceAppService.findDeviceByCode(inst.getNext_device_code());
                int start_device_code = Integer.parseInt(start_device.getAddress());
                int next_device_code = Integer.parseInt(next_device.getAddress());
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("vehicle_code", inst.getVehicle_code());
                jo.put("status", "1");
                jo.put("device_code", this.device_code);
                jo.put("task_code", inst.getTask_code());
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);

                if (resp.getStatus() ==200) {

                    inst.setInstruction_status("1");
                    inst.setExecute_status("1");
                    instructionService.update(inst);
                    writing("to_line", 1);

                } else {
                    writing("to_line", 1);

                }
                //取货完成请求离开
            } else if (StrUtil.equals(type, "2")) {
                Device start_device = deviceAppService.findDeviceByCode(inst.getStart_device_code());
                Device next_device = deviceAppService.findDeviceByCode(inst.getNext_device_code());
                int start_device_code = Integer.parseInt(start_device.getAddress());
                int next_device_code = Integer.parseInt(next_device.getAddress());
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("vehicle_code", inst.getVehicle_code());
                jo.put("status", "2");
                jo.put("device_code", this.device_code);
                jo.put("task_code", inst.getTask_code());
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);
                if (resp.getStatus() ==200) {

                    inst.setInstruction_status("1");
                    inst.setExecute_status("2");
                    instructionService.update(inst);
                } else {

                }
                //请求放货
            }  else if (StrUtil.equals(type, "3")) {
                Device start_device = deviceAppService.findDeviceByCode(inst.getStart_device_code());
                Device next_device = deviceAppService.findDeviceByCode(inst.getNext_device_code());
                int start_device_code = Integer.parseInt(start_device.getAddress());
                int next_device_code = Integer.parseInt(next_device.getAddress());
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("vehicle_code", inst.getVehicle_code());
                jo.put("status", "3");
                jo.put("device_code", this.device_code);
                jo.put("task_code", inst.getTask_code());
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);
                if (resp.getStatus() ==200) {
                    inst.setInstruction_status("1");
                    inst.setExecute_status("3");
                    instructionService.update(inst);
                } else {

                }
            } else if (StrUtil.equals(type, "4")) {
                Device start_device = deviceAppService.findDeviceByCode(inst.getStart_device_code());
                Device next_device = deviceAppService.findDeviceByCode(inst.getNext_device_code());
                int start_device_code = Integer.parseInt(start_device.getAddress());
                int next_device_code = Integer.parseInt(next_device.getAddress());
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("vehicle_code", inst.getVehicle_code());
                jo.put("status", "4");
                jo.put("device_code", this.device_code);
                jo.put("task_code", inst.getTask_code());
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);
                if (resp.getStatus() ==200) {
                    writing("to_line", 2);
                    inst.setInstruction_status("2");
                    inst.setExecute_status("4");
                    instructionService.finish(inst);
                } else {

                }
            } else if (StrUtil.equals(type, "5")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "5");
                jo.put("device_code", this.address);
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);
                if (resp.getStatus() == 200) {
                    writing("to_di_low", 0);
                    writing("to_di_height", 0);
                } else {

                }
            }  else if (StrUtil.equals(type, "6")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "6");
                jo.put("device_code", this.device_code);
                ja.add(jo);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                if (resp.getStatus() == 200) {
                    writing("to_di_height", 0);
                    writing("to_di_low", 0);
                } else {

                }
            }  else if (StrUtil.equals(type, "7")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "7");
                jo.put("device_code", this.device_code);
                ja.add(jo);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                if (resp.getStatus() == 200) {
                    writing("to_di_low", 0);
                } else {

                }
            }
        }
        return true;
    }

    public boolean feedAgvAction(String type) throws Exception {
        Date date = new Date();
        if (date.getTime() - this.instruction_require_time.getTime() < (long) this.instruction_require_time_out) {
            log.trace("触发时间因为小于{}毫秒，而被无视", this.instruction_require_time_out);
            return false;
        } else {
            if (StrUtil.equals(type, "5")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "5");
                jo.put("device_code", "A4");
                ja.add(jo);
                LuceneLogDto reqlogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("请求参数:"+jo)
                        .requestparam("请求参数:"+jo)
                        .method("feedAgvTaskStatus")
                        .build();
                luceneExecuteLogService.deviceExecuteLog(reqlogDto);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                LuceneLogDto resplogDto = LuceneLogDto.builder()
                        .device_code(device_code)
                        .content("返回参数:"+resp.body())
                        .responseparam("返回参数:"+resp.body())
                        .method("feedAgvTaskStatus")
                        .status_code(String.valueOf(resp.getStatus()))
                        .build();
                luceneExecuteLogService.deviceExecuteLog(resplogDto);                if (resp.getStatus() == 200) {
                    writing("to_di_low", 0);
                    writing("to_di_height", 0);
                } else {
                    writing("to_di_low", 0);
                    writing("to_di_height", 0);
                }
            }  else if (StrUtil.equals(type, "6")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "6");
                jo.put("device_code", "A4");
                ja.add(jo);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                if (resp.getStatus() == 200) {
                    writing("to_di_height", 0);
                    writing("to_di_low", 0);
                } else {
                    writing("to_di_height", 0);
                    writing("to_di_low", 0);
                }
            }  else if (StrUtil.equals(type, "7")) {
                JSONArray ja = new JSONArray();
                JSONObject jo = new JSONObject();
                jo.put("status", "7");
                jo.put("device_code", "A4");
                ja.add(jo);
                HttpResponse resp = acsToWmsService.feedAgvTaskStatus(ja);
                if (resp.getStatus() == 200) {
                    writing("to_di_height", 0);
                } else {
                    writing("to_di_height", 0);
                }
            }
        }
        return true;
    }
}

