| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | |
| | | 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; |
| | | |
| | |
| | | private Button axleBackBtn8; |
| | | private Button axleFrontBtn9; |
| | | private Button axleBackBtn9; |
| | | |
| | | private Button viewAllLogsButton; |
| | | |
| | | |
| | | |
| | |
| | | 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(); |
| | |
| | | |
| | | } |
| | | |
| | | 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 |
| | |
| | | //SocketManager.getInstance().disconnect(); |
| | | //SocketManager.getInstance().stopServer(); |
| | | //socketManager.stopServer(); // 停止服务器 |
| | | // 移除定时任务,避免内存泄漏 |
| | | handler.removeCallbacksAndMessages(null); |
| | | } |
| | | |
| | | public static void upClient(AGVCar agvCar) { |
| | |
| | | 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); |
| | | } |
| | | } |