From d5722c5a50829df634f96bff131fb8cc01f6a17c Mon Sep 17 00:00:00 2001
From: whycq <913841844@qq.com>
Date: 星期六, 17 八月 2024 16:27:57 +0800
Subject: [PATCH] #

---
 app/src/main/java/com/example/agvcontroller/MainActivity.java |  251 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 218 insertions(+), 33 deletions(-)

diff --git a/app/src/main/java/com/example/agvcontroller/MainActivity.java b/app/src/main/java/com/example/agvcontroller/MainActivity.java
index 7d9de0f..b77dcb1 100644
--- a/app/src/main/java/com/example/agvcontroller/MainActivity.java
+++ b/app/src/main/java/com/example/agvcontroller/MainActivity.java
@@ -4,82 +4,267 @@
 import androidx.recyclerview.widget.RecyclerView;
 
 import android.content.Context;
+import android.content.Intent;
+import android.content.res.ColorStateList;
+import android.graphics.Color;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.os.Vibrator;
 import android.util.Log;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
 import android.view.View;
 import android.widget.Button;
 
+import com.example.agvcontroller.action.ForwardBackAction;
+import com.example.agvcontroller.action.HandInAction;
+import com.example.agvcontroller.action.RotatopnLeftRight;
+import com.example.agvcontroller.protocol.AgvAction;
+import com.example.agvcontroller.action.ForceSwitchAction;
+import com.example.agvcontroller.action.HandOutAction;
+import com.example.agvcontroller.socket.NettyServerHandler;
 import com.example.agvcontroller.socket.SocketManager;
-import com.example.agvcontroller.socket.SocketTask;
+import com.example.agvcontroller.utils.DialogUtil;
+import com.example.agvcontroller.utils.SnowflakeIdWorker;
 
 import java.net.Socket;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import io.netty.util.CharsetUtil;
 
 public class MainActivity extends AppCompatActivity {
+
+    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 Vibrator vibrator;
-    private Button vibrateButton;
+    private Button stopBtn;  // 鎬ュ仠鎸夐挳
+//    private Button forceSwitchBtn;  // 鎵嬭嚜鍔ㄥ己鍒跺紑鍏�
+    private Button rotatopnLeftBtn;  // 宸︽棆杞�
+    private Button rotatopnRightBtn; // 鍙虫棆杞�
+    private Button goForwardBtn;  // 鍓嶈繘
+    private Button goBackwardBtn;  // 鍚庨��
+    private Button modelSwitchBtn;  // 杩涘叆銆侀��鍑� 鎵嬪姩
     private Socket socket;
+    private String AgvNo = "12";
     SocketManager socketManager;
+
+    private int autoVal = 1;
+    private boolean isAuto = true;
+    private boolean isGoForward = false;
+    private boolean isGoBackward = false;
+    private boolean isRotatopnRight = false;
+    private boolean isRotatopnLeft = false;
+
+
+
+
+    private Handler handler = new Handler(new Handler.Callback() {
+        @Override
+        public boolean handleMessage(Message msg) {
+            if (isGoForward) {
+                String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16);
+                AgvAction agvAction = new AgvAction<>(ForwardBackAction.class)
+                        .setAgvNo(AgvNo)
+                        .setSerialNo(substring)
+                        .setVal(1)
+                        .bodySync((action) -> action.setPwd((short) 10));
+                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                handler.sendEmptyMessageDelayed(0, 100);
+            }
+            if (isGoBackward) {
+                String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16);
+                AgvAction agvAction = new AgvAction<>(ForwardBackAction.class)
+                        .setAgvNo(AgvNo)
+                        .setSerialNo(substring)
+                        .setVal(1)
+                        .bodySync((action) -> action.setPwd((short) -10));
+                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                handler.sendEmptyMessageDelayed(0, 100);
+            }
+            if (isRotatopnRight) {
+                String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16);
+                AgvAction agvAction = new AgvAction<>(RotatopnLeftRight.class)
+                        .setAgvNo(AgvNo)
+                        .setSerialNo(substring)
+                        .setVal(1)
+                        .bodySync((action) -> action.setPwd((short) 10));
+                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                handler.sendEmptyMessageDelayed(0, 100);
+            }
+            if (isRotatopnLeft) {
+                String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16);
+                AgvAction agvAction = new AgvAction<>(RotatopnLeftRight.class)
+                        .setAgvNo(AgvNo)
+                        .setSerialNo(substring)
+                        .setVal(1)
+                        .bodySync((action) -> action.setPwd((short) -10));
+                nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                handler.sendEmptyMessageDelayed(0, 100);
+            }
+            return false;
+        }
+    });
+
+    String clientId;
+    NettyServerHandler nettyServerHandler;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
-        // 浣滀负瀹㈡埛绔繛鎺�
-        //SocketManager.getInstance().setHostAndPort("192.168.4.188", 802);
-        //SocketManager.getInstance().connect();
-
-        //SocketManager.getInstance().setPort(8080);
-        //SocketManager.getInstance().startServer();
-
-        // 璁剧疆绔彛鍙峰苟鍚姩鏈嶅姟绔�
-        //SocketManager.getInstance().setPort(8080);
-        socketManager = new SocketManager();
-        socketManager.startServer(8080);
-
-        // 鍚姩鏈嶅姟绔�
-        //new Thread(() -> {
-        //    SocketManager.getInstance().startServer();
-        //}).start();
-
+        // 鎸夐挳闇囧姩
         vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
