Browse Source

优化线程多创建

炒锅机器人_一拖多
liup 1 week ago
parent
commit
2c085a0090
5 changed files with 156 additions and 395 deletions
  1. +0
    -329
      .idea/other.xml
  2. +1
    -0
      app/src/main/java/com/bonait/bnframework/business/MainInit.java
  3. +68
    -28
      app/src/main/java/com/bonait/bnframework/business/RecordManager.java
  4. +61
    -0
      app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java
  5. +26
    -38
      app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java

+ 0
- 329
.idea/other.xml View File

@@ -1,329 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="direct_access_persist.xml">
<option name="deviceSelectionList">
<list>
<PersistentDeviceSelectionData>
<option name="api" value="27" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="F01L" />
<option name="id" value="F01L" />
<option name="manufacturer" value="FUJITSU" />
<option name="name" value="F-01L" />
<option name="screenDensity" value="360" />
<option name="screenX" value="720" />
<option name="screenY" value="1280" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="28" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="SH-01L" />
<option name="id" value="SH-01L" />
<option name="manufacturer" value="SHARP" />
<option name="name" value="AQUOS sense2 SH-01L" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="Lenovo" />
<option name="codename" value="TB370FU" />
<option name="id" value="TB370FU" />
<option name="manufacturer" value="Lenovo" />
<option name="name" value="Tab P12" />
<option name="screenDensity" value="340" />
<option name="screenX" value="1840" />
<option name="screenY" value="2944" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="samsung" />
<option name="codename" value="a51" />
<option name="id" value="a51" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy A51" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="akita" />
<option name="id" value="akita" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="b0q" />
<option name="id" value="b0q" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S22 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="32" />
<option name="brand" value="google" />
<option name="codename" value="bluejay" />
<option name="id" value="bluejay" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="caiman" />
<option name="id" value="caiman" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro" />
<option name="screenDensity" value="360" />
<option name="screenX" value="960" />
<option name="screenY" value="2142" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="id" value="comet" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="29" />
<option name="brand" value="samsung" />
<option name="codename" value="crownqlteue" />
<option name="id" value="crownqlteue" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Note9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2220" />
<option name="screenY" value="1080" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="dm3q" />
<option name="id" value="dm3q" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S23 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="e1q" />
<option name="id" value="e1q" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S24" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix_camera" />
<option name="id" value="felix_camera" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold (Camera-enabled)" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="gts8uwifi" />
<option name="id" value="gts8uwifi" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S8 Ultra" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1848" />
<option name="screenY" value="2960" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="husky" />
<option name="id" value="husky" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8 Pro" />
<option name="screenDensity" value="390" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="motorola" />
<option name="codename" value="java" />
<option name="id" value="java" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="G20" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="komodo" />
<option name="id" value="komodo" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro XL" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="lynx" />
<option name="id" value="lynx" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="google" />
<option name="codename" value="oriole" />
<option name="id" value="oriole" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="panther" />
<option name="id" value="panther" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q5q" />
<option name="id" value="q5q" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold5" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1812" />
<option name="screenY" value="2176" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q6q" />
<option name="id" value="q6q" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1856" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="r11" />
<option name="id" value="r11" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Watch" />
<option name="screenDensity" value="320" />
<option name="screenX" value="384" />
<option name="screenY" value="384" />
<option name="type" value="WEAR_OS" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="redfin" />
<option name="id" value="redfin" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 5" />
<option name="screenDensity" value="440" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="shiba" />
<option name="id" value="shiba" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="tangorpro" />
<option name="id" value="tangorpro" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Tablet" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="tokay" />
<option name="id" value="tokay" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
</list>
</option>
</component>
</project>

+ 1
- 0
app/src/main/java/com/bonait/bnframework/business/MainInit.java View File

@@ -62,6 +62,7 @@ public class MainInit {
*/
public static void Init(Application app)
{
RecordManager.startSave();
//0.全局异常捕获,保存为SD卡本级目录 crash
CrashHandler.getInstance().init(app);



+ 68
- 28
app/src/main/java/com/bonait/bnframework/business/RecordManager.java View File

@@ -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<BPA_LOG_RECORD> msgQueue=new ConcurrentLinkedQueue<BPA_LOG_RECORD>();
public static void startSave(){
ThreadManager.get().StartLong("日志保存", true, ()->{
List<BPA_LOG_RECORD> 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();
// }
// }));
}

/**


+ 61
- 0
app/src/main/java/com/bonait/bnframework/common/db/util/LogRecordUtil.java View File

@@ -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<BPA_LOG_RECORD> 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<String, Object> 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


+ 26
- 38
app/src/main/java/com/bonait/bnframework/modbus/ModbusCenter.java View File

@@ -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);

});



Loading…
Cancel
Save