From 1a52540afdaee8d817adb5f58e8088b6c38a4ae3 Mon Sep 17 00:00:00 2001 From: fyf Date: Wed, 7 Jun 2023 18:22:03 +0800 Subject: [PATCH] 323232 --- .../example/bpa/Model/BPA_HeartPackage.java | 15 ++++++ .../example/bpa/Model/Message_HeartModel.java | 7 +++ .../com/example/bpa/config/ConfigName.java | 3 ++ .../com/example/bpa/service/OrderServer.java | 50 ++++++++++++++++--- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/example/bpa/Model/BPA_HeartPackage.java create mode 100644 app/src/main/java/com/example/bpa/Model/Message_HeartModel.java diff --git a/app/src/main/java/com/example/bpa/Model/BPA_HeartPackage.java b/app/src/main/java/com/example/bpa/Model/BPA_HeartPackage.java new file mode 100644 index 0000000..59dfe2f --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/BPA_HeartPackage.java @@ -0,0 +1,15 @@ +package com.example.bpa.Model; + +public class BPA_HeartPackage { + public int MessageId; + + public byte MessageVersion; + + public int ClientId; + + public int ClientType;//9 是MORKT + + public String Timestamp; + + public Message_HeartModel Message; +} diff --git a/app/src/main/java/com/example/bpa/Model/Message_HeartModel.java b/app/src/main/java/com/example/bpa/Model/Message_HeartModel.java new file mode 100644 index 0000000..1400556 --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/Message_HeartModel.java @@ -0,0 +1,7 @@ +package com.example.bpa.Model; + +public class Message_HeartModel { + public int Healthy; + + public int DeviceType;//9 是MORKT +} diff --git a/app/src/main/java/com/example/bpa/config/ConfigName.java b/app/src/main/java/com/example/bpa/config/ConfigName.java index 589fd3e..491e3e9 100644 --- a/app/src/main/java/com/example/bpa/config/ConfigName.java +++ b/app/src/main/java/com/example/bpa/config/ConfigName.java @@ -52,6 +52,8 @@ public class ConfigName { * 店铺设备号 */ public String AutKey="152.146"; + public String ClientId="152"; + public String DevId="146"; /** * 店铺设备号 */ @@ -79,6 +81,7 @@ public class ConfigName { * 订阅主题 */ public String mqtt_topic="DOWN/MORKT/BUSINESS/"; + public String mqtt_hearttopic="UP/MORKT/HEARTBEAT"; //endregion //region 环境配置 diff --git a/app/src/main/java/com/example/bpa/service/OrderServer.java b/app/src/main/java/com/example/bpa/service/OrderServer.java index 9054239..a8e80bf 100644 --- a/app/src/main/java/com/example/bpa/service/OrderServer.java +++ b/app/src/main/java/com/example/bpa/service/OrderServer.java @@ -4,12 +4,14 @@ import android.util.Log; import com.example.bpa.Model.BPAPackage; import com.example.bpa.Model.BPA_GoodsInfo; +import com.example.bpa.Model.BPA_HeartPackage; import com.example.bpa.Model.BatchingsInfo; import com.example.bpa.Model.Batchingstechnology; import com.example.bpa.Model.GoodsBom; import com.example.bpa.Model.IMessage; import com.example.bpa.Model.IRun; import com.example.bpa.Model.IThread; +import com.example.bpa.Model.Message_HeartModel; import com.example.bpa.Model.PushDeviceGoods; import com.example.bpa.config.ConfigName; import com.example.bpa.db.QueryDB; @@ -88,17 +90,16 @@ public class OrderServer { */ public void MqttInit() { - String ddid=ConfigName.getInstance().AutKey; - String devid=ConfigName.getInstance().AutKey; + ConfigName.getInstance().ClientId=ConfigName.getInstance().AutKey; + ConfigName.getInstance().DevId=ConfigName.getInstance().AutKey; if(ConfigName.getInstance().AutKey.contains(".")) { String[] str= ConfigName.getInstance().AutKey.split("[.]"); - ddid=str[0]; - devid=str[1]; + ConfigName.getInstance().ClientId=str[0]; + ConfigName.getInstance().DevId=str[1]; } //消息回调 - String finalDevid = devid; MQTT.get().callback=new IMessage() { @Override public void MessageRecive(String topic, String Message) { @@ -110,7 +111,7 @@ public class OrderServer { if(!msg.isEmpty()) { BPAPackage model=new Json().jsonToobject(BPAPackage.class,((String)msg)); - if((model.ClientId+"").equals(finalDevid) && model.ClientType==9) + if((model.ClientId+"").equals(ConfigName.getInstance().DevId) && model.ClientType==9) { MessageLog.ShowInfo("收到远程更新数据通知!"); RefreshTheData(model.Message); @@ -123,12 +124,11 @@ public class OrderServer { } }; //连接成功标志 - String finalDdid = ddid; MQTT.get().ConnectOk=new IRun() { @Override public void Run() { - String[] Str={ConfigName.getInstance().mqtt_topic+ finalDdid}; + String[] Str={ConfigName.getInstance().mqtt_topic+ ConfigName.getInstance().ClientId}; MQTT.get().Subscrib(Str); } }; @@ -164,9 +164,43 @@ public class OrderServer { public void RunComplete() throws InterruptedException { } }); + + ThreadManager.Get().StartLong("心跳服务", true, new IThread() { + @Override + public void Run() throws InterruptedException { + + if(MQTT.get().IsConnect) + { + try { + PushHeart(); + } catch (Exception e) { + + } + } + Thread.sleep(1000); + } + @Override + public void RunComplete() throws InterruptedException { + } + }); } + public void PushHeart() throws Exception { + BPA_HeartPackage heart=new BPA_HeartPackage(); + heart.MessageId=16; + heart.ClientId=Integer.parseInt(ConfigName.getInstance().DevId); + heart.ClientType=9; + Message_HeartModel mode=new Message_HeartModel(); + mode.DeviceType=9; + mode.Healthy=1; + heart.Message=mode; + heart.MessageVersion=0; + heart.Timestamp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); + String Message= new Json().objectToJson(BPA_HeartPackage.class,heart); + String msg= new AES().Encrypt(Message); + MQTT.get().publish(ConfigName.getInstance().mqtt_hearttopic,msg); + } /** * 刷新数据 * @param data