From 2c085a0090fa0b5c2100c68ecf7de2e7d3f5592a Mon Sep 17 00:00:00 2001 From: liup <1454939542@qq.com> Date: Mon, 16 Dec 2024 09:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=BF=E7=A8=8B=E5=A4=9A?= =?UTF-8?q?=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/other.xml | 329 ------------------ .../bonait/bnframework/business/MainInit.java | 1 + .../bnframework/business/RecordManager.java | 96 +++-- .../common/db/util/LogRecordUtil.java | 61 ++++ .../bnframework/modbus/ModbusCenter.java | 64 ++-- 5 files changed, 156 insertions(+), 395 deletions(-) delete mode 100644 .idea/other.xml diff --git a/.idea/other.xml b/.idea/other.xml deleted file mode 100644 index 104e542e..00000000 --- a/.idea/other.xml +++ /dev/null @@ -1,329 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/business/MainInit.java b/app/src/main/java/com/bonait/bnframework/business/MainInit.java index c3e7a1d1..d649334e 100644 --- a/app/src/main/java/com/bonait/bnframework/business/MainInit.java +++ b/app/src/main/java/com/bonait/bnframework/business/MainInit.java @@ -62,6 +62,7 @@ public class MainInit { */ public static void Init(Application app) { + RecordManager.startSave(); //0.全局异常捕获,保存为SD卡本级目录 crash CrashHandler.getInstance().init(app); diff --git a/app/src/main/java/com/bonait/bnframework/business/RecordManager.java b/app/src/main/java/com/bonait/bnframework/business/RecordManager.java index bd996867..12c133ce 100644 --- a/app/src/main/java/com/bonait/bnframework/business/RecordManager.java +++ b/app/src/main/java/com/bonait/bnframework/business/RecordManager.java @@ -13,8 +13,11 @@ import com.bonait.bnframework.common.db.util.OrderListUtil; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; +import java.util.concurrent.ConcurrentLinkedQueue; /** * @author: liup @@ -23,6 +26,20 @@ import java.util.Date; */ public class RecordManager { private static RecordManager mInstance; //实例变量设置私有,防止直接通过类名访问 + private static ConcurrentLinkedQueue msgQueue=new ConcurrentLinkedQueue(); + public static void startSave(){ + ThreadManager.get().StartLong("日志保存", true, ()->{ + List list = new ArrayList<>(); + while(!msgQueue.isEmpty()){ + BPA_LOG_RECORD bean= msgQueue.poll(); + list.add(bean); + } + LogRecordUtil.addList(list); + Thread.sleep(30000); + }); + + } + public static synchronized RecordManager getInstance() { //静态同步方法作为唯一的实例对象获取方式 if (mInstance == null) { @@ -42,34 +59,57 @@ public class RecordManager { * @param desc */ public void addLogRecord(String typeName,String desc){ - ThreadManager.get().execute(new Thread(()->{ - try{ - BPA_LOG_RECORD log = new BPA_LOG_RECORD(); - log.userID = ConfigName.getInstance().user.userID; - log.typeName = typeName; - log.describe = desc; - log.userName = ConfigName.getInstance().user.name; - switch (typeName){ - case "登录日志":log.typeId=1; - break; - case "角色操作日志":log.typeId=2; - break; - case "数据接收":log.typeId=3; - break; - case "上传日志":log.typeId=4; - break; - case "订单处理日志":log.typeId=5; - break; - case "设备异常日志":log.typeId=6; - break; - case "页面上报日志":log.typeId=7; - break; - } - LogRecordUtil.add(log); - }catch (Exception e){ - e.printStackTrace(); - } - })); + BPA_LOG_RECORD log = new BPA_LOG_RECORD(); + log.userID = ConfigName.getInstance().user.userID; + log.typeName = typeName; + log.describe = desc; + log.userName = ConfigName.getInstance().user.name; + switch (typeName){ + case "登录日志":log.typeId=1; + break; + case "角色操作日志":log.typeId=2; + break; + case "数据接收":log.typeId=3; + break; + case "上传日志":log.typeId=4; + break; + case "订单处理日志":log.typeId=5; + break; + case "设备异常日志":log.typeId=6; + break; + case "页面上报日志":log.typeId=7; + break; + } + msgQueue.add(log); + +// ThreadManager.get().execute(new Thread(()->{ +// try{ +// BPA_LOG_RECORD log = new BPA_LOG_RECORD(); +// log.userID = ConfigName.getInstance().user.userID; +// log.typeName = typeName; +// log.describe = desc; +// log.userName = ConfigName.getInstance().user.name; +// switch (typeName){ +// case "登录日志":log.typeId=1; +// break; +// case "角色操作日志":log.typeId=2; +// break; +// case "数据接收":log.typeId=3; +// break; +// case "上传日志":log.typeId=4; +// break; +// case "订单处理日志":log.typeId=5; +// break; +// case "设备异常日志":log.typeId=6; +// break; +// case "页面上报日志":log.typeId=7; +// break; +// } +// LogRecordUtil.add(log); +// }catch (Exception e){ +// e.printStackTrace(); +// } +// })); } /** diff --git a/app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java b/app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java index efba5d7f..b3e507b0 100644 --- a/app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java +++ b/app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java @@ -1,10 +1,18 @@ package com.bonait.bnframework.common.db.util; +import android.content.ContentValues; +import android.database.sqlite.SQLiteDatabase; + import com.apkfuns.logutils.LogUtils; +import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.db.QueryDB; +import com.bonait.bnframework.common.db.file.DBHelper; import com.bonait.bnframework.common.db.mode.BPA_LOG_RECORD; +import com.bonait.bnframework.common.helper.Tools; import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * @author: liup @@ -19,7 +27,60 @@ public class LogRecordUtil { public static boolean add(BPA_LOG_RECORD data) { return QueryDB.Add(BPA_LOG_RECORD.class, data); } + /** + * 批量新增 + * @param list + * @return + */ + public static boolean addList( List list) { + if(list.isEmpty()){ + return true; + } + QueryDB.lock.lock(); + SQLiteDatabase db = DBHelper.getInstance(ConfigName.getInstance().dishesCon).getWritableDatabase(); + try { + long insert=-1; + db.beginTransaction(); + for(int i = 0; i < list.size(); i++){ + ContentValues cv = new ContentValues(); + Map map = Tools.getObjValue(list.get(i)); + if (map.get("id").toString().isEmpty()) + return false; + for (String key : map.keySet()) { + Object value = map.get(key); + if (value instanceof String) { + cv.put(key, (String) value); + } else if (value instanceof Integer) { + cv.put(key, ((Integer) value).intValue()); + } else if (value instanceof Double) { + cv.put(key, ((Double) value).doubleValue()); + } else if (value instanceof Float) { + cv.put(key, ((Float) value).floatValue()); + } else if (value instanceof Long) { + cv.put(key, ((Long) value).longValue()); + } else if (value instanceof Boolean) { + cv.put(key, ((Boolean) value).booleanValue()); + } + } + insert = db.insertOrThrow(BPA_LOG_RECORD.class.getSimpleName(), null, cv); + if (insert == -1) { + throw new Exception("Failed to insert data at index " + i); + } + } + LogUtils.d("addList 批量添加日志"); + + db.setTransactionSuccessful(); + return insert > 0; + } catch (Exception e) { + LogUtils.d("addList 批量新增异常"); + } finally { + db.endTransaction(); + db.close(); + QueryDB.lock.unlock(); + } + return false; + } /** * 修改日志数据 * @param data diff --git a/app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java b/app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java index 1f171892..7419204a 100644 --- a/app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java +++ b/app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java @@ -84,13 +84,18 @@ public class ModbusCenter { public static void Listening() { - ThreadManager.get().StartLong("炒锅1PLC设备数据监听", true, () -> { + ThreadManager.get().StartLong("炒锅PLC设备数据监听", true, () -> { try { if (Wok1ModbusTcpServer.get().plcIsConnect) { WritePlc(1,"心跳位", 0, null); TempControl(1); ReadPLCStatus(1); } + if (Wok2ModbusTcpServer.get().plcIsConnect) { + WritePlc(2,"心跳位", 0, null); + TempControl(2); + ReadPLCStatus(2); + } try{ if(Wok1ModbusTcpServer.get().plcIsConnect){ int i = 0; @@ -132,29 +137,6 @@ public class ModbusCenter { }else { Wok1ModbusTcpServer.get().ReConnect("Connection refused"); } - }catch (Exception ex){ - LogUtils.d("异常信息:" + ex.getMessage()); - }finally { - if (Wok1ModbusTcpServer.get().plcIsConnect) { - WritePlc(1,"心跳位", 0, null); - TempControl(1); - ReadPLCStatus(1); - } - } - } catch (Exception ex) { - LogUtils.d("异常信息:" + ex.getMessage()); - } - Thread.sleep(50); - - }); - ThreadManager.get().StartLong("炒锅2PLC设备数据监听", true, () -> { - try { - if (Wok2ModbusTcpServer.get().plcIsConnect) { - WritePlc(2,"心跳位", 0, null); - TempControl(2); - ReadPLCStatus(2); - } - try{ if(Wok2ModbusTcpServer.get().plcIsConnect){ int i=0; for (Res_PLCADDRESS item : ConfigName.getInstance().PlcAddress_Wok) { @@ -197,18 +179,34 @@ public class ModbusCenter { }catch (Exception ex){ LogUtils.d("异常信息:" + ex.getMessage()); }finally { + if (Wok1ModbusTcpServer.get().plcIsConnect) { + WritePlc(1,"心跳位", 0, null); + TempControl(1); + ReadPLCStatus(1); + } if (Wok2ModbusTcpServer.get().plcIsConnect) { WritePlc(2,"心跳位", 0, null); TempControl(2); ReadPLCStatus(2); } } + + if (Wok2ModbusTcpServer.get().plcIsConnect) { + WritePlc(1,"心跳位", 0, null); + WritePlc(2,"心跳位", 0, null); + TempControl(2); + ReadPLCStatus(2); + } + } catch (Exception ex) { LogUtils.d("异常信息:" + ex.getMessage()); } - Thread.sleep(50); + Thread.sleep(100); + }); - ThreadManager.get().StartLong("货架PLC设备数据监听", true, () -> { + + + ThreadManager.get().StartLong("货架和机器人PLC设备数据监听", true, () -> { try { try{ if(RackModbusTcpServer.get().plcIsConnect){ @@ -283,17 +281,7 @@ public class ModbusCenter { }else { RackModbusTcpServer.get().ReConnect("Connection refused"); } - }catch (Exception ex){ - LogUtils.d("异常信息:" + ex.getMessage()); - } - } catch (Exception ex) { - LogUtils.d("异常信息:" + ex.getMessage()); - } - Thread.sleep(50); - }); - ThreadManager.get().StartLong("机器人PLC设备数据监听", true, () -> { - try { - try{ + if(RobotModbusTcpServer.get().plcIsConnect){ int i = 0; for (Res_PLCADDRESS item : ConfigName.getInstance().PlcAddress_Robot) { @@ -356,7 +344,7 @@ public class ModbusCenter { } catch (Exception ex) { LogUtils.d("异常信息:" + ex.getMessage()); } - Thread.sleep(50); + Thread.sleep(100); });