| | |
| | | import android.widget.Button; |
| | | |
| | | import com.example.agvcontroller.protocol.AgvAction; |
| | | import com.example.agvcontroller.protocol.ForceSwitchAction; |
| | | import com.example.agvcontroller.protocol2.PackagePart; |
| | | 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.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; |
| | | |
| | | |
| | | public class MainActivity extends AppCompatActivity { |
| | | |
| | | public static final Map<String, Object> map = new ConcurrentHashMap(); |
| | | |
| | | private RecyclerView recyclerView; |
| | | private ItemAdapter itemAdapter; |
| | |
| | | private Button rotatopnLeftBtn; // 左旋转 |
| | | private Button goForwardBtn; // 前进 |
| | | private Button goBackwardBtn; // 后退 |
| | | private Button modelSwitchBtn; // 进入、退出 手动 |
| | | private Socket socket; |
| | | |
| | | SocketManager socketManager; |
| | |
| | | |
| | | |
| | | |
| | | |
| | | private Handler handler = new Handler(new Handler.Callback() { |
| | | @Override |
| | | public boolean handleMessage(Message msg) { |
| | | String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,15); |
| | | |
| | | AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) |
| | | .setAgvNo("1") |
| | | .setSerialNo(substring) |
| | | .setVal(1) |
| | | .bodySync((action) -> action.setPwd((short) 21)); |
| | | |
| | | |
| | | if (isDowm) { |
| | | System.out.println(agvAction); |
| | | 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); |
| | | handler.sendEmptyMessageDelayed(0, 100); |
| | | |
| | | |
| | | |
| | | } |
| | | 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; |
| | | } |
| | |
| | | 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) { |
| | | if (vibrator != null && vibrator.hasVibrator()) { |
| | | vibrator.vibrate(500); |
| | | } |
| | | String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,15); |
| | | |
| | | AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) |
| | | 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); // 发送消息到客户端 |
| | | } |
| | | }); |
| | | |
| | | // 强制模式开关 |
| | | 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()); |
| | |
| | | @Override |
| | | public boolean onTouch(View view, MotionEvent motionEvent) { |
| | | if (view.getId() == R.id.btn_go_forward) { |
| | | Log.i("message1","1111"); |
| | | } else if (view.getId() == R.id.btn_go_backward) { |
| | | Log.i("message1","22222"); |
| | | 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; |
| | | } |