瀏覽代碼

fix:修复emqx设备重复上线

xiwa 2 年之前
父節點
當前提交
610de594fe
共有 1 個文件被更改,包括 39 次插入15 次删除
  1. 39 15
      data/components/6c095554-35e7-4e9d-a8d2-bb919e9479f4/component.js

+ 39 - 15
data/components/6c095554-35e7-4e9d-a8d2-bb919e9479f4/component.js

@@ -249,7 +249,7 @@ this.onReceive=function(head,type,payload){
     var fun = messageHandler[topic];
 
     if(fun){
-        result = fun(head,type,payload)
+        return fun(head,type,payload)
     }else{
         var arr= topic.split('/');
         if(arr.length<6){
@@ -260,24 +260,48 @@ this.onReceive=function(head,type,payload){
 
         //子设备注册
         if(topic.endsWith('/register')){
-            result =  subRegister(topic,{productKey:pk,deviceName:dn}, payload);
+            return subRegister(topic,{productKey:pk,deviceName:dn}, payload);
         }
-        else {
-            //数据上报
-            result = {
-                type: "report",
-                data: {
-                    productKey: pk,
-                    deviceName: dn,
-                    mid: payload.id,
-                    content: {
-                        topic: topic,
-                        payload: payload
-                    }
-                }
+
+      //数据上报
+      var reply=
+      {
+        productKey:pk,
+        deviceName:dn,
+        mid:payload.id,
+        content:{
+          topic:topic.replace("/s/","/c/")+"_reply",
+          payload:JSON.stringify({
+            id:payload.id,
+            method: payload.method+"_reply",
+            code:0,
+          })
+        }
+      };
+
+        var action={};
+        if(!topic.endsWith("_reply")){
+            //需要回复的消息
+            action={
+              type:"ack",
+              content:JSON.stringify(reply)
             }
         }
 
+      return {
+        type:"report",
+        data:{
+          productKey:pk,
+          deviceName:dn,
+          mid:payload.id,
+          content:{
+            topic:topic,
+            payload:payload
+          }
+        },
+        action:action
+      }
+
     }
     return result;