ZhangNing преди 7 месеца
родител
ревизия
f9ba75a9ed

+ 22 - 18
src/main/java/com/xyhy/xingshenmqttcluster/common/message/MessageHandle.java

@@ -10,64 +10,68 @@ public class MessageHandle {
 
     public static final Logger log =  LoggerFactory.getLogger(MessageHandle.class);
 
-    public static void vehicleOnline(String s, JSONObject msg){
+    public static void vehicleOnline(String topic, JSONObject msg){
 
     }
 
-    public static void vehicleOffline(String s, JSONObject msg){
+    public static void vehicleOffline(String topic, JSONObject msg){
 
     }
 
-    public static void fuelState(String s, JSONObject msg){
+    public static void fuelState(String topic, JSONObject msg){
 
     }
 
-    public static void vehicleState(String s, JSONObject msg){
+    public static void vehicleState(String topic, JSONObject msg){
 
     }
 
-    public static void vehicleStart(String s, JSONObject msg){
-        String capacityResourceObjectId = msg.getString("capacityResourceObjectId");
+    public static void vehicleStart(String topic, JSONObject msg){
+        JSONObject msgBody = JSONObject.parseObject(msg.getString("msgBody"));
+        String capacityResourceObjectId = msgBody.getString("capacityResourceObjectId");
         String url = Common.getConfigByName("charging.interface.api.url.test") + "/tcpcarControl";
         String param = "ivCapacityResourceld" + "=" + capacityResourceObjectId + "&" + "messageType" + "=" + "2";
+        log.info(param);
         Object o = HttpRequest.sendPost(url, param);
-        log.info(o.toString());
+        log.info(msg.toJSONString());
     }
 
-    public static void arriveStationRequest(String s, JSONObject msg){
-        String capacityResourceObjectId = msg.getString("capacityResourceObjectId");
-        JSONObject data = JSONObject.parseObject(msg.getString("data"));
-        JSONObject station = JSONObject.parseObject(data.getString("station"));
+    public static void arriveStationRequest(String topic, JSONObject msg){
+        JSONObject msgBody = JSONObject.parseObject(msg.getString("msgBody"));
+        String capacityResourceObjectId = msgBody.getString("capacityResourceId");
+        JSONObject data = msgBody.getJSONObject("data");
+        JSONObject station = data.getJSONObject("station");
         String stationId = station.getString("stationId");
         String url = Common.getConfigByName("charging.interface.api.url.test") + "/tcpcarControl";
         String param = "ivCapacityResourceld" + "=" + capacityResourceObjectId + "&" +
                        "ivParkPointId" + "=" + stationId + "&" +
                        "messageType" + "=" + "1";
+        log.info(param);
         Object o = HttpRequest.sendPost(url, param);
-        log.info(o.toString());
+        log.info(msg.toJSONString());
     }
 
-    public static void arrivePoint(String s, JSONObject msg){
+    public static void arrivePoint(String topic, JSONObject msg){
 
     }
 
-    public static void leavePoint(String s, JSONObject msg){
+    public static void leavePoint(String topic, JSONObject msg){
 
     }
 
-    public static void gotoPoint(String s, JSONObject msg){
+    public static void gotoPoint(String topic, JSONObject msg){
 
     }
 
-    public static void taskBegin(String s, JSONObject msg){
+    public static void taskBegin(String topic, JSONObject msg){
 
     }
 
-    public static void taskOver(String s, JSONObject msg){
+    public static void taskOver(String topic, JSONObject msg){
 
     }
 
-    public static void startCarFailMsg(String s, JSONObject msg){
+    public static void startCarFailMsg(String topic, JSONObject msg){
 
     }
 }

+ 27 - 13
src/main/java/com/xyhy/xingshenmqttcluster/common/mqtt/MqttGetMessage.java

@@ -22,31 +22,45 @@ public class MqttGetMessage extends Common implements MqttCallback {
     public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
         JSONObject msg = JSONObject.parseObject(new String(mqttMessage.getPayload()));
         String msgType = msg.getString("msgType");
-        switch (msgType){
-            case "1":
+        int i = Integer.parseInt(msgType);
+//        log.info(new String(mqttMessage.getPayload()));
+        switch (i) {
+            case 1:
                 MessageHandle.vehicleOnline(s, msg);
-            case "2":
+                break;
+            case 2:
                 MessageHandle.vehicleOffline(s, msg);
-            case "3":
+                break;
+            case 3:
                 MessageHandle.fuelState(s, msg);
-            case "4":
+                break;
+            case 4:
                 MessageHandle.vehicleState(s, msg);
-            case "6":
+                break;
+            case 6:
                 MessageHandle.vehicleStart(s, msg);
-            case "19":
+                break;
+            case 19:
                 MessageHandle.arriveStationRequest(s, msg);
-            case "102":
+                break;
+            case 102:
                 MessageHandle.arrivePoint(s, msg);
-            case "103":
+                break;
+            case 103:
                 MessageHandle.leavePoint(s, msg);
-            case "101":
+                break;
+            case 101:
                 MessageHandle.gotoPoint(s, msg);
-            case "800":
+                break;
+            case 800:
                 MessageHandle.taskBegin(s, msg);
-            case "801":
+                break;
+            case 801:
                 MessageHandle.taskOver(s, msg);
-            case "501":
+                break;
+            case 501:
                 MessageHandle.startCarFailMsg(s, msg);
+                break;
         }
     }
 

+ 8 - 2
src/main/java/com/xyhy/xingshenmqttcluster/common/mqtt/MqttServer.java

@@ -18,7 +18,7 @@ public class MqttServer extends Common {
 
     private String host = Common.getConfigByName("MQTT.server.hostUrl");
 
-    private String clientId = Common.getConfigByName("project.name");
+    private final String clientId = "DC" + (int) (Math.random() * 100000000);
 
     private String username = Common.getConfigByName("MQTT.server.hostName");
 
@@ -59,11 +59,17 @@ public class MqttServer extends Common {
         try {
             client=new MqttClient(host,clientID,new MemoryPersistence());
             MqttConnectOptions options=new MqttConnectOptions();
-            options.setCleanSession(true);
+//            options.setCleanSession(true);
+//            options.setUserName(username);
+//            options.setPassword(password.toCharArray());
+//            options.setConnectionTimeout(timeout);
+//            options.setKeepAliveInterval(keeplive);
+            options.setCleanSession(false);
             options.setUserName(username);
             options.setPassword(password.toCharArray());
             options.setConnectionTimeout(timeout);
             options.setKeepAliveInterval(keeplive);
+            options.setAutomaticReconnect(true);
             _client = client;
             try {
                 client.setCallback(new MqttGetMessage());

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

@@ -3,8 +3,8 @@
 MQTT.server.hostUrl=wss://console.api.xingshentech.com:443/ws/mqtt
 MQTT.server.hostName=zdcx
 MQTT.server.password=zdcx0115
-MQTT.server.timeout=10
-MQTT.server.keepAlive=60
+MQTT.server.timeout=100000
+MQTT.server.keepAlive=60000
 
 
 
@@ -12,11 +12,11 @@ MQTT.server.keepAlive=60
 
 ##MQTT.server.Subscribe=$queue\\101,$queue\\103,$queue\\105,$queue\\201,$queue\\302,$queue\\303,$queue\\305,$queue\\306,$queue\\309,$queue\\401,$queue\\501
 ##MQTT.server.Subscribe=xs/v1/business/boot/task/notify/important/msg/queue/34,xs/v1/business/boot/boot/task/notify/msg/queue/34
-MQTT.server.Subscribe=xs/business/boot/task/notify/important/msg/queue/6
-#MQTT.server.Subscribe=xs/v1/business/boot/boot/task/notify/msg/queue/34
+#MQTT.server.Subscribe=xs/business/boot/task/notify/important/msg/queue/6
+MQTT.server.Subscribe=xs/v1/business/boot/boot/task/notify/msg/queue/34,xs/v1/business/boot/task/notify/important/msg/queue/34
 
 #######WEB SERVER###########
-server.port=8080
+server.port=20002
 
 
 #######REDIS SERVER#########