shiwn преди 4 месеца
родител
ревизия
d02711a9a3

+ 32 - 0
src/main/java/com/sw/patroleditor/common/enumData/RobotTypeEnum.java

@@ -0,0 +1,32 @@
+package com.sw.patroleditor.common.enumData;
+
+/**
+ * Created by shiwn on 2024/7/2 15:20
+ */
+public enum RobotTypeEnum {
+    SW_ROBOT(1, "赛为机器人");
+
+    private int type;
+    private String name;
+
+    RobotTypeEnum(int type, String name) {
+        this.type = type;
+        this.name = name;
+    }
+
+    public int getType() {
+        return type;
+    }
+
+    public void setType(int type) {
+        this.type = type;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 104 - 0
src/main/java/com/sw/patroleditor/component/robot/ctrl/CtrlInterface.java

@@ -0,0 +1,104 @@
+package com.sw.patroleditor.component.robot.ctrl;
+
+
+import com.sw.patroleditor.common.enumData.RobotTypeEnum;
+import com.sw.patroleditor.component.robot.message.Move;
+
+import java.net.ConnectException;
+
+public class CtrlInterface implements SWInterface {
+
+
+    private static volatile CtrlInterface globalInterface;
+
+    private static String url;
+
+    private static int ROBOT_TYPE = RobotTypeEnum.SW_ROBOT.getType(); //1:SW
+
+
+    //private 私有,禁止直接new()
+    private CtrlInterface() {
+    }
+
+    //连接方式一 ,默认选择赛为机器人作为接口对象
+    public static CtrlInterface connection(String host, int port) {
+
+        url = "http://" + host + ":" + port;
+
+        synchronized (url) {
+            globalInterface = new CtrlInterface();
+            return globalInterface;
+        }
+    }
+
+    //连接方式二 , 指定机器人类型作为接口对象
+    public static CtrlInterface connection(String host, int port, int type) {
+        ROBOT_TYPE = type;
+        return connection(host, port);
+    }
+
+    /**
+     * 特殊控制指令,关机、重启、急停、一键返回,前置充电等
+     *
+     * @return String
+     * @throws ConnectException
+     */
+    public String specialtrl(String command) throws ConnectException {
+        return globalInterface.specialtrl_SW(url, command);
+    }
+
+    /**
+     * 机器人移动
+     *
+     * @param mo 移动类
+     * @return String
+     * @throws ConnectException
+     */
+    public String robotCtrl(Move mo) throws ConnectException {
+        return globalInterface.robotCtrl_SW(url, mo);
+    }
+
+    /**
+     * 打开补光灯
+     *
+     * @param openStatus true/false
+     * @return String
+     * @throws ConnectException
+     */
+    public String openLight(boolean openStatus) throws ConnectException {
+        return globalInterface.openLight_SW(url, openStatus);
+    }
+
+    /**
+     * 打开雨刷
+     *
+     * @param openStatus true/false
+     * @return String
+     * @throws ConnectException
+     */
+    public String openWiper(boolean openStatus) throws ConnectException {
+        return globalInterface.openWiper_SW(url, openStatus);
+    }
+
+    /**
+     * 打开避障雷达
+     *
+     * @param openStatus true/false
+     * @return String
+     * @throws ConnectException
+     */
+    public String openRadar(boolean openStatus) throws ConnectException {
+        return globalInterface.openRadar_SW(url, openStatus);
+    }
+
+    /**
+     * 机器人模式切换
+     *
+     * @param modeType 3:自动巡检模式,4:手动模式
+     * @return String
+     * @throws ConnectException
+     */
+    public String modeChange(int modeType) throws ConnectException {
+        return globalInterface.modeChange_SW(url, modeType);
+    }
+}

+ 148 - 0
src/main/java/com/sw/patroleditor/component/robot/ctrl/SWInterface.java

@@ -0,0 +1,148 @@
+package com.sw.patroleditor.component.robot.ctrl;
+
+import com.sw.patroleditor.component.robot.message.Move;
+import com.sw.patroleditor.component.robot.message.RobotCtrl;
+import com.sw.patroleditor.component.robot.message.SpecialCtrl;
+import com.sw.patroleditor.component.robot.node.GlobalPublishNode;
+
+import java.net.ConnectException;
+
+public interface SWInterface {
+
+    /**
+     * 特殊状态控制
+     */
+    default String specialtrl_SW(String url, String command) throws ConnectException {
+        GlobalPublishNode<SpecialCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/special_ctrl");
+
+        SpecialCtrl specialCtrl = new SpecialCtrl();
+        switch (command) {
+            case "shutdown":
+                specialCtrl.setShutdown(true); //关机
+                break;
+            case "reboot":
+                specialCtrl.setReboot(true); //重启
+                break;
+            case "stop":
+                specialCtrl.setStop(true);//急停
+                break;
+            case "release":
+                specialCtrl.setRelease(true);//急停释放
+                break;
+            case "goback":
+                specialCtrl.setGoback(true);//一键返航
+                break;
+            case "charge":
+                specialCtrl.setCharge(true);//强制充电
+                break;
+        }
+
+        publishNode.setMessage(specialCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+
+    /**
+     * 移动控制
+     */
+    default String robotCtrl_SW(String url, Move mo) throws ConnectException {
+        GlobalPublishNode<RobotCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/robot_ctrl");
+
+        RobotCtrl robotCtrl = new RobotCtrl();
+        robotCtrl.setRobotMode(1);
+        robotCtrl.setPosX(mo.getX());
+        robotCtrl.setPosY(mo.getY());
+        robotCtrl.setPosZ(mo.getZ());
+        robotCtrl.setWorkSpeed(mo.getSpeed());
+
+        publishNode.setMessage(robotCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+
+    /**
+     * 补光灯控制
+     */
+    default String openLight_SW(String url, boolean openStatus) throws ConnectException {
+        GlobalPublishNode<RobotCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/robot_ctrl");
+
+        RobotCtrl robotCtrl = new RobotCtrl();
+        robotCtrl.setPtzMode(1);
+        robotCtrl.setOpenLight(openStatus);
+        publishNode.setMessage(robotCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+
+    /**
+     * 雨刷控制
+     */
+    default String openWiper_SW(String url, boolean openStatus) throws ConnectException {
+        GlobalPublishNode<RobotCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/robot_ctrl");
+
+        RobotCtrl robotCtrl = new RobotCtrl();
+        robotCtrl.setPtzMode(1);
+
+        robotCtrl.setOpenWiper(openStatus);
+        publishNode.setMessage(robotCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+
+    /**
+     * 避障雷达控制
+     */
+    default String openRadar_SW(String url, boolean openStatus) throws ConnectException {
+        GlobalPublishNode<RobotCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/robot_ctrl");
+
+        RobotCtrl robotCtrl = new RobotCtrl();
+        robotCtrl.setPtzMode(1);
+        robotCtrl.setOpenRadar(openStatus);
+        publishNode.setMessage(robotCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+
+    /**
+     * 机器人模式切换
+     */
+    default String modeChange_SW(String url, int modeType) throws ConnectException {
+        GlobalPublishNode<SpecialCtrl> publishNode =
+                new GlobalPublishNode(url, "agv_interface/special_ctrl");
+
+        SpecialCtrl specialCtrl = new SpecialCtrl();
+        switch (modeType) {
+            case 3:
+                specialCtrl.setCtrlMode(modeType);
+                break;
+            case 4:
+                specialCtrl.setCtrlMode(modeType);
+                break;
+            default:
+                specialCtrl.setCtrlMode(-1);
+                break;
+        }
+
+        publishNode.setMessage(specialCtrl);
+        publishNode.publish();
+
+        //读取响应结果
+        return publishNode.getResult();
+    }
+}
+

+ 52 - 0
src/main/java/com/sw/patroleditor/component/robot/message/Move.java

@@ -0,0 +1,52 @@
+package com.sw.patroleditor.component.robot.message;
+
+public class Move {
+
+    private int mode = 1; //1手动,2定点移动
+
+    private int x;//左移\右移, yaw左转\右转
+    private int y;//前进\后退, pitch仰\俯
+    private int z;//上升\下降, roll左斜\右斜
+
+    private int speed;
+
+    public int getX() {
+        return x;
+    }
+
+    public void setX(int x) {
+        this.x = x;
+    }
+
+    public int getY() {
+        return y;
+    }
+
+    public void setY(int y) {
+        this.y = y;
+    }
+
+    public int getZ() {
+        return z;
+    }
+
+    public void setZ(int z) {
+        this.z = z;
+    }
+
+    public int getSpeed() {
+        return speed;
+    }
+
+    public void setSpeed(int speed) {
+        this.speed = speed;
+    }
+
+    public int getMode() {
+        return mode;
+    }
+
+    public void setMode(int mode) {
+        this.mode = mode;
+    }
+}

+ 215 - 0
src/main/java/com/sw/patroleditor/component/robot/message/RobotCtrl.java

@@ -0,0 +1,215 @@
+package com.sw.patroleditor.component.robot.message;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class RobotCtrl {
+
+    @JsonProperty(value = "robot_mode")
+    private int robotMode;
+    @JsonProperty(value = "pos_x")
+    private int posX;
+    @JsonProperty(value = "pos_y")
+    private int posY;
+    @JsonProperty(value = "pos_z")
+    private int posZ;
+    @JsonProperty(value = "work_speed")
+    private int workSpeed;
+
+    //云台控制
+    @JsonProperty(value = "ptz_mode")
+    private int ptzMode;
+    private int yaw;
+    private int pitch;
+    private int roll;
+    @JsonProperty(value = "ptz_speed")
+    private int ptzSpeed;
+
+    private int ratio;//倍率
+    @JsonProperty(value = "focal_distance")
+    private int focalDistance;//焦距
+    private int diaphragm;//光圈
+    private int focusing; //对焦(0自动,1手动)
+
+    @JsonProperty(value = "open_camera")
+    private boolean openCamera;
+    @JsonProperty(value = "open_infrared")
+    private boolean openInfrared;
+    @JsonProperty(value = "open_wiper")
+    private boolean openWiper;
+    @JsonProperty(value = "open_light")
+    private boolean openLight;
+    @JsonProperty(value = "open_radar") //雷达开关
+    private boolean openRadar;
+    @JsonProperty(value = "open_room")  //充电房开关
+    private boolean openRoom;
+    @JsonProperty(value = "open_robot") //机器人开关
+    private boolean openRobot;
+
+    public int getRobotMode() {
+        return robotMode;
+    }
+
+    public void setRobotMode(int robotMode) {
+        this.robotMode = robotMode;
+    }
+
+    public int getPosX() {
+        return posX;
+    }
+
+    public void setPosX(int posX) {
+        this.posX = posX;
+    }
+
+    public int getPosY() {
+        return posY;
+    }
+
+    public void setPosY(int posY) {
+        this.posY = posY;
+    }
+
+    public int getPosZ() {
+        return posZ;
+    }
+
+    public void setPosZ(int posZ) {
+        this.posZ = posZ;
+    }
+
+    public int getWorkSpeed() {
+        return workSpeed;
+    }
+
+    public void setWorkSpeed(int workSpeed) {
+        this.workSpeed = workSpeed;
+    }
+
+    public int getPtzMode() {
+        return ptzMode;
+    }
+
+    public void setPtzMode(int ptzMode) {
+        this.ptzMode = ptzMode;
+    }
+
+    public int getYaw() {
+        return yaw;
+    }
+
+    public void setYaw(int yaw) {
+        this.yaw = yaw;
+    }
+
+    public int getPitch() {
+        return pitch;
+    }
+
+    public void setPitch(int pitch) {
+        this.pitch = pitch;
+    }
+
+    public int getRoll() {
+        return roll;
+    }
+
+    public void setRoll(int roll) {
+        this.roll = roll;
+    }
+
+    public int getPtzSpeed() {
+        return ptzSpeed;
+    }
+
+    public void setPtzSpeed(int ptzSpeed) {
+        this.ptzSpeed = ptzSpeed;
+    }
+
+    public int getRatio() {
+        return ratio;
+    }
+
+    public void setRatio(int ratio) {
+        this.ratio = ratio;
+    }
+
+    public int getFocalDistance() {
+        return focalDistance;
+    }
+
+    public void setFocalDistance(int focalDistance) {
+        this.focalDistance = focalDistance;
+    }
+
+    public int getDiaphragm() {
+        return diaphragm;
+    }
+
+    public void setDiaphragm(int diaphragm) {
+        this.diaphragm = diaphragm;
+    }
+
+    public int getFocusing() {
+        return focusing;
+    }
+
+    public void setFocusing(int focusing) {
+        this.focusing = focusing;
+    }
+
+    public boolean isOpenCamera() {
+        return openCamera;
+    }
+
+    public void setOpenCamera(boolean openCamera) {
+        this.openCamera = openCamera;
+    }
+
+    public boolean isOpenInfrared() {
+        return openInfrared;
+    }
+
+    public void setOpenInfrared(boolean openInfrared) {
+        this.openInfrared = openInfrared;
+    }
+
+    public boolean isOpenWiper() {
+        return openWiper;
+    }
+
+    public void setOpenWiper(boolean openWiper) {
+        this.openWiper = openWiper;
+    }
+
+    public boolean isOpenLight() {
+        return openLight;
+    }
+
+    public void setOpenLight(boolean openLight) {
+        this.openLight = openLight;
+    }
+
+    public boolean isOpenRadar() {
+        return openRadar;
+    }
+
+    public void setOpenRadar(boolean openRadar) {
+        this.openRadar = openRadar;
+    }
+
+    public boolean isOpenRoom() {
+        return openRoom;
+    }
+
+    public void setOpenRoom(boolean openRoom) {
+        this.openRoom = openRoom;
+    }
+
+    public boolean isOpenRobot() {
+        return openRobot;
+    }
+
+    public void setOpenRobot(boolean openRobot) {
+        this.openRobot = openRobot;
+    }
+}

+ 86 - 0
src/main/java/com/sw/patroleditor/component/robot/message/SpecialCtrl.java

@@ -0,0 +1,86 @@
+package com.sw.patroleditor.component.robot.message;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class SpecialCtrl {
+    //关闭
+    private boolean shutdown = false;
+    //重启
+    private boolean reboot = false;
+    //停止
+    private boolean stop = false;
+    //恢复
+    private boolean release = false;//急停释放
+    //一键返航
+    private boolean goback = false;
+    //强制充电
+    private boolean charge = false;
+
+    @JsonProperty(value = "ctrl_mode")
+    private int ctrlMode = -1;
+//    #0  待机(默认模式),1:开机(运行模式)
+//    #2:休息模式
+//    #3:自动巡检模式
+//    #4:手动模式
+//    #5:充电模式
+//    #6:暂停模式
+//    #7:故障检修状态
+//    #8:遥控模式
+//    #9: 紧急定位模式(半自动模式)
+
+    public boolean isShutdown() {
+        return shutdown;
+    }
+
+    public void setShutdown(boolean shutdown) {
+        this.shutdown = shutdown;
+    }
+
+    public boolean isReboot() {
+        return reboot;
+    }
+
+    public void setReboot(boolean reboot) {
+        this.reboot = reboot;
+    }
+
+    public boolean isStop() {
+        return stop;
+    }
+
+    public void setStop(boolean stop) {
+        this.stop = stop;
+    }
+
+    public boolean isGoback() {
+        return goback;
+    }
+
+    public void setGoback(boolean goback) {
+        this.goback = goback;
+    }
+
+    public boolean isCharge() {
+        return charge;
+    }
+
+    public void setCharge(boolean charge) {
+        this.charge = charge;
+    }
+
+    public int getCtrlMode() {
+        return ctrlMode;
+    }
+
+    public void setCtrlMode(int ctrlMode) {
+        this.ctrlMode = ctrlMode;
+    }
+
+    public boolean isRelease() {
+        return release;
+    }
+
+    public void setRelease(boolean release) {
+        this.release = release;
+    }
+}

+ 79 - 0
src/main/java/com/sw/patroleditor/component/robot/node/GlobalPublishNode.java

@@ -0,0 +1,79 @@
+package com.sw.patroleditor.component.robot.node;
+
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.PropertyNamingStrategy;
+import com.alibaba.fastjson.serializer.SerializeConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.sw.patroleditor.component.tools.HttpJavaTool;
+
+import java.net.ConnectException;
+
+public class GlobalPublishNode<T> {
+
+    private T message;
+
+    private String messageType;
+
+    private String url;
+
+    private String result;
+
+    public GlobalPublishNode(String url, String messageType) {
+        super();
+        this.url = url;
+        this.messageType = "/" + messageType;
+    }
+
+    public T getMessage() {
+        return message;
+    }
+
+    public void setMessage(T message) {
+        this.message = message;
+    }
+
+    /**
+     * 获取节点名称
+     */
+    public String getDefaultNodeName() {
+        return "http-java" + messageType;
+    }
+
+    public void publish() throws ConnectException {
+        if (url == null || url.isEmpty()) return;
+
+        if (messageType == null || messageType.isEmpty()) return;
+
+        //驼峰改为下划线
+        SerializeConfig serializeConfig = new SerializeConfig();
+        serializeConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
+        //取消Null值过滤
+        SerializerFeature notNull = SerializerFeature.WriteMapNullValue;
+
+        String json = JSONObject.toJSONString(message, serializeConfig, notNull);
+        if (json == null || json.isEmpty()) return;
+
+        System.out.println(json);
+        this.result = HttpJavaTool.doPostBody(url + messageType, json);
+    }
+
+    public String getResult() {
+        return result;
+    }
+
+    public void execute() {
+    }
+
+    public static void main(String[] args) {
+        GlobalPublishNode<String> publishNode =
+                new GlobalPublishNode("http://192.168.1.168:9091", "user_msgs/task_param");
+        publishNode.setMessage("{\"id\":\"我搜接受对方\"}");
+        try {
+            publishNode.publish();
+        } catch (ConnectException e) {
+            e.printStackTrace();
+        }
+        System.out.println(publishNode.getResult());
+    }
+
+}

+ 252 - 0
src/main/java/com/sw/patroleditor/component/tools/HttpJavaTool.java

@@ -0,0 +1,252 @@
+package com.sw.patroleditor.component.tools;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.UnknownHostException;
+
+
+public class HttpJavaTool {
+
+    public static String doGet(String httpurl) {
+        HttpURLConnection connection = null;
+        InputStream is = null;
+        BufferedReader br = null;
+        String result = null;// 返回结果字符串
+        try {
+            // 创建远程url连接对象
+            URL url = new URL(httpurl);
+            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
+            connection = (HttpURLConnection) url.openConnection();
+            // 设置连接方式:get
+            connection.setRequestMethod("GET");
+            // 设置连接主机服务器的超时时间:15000毫秒
+            connection.setConnectTimeout(15000);
+            // 设置读取远程返回的数据时间:60000毫秒
+            connection.setReadTimeout(60000);
+            connection.setRequestProperty("Charsert", "UTF-8");//编码
+            // 发送请求
+            connection.connect();
+            // 通过connection连接,获取输入流
+            if (connection.getResponseCode() == 200) {
+                is = connection.getInputStream();
+                // 封装输入流is,并指定字符集
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+                // 存放数据
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r\n");
+                }
+                result = sbf.toString();
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            if (null != br) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            if (null != is) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            connection.disconnect();// 关闭远程连接
+        }
+
+        return result;
+    }
+
+    public static String doPost(String httpUrl, String param) {
+
+        HttpURLConnection connection = null;
+        InputStream is = null;
+        OutputStream os = null;
+        BufferedReader br = null;
+        String result = null;
+        try {
+            URL url = new URL(httpUrl);
+            // 通过远程url连接对象打开连接
+            connection = (HttpURLConnection) url.openConnection();
+            // 设置连接请求方式
+            connection.setRequestMethod("POST");
+            // 设置连接主机服务器超时时间:15000毫秒
+            connection.setConnectTimeout(15000);
+            // 设置读取主机服务器返回数据超时时间:60000毫秒
+            connection.setReadTimeout(60000);
+
+            // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
+            connection.setDoOutput(true);
+            // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
+            connection.setDoInput(true);
+            //  设置cookie(中元特定)
+            String zyCookie = "_ssid_lwspp_=1634172987-23141";
+            connection.setRequestProperty("Cookie", zyCookie);
+            // 不允许使用缓存
+            connection.setUseCaches(false);
+            connection.setRequestProperty("Charsert", "UTF-8");//编码
+            // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
+            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+            // 通过连接对象获取一个输出流
+            os = connection.getOutputStream();
+            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
+            os.write(param.getBytes());
+            // 通过连接对象获取一个输入流,向远程读取
+            if (connection.getResponseCode() == 200) {
+
+                is = connection.getInputStream();
+                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                // 循环遍历一行一行读取数据
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r\n");
+                }
+                result = sbf.toString();
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            if (null != br) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != os) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != is) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            // 断开与远程地址url的连接
+            connection.disconnect();
+        }
+        return result;
+    }
+
+    public static String doPostBody(String httpUrl, String json) {
+        HttpURLConnection connection = null;
+        InputStream is = null;
+        OutputStream os = null;
+        BufferedReader br = null;
+        String result = null;
+        try {
+            URL url = new URL(httpUrl);
+            // 通过远程url连接对象打开连接
+            connection = (HttpURLConnection) url.openConnection();
+            // 设置连接请求方式
+            connection.setRequestMethod("POST");
+            // 设置连接主机服务器超时时间:15000毫秒
+            connection.setConnectTimeout(15000);
+            // 设置读取主机服务器返回数据超时时间:60000毫秒
+            connection.setReadTimeout(60000);
+
+            // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
+            connection.setDoOutput(true);
+            // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
+            connection.setDoInput(true);
+            //  设置cookie(中元特定)
+            String zyCookie = "_ssid_lwspp_=1634172987-23141";
+            connection.setRequestProperty("Cookie", zyCookie);
+            // 不允许使用缓存
+            connection.setUseCaches(false);
+            connection.setRequestProperty("Charsert", "UTF-8");//编码
+            // 设置传入参数的格式:json形式
+            connection.setRequestProperty("Content-Type", "application/json");// 设置发送数据的格式
+            // 设置接收数据的格式:json形式
+            connection.setRequestProperty("Accept", "application/json");
+            // 通过连接对象获取一个输出流
+            os = connection.getOutputStream();
+            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
+            os.write(json.getBytes());
+            // 通过连接对象获取一个输入流,向远程读取
+            if (connection.getResponseCode() == 200) {
+
+                is = connection.getInputStream();
+                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                // 循环遍历一行一行读取数据
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r\n");
+                }
+                result = sbf.toString();
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            if (null != br) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != os) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != is) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            // 断开与远程地址url的连接
+            connection.disconnect();
+        }
+        return result;
+    }
+
+    public static void main(String[] args) {
+        String result = HttpJavaTool.doPostBody("http://192.168.1.168:9091/user_msgs/task_param", "{\"id\":\"我搜接受对方\"}");
+        //System.out.println(result);
+    }
+
+
+}

+ 5 - 3
src/main/java/com/sw/patroleditor/exception/ErrorCode.java

@@ -7,9 +7,7 @@ package com.sw.patroleditor.exception;
 public enum ErrorCode {
     SUCCESS(0, "成功"),
 
-    FAIL(1,"失败"),
-
-    OPERATION_FAILED(1, "操作失败"),
+    FAIL(1, "失败"),
 
     //    系统用户相关
     ACCOUNT_NOT_EXIST(1, "用户不存在"),
@@ -34,6 +32,10 @@ public enum ErrorCode {
 
     CONFIRM_PASSWORD_ERROR(1, "两次密码输入不一致"),
 
+    //  机器人相关
+    OPERATION_FAILED(1, "操作失败"),
+    ROBOT_RETURN_NULL(1, "机器人无返回数据!"),
+
 
     //API通用状态码
     KEYID_ERROR(202, "无效key"),

+ 97 - 0
src/main/java/com/sw/patroleditor/service/impl/RobotCtrlServiceImpl.java

@@ -1,18 +1,47 @@
 package com.sw.patroleditor.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.sw.patroleditor.component.robot.ctrl.CtrlInterface;
+import com.sw.patroleditor.component.robot.message.Move;
 import com.sw.patroleditor.domain.vo.RobotModelVO;
 import com.sw.patroleditor.domain.vo.RobotMoveCtrlVO;
 import com.sw.patroleditor.domain.vo.RobotSpecialCtrlVO;
 import com.sw.patroleditor.domain.vo.RobotToolCtrlVO;
+import com.sw.patroleditor.exception.BusinessException;
+import com.sw.patroleditor.exception.ErrorCode;
 import com.sw.patroleditor.service.RobotCtrlService;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.util.Map;
+
 /**
  * Created by shiwn on 2024/7/2 14:05
  */
 @Service
 public class RobotCtrlServiceImpl implements RobotCtrlService {
+    private static final Logger logger = LoggerFactory.getLogger(RobotCtrlServiceImpl.class);
+
+    /**
+     * 机器人id
+     */
+    @Value("${robot.id}")
+    private int robotId = 0;
+    /**
+     * 机器人ip
+     */
+    @Value("${robot.ip}")
+    private String robotIp = null;
+    /**
+     * 机器人端口
+     */
+    @Value("${robot.port}")
+    private int robotPort = 0;
 
     /**
      * @Description: 机器人特殊状态控制
@@ -23,6 +52,15 @@ public class RobotCtrlServiceImpl implements RobotCtrlService {
      */
     @Override
     public boolean specialCtrl(RobotSpecialCtrlVO vo) {
+        //  控制
+        try {
+            String resp = CtrlInterface.connection(robotIp, robotPort).specialtrl(vo.getType());
+            //   判断结果
+            return isSuccess(resp);
+        } catch (Exception e) {
+            e.printStackTrace();
+            logger.error("机器人特殊状态控制失败,异常:{}", e.getMessage());
+        }
         return false;
     }
 
@@ -35,6 +73,17 @@ public class RobotCtrlServiceImpl implements RobotCtrlService {
      */
     @Override
     public boolean robotMoveCtrl(RobotMoveCtrlVO vo) {
+        //  控制
+        Move move = new Move();
+        BeanUtils.copyProperties(vo, move);
+        try {
+            String resp = CtrlInterface.connection(robotIp, robotPort).robotCtrl(move);
+            //   判断结果
+            return isSuccess(resp);
+        } catch (Exception e) {
+            e.printStackTrace();
+            logger.error("机器人移动控制失败,异常:{}", e.getMessage());
+        }
         return false;
     }
 
@@ -47,6 +96,22 @@ public class RobotCtrlServiceImpl implements RobotCtrlService {
      */
     @Override
     public boolean toolCtrl(RobotToolCtrlVO vo) {
+        //  控制
+        String resp = "";
+        try {
+            if (vo.getType() == 1) {
+                //  雨刷
+                resp = CtrlInterface.connection(robotIp, robotPort).openWiper(vo.getOpstatus());
+            } else {
+                //  补光灯
+                resp = CtrlInterface.connection(robotIp, robotPort).openLight(vo.getOpstatus());
+            }
+            //   判断结果
+            return isSuccess(resp);
+        } catch (Exception e) {
+            e.printStackTrace();
+            logger.error("机器人补光灯状态控制失败,异常:{}", e.getMessage());
+        }
         return false;
     }
 
@@ -59,6 +124,38 @@ public class RobotCtrlServiceImpl implements RobotCtrlService {
      */
     @Override
     public boolean modelChange(RobotModelVO vo) {
+        try {
+            String resp = CtrlInterface.connection(robotIp, robotPort).modeChange(vo.getModel());
+            //   判断结果
+            return isSuccess(resp);
+        } catch (Exception e) {
+            e.printStackTrace();
+            logger.error("机器人模式切换失败,异常:{}", e.getMessage());
+        }
         return false;
     }
+
+    /**
+     * @Description: 解析机器人返回数据
+     * @Date: 2024/7/2 15:36
+     * @Author: shiwn
+     * @Param: str:机器人返回数据
+     */
+    private boolean isSuccess(String str) {
+        //  判断是否有返回值
+        if (org.apache.commons.lang3.StringUtils.isEmpty(str)) {
+            throw new BusinessException(ErrorCode.ROBOT_RETURN_NULL);
+        }
+        //  判断结果
+        Map<String, String> map = JSON.parseObject(str, Map.class);
+        Integer code = Integer.parseInt(String.valueOf(map.get("code")));
+        if (!code.equals(ErrorCode.SUCCESS.getCode())) {
+            String msg = String.valueOf(map.get("msg"));
+            if (StringUtils.isEmpty(msg)) {
+                msg = ErrorCode.OPERATION_FAILED.getMsg();
+            }
+            throw new BusinessException(1, msg);
+        }
+        return true;
+    }
 }

+ 5 - 0
src/main/resources/application-dev.properties

@@ -30,6 +30,11 @@ spring.datasource.filters=stat,log4j
 # 合并多个DruidDataSource的监控数据
 spring.datasource.useGlobalDataSourceStat=true
 
+############# 配置参数
+robot.id=0
+robot.ip=0
+robot.port=0
+