From 5bdad72f5d5077ca875dd03cfdaafb3d7aba93da Mon Sep 17 00:00:00 2001 From: whycq <913841844@qq.com> Date: 星期五, 16 八月 2024 14:36:58 +0800 Subject: [PATCH] # --- app/src/main/java/com/example/agvcontroller/MainActivity.java | 148 ++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 114 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/example/agvcontroller/MainActivity.java b/app/src/main/java/com/example/agvcontroller/MainActivity.java index 2df3a7f..b08e87c 100644 --- a/app/src/main/java/com/example/agvcontroller/MainActivity.java +++ b/app/src/main/java/com/example/agvcontroller/MainActivity.java @@ -6,6 +6,8 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.os.Vibrator; import android.util.Log; import android.view.KeyEvent; @@ -13,62 +15,132 @@ import android.view.View; import android.widget.Button; +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.SnowflakeIdWorker; import java.net.Socket; -import java.util.Arrays; +import java.sql.Time; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeoutException; -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(); private RecyclerView recyclerView; private ItemAdapter itemAdapter; private List<Item> itemList; private Vibrator vibrator; - private Button vibrateButton; + private Button stopBtn; // 鎬ュ仠鎸夐挳 + private Button forceSwitchBtn; // 鎵嬭嚜鍔ㄥ己鍒跺紑鍏� + private Button rotatopnRightBtn; // 鍙虫棆杞� + private Button rotatopnLeftBtn; // 宸︽棆杞� + private Button goForwardBtn; // 鍓嶈繘 + private Button goBackwardBtn; // 鍚庨�� private Socket socket; + SocketManager socketManager; + + private boolean isDowm = false; + private boolean isOpen = false; + + + + + private Handler handler = new Handler(new Handler.Callback() { + @Override + public boolean handleMessage(Message msg) { + if (isDowm) { + String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16); + AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) + .setAgvNo("1") + .setSerialNo(substring) + .setVal(1) + .bodySync((action) -> action.setPwd((short) 21)); + + 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) { + String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,16); + AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) + .setAgvNo("12") + .setSerialNo(substring) + .setVal(1) + .bodySync((action) -> action.setPwd((short) 21)); + 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); + // 鎸夐挳闇囧姩 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); Intent intent = getIntent(); - //String clientId = intent.getStringExtra("message"); clientId = intent.getStringExtra("message"); Log.i("message1",clientId); - // 鍒濆鍖栧崟杞翠娇鑳� - byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 绀轰緥娑堟伅 - nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� - //vibrateButton.setOnClickListener(new View.OnClickListener() { - // @Override - // public void onClick(View v) { - // if (vibrator != null && vibrator.hasVibrator()) { - // //vibrator.vibrate(500); - // } - // - // //String clientId = "/192.168.4.188:63160"; - // byte[] message1 = new byte[]{0x01, 0x02, 0x03, 0x04}; // 绀轰緥娑堟伅 - // Log.i("message2",clientId); - // Log.i("message3", Arrays.toString(message1)); - // - // nettyServerHandler.sendMessageToClient(clientId, message1); // 鍙戦�佹秷鎭埌瀹㈡埛绔� - // } - //}); - vibrateButton.setOnTouchListener(new CarTouchButton()); + // 杩涘嚭鎵嬪姩妯″紡 + 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("12") + .setSerialNo(substring) + .setVal(1) + .bodySync((action) -> action.setPwd((short) 21)); + nettyServerHandler.sendMessageToClient(clientId, agvAction); // 鍙戦�佹秷鎭埌瀹㈡埛绔� + } + }); + stopBtn.setOnTouchListener(new CarTouchButton()); + goForwardBtn.setOnTouchListener(new CarTouchButton()); + goBackwardBtn.setOnTouchListener(new CarTouchButton()); } @@ -76,21 +148,29 @@ @Override public boolean onTouch(View view, MotionEvent motionEvent) { - if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) { - byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 绀轰緥娑堟伅 - nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� - } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) { - byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x07}; // 绀轰緥娑堟伅 - nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� + if (view.getId() == R.id.btn_go_forward) { + if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) { + isDowm = true; + handler.sendEmptyMessage(0); + } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) { + isDowm = false; + } + } else if (view.getId() == R.id.btn_go_backward) { + if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) { + isOpen = true; + handler.sendEmptyMessage(0); + } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) { + isOpen = false; + } } return false; } } + @Override protected void onDestroy() { super.onDestroy(); //SocketManager.getInstance().disconnect(); - //SocketManager.getInstance().stopServer(); //socketManager.stopServer(); // 鍋滄鏈嶅姟鍣� } -- Gitblit v1.9.1