diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index d0d1fbbd..5a3ca828 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -52,6 +52,10 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; import java.util.concurrent.atomic.AtomicInteger;; @@ -144,6 +148,29 @@ public class MpMasterService extends Service { private String mIccid1 = null; private String mIccid2 = null; + //用于创建单例线程保证该线程在项目中唯一 + private static final AtomicBoolean created = new AtomicBoolean(false); +// private static Thread batteryVoltageThread; + +// public static Thread getInstance(Runnable runnable) { +// if (!created.getAndSet(true)) { +// batteryVoltageThread = new Thread(runnable); +// batteryVoltageThread.start(); +// } +// return batteryVoltageThread; +// } + + + private static ExecutorService batteryVoltageThread; + + public static synchronized ExecutorService getSingleThreadExecutor() { + if (batteryVoltageThread == null || batteryVoltageThread.isShutdown()) { + batteryVoltageThread = Executors.newSingleThreadExecutor(); + } + return batteryVoltageThread; + } + + AtomicInteger reqCode = new AtomicInteger(0); public MpMasterService() { @@ -427,7 +454,7 @@ public class MpMasterService extends Service { if (!isMpAppRunning) { // Restart MpApp - MpMasterService.restartMpApp(context,"MpMST Keep Alive Detection: NO Lock"); + MpMasterService.restartMpApp(context, "MpMST Keep Alive Detection: NO Lock"); logger.warning("Restart MpAPP as There is NO Lock"); mTimeToStartMpApp = ts; return; @@ -443,7 +470,7 @@ public class MpMasterService extends Service { // MpApp is not running if (ts - mTimeToStartMpApp >= 1800000) { // 30 minutes 30 * 60 * 1000 if (false) { - MpMasterService.restartMpApp(context.getApplicationContext(),"MpMST Keep Alive Detection"); + MpMasterService.restartMpApp(context.getApplicationContext(), "MpMST Keep Alive Detection"); } logger.warning("Restart MpAPP as it is NOT Running Prev MPAPP HB=" + @@ -694,7 +721,7 @@ public class MpMasterService extends Service { int restart = intent.getIntExtra("restart", 0); mService.logger.info("Update Config Fired ACTION=" + action + " restart=" + restart); if (restart != 0) { - MpMasterService.restartMpMasterApp(context,"Config Updated"); + MpMasterService.restartMpMasterApp(context, "Config Updated"); } else { mService.loadConfig(); mService.registerHeartbeatTimer(); @@ -1087,20 +1114,37 @@ public class MpMasterService extends Service { } private void buildChargingBatteryVoltage(long ts) { - int val = 0; - for (int idx = 0; idx < 3; idx++) { - val = MpMasterService.getInt(112); - if (val > 0) { - break; - } + if (batteryVoltageThread != null && !batteryVoltageThread.isShutdown()) { + logger.info("电压线程在执行退出"); + return; } - - if (val > 0) { - if (val > mMaxBCV) { - mMaxBCV = val; - mMaxBCVTime = ts; + getSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + logger.info("电压线程编号" + batteryVoltageThread.hashCode()); + int val = 0; + for (int idx = 0; idx < 3; idx++) { + logger.info("电压测试第" + idx + "次开始读取"); + val = MpMasterService.getInt(112); + logger.info("电压测试第" + idx + "次读取结束 " + val); + if (val > 0) { + break; + } + } + if (val > 0) { + if (val > mMaxBCV) { + mMaxBCV = val; + mMaxBCVTime = ts; + } + } } - } + }); + } public String getAndResetMaxBCV() { @@ -1163,7 +1207,7 @@ public class MpMasterService extends Service { if (rebootType == 0) { logger.warning("Recv REBOOT MpMst APP cmd"); Context context = MpMasterService.this.getApplicationContext(); - MpMasterService.restartMpMasterApp(context,reason); + MpMasterService.restartMpMasterApp(context, reason); } else { logger.warning("Recv RESET cmd"); SysApi.reboot(MpMasterService.this.getApplicationContext()); @@ -1256,8 +1300,8 @@ public class MpMasterService extends Service { return 0; } -//重启运维应用 - public static void restartMpMasterApp(Context context,String reason) { + //重启运维应用 + public static void restartMpMasterApp(Context context, String reason) { Intent intent = new Intent(context, MainActivity.class); if (intent != null) { if (!TextUtils.isEmpty(reason)) { @@ -1270,7 +1314,7 @@ public class MpMasterService extends Service { System.exit(0); } -//重启MpApp应用 + //重启MpApp应用 public static void restartMpApp(Context context, String reason) { SysApi.forceStopApp(context, MicroPhotoContext.PACKAGE_NAME_MPAPP); @@ -1295,8 +1339,8 @@ public class MpMasterService extends Service { } } -//根据包名重启应用 - public static void restartAppByPackage(Context context,String packagename, String reason) { + //根据包名重启应用 + public static void restartAppByPackage(Context context, String packagename, String reason) { SysApi.forceStopApp(context, packagename); try { @@ -1390,7 +1434,7 @@ public class MpMasterService extends Service { } } copyAssetsDir(context, "mpapp/data", tmpDestPath.getAbsolutePath()); - MpMasterService.restartMpApp(context.getApplicationContext(),"FIRST Config Init"); + MpMasterService.restartMpApp(context.getApplicationContext(), "FIRST Config Init"); } };