From 17bb52d9337328323f5f8d2a806cf4f445673b4a Mon Sep 17 00:00:00 2001 From: whycq <913841844@qq.com> Date: 星期四, 06 二月 2025 09:20:55 +0800 Subject: [PATCH] # --- app/src/main/java/com/example/agvcontroller/MainActivity.java | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 92 insertions(+), 0 deletions(-) diff --git a/app/src/main/java/com/example/agvcontroller/MainActivity.java b/app/src/main/java/com/example/agvcontroller/MainActivity.java index 9e7b4e1..19e8749 100644 --- a/app/src/main/java/com/example/agvcontroller/MainActivity.java +++ b/app/src/main/java/com/example/agvcontroller/MainActivity.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.os.Vibrator; import android.util.Log; @@ -45,7 +46,12 @@ import com.example.agvcontroller.utils.DialogUtil; import com.example.agvcontroller.utils.SnowflakeIdWorker; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + import java.net.Socket; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -55,6 +61,11 @@ public static final Map<String, Object> map = new ConcurrentHashMap(); public static final Map<String, Object> car_num = new ConcurrentHashMap(); + + private static TextView recentLogTextView; + private static Handler mainHandler; + private static List<String> logList = AGVApplication.getLogList(); + private static final int MAX_RECENT_LOGS = 10; // 鏈�澶氭樉绀� 10 鏉℃渶鏂版棩蹇� private static TextView agvBattery; @@ -113,6 +124,8 @@ private Button axleBackBtn8; private Button axleFrontBtn9; private Button axleBackBtn9; + + private Button viewAllLogsButton; @@ -452,6 +465,32 @@ liftResetBtn = findViewById(R.id.btn_lift_reset); allResetBtn = findViewById(R.id.btn_all_reset); + viewAllLogsButton = findViewById(R.id.view_all_logs_button); + recentLogTextView = findViewById(R.id.recent_log_text_view); + mainHandler = new Handler(Looper.getMainLooper()); + + // 鍒濆鍖栨棩蹇楁樉绀� + updateRecentLogTextView(); + + // 鍚姩涓�涓畾鏃朵换鍔℃潵瀹氭湡妫�鏌ユ棩蹇楀垪琛ㄧ殑鍙樺寲 + handler.postDelayed(new Runnable() { + @Override + public void run() { + updateLogDisplay(); + handler.postDelayed(this, 1000); // 姣忕妫�鏌ヤ竴娆� + } + }, 100); + + // 娉ㄥ唽 EventBus + EventBus.getDefault().register(this); + + // 璁剧疆鏌ョ湅鍏ㄩ儴鏃ュ織鎸夐挳鐨勭偣鍑讳簨浠� + viewAllLogsButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + showAllLogsActivity(); + } + }); Intent intent = getIntent(); @@ -1130,6 +1169,25 @@ } + private void updateLogDisplay() { + // 鑾峰彇鏈�鏂扮殑鏃ュ織鍒楄〃 + List<String> logs = AGVApplication.getLogList(); + // 鑾峰彇鏈�鍚庝袱鏉℃棩蹇� + int size = logs.size(); + if (size == 0) { + recentLogTextView.setText(""); + } else if (size == 1) { + recentLogTextView.setText(logs.get(0)); + } else { + String logText = logs.get(size - 2) + "\n" + // 鑾峰彇鍊掓暟绗簩鏉℃棩蹇� + logs.get(size - 1); // 鑾峰彇鏈�鍚庝竴鏉℃棩蹇� + // 鏇存柊 TextView 鐨勬樉绀哄唴瀹� + Log.d("logs", "updateLogDisplay: " + logText); + recentLogTextView.setText(logText); + } + } + + private class CarTouchButton implements View.OnTouchListener { @Override @@ -1363,6 +1421,8 @@ //SocketManager.getInstance().disconnect(); //SocketManager.getInstance().stopServer(); //socketManager.stopServer(); // 鍋滄鏈嶅姟鍣� + // 绉婚櫎瀹氭椂浠诲姟锛岄伩鍏嶅唴瀛樻硠婕� + handler.removeCallbacksAndMessages(null); } public static void upClient(AGVCar agvCar) { @@ -1377,4 +1437,36 @@ agvBattery.setText(batteryLevel); } } + + // EventBus 璁㈤槄鏂规硶锛氭帴鏀舵棩蹇楀瓧绗︿覆 + @Subscribe(threadMode = ThreadMode.MAIN) + public void onLogMessageReceived(String logMessage) { + addLog(logMessage); + } + // 鏂规硶锛氭洿鏂版渶杩戠殑鏃ュ織鏄剧ず + public static void updateRecentLogTextView() { + mainHandler.post(new Runnable() { + @Override + public void run() { + StringBuilder logBuilder = new StringBuilder(); + int start = Math.max(0, logList.size() - MAX_RECENT_LOGS); + for (int i = start; i < logList.size(); i++) { + logBuilder.append(logList.get(i)).append("\n"); + } + recentLogTextView.setText(logBuilder.toString()); + } + }); + } + // 闈欐�佹柟娉曪細娣诲姞鏃ュ織 + public static void addLog(String log) { + logList.add(log); + updateRecentLogTextView(); + } + + // 鏂规硶锛氬惎鍔� LogActivity 骞朵紶閫掓棩蹇楁暟鎹� + private void showAllLogsActivity() { + Intent intent = new Intent(this, LogActivity.class); + intent.putStringArrayListExtra("log_list", new ArrayList<>(logList)); + startActivity(intent); + } } \ No newline at end of file -- Gitblit v1.9.1