package org.nl.acs.agv.server.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.AcsConfig;
import org.nl.acs.agv.AgvUtil;
import org.nl.acs.agv.server.MagicAgvService;
import org.nl.acs.agv.server.dto.AgvDto;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device_driver.basedriver.standard_inspect_site.StandardInspectSiteDeviceDriver;
import org.nl.acs.device_driver.basedriver.standard_ordinary_site.StandardOrdinarySiteDeviceDriver;
import org.nl.acs.device_driver.basedriver.standard_storage.StandardStorageDeviceDriver;
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.log.LokiLog;
import org.nl.acs.log.LokiLogType;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.opc.DeviceAppServiceImpl;
import org.nl.acs.device.enums.DeviceType;
import org.nl.common.exception.BadRequestException;
import org.nl.system.service.param.ISysParamService;
import org.nl.config.SpringContextHolder;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class MagicAgvServiceImpl implements MagicAgvService {
    private final DeviceAppService deviceAppService;
    private final ISysParamService paramService;
    private final AcsToWmsService acsToWmsService;


    Map<String, AgvDto> AGVDeviceStatus = new HashMap();

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse sendAgvInstToMagic(String instcode) throws Exception {
        InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
        Instruction inst = instructionService.findByCodeFromCache(instcode);
        HttpResponse result = this.sendAgvInstToMagic(inst);
        return result;
    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse queryAgvInstStatus(String instCode) {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/transportOrders/" + instCode;

            HttpResponse result = HttpRequest.get(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("查询agv指令数据:" + result.body());
            return result;
        } else {
            return null;
        }

    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse sendAgvInstToMagic(Instruction inst) throws Exception {

        JSONArray ja = new JSONArray();
        JSONObject orderjo = new JSONObject();
        String instno = inst.getInstruction_code();
        String compound_inst_data = inst.getCompound_inst_data();
        String[] str = null;
        List<String> pathlist = null;

        if (StrUtil.equals("2", inst.getInstruction_type()) && !StrUtil.isEmpty(compound_inst_data)) {
            str = compound_inst_data.split("->");
            pathlist = Arrays.asList(str);

            for (int i = 0; i < pathlist.size(); i++) {
                String device_code = pathlist.get(i);
                Device device = deviceAppService.findDeviceByCode(device_code);
                if (ObjectUtil.isEmpty(device)) {
                    throw new BadRequestException("未找到该设备【" + device_code + "】");
                }
                if (i == 0) {
                    ja.add(destination(device_code, "Load", "1", "1"));
                } else if (i == pathlist.size() - 1) {
                    ja.add(destination(device_code, "Unload", "1", "1"));
                } else {
                    ja.add(destination(device_code, "NOP", "1", "5"));
                }

            }

            //指定agv车号 暂时不用
            //orderjo.put("intendedVehicle", "");
            //设置任务最终时间
            orderjo.put("deadline", AgvUtil.getNextDay(1));

            orderjo.put("destinations", ja);

            JSONObject prijo = new JSONObject();
            prijo.put("key", "priority");
            prijo.put("value", inst.getPriority());

            JSONArray prija = new JSONArray();
            prija.add(prijo);
            orderjo.put("properties", prija);


        } else {

            String startAddress = inst.getStart_point_code();
            String nextAddress = inst.getNext_point_code();
            DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class);
            Device nextdevice = appService.findDeviceByCode(nextAddress);
            Device startdevice = appService.findDeviceByCode(startAddress);
            //指定agv车号 暂时不用
            //orderjo.put("intendedVehicle", "");
            //设置任务最终时间
            orderjo.put("deadline", AgvUtil.getNextDay(1));
            //拼接起点子任务动作，待完善，动作码需要从数据库取值
            ja.add(destination(startAddress, "Load", "1", "1"));
            //拼接终点子任务动作，待完善，动作码需要从数据库取值
            ja.add(destination(nextAddress, "Unload", "1", "1"));

            orderjo.put("destinations", ja);

            JSONObject prijo = new JSONObject();
            prijo.put("key", "priority");
            prijo.put("value", inst.getPriority());

            JSONArray prija = new JSONArray();
            prija.add(prijo);
            orderjo.put("properties", prija);

        }

        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/transportOrders/" + instno;

            log.info("下发agv指令参数:{}", orderjo.toString());

            HttpResponse result = null;
            try {
                result = HttpRequest.post(agvurl)
                        .body(String.valueOf(orderjo))//表单内容
                        .timeout(20000)//超时，毫秒
                        .execute();
            } catch (Exception e) {
                throw new RuntimeException("下发agv失败！");
            }
            return result;
        } else {
            return null;
        }

    }

    /**
     * 返回一个点位操作子任务
     *
     * @param locationName   点位
     * @param operation      点位操作
     * @param propertiesType 子任务类型
     * @param pro            子任务参数
     *                       调用demo:destination("sh15p", "Spin", "2", "3.14")
     *                       demo:destination("cz14", "JackUnload", "3", "")
     * @return
     */
    public static JSONObject destination(String locationName, String operation, String propertiesType, String pro) {
        //新增业务订单
        JSONObject destinationOrder = new JSONObject();
        //目标工作站
        destinationOrder.put("locationName", locationName);
        //机器人在工作站要执行的操作
        destinationOrder.put("operation", operation);
        if (propertiesType.equals("1")) {//取货前等待、取货后等待

            //pro 1  进入离开等待
            if ("1".equals(pro)) {
                JSONArray properties = new JSONArray();
                JSONObject pro1 = new JSONObject();
                pro1.put("key", "EntryRequired");
                pro1.put("value", "True");
                properties.add(pro1);
                JSONObject pro2 = new JSONObject();
                pro2.put("key", "PauseOnStation");
                pro2.put("value", "True");
                properties.add(pro2);
                destinationOrder.put("properties", properties);
                //进入等待 离开不等待
            } else if ("2".equals(pro)) {
                JSONArray properties = new JSONArray();
                JSONObject pro1 = new JSONObject();
                pro1.put("key", "EntryRequired");
                pro1.put("value", "True");
                properties.add(pro1);
                JSONObject pro2 = new JSONObject();
                pro2.put("key", "PauseOnStation");
                pro2.put("value", "False");
                properties.add(pro2);
                destinationOrder.put("properties", properties);
                //进入不等待 离开等待
            } else if ("3".equals(pro)) {
                JSONArray properties = new JSONArray();
                JSONObject pro1 = new JSONObject();
                pro1.put("key", "EntryRequired");
                pro1.put("value", "False");
                properties.add(pro1);
                JSONObject pro2 = new JSONObject();
                pro2.put("key", "PauseOnStation");
                pro2.put("value", "True");
                properties.add(pro2);
                destinationOrder.put("properties", properties);
                //不等待
            } else if ("4".equals(pro)) {
                JSONArray properties = new JSONArray();
                JSONObject pro1 = new JSONObject();
                pro1.put("key", "EntryRequired");
                pro1.put("value", "False");
                properties.add(pro1);
                JSONObject pro2 = new JSONObject();
                pro2.put("key", "PauseOnStation");
                pro2.put("value", "False");
                properties.add(pro2);
                destinationOrder.put("properties", properties);
            } else if ("5".equals(pro)) {
                JSONArray properties = new JSONArray();
                JSONObject pro2 = new JSONObject();
                pro2.put("key", "PauseOnStation");
                pro2.put("value", "True");
                properties.add(pro2);
                destinationOrder.put("properties", properties);
            }

        } else if (propertiesType.equals("2")) {//Spin转动
            JSONArray properties = new JSONArray();
            JSONObject pro1 = new JSONObject();
            pro1.put("key", "global_spin_angle");//坐标系类型，global_spin_angle为全局坐标系
            pro1.put("value", pro);//弧度值，如3.14
            properties.add(pro1);
            JSONObject pro2 = new JSONObject();
            pro2.put("key", "spin_direction");//固定值
            pro2.put("value", "0");//弧度值，如0
            properties.add(pro2);
            destinationOrder.put("properties", properties);
        } else if (propertiesType.equals("3")) {//JackUnload,Jackload不操作
            JSONArray properties = new JSONArray();
            JSONObject pro1 = new JSONObject();
            pro1.put("key", "recognize");//固定值
            pro1.put("value", "false");//固定值
            properties.add(pro1);
            destinationOrder.put("properties", properties);
        } else if (propertiesType.equals("4")) {
            JSONArray properties = new JSONArray();
            JSONObject pro1 = new JSONObject();
            pro1.put("key", "robot_spin_angle");//坐标系类型，robot_spin_angle为机器人坐标系
            pro1.put("value", pro);//弧度值，如3.14
            properties.add(pro1);
            JSONObject pro2 = new JSONObject();
            pro2.put("key", "spin_direction");//固定值
            pro2.put("value", "0");//弧度值，如0
            properties.add(pro2);
            destinationOrder.put("properties", properties);
        }
        return destinationOrder;
    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse queryMagicAgvDeviceStatus() {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();
            agvurl = agvurl + ":" + agvport + "/v1/vehicles";
            HttpResponse result = HttpRequest.get(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("查询agv状态数据:{}", result.body());
            if (result.getStatus() == 200) {
                JSONArray ja = (JSONArray) JSONArray.parse(result.body());
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    String name = jo.getString("name");
                    String state = jo.getString("state");
                    String energyLevel = jo.getString("energyLevel");
                    String transportOrder = jo.getString("transportOrder");
                    String positionAngle = jo.getString("positionAngle");
                    String positionX = jo.getString("positionX");
                    String positionY = jo.getString("positionY");
                    AgvDto dto = new AgvDto();
                    dto.setName(name);
                    dto.setEnergyLevel(energyLevel);
                    dto.setState(state);
                    dto.setPositionAngle(positionAngle);
                    dto.setPositionX(positionX);
                    dto.setPositionY(positionY);
                    dto.setTransportOrder(transportOrder);

                    if (AGVDeviceStatus.containsKey(name)) {
                        AGVDeviceStatus.remove(name);
                        AGVDeviceStatus.put(name, dto);
                    } else {
                        AGVDeviceStatus.put(name, dto);
                    }
                }
            }
            return result;
        } else {
            return null;
        }


    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse deleteAgvInst(String instCode) {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/transportOrders/" + instCode + "/withdrawal";
            log.info("删除agv指令请求agvurl:{}", agvurl);

            HttpResponse result = null;
            try {
                result = HttpRequest.post(agvurl)
                        .timeout(20000)//超时，毫秒
                        .execute();
                log.info("删除agv指令请求反馈:{}", result);
            } catch (Exception e) {
                throw new RuntimeException("下发agv失败！");
            }
            return result;
        } else {
            return null;
        }

    }

    //1请求取货 2取货完成 3请求放货 4放货完成 5取货完成离开 6放货完成离开 7请求进入区域 8请求离开区域
    @LokiLog(type = LokiLogType.AGV)
    @Override
    public synchronized String process(String jobno, String type, String address, String action, String processingVehicle) {
        log.info("查询到AGV请求参数,jobno:{},address:{}", jobno + ",address:" + address + ",type:" + type + ",action:" + action);
        boolean is_feedback = false;
        String str = "";
        String backaddress = address;
        if (address.indexOf(".") > 0) {
            str = address.substring(address.indexOf("."), address.length());
            address = address.substring(0, address.indexOf("."));
        } else if (address.indexOf("-") > 0) {
            address = address.substring(0, address.indexOf("-"));
        }
        InstructionService instructionService = SpringContextHolder.getBean("instructionServiceImpl");
        Instruction inst = instructionService.findByCodeFromCache(jobno);


        DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class);
        Device addressdevice = appService.findDeviceByCode(address);
        StandardInspectSiteDeviceDriver standardInspectSiteDeviceDriver;
        StandardOrdinarySiteDeviceDriver standardOrdinarySiteDeviceDriver;
        //请求进入
        if ("onEntry".equals(type)) {

            if (addressdevice.getDeviceDriver() instanceof StandardInspectSiteDeviceDriver) {
                standardInspectSiteDeviceDriver = (StandardInspectSiteDeviceDriver) addressdevice.getDeviceDriver();
                //请求取货
                if ("Load".equals(action)) {
                    if (standardInspectSiteDeviceDriver.getMode() == 2 && standardInspectSiteDeviceDriver.getMove() > 0) {
                        inst.setExecute_status("1");
                        is_feedback = true;
                    }
                    //请求放货
                } else if ("Unload".equals(action)) {
                    if (standardInspectSiteDeviceDriver.getMode() == 2 && standardInspectSiteDeviceDriver.getMove() == 0) {
                        inst.setExecute_status("3");
                        is_feedback = true;
                    }
                }
            }


            if (addressdevice.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
                standardOrdinarySiteDeviceDriver = (StandardOrdinarySiteDeviceDriver) addressdevice.getDeviceDriver();
                //请求取货
                if ("Load".equals(action)) {
                    if (addressdevice.getHas_goods() > 0) {
                        inst.setExecute_status("1");
                        is_feedback = true;
                    }
                    //请求放货
                } else if ("Unload".equals(action)) {
                    if (addressdevice.getHas_goods() == 0) {
                        inst.setExecute_status("3");
                        is_feedback = true;
                    }
                }
            }

            if (addressdevice.getDeviceDriver() instanceof StandardStorageDeviceDriver) {
                //请求取货
                if ("Load".equals(action)) {
                    inst.setExecute_status("1");
                    is_feedback = true;
                    //请求放货
                } else if ("Unload".equals(action)) {
                    inst.setExecute_status("3");
                    is_feedback = true;
                }
            }

            //取放货完成
        } else if ("onStation".equals(type)) {
            System.out.println("取放货完成");
            if (addressdevice.getDeviceDriver() instanceof StandardInspectSiteDeviceDriver) {
                standardInspectSiteDeviceDriver = (StandardInspectSiteDeviceDriver) addressdevice.getDeviceDriver();
                //取货完成
                if ("Load".equals(action)) {
                    if (standardInspectSiteDeviceDriver.getMode() == 2 && standardInspectSiteDeviceDriver.getMove() == 0) {
                        inst.setExecute_device_code(processingVehicle);
                        inst.setExecute_status("2");
                        is_feedback = true;
                    }
                    //放货完成
                } else if ("Unload".equals(action)) {
                    if (standardInspectSiteDeviceDriver.getMode() == 2 && standardInspectSiteDeviceDriver.getMove() > 0) {
                        inst.setExecute_device_code(address);
                        inst.setExecute_status("4");
                        is_feedback = true;
                    }
                }
            }


            if (addressdevice.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
                standardOrdinarySiteDeviceDriver = (StandardOrdinarySiteDeviceDriver) addressdevice.getDeviceDriver();
                //取货完成
                if ("Load".equals(action)) {
                    inst.setExecute_device_code(processingVehicle);
                    inst.setExecute_status("2");
                    is_feedback = true;
                    //放货完成
                } else if ("Unload".equals(action)) {
                    inst.setExecute_device_code(address);
                    inst.setExecute_status("4");
                    is_feedback = true;
                }

            }

            if (addressdevice.getDeviceDriver() instanceof StandardStorageDeviceDriver) {
                //取货完成
                if ("Load".equals(action)) {
                    inst.setExecute_device_code(processingVehicle);
                    inst.setExecute_status("2");
                    is_feedback = true;
                    //放货完成
                } else if ("Unload".equals(action)) {
                    inst.setExecute_device_code(processingVehicle);
                    inst.setExecute_status("4");
                    is_feedback = true;
                }
            }
        }

        JSONObject requestjo = new JSONObject();
        JSONArray ja = new JSONArray();
        if (is_feedback) {
            String param = "";
            JSONObject jo = new JSONObject();
            if (str.length() > 0) {
                backaddress = backaddress + str;
            }
            if ("onEntry".equals(type)) {
                param = "EntryPermitted-" + backaddress + action;
            } else if ("onStation".equals(type)) {
                param = "ContinueOnStation-" + backaddress + action;
            }

            jo.put("key", param);
            jo.put("value", "False");
            ja.add(jo);
            requestjo.put("properties", ja);
            log.info("反馈AGV请求数据:{}", requestjo);
            System.out.println("back agv:" + requestjo);

            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/transportOrders/" + jobno + "/interact";

            HttpResponse result = HttpRequest.post(agvurl)
                    .body(String.valueOf(requestjo))
                    .timeout(20000)//超时，毫秒
                    .execute();
        }
        is_feedback = false;
        return requestjo.toString();

    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public String requestAck(String address) {
        log.info("AGV请求离开参数:{}", address);
        boolean flag = false;

        if (address.indexOf(".") > 0) {
            address = address.substring(0, address.indexOf("."));
        }
        if (address.indexOf("-") > 0) {
            address = address.substring(0, address.indexOf("-"));
        }
        InstructionService instructionService = SpringContextHolder.getBean("instructionServiceImpl");

        DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class);
        Device addressdevice = appService.findDeviceByCode(address);
        StandardInspectSiteDeviceDriver standardInspectSiteDeviceDriver;
        StandardOrdinarySiteDeviceDriver standardOrdinarySiteDeviceDriver;
        StandardStorageDeviceDriver standardStorageDeviceDriver;

        //mes = AgvClientXZ.mes(zlbh, device, type);
        //暂定 0就绪 1请求取货 2取货完成 3请求放货 4放货完成 5取货完成离开 6放货完成离开 7请求进入区域 8请求离开区域
        if ((addressdevice.getDeviceDriver() instanceof StandardStorageDeviceDriver)) {
            flag = true;
        }

        if (addressdevice.getDeviceDriver() instanceof StandardInspectSiteDeviceDriver) {
            standardInspectSiteDeviceDriver = (StandardInspectSiteDeviceDriver) addressdevice.getDeviceDriver();
            flag = true;
            log.info("agvstatus:" + standardInspectSiteDeviceDriver.getFlag() + "");
            //取货完成离开
            if (standardInspectSiteDeviceDriver.getFlag() == 2) {
                if (standardInspectSiteDeviceDriver.getMove() == 0) {
                    flag = true;
                    standardInspectSiteDeviceDriver.setFlag(5);
                }
            }
            //放货完成离开
            if (standardInspectSiteDeviceDriver.getFlag() == 4) {
                if (standardInspectSiteDeviceDriver.getMove() == 1) {
                    flag = true;
                    standardInspectSiteDeviceDriver.setFlag(6);
                }
            }
        }


        if (addressdevice.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
            standardOrdinarySiteDeviceDriver = (StandardOrdinarySiteDeviceDriver) addressdevice.getDeviceDriver();
            flag = true;
            log.info("agvstatus:" + standardOrdinarySiteDeviceDriver.getFlag() + "");

            //取货完成离开
            if (standardOrdinarySiteDeviceDriver.getFlag() == 2) {
                flag = true;
                standardOrdinarySiteDeviceDriver.setFlag(5);
            }
            //放货完成离开
            if (standardOrdinarySiteDeviceDriver.getFlag() == 4) {
                flag = true;
                standardOrdinarySiteDeviceDriver.setFlag(6);
            }
        }

        if (flag) {
            log.info("AGV请求离开，反馈成功:{}", address);
            System.out.println("AGV请求离开，反馈成功 " + address);
            return "OK";
        } else {
            log.info("AGV请求离开，反馈失败{}", address);
            System.out.println("AGV请求离开，反馈失败 " + address);
            throw new RuntimeException("AGV请求离开，反馈失败 " + address);
        }

    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse pause() throws Exception {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/vehicles/pause";
            log.info("暂停所有agv请求:{}", agvurl);

            HttpResponse result = HttpRequest.post(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("暂停所有agv请求反馈:{}", result);
            String type = "";
            if (result.getStatus() == 200) {
                type = "info";
            } else {
                type = "error";
            }
            return result;
        } else {
            return null;
        }
    }


    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse pause(String device_code) throws Exception {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/vehicles/" + device_code + "/pause";
            log.info("暂停{} agv请求:{}", device_code, agvurl);

            HttpResponse result = HttpRequest.post(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("暂停{} agv请求反馈:{}", device_code, result);
            String type = "";
            return result;
        } else {
            return null;
        }

    }


    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse resume() throws Exception {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/vehicles/resume";
            log.info("恢复所有agv请求:{}", agvurl);

            HttpResponse result = HttpRequest.post(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("恢复所有agv请求反馈:{}", result);
            String type = "";
            if (result.getStatus() == 200) {
                type = "info";
            } else {
                type = "error";
            }
            return result;
        } else {
            return null;
        }


    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public HttpResponse resume(String device_code) throws Exception {
        if (StrUtil.equals(paramService.findByCode(AcsConfig.FORKAGV).getValue(), "1")) {
            String agvurl = paramService.findByCode(AcsConfig.AGVURL).getValue();
            String agvport = paramService.findByCode(AcsConfig.AGVPORT).getValue();

            agvurl = agvurl + ":" + agvport + "/v1/vehicles/" + device_code + "/resume";
            log.info("恢复{} agv请求:{}", device_code, agvurl);

            HttpResponse result = HttpRequest.post(agvurl)
                    .timeout(20000)//超时，毫秒
                    .execute();
            log.info("恢复{} agv请求反馈:{}", device_code, result);

            return result;
        } else {
            return null;
        }
    }

    @LokiLog(type = LokiLogType.AGV)
    @Override
    public String queryDoorStatus(String device) {
        log.info("AGV查询自动门状态,参数：{}", device);
        int type = Integer.parseInt(paramService.findByCode(AcsConfig.BUSINESSTYPE).getValue());
        switch (type) {
            case 4:
                if (StrUtil.equals(paramService.findByCode(AcsConfig.HASOTHERSYSTEM).getValue(), "1")) {
                    String result = acsToWmsService.queryDoorsStatus().body();
                    JSONArray ja = JSONArray.parseArray(result);
                    log.info("AGV查询自动门状态,反馈：{}", ja.toString());
                    return ja.toString();
                }
                return null;
            default:
                if (StrUtil.equals("doors", device)) {
                    List<Device> list = deviceAppService.findDevice(DeviceType.autodoor);
                    JSONArray ja = new JSONArray();
                    for (int i = 0; i < list.size(); i++) {
                        Device doordevice = deviceAppService.findDeviceByCode(list.get(i).getDevice_code());
                        if (ObjectUtil.isEmpty(doordevice)) {
                            throw new BadRequestException("未找到对应设备");
                        }
                        String mes = null;

                    }
                    log.info("AGV查询自动门状态,反馈：{}", ja.toString());
                    return ja.toString();

                } else {
                    Device doordevice = deviceAppService.findDeviceByCode(device);
                    if (ObjectUtil.isEmpty(doordevice)) {
                        throw new BadRequestException("未找到对应设备");
                    }
                    String mes = null;
                    JSONObject jo = JSONObject.parseObject(mes);
                    log.info("AGV查询自动门状态,反馈：{}", jo.toString());
                    return jo.toString();
                }
        }
    }

    @Override
    public Map<String, AgvDto> findAllAgvFromCache() {
        return AGVDeviceStatus;
    }
}
