#
whycq
2024-08-17 20245e35da8a9d809b2de91f4ec29dd39127f9d5
app/src/main/java/com/example/agvcontroller/MainActivity.java
@@ -20,14 +20,13 @@
import com.example.agvcontroller.action.HandOutAction;
import com.example.agvcontroller.socket.NettyServerHandler;
import com.example.agvcontroller.socket.SocketManager;
import com.example.agvcontroller.utils.DialogUtil;
import com.example.agvcontroller.utils.SnowflakeIdWorker;
import java.net.Socket;
import java.sql.Time;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
public class MainActivity extends AppCompatActivity {
@@ -44,6 +43,7 @@
    private Button rotatopnLeftBtn;  // 左旋转
    private Button goForwardBtn;  // 前进
    private Button goBackwardBtn;  // 后退
    private Button modelSwitchBtn;  // 进入、退出 手动
    private Socket socket;
    SocketManager socketManager;
@@ -68,25 +68,7 @@
                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 发送消息到客户端
                handler.sendEmptyMessageDelayed(0, 100);
                boolean result = false;
                long timestamp = System.currentTimeMillis();
                while (System.currentTimeMillis() - timestamp < 5000) {
                    Object o = map.get(substring);
                    if (null != o) {
                        result = true;
                        map.remove(o);
                        break;
                    }
                }
                if (result) {
                    // alert ok
                } else {
                    throw new TimeoutException("超时");
                }
            }
            if (isOpen) {
@@ -117,12 +99,13 @@
        forceSwitchBtn = findViewById(R.id.btn_force_switch);
        goForwardBtn = findViewById(R.id.btn_go_forward);
        goBackwardBtn = findViewById(R.id.btn_go_backward);
        modelSwitchBtn = findViewById(R.id.btn_model_switch);
        Intent intent = getIntent();
        clientId = intent.getStringExtra("message");
        Log.i("message1",clientId);
        // 进出手动模式
        // 强制模式开关
        forceSwitchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
@@ -138,6 +121,47 @@
                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 发送消息到客户端
            }
        });
        // 强制模式开关
        modelSwitchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vibrator != null && vibrator.hasVibrator()) {
                    vibrator.vibrate(500);
                }
                String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16);
                AgvAction agvAction = new AgvAction<>(HandOutAction.class)
                        .setAgvNo("12")
                        .setSerialNo(substring)
                        .setVal(1)
                        .bodySync((action) -> action.setPwd((short) 21));
                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 发送消息到客户端
                boolean result = false;
                long timestamp = System.currentTimeMillis();
                Log.i("substring",substring);
                while (System.currentTimeMillis() - timestamp < 20000) {
                    Object o = map.get(substring);
                    if (null != o) {
                        result = true;
                        map.remove(o);
                        break;
                    }
                }
                if (result) {
                    // alert ok  修改按钮样式 名称
                    modelSwitchBtn.setText("自动模式");
                } else {
//                    throw new TimeoutException("超时");
                    DialogUtil.showAlertDialog(MainActivity.this, "提示", "请求应答超时!");
                }
            }
        });
        stopBtn.setOnTouchListener(new CarTouchButton());
        goForwardBtn.setOnTouchListener(new CarTouchButton());
        goBackwardBtn.setOnTouchListener(new CarTouchButton());