Explorar el Código

修改读取mqtt

huhao@jgai.com hace 2 semanas
padre
commit
3257b1c01c

+ 1 - 0
.gitattributes

@@ -1 +1,2 @@
 .preview/*
+product/phone/.preview/*

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 *.iml
 build/
 .preview/*
+product/phone/.preview/*
 /.idea
 /.gradle
 local.properties

+ 1 - 1
product/phone/src/main/ets/common/constants/BusinessConstant.ets

@@ -7,7 +7,7 @@ export default class BusinessConstant {
   static readonly mqttPublishTopic: string = 'station13/cmd/devices';
   static readonly mqttUrl: string = 'tcp://192.168.1.3';
   // 每个clientId不重复
-  static readonly mqttClientId: string = 'station11' + Date.now().toString(36);
+  static readonly mqttClientId: string = 'station13' + Date.now().toString(36);
   static readonly mqttUserName: string = '';
   static readonly mqttPassword: string = '';
   // 电烙铁允许最大、最小温度

+ 5 - 3
product/phone/src/main/ets/view/DeviceView.ets

@@ -986,7 +986,7 @@ export struct DeviceView {
         // this.showLog(msg);
         let dataStr = JSON.stringify(data);
         console.info('hhtest', "messageArrived message:" + dataStr);
-        if (dataStr.indexOf('Assemble_500W') !== -1) {
+        if (dataStr.indexOf(this.subscribeTopic) === -1) {
           return
         }
         let updateData: MqttUploadData = JSON.parse(data.payload) as MqttUploadData;
@@ -1062,9 +1062,11 @@ export struct DeviceView {
               case DeviceType.CardReader:
                 collectFlag[5] = true
                 if (element.tag! === 'RfidCardNum1') {
-                  device5.RfidCardNum1 = element.value?.toString(16)
+                  let value = element.value?.toString(16)
+                  device5.RfidCardNum1 = value === '0' ? '' : value
                 } else if (element.tag! === 'RfidCardNum2') {
-                  device5.RfidCardNum2 = element.value?.toString(16)
+                  let value = element.value?.toString(16)
+                  device5.RfidCardNum2 = value === '0' ? '' : value
                 }
                 device5.OnlineStatus = 1
                 break;

+ 26 - 1
product/phone/src/main/ets/view/HomeView.ets

@@ -22,6 +22,7 @@ export struct HomeView {
   private mqttAsyncClient: MqttClient | null = null;
   //  Set Client Configuration
   @State subscribeTopic: string = BusinessConstant.mqttSubscribeTopic;
+  @State subscribeTopic2: string = BusinessConstant.mqttSubscribeTopic2;
   @State publishTopic: string = BusinessConstant.mqttPublishTopic;
   @State payload: string = '';
   @State url: string = BusinessConstant.mqttUrl;
@@ -1112,6 +1113,30 @@ export struct HomeView {
         });
       }
     }
+    if (this.subscribeTopic === this.subscribeTopic2) {
+      return
+    }
+    let subscribeOption2: MqttSubscribeOptions = {
+      topic: this.subscribeTopic2,
+      qos: 2
+    }
+    if (await this.isConnected()) {
+      if (this.isPromise) {
+        this.mqttAsyncClient.subscribe(subscribeOption2).then((data: MqttResponse) => {
+          console.info('hhtest', "subscribe success result:" + JSON.stringify(data));
+        }).catch((err: MqttResponse) => {
+          console.info('hhtest', "subscribe fail result:" + JSON.stringify(err));
+        })
+      } else {
+        this.mqttAsyncClient.subscribe(subscribeOption2, (err: Error, data: MqttResponse) => {
+          if (!err) {
+            console.info('hhtest', "subscribe result:" + JSON.stringify(data));
+          } else {
+            console.info('hhtest', "subscribe error:" + JSON.stringify(err));
+          }
+        });
+      }
+    }
   }
 
   messageArrived(): undefined | void {
@@ -1124,7 +1149,7 @@ export struct HomeView {
         // this.showLog(msg);
         let dataStr = JSON.stringify(data);
         console.info('hhtest', "messageArrived message:" + dataStr);
-        if (dataStr.indexOf('Assemble_500W') !== -1) {
+        if (dataStr.indexOf(this.subscribeTopic2) === -1) {
           return
         }
         let updateData: MqttUploadData = JSON.parse(data.payload) as MqttUploadData;