#
whycq
2024-08-14 8a340545d76af29dd85d4be2812353294d236011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.example.agvcontroller;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
 
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.MotionEvent;
import android.view.View;
import android.widget.Button;
 
import com.example.agvcontroller.protocol2.AgvPackage;
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 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 RecyclerView recyclerView;
    private ItemAdapter itemAdapter;
    private List<Item> itemList;
    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) {
            // 在这里进行打印输出
            System.out.println("打印输出");
            if (isDowm) {
                byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息
                nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端
                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);
 
        Intent intent = getIntent();
        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); // 发送消息到客户端
 
        //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());
 
    }
 
    private class CarTouchButton implements View.OnTouchListener {
 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
                isDowm = true;
                handler.sendEmptyMessage(0);
            } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
                isDowm = false;
            }
            return false;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //SocketManager.getInstance().disconnect();
 
        //SocketManager.getInstance().stopServer();
        //socketManager.stopServer(); // 停止服务器
    }
}