From 8ebcb391329a7cef64dc5365320480869f1f6b57 Mon Sep 17 00:00:00 2001 From: fyf Date: Tue, 16 May 2023 18:12:51 +0800 Subject: [PATCH] =?UTF-8?q?MQTT=20=E6=8E=A5=E6=94=B6=E8=AE=A2=E5=8D=95->?= =?UTF-8?q?=E5=AD=90=E8=AE=A2=E5=8D=95=E5=88=B6=E4=BD=9C=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/bpa/MainActivity.java | 2 +- .../main/java/com/example/bpa/app/ICSApp.java | 7 +++ .../java/com/example/bpa/config/DataBus.java | 34 ++++++++----- .../main/java/com/example/bpa/db/QueryDB.java | 4 +- .../com/example/bpa/db/file/DBHelper.java | 34 ++++++++++++- .../java/com/example/bpa/db/file/Table.java | 9 ++++ .../com/example/bpa/db/mode/BPA_SUBORDER.java | 6 +-- .../java/com/example/bpa/helper/MQTT.java | 4 ++ .../example/bpa/service/BusinessServer.java | 25 ++++++---- .../com/example/bpa/service/OrderServer.java | 50 ++++++++++++++++++- .../bpa/view/adapter/maingoods_adapter.java | 22 +++++++- .../example/bpa/view/from/yfpf_activity.java | 20 +++++++- .../example/bpa/view/mode/ResGoodsMake.java | 6 +++ .../bpa/view/mode/ResReceiveOrder.java | 20 ++++++++ 14 files changed, 209 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/example/bpa/view/mode/ResReceiveOrder.java diff --git a/app/src/main/java/com/example/bpa/MainActivity.java b/app/src/main/java/com/example/bpa/MainActivity.java index 6f736fc..6b65ad0 100644 --- a/app/src/main/java/com/example/bpa/MainActivity.java +++ b/app/src/main/java/com/example/bpa/MainActivity.java @@ -69,7 +69,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen */ @Override protected void onDestroy() { - MQTT.get().ConnMqttBroken(false);//释放mqtt + //MQTT.get().ConnMqttBroken(false);//释放mqtt ModbusTcpHelper.get().release();//释放modbus super.onDestroy(); } diff --git a/app/src/main/java/com/example/bpa/app/ICSApp.java b/app/src/main/java/com/example/bpa/app/ICSApp.java index df5da9b..2c4a271 100644 --- a/app/src/main/java/com/example/bpa/app/ICSApp.java +++ b/app/src/main/java/com/example/bpa/app/ICSApp.java @@ -7,10 +7,12 @@ import android.util.Log; import com.example.bpa.Model.IMessageLogNotify; import com.example.bpa.config.ConfigName; import com.example.bpa.db.QueryDB; +import com.example.bpa.db.file.DBHelper; import com.example.bpa.db.mode.BPA_ALERTLOG; import com.example.bpa.db.mode.BPA_LOG; import com.example.bpa.db.mode.BPA_SILOS; import com.example.bpa.db.mode.BPA_SILOSANDMATERIAL; +import com.example.bpa.db.mode.BPA_SUBORDER; import com.example.bpa.db.mode.BPA_SUGAR; import com.example.bpa.db.mode.BPA_USER; import com.example.bpa.helper.MessageLog; @@ -210,4 +212,9 @@ public class ICSApp extends Application { // QueryDB.AddSugar(sugar_B_2); // } } + + public void DBdelete() + { + DBHelper.getInstance(ConfigName.getInstance().dishesCon).DeleteCreateTables(BPA_SUBORDER.class,null); + } } diff --git a/app/src/main/java/com/example/bpa/config/DataBus.java b/app/src/main/java/com/example/bpa/config/DataBus.java index 32e971f..9fcb412 100644 --- a/app/src/main/java/com/example/bpa/config/DataBus.java +++ b/app/src/main/java/com/example/bpa/config/DataBus.java @@ -2,6 +2,7 @@ package com.example.bpa.config; import com.example.bpa.Model.ResultA; import com.example.bpa.R; +import com.example.bpa.db.mode.BPA_SUBORDER; import com.example.bpa.service.DeviceData; import com.example.bpa.db.QueryDB; import com.example.bpa.db.mode.BPA_ALERTLOG; @@ -65,16 +66,17 @@ public class DataBus { */ public makegood_adapter goodmakeadapter; /** - * 增加一个商品 - * @param id + * 增加一个订单商品 + * @param suborder */ - public void AddGoodsMake(String id) + public void AddGoodsMake(BPA_SUBORDER suborder) { try { ResGoodsMake make=new ResGoodsMake(); - make.recipes=QueryDB.GetGoodsSrecipeList(id); - make.good=QueryDB.GetGoodsId(id); + make.recipes=QueryDB.GetGoodsSrecipeList(suborder.goodsID); + make.good=QueryDB.GetGoodsId(suborder.goodsID); + make.subOrder=suborder; make.makeStatus= MakeStatus.等待制作; make.makeMs=""; make.makeProcess=0; @@ -85,19 +87,25 @@ public class DataBus { } /** - * 根据商品id,物料id修改制作进度 - * @param id + * 根据子订单id,物料id修改制作进度 + * @param suborderID * @param makeMs * @param count */ - public void UpdateGoodsMake(String id,String makeMs,int count) + public void UpdateGoodsMake(String suborderID,String makeMs,int count) { try { for (int m=0;m tables = new ArrayList(); + Table t; + t = new Table(TabName); // 创建表名称 + for (Field field : c.getFields()){ + String name=field.getName(); + String type=field.getType().getSimpleName(); + if(type.equals("String") || type.equals("Date")) + { + type="text"; + } + t.addFeild(new Feild(name, type)); // 字段名称 + } + tables.add(t); + for (Table table : tables) { + db.execSQL(table.deleteSql()); + db.execSQL(table.createSQL(premarykey)); + } + db.setTransactionSuccessful(); + db.endTransaction(); + } } diff --git a/app/src/main/java/com/example/bpa/db/file/Table.java b/app/src/main/java/com/example/bpa/db/file/Table.java index dbeb7b2..d21c522 100644 --- a/app/src/main/java/com/example/bpa/db/file/Table.java +++ b/app/src/main/java/com/example/bpa/db/file/Table.java @@ -81,4 +81,13 @@ public class Table { Log.i("sql:",sb.toString()); return sb.toString(); } + + /** + * 删除表 + * @return + */ + public String deleteSql() + { + return "drop table "+this.name; + } } diff --git a/app/src/main/java/com/example/bpa/db/mode/BPA_SUBORDER.java b/app/src/main/java/com/example/bpa/db/mode/BPA_SUBORDER.java index 9e146a2..7766e51 100644 --- a/app/src/main/java/com/example/bpa/db/mode/BPA_SUBORDER.java +++ b/app/src/main/java/com/example/bpa/db/mode/BPA_SUBORDER.java @@ -8,9 +8,9 @@ public class BPA_SUBORDER extends ModeBase{ //主订单ID public String orderID; //商品ID - public int goodsID; + public String goodsID; //数量:例如 *1 *2 - public String number; - //状态:0 未开始 1 已制作 2 已超时 + public int number; + //状态:0 未开始 1 制作中 2 已制作 3 已超时 public int status; } diff --git a/app/src/main/java/com/example/bpa/helper/MQTT.java b/app/src/main/java/com/example/bpa/helper/MQTT.java index 8879cb3..0f21d94 100644 --- a/app/src/main/java/com/example/bpa/helper/MQTT.java +++ b/app/src/main/java/com/example/bpa/helper/MQTT.java @@ -54,6 +54,7 @@ public class MQTT { public IMessage callback=null;//消息回调 public IRun ConnectOk=null; public IRun Disconnect=null; + public boolean IsConnect=false; //endregion //region 公有函数 @@ -82,6 +83,7 @@ public class MQTT { mqttClient.setCallback(new MqttCallback() { @Override//连接丢失后,会执行这里 public void connectionLost(Throwable throwable) { + IsConnect=false; MessageLog.ShowInfo("mqtt断开连接,尝试重新连接..."); if(Disconnect!=null) { @@ -122,6 +124,7 @@ public class MQTT { mqttClient.connect(options);//连接服务器,连接不上会阻塞在这 } MessageLog.ShowInfo("mqtt连接成功!"); + IsConnect=true; if(ConnectOk!=null) { ConnectOk.Run(); @@ -134,6 +137,7 @@ public class MQTT { if (mqttClient!=null && mqttClient.isConnected()) { mqttClient.disconnect(); } + IsConnect=false; MessageLog.ShowInfo("mqtt断开成功!"); } catch (MqttException e) { MessageLog.ShowError("mqtt断开失败!"+e.getMessage()); diff --git a/app/src/main/java/com/example/bpa/service/BusinessServer.java b/app/src/main/java/com/example/bpa/service/BusinessServer.java index b23b1bf..17dc0d3 100644 --- a/app/src/main/java/com/example/bpa/service/BusinessServer.java +++ b/app/src/main/java/com/example/bpa/service/BusinessServer.java @@ -8,6 +8,7 @@ import com.example.bpa.config.DataBus; import com.example.bpa.db.QueryDB; import com.example.bpa.db.mode.BPA_GOODS; import com.example.bpa.db.mode.BPA_SILOS; +import com.example.bpa.db.mode.BPA_SUBORDER; import com.example.bpa.db.mode.BPA_SUGAR; import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.ModbusTcpServer; @@ -39,14 +40,14 @@ public class BusinessServer { /** * 商品制作队列 */ - ConcurrentLinkedQueue Commoditys = new ConcurrentLinkedQueue(); + ConcurrentLinkedQueue Commoditys = new ConcurrentLinkedQueue(); /** - * 增加一个订单 + * 增加一个子订单 * * @param info 配方信息 */ - public void AddCommodity(String info) { + public void AddCommodity(BPA_SUBORDER info) { //1.添加制作队列 Commoditys.offer(info); //2.数据中心添加制作队列商品数据 @@ -62,7 +63,8 @@ public class BusinessServer { public void Run() throws InterruptedException { while (Commoditys.size() > 0) { //1.取出需要制作的订单商品 验证物料是否充足 - String GoodId = Commoditys.poll(); + BPA_SUBORDER suborder=Commoditys.poll(); + String GoodId = suborder.goodsID; // boolean IsMake=true; ResultA resultA= DataBus.getInstance().InspectMaterial(GoodId); // if(resultA.IsMake==false)//如果商品物料不足 @@ -96,7 +98,7 @@ public class BusinessServer { List goodsRecipes= entry.getValue(); String RecipesNames=""; for (ResGoodsRecipe item:goodsRecipes ){RecipesNames+=item.materialName+",";} - DataBus.getInstance().UpdateGoodsMake(good.id,"步骤【" + key+ "】准备下发物料:"+RecipesNames,count); + DataBus.getInstance().UpdateGoodsMake(suborder.id,"步骤【" + key+ "】准备下发物料:"+RecipesNames,count); for (int m = 0; m < goodsRecipes.size(); m++) { ResGoodsRecipe recipe=goodsRecipes.get(m); int materialType = recipe.materialType; @@ -166,12 +168,12 @@ public class BusinessServer { } //更新缓存商品制作列表状态 count+=goodsRecipes.size(); - DataBus.getInstance().UpdateGoodsMake(good.id,"步骤【" + key+ "】:"+RecipesNames+"-配料完成!",count); + DataBus.getInstance().UpdateGoodsMake(suborder.id,"步骤【" + key+ "】:"+RecipesNames+"-配料完成!",count); MessageLog.ShowUserMessage(UserLogEnum.订单处理日志,"["+good.name+"]步骤【" + key + "】下发"+RecipesNames+"执行完成!"); } //endregion //5.设置数据中心商品已制作完成 - DataBus.getInstance().DeleteGoodsMake(GoodId); + DataBus.getInstance().DeleteGoodsMake(suborder.id); MessageLog.ShowUserMessage(UserLogEnum.订单处理日志,"["+good.name+"]-订单执行完成,请取餐!"); } Thread.sleep(1000); @@ -190,7 +192,8 @@ public class BusinessServer { public void Run() throws InterruptedException { while (Commoditys.size() > 0) { //1.取出需要制作的订单商品 验证物料是否充足 - String GoodId = Commoditys.poll(); + BPA_SUBORDER suborder=Commoditys.poll(); + String GoodId = suborder.goodsID; // boolean IsMake=true; // ResultA resultA= DataBus.getInstance().InspectMaterial(GoodId); // if(resultA.IsMake==false)//如果商品物料不足 @@ -223,7 +226,7 @@ public class BusinessServer { List goodsRecipes= entry.getValue(); String RecipesNames=""; for (ResGoodsRecipe item:goodsRecipes ){RecipesNames+=item.materialName+",";} - DataBus.getInstance().UpdateGoodsMake(good.id,"步骤【" + key+ "】准备下发物料:"+RecipesNames,count); + DataBus.getInstance().UpdateGoodsMake(suborder.id,"步骤【" + key+ "】准备下发物料:"+RecipesNames,count); for (int m = 0; m < goodsRecipes.size(); m++) { ResGoodsRecipe recipe=goodsRecipes.get(m); int materialType = recipe.materialType; @@ -292,12 +295,12 @@ public class BusinessServer { // } //更新缓存商品制作列表状态 count+=goodsRecipes.size(); - DataBus.getInstance().UpdateGoodsMake(good.id,"步骤【" + key+ "】:"+RecipesNames+"-配料完成!",count); + DataBus.getInstance().UpdateGoodsMake(suborder.id,"步骤【" + key+ "】:"+RecipesNames+"-配料完成!",count); MessageLog.ShowUserMessage(UserLogEnum.订单处理日志,"["+good.name+"]步骤【" + key + "】下发"+RecipesNames+"执行完成!"); } //endregion //5.设置数据中心商品已制作完成 - DataBus.getInstance().DeleteGoodsMake(GoodId); + DataBus.getInstance().DeleteGoodsMake(suborder.id); MessageLog.ShowUserMessage(UserLogEnum.订单处理日志,"["+good.name+"]-订单执行完成,请取餐!"); } Thread.sleep(1000); 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 d118075..a6de64a 100644 --- a/app/src/main/java/com/example/bpa/service/OrderServer.java +++ b/app/src/main/java/com/example/bpa/service/OrderServer.java @@ -3,9 +3,15 @@ package com.example.bpa.service; import com.example.bpa.Model.IMessage; import com.example.bpa.Model.IRun; import com.example.bpa.Model.IThread; +import com.example.bpa.db.QueryDB; +import com.example.bpa.db.mode.BPA_SUBORDER; +import com.example.bpa.helper.Json; import com.example.bpa.helper.MQTT; import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.ThreadManager; +import com.example.bpa.view.mode.ResReceiveOrder; + +import java.util.concurrent.ConcurrentLinkedQueue; /** * 订单服务 @@ -26,7 +32,14 @@ public class OrderServer { } //endregion - //region + //region 变量 + /** + * 订单接收队列 + */ + ConcurrentLinkedQueue Commoditys = new ConcurrentLinkedQueue(); + //endreigon + + //region 公共函数 /** * 初始化 @@ -57,6 +70,14 @@ public class OrderServer { @Override public void MessageRecive(String topic, String Message) { MessageLog.ShowInfo("收到主题:"+topic+",数据:"+Message); + try { + if(!Message.isEmpty()) + { + ResReceiveOrder model=new Json().jsonToobject(ResReceiveOrder.class,((String)Message)); + Commoditys.offer(model); + } + } catch (Exception e) { + } } }; //连接成功标志 @@ -69,6 +90,33 @@ public class OrderServer { }; //初始化MQTT连接 MQTT.get().Connect("emqx_u_block","emqx_p_admin8765490789","10.2.1.21",1883); + + ThreadManager.Get().StartLong("云订单接收处理", true, new IThread() { + @Override + public void Run() throws InterruptedException { + if(MQTT.get().IsConnect) + { + while (Commoditys.size() > 0) { + //1.取出订单 + ResReceiveOrder order = Commoditys.poll(); + if(order.order!=null && order.subOrder!=null) + { + QueryDB.AddOrder(order.order); + for(BPA_SUBORDER suborder: order.subOrder) + { + QueryDB.AddSubOrder(suborder); + //向订单执行队列增加子订单 + BusinessServer.Get().AddCommodity(suborder); + } + } + } + } + Thread.sleep(2000); + } + @Override + public void RunComplete() throws InterruptedException { + } + }); } //endregion } diff --git a/app/src/main/java/com/example/bpa/view/adapter/maingoods_adapter.java b/app/src/main/java/com/example/bpa/view/adapter/maingoods_adapter.java index 8f8b425..c509256 100644 --- a/app/src/main/java/com/example/bpa/view/adapter/maingoods_adapter.java +++ b/app/src/main/java/com/example/bpa/view/adapter/maingoods_adapter.java @@ -12,6 +12,10 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.bpa.R; +import com.example.bpa.config.ConfigName; +import com.example.bpa.db.QueryDB; +import com.example.bpa.db.mode.BPA_ORDER; +import com.example.bpa.db.mode.BPA_SUBORDER; import com.example.bpa.service.BusinessServer; import com.example.bpa.config.DataBus; import com.example.bpa.helper.T; @@ -85,8 +89,22 @@ public class maingoods_adapter extends RecyclerView.Adapter recipes; + /** + * 商品订单数据 + */ + public BPA_SUBORDER subOrder; /** * 制作状态 */ @@ -36,6 +41,7 @@ public class ResGoodsMake { public ResGoodsMake() { good=new BPA_GOODS(); + subOrder=new BPA_SUBORDER(); recipes=new ArrayList<>(); makeStatus=MakeStatus.等待制作; makeMs=""; diff --git a/app/src/main/java/com/example/bpa/view/mode/ResReceiveOrder.java b/app/src/main/java/com/example/bpa/view/mode/ResReceiveOrder.java new file mode 100644 index 0000000..140fabe --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/mode/ResReceiveOrder.java @@ -0,0 +1,20 @@ +package com.example.bpa.view.mode; + +import com.example.bpa.db.mode.BPA_ORDER; +import com.example.bpa.db.mode.BPA_SUBORDER; + +import java.util.List; + +/** + * 接收订单Model + */ +public class ResReceiveOrder { + /** + * 主订单 + */ + public BPA_ORDER order; + /** + * 子订单 + */ + public List subOrder; +}