-        vibrateButton = findViewById(R.id.btn_stop);
+        stopBtn = findViewById(R.id.btn_stop);
+//        forceSwitchBtn = findViewById(R.id.btn_force_switch);
+        goForwardBtn = findViewById(R.id.btn_go_forward);
+        goBackwardBtn = findViewById(R.id.btn_go_backward);
+        rotatopnLeftBtn = findViewById(R.id.btn_go_backward);
+        rotatopnRightBtn = findViewById(R.id.btn_go_forward);
+        modelSwitchBtn = findViewById(R.id.btn_model_switch);
 
-        vibrateButton.setOnClickListener(new View.OnClickListener() {
+        Intent intent = getIntent();
+        clientId = intent.getStringExtra("message");
+        Log.i("message1",clientId);
+
+        // 寮哄埗妯″紡寮�鍏�
+//        forceSwitchBtn.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(AgvNo)
+//                        .setSerialNo(substring)
+//                        .setVal(1)
+//                        .bodySync((action) -> action.setPwd((short) 21));
+//                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);
+                if (isAuto) {
+                    AgvAction agvAction = new AgvAction<>(HandInAction.class)
+                            .setAgvNo(AgvNo)
+                            .setSerialNo(substring)
+                            .setVal(autoVal)
+                            .bodySync((action) -> action.setPwd((short) 21));
+                    nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                } else {
+                    AgvAction agvAction = new AgvAction<>(HandOutAction.class)
+                            .setAgvNo(AgvNo)
+                            .setSerialNo(substring)
+                            .setVal(autoVal)
+                            .bodySync((action) -> action.setPwd((short) 21));
+                    nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+                }
 
-                //socket = new Socket();
-                //new SocketTask().execute("192.168.4.188", 802);
-                //ByteBuf message = Unpooled.copiedBuffer("Hello, Netty!", CharsetUtil.UTF_8);
-                //SocketManager.getInstance().sendMessage(message);
 
-                String clientId = "/192.168.4.188:63160";
-                byte[] message = new byte[]{0x01, 0x02, 0x03, 0x04}; // 绀轰緥娑堟伅
-                socketManager.sendMessage(clientId, message); // 鍙戦�佹秷鎭埌瀹㈡埛绔�
+
+
+
+                boolean result = false;
+                long timestamp = System.currentTimeMillis();
+
+                Log.i("substring",substring);
+                while (System.currentTimeMillis() - timestamp < 5000) {
+
+                    Object o = map.get(substring);
+
+                    if (null != o) {
+                        result = true;
+                        isAuto = !isAuto;
+                        map.remove(o);
+                        break;
+                    }
+                }
+
+                if (result) {
+                    // alert ok  淇敼鎸夐挳鏍峰紡 鍚嶇О
+                    if (isAuto) {
+                        modelSwitchBtn.setText("鑷姩妯″紡");
+                        autoVal = 0;
+//                        modelSwitchBtn.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
+                    } else {
+                        autoVal = 1;
+                        modelSwitchBtn.setText("鎵嬪姩妯″紡");
+                    }
+
+                } else {
+//                    throw new TimeoutException("瓒呮椂");
+                    DialogUtil.showAlertDialog(MainActivity.this, "鎻愮ず", "璇锋眰搴旂瓟瓒呮椂锛�");
+                }
             }
         });
+        stopBtn.setOnTouchListener(new CarTouchButton());
+        goForwardBtn.setOnTouchListener(new CarTouchButton());
+        goBackwardBtn.setOnTouchListener(new CarTouchButton());
+        rotatopnLeftBtn.setOnTouchListener(new CarTouchButton());
+        rotatopnRightBtn.setOnTouchListener(new CarTouchButton());
 
+    }
+
+    private class CarTouchButton implements View.OnTouchListener {
+
+        @Override
+        public boolean onTouch(View view, MotionEvent motionEvent) {
+            if (view.getId() == R.id.btn_go_forward) {
+                // 鍓嶈繘
+                if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
+                    isGoForward = true;
+                    handler.sendEmptyMessage(0);
+                } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
+                    isGoForward = false;
+                }
+            } else if (view.getId() == R.id.btn_go_backward) {
+                // 鍚庨��
+                if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
+                    isGoBackward = true;
+                    handler.sendEmptyMessage(0);
+                } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
+                    isGoBackward = false;
+                }
+            } else if (view.getId() == R.id.btn_rotatopn_left) {
+                // 宸︽棆杞�
+                if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
+                    isRotatopnLeft = true;
+                    handler.sendEmptyMessage(0);
+                } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
+                    isRotatopnLeft = false;
+                }
+            } else if (view.getId() == R.id.btn_rotatopn_right) {
+                // 鍙虫棆杞�
+                if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
+                    isRotatopnRight = true;
+                    handler.sendEmptyMessage(0);
+                } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
+                    isRotatopnRight = false;
+                }
+            }
+            return false;
+        }
     }
 
     @Override
     protected void onDestroy() {
         super.onDestroy();
         //SocketManager.getInstance().disconnect();
-
         //SocketManager.getInstance().stopServer();
-        socketManager.stopServer(); // 鍋滄鏈嶅姟鍣�
+        //socketManager.stopServer(); // 鍋滄鏈嶅姟鍣�
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1