| | |
| | | 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 android.widget.GridLayout; |
| | | import android.widget.RelativeLayout; |
| | | import android.widget.Switch; |
| | | import android.widget.TextView; |
| | | |
| | | import com.example.agvcontroller.action.AllResetAction; |
| | | import com.example.agvcontroller.action.BackPaddle; |
| | |
| | | import com.example.agvcontroller.action.HandLift; |
| | | import com.example.agvcontroller.action.LiftResetAction; |
| | | import com.example.agvcontroller.action.LoadResetAction; |
| | | import com.example.agvcontroller.action.LoginReset; |
| | | import com.example.agvcontroller.action.RotatopnLeftRight; |
| | | import com.example.agvcontroller.action.SingleSwitchAction; |
| | | import com.example.agvcontroller.action.SingleSwitchRunAction; |
| | |
| | | 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 RecyclerView recyclerView; |
| | | private ItemAdapter itemAdapter; |
| | | private List<Item> itemList; |
| | | 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 static TextView agvSocket; |
| | | private static TextView agvNo; |
| | | private static TextView agvStatus; |
| | | private static TextView agvPositionId; |
| | | private static TextView agvPositionX; |
| | | private static TextView agvPositionY; |
| | | private static TextView agvForkHeight; |
| | | private static TextView agvForkExtend; |
| | | private static TextView agvForkAngle; |
| | | private static TextView agvError; |
| | | private Button reLogin; |
| | | |
| | | |
| | | |
| | | private Vibrator vibrator; |
| | | private Button stopBtn; // 急停按钮 |
| | | // private Button forceSwitchBtn; // 手自动强制开关 |
| | |
| | | private Button axleFrontBtn9; |
| | | private Button axleBackBtn9; |
| | | |
| | | private Button viewAllLogsButton; |
| | | |
| | | |
| | | |
| | | private Button bodySwitchBtn; // 底盘/货叉切换 |
| | | private Socket socket; |
| | | private String AgvNo = "1"; |
| | | |
| | | private RelativeLayout layout_bottom; |
| | | private RelativeLayout layout_top; |
| | | private GridLayout layout_single; |
| | |
| | | return false; |
| | | } |
| | | }); |
| | | |
| | | static String AgvNo = "1"; |
| | | static String clientId; |
| | | NettyServerHandler nettyServerHandler; |
| | | |
| | | |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.activity_main); |
| | | |
| | | agvBattery = findViewById(R.id.agv_battery); |
| | | agvSocket = findViewById(R.id.socket); |
| | | agvNo = findViewById(R.id.agv_no); |
| | | agvStatus = findViewById(R.id.agv_status); |
| | | agvPositionId = findViewById(R.id.agv_position_id); |
| | | agvPositionX = findViewById(R.id.agv_position_x); |
| | | agvPositionY = findViewById(R.id.agv_position_y); |
| | | agvForkHeight = findViewById(R.id.agv_position_height); |
| | | agvForkExtend = findViewById(R.id.agv_ford); |
| | | agvForkAngle = findViewById(R.id.agv_ratio); |
| | | agvError = findViewById(R.id.agv_error); |
| | | reLogin = findViewById(R.id.relogin_button); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | // 按钮震动 |
| | |
| | | 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(); |
| | | if (intent != null) { |
| | | String newClientId = intent.getStringExtra("clientId"); |
| | | if (newClientId != null) { |
| | | clientId = newClientId; |
| | | AgvNo = intent.getStringExtra("agvNo"); |
| | | } |
| | | AGVCar car = (AGVCar) intent.getSerializableExtra("item"); |
| | | clientId = car.getClientId(); |
| | | AgvNo = car.getAgvNo(); |
| | | } |
| | | |
| | | Log.i("message1",clientId); |
| | | |
| | | reLogin.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16); |
| | | AgvAction agvAction = new AgvAction<>(LoginReset.class) |
| | | .setAgvNo(AgvNo) |
| | | .setSerialNo(substring) |
| | | .setVal(1) |
| | | .bodySync((action) -> action.setPwd((short) 1)); |
| | | nettyServerHandler.sendMessageToClient(clientId, agvAction); // 发送消息到客户端 |
| | | } |
| | | }); |
| | | // 前拨杆伸出 |
| | | frontPaddleExtendBtn.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | |
| | | |
| | | } |
| | | |
| | | 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(String newClientId) { |
| | | clientId = newClientId; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static void upClient(AGVCar agvCar) { |
| | | clientId = agvCar.getClientId(); |
| | | AgvNo = agvCar.getAgvNo(); |
| | | agvNo.setText("AGV编号:" + AgvNo); |
| | | agvBattery.setText("电量:" + agvCar.getBattery() + "%"); |
| | | agvSocket.setText(agvCar.getStatus() == 1 ? "已连接" : "未连接"); |
| | | agvStatus.setText("AGV状态:" + agvCar.getStatus()); |
| | | agvPositionId.setText("ID:" + agvCar.getPositionID()); |
| | | agvPositionX.setText("(X):" + agvCar.getPositionX()); |
| | | agvPositionY.setText("(Y):" + agvCar.getPositionY()); |
| | | agvForkHeight.setText("当前高度:" + agvCar.getForkHeight() + "mm"); |
| | | agvForkExtend.setText("货叉伸出距离:" + agvCar.getForkExtend() + "mm"); |
| | | agvForkAngle.setText("货叉旋转角度:" + agvCar.getForkAngle() + "°"); |
| | | agvError.setText("AGV故障:" + agvCar.getAgvError()); |
| | | // updateAgvBatteryText("电量:" + agvCar.getBattery() + "%"); |
| | | } |
| | | |
| | | private static void updateAgvBatteryText(String batteryLevel) { |
| | | if (agvBattery != null) { |
| | | 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); |
| | | } |
| | | } |