From 9069846c2100edbf6f4b3b2ec2f62aa7977fcaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Wed, 3 Jan 2024 09:33:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E7=82=92=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/ExecuteTheRecipe.java | 95 ++++++++++++------- .../activity/BottomNavigationNewActivity.java | 1 + 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java index 3c43682b..8c015d8c 100644 --- a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java +++ b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java @@ -420,7 +420,8 @@ public class ExecuteTheRecipe { } if (val > 0) { - Thread.sleep(val * 1000); + ThreadDelay(val); + //Thread.sleep(val * 1000); } } catch (Exception ex) { ToastUtils.error("异常信息:" + ex.getMessage()); @@ -463,7 +464,7 @@ public class ExecuteTheRecipe { final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); Log.e("等待反馈", name + ",等待中"); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * whileTime) { break; } else { @@ -480,8 +481,9 @@ public class ExecuteTheRecipe { } } - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } } catch (Exception ex) { ToastUtils.error("异常信息:" + ex.getMessage()); @@ -592,7 +594,7 @@ public class ExecuteTheRecipe { String name = "平移轴在" + writeValue; final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * whileTime) { break; } else { @@ -610,7 +612,7 @@ public class ExecuteTheRecipe { name = "倒菜完成"; IsComplete[0] = false; a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * whileTime) { break; } else { @@ -647,8 +649,9 @@ public class ExecuteTheRecipe { val = Integer.parseInt(value); } } - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } Log.d("烹饪时间", "烹饪时间完成了"); @@ -739,8 +742,9 @@ public class ExecuteTheRecipe { } } - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } } catch (Exception ex) { @@ -767,7 +771,7 @@ public class ExecuteTheRecipe { if (ExecuteCurrentOperation != null) { ExecuteCurrentOperation.Run("加水" + "|" + val); } - if (val > 0) { + if (val > 0 && !IsForcedEnd) { WritePLC("炒锅抽水", true, null); final boolean[] IsComplete = {false}; new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @@ -802,9 +806,10 @@ public class ExecuteTheRecipe { String key = entry.getKey(); String value = entry.getValue(); ToastUtils.info("工序:" + key + "," + value); - if (key.contains("(秒)")) { + if (key.contains("(秒)") && !IsForcedEnd) { int val = Integer.parseInt(value); - Thread.sleep(val * 1000); + ThreadDelay(val); + //Thread.sleep(val * 1000); } } } catch (Exception ex) { @@ -828,8 +833,9 @@ public class ExecuteTheRecipe { ToastUtils.info("工序:" + key + "," + value); if (key.contains("(秒)")) { int val = Integer.parseInt(value); - if (all_list.size() > 0) { - Thread.sleep(val * 1000); + if (all_list.size() > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } } } @@ -842,7 +848,7 @@ public class ExecuteTheRecipe { String name = "出餐启动反馈"; final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 3)) { break; } else { @@ -873,9 +879,10 @@ public class ExecuteTheRecipe { String key = entry.getKey(); String value = entry.getValue(); ToastUtils.info("工序:" + key + "," + value); - if (key.contains("(秒)")) { + if (key.contains("(秒)") && !IsForcedEnd) { int val = Integer.parseInt(value); - Thread.sleep(val * 1000); + ThreadDelay(val); + //Thread.sleep(val * 1000); } } @@ -885,7 +892,7 @@ public class ExecuteTheRecipe { String name = "炒锅清洗反馈"; final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 5)) { break; } else { @@ -1003,7 +1010,7 @@ public class ExecuteTheRecipe { String name = writeValue + "倒菜完成"; final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * whileTime) { break; } else { @@ -1035,8 +1042,9 @@ public class ExecuteTheRecipe { val = Integer.parseInt(value); } } - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } Log.d("烹饪时间", "烹饪时间完成了"); @@ -1141,7 +1149,7 @@ public class ExecuteTheRecipe { final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 10)) { break; } else { @@ -1176,8 +1184,9 @@ public class ExecuteTheRecipe { val = Integer.parseInt(value); } } - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } Log.d("烹饪时间", "烹饪时间完成了"); @@ -1220,7 +1229,7 @@ public class ExecuteTheRecipe { final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 10)) { break; } else { @@ -1240,8 +1249,9 @@ public class ExecuteTheRecipe { BPA_GOODSRECIPE data3 = Get位置("高速", "炒菜位1"); ExecuteOperationSteps(data3.processname, data3.processvalue); - if (val > 0) { - Thread.sleep(val * 1000); + if (val > 0 && !IsForcedEnd) { + ThreadDelay(val); + //Thread.sleep(val * 1000); } } catch (Exception ex) { ToastUtils.error("异常信息:" + ex.getMessage()); @@ -1264,7 +1274,8 @@ public class ExecuteTheRecipe { if (key.contains("(秒)")) { int val = Integer.parseInt(value); if (all_list.size() > 0) { - Thread.sleep(val * 1000); + ThreadDelay(val); + //Thread.sleep(val * 1000); } } } @@ -1277,7 +1288,7 @@ public class ExecuteTheRecipe { //等待机器人就位 final boolean[] IsComplete = {false}; long a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 10)) { break; } else { @@ -1297,7 +1308,7 @@ public class ExecuteTheRecipe { String name = "出餐启动反馈"; IsComplete[0] = false; a = System.currentTimeMillis(); - while (!IsComplete[0]) { + while (!IsComplete[0] && !IsForcedEnd) { if ((System.currentTimeMillis() - a) > 1000 * (whileTime * 3)) { break; } else { @@ -1867,8 +1878,7 @@ public class ExecuteTheRecipe { //强制结束任务 Object IsStop = ReadMainPLC("强制结束任务"); if (IsStop != null) { - if ((boolean) IsStop) - { + if ((boolean) IsStop) { WriteMainPLC("强制结束任务", false, null);//复位 ExecuteTheRecipe.IsPause = false; @@ -1930,4 +1940,23 @@ public class ExecuteTheRecipe { } } //endregion + + //region 延迟函数 + + /** + * 线程延迟函数(避免线程阻塞等待) + * + * @param delay + */ + public static void ThreadDelay(int delay) { + long startTime = System.currentTimeMillis(); + while ((System.currentTimeMillis() - startTime) <= (delay * 1000) && !IsForcedEnd) // + { + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + } + } + } + //endregion } diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/activity/BottomNavigationNewActivity.java b/app/src/main/java/com/bonait/bnframework/modules/home/activity/BottomNavigationNewActivity.java index 4057bdf4..9df02fd0 100644 --- a/app/src/main/java/com/bonait/bnframework/modules/home/activity/BottomNavigationNewActivity.java +++ b/app/src/main/java/com/bonait/bnframework/modules/home/activity/BottomNavigationNewActivity.java @@ -173,6 +173,7 @@ public class BottomNavigationNewActivity extends BaseActivity { runOnUiThread(new Runnable() { @Override public void run() { + ExecuteTheRecipe.StopForcedEnd(); cooking.SetStartStaus(true); ExecuteTheRecipe.IsPause = false; ExecuteTheRecipe.WritePLC("暂停开关", false, null);