| | |
| | | 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; |
| | |
| | | import android.view.View; |
| | | 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.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.util.List; |
| | | |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.buffer.Unpooled; |
| | | import io.netty.util.CharsetUtil; |
| | | |
| | | public class MainActivity extends AppCompatActivity { |
| | | |
| | |
| | | private Vibrator vibrator; |
| | | private Button vibrateButton; |
| | | private Socket socket; |
| | | |
| | | SocketManager socketManager; |
| | | |
| | | private boolean isDowm = false; |
| | | private Handler handler = new Handler(new Handler.Callback() { |
| | | @Override |
| | | public boolean handleMessage(Message msg) { |
| | | String substring = String.valueOf(new SnowflakeIdWorker().nextId()).substring(0,3); |
| | | |
| | | AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) |
| | | .setAgvNo("1") |
| | | .setSerialNo(substring) |
| | | .setVal(1) |
| | | .bodySync((action) -> action.setPwd((short) 21)); |
| | | |
| | | |
| | | if (isDowm) { |
| | | System.out.println(agvAction); |
| | | // byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息 |
| | | nettyServerHandler.sendMessageToClient(clientId, agvAction); // 发送消息到客户端 |
| | | // handler.sendEmptyMessageDelayed(0, 100); |
| | | } |
| | | return false; |
| | | } |
| | | }); |
| | | |
| | | String clientId; |
| | | NettyServerHandler nettyServerHandler; |
| | |
| | | vibrateButton = findViewById(R.id.btn_stop); |
| | | |
| | | Intent intent = getIntent(); |
| | | //String clientId = intent.getStringExtra("message"); |
| | | clientId = intent.getStringExtra("message"); |
| | | Log.i("message1",clientId); |
| | | // 初始化单轴使能 |
| | | int single = 9; |
| | | int len = PackagePart.UNIQUENO.getLen() |
| | | + PackagePart.TIMESTAMP.getLen() |
| | | + PackagePart.COMMAND_MARK.getLen() |
| | | + 16 + 1 + 1 + 1; |
| | | for (int i = 0; i < single; i++) { |
| | | |
| | | } |
| | | byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息 |
| | | nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端 |
| | | |
| | |
| | | @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); // 发送消息到客户端 |
| | | isDowm = true; |
| | | handler.sendEmptyMessage(0); |
| | | } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) { |
| | | byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x07}; // 示例消息 |
| | | nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端 |
| | | isDowm = false; |
| | | } |
| | | return false; |
| | | } |