#
whycq
2024-08-19 c5944fcf50cdb31c6c05bdcf6a70d9ffe0c031db
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.example.agvcontroller;
 
import androidx.appcompat.app.AppCompatActivity;
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 android.widget.RelativeLayout;
 
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.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();
    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 stopBtn;  // 急停按钮
//    private Button forceSwitchBtn;  // 手自动强制开关
    private Button rotatopnLeftBtn;  // 左旋转
    private Button rotatopnRightBtn; // 右旋转
    private Button goForwardBtn;  // 前进
    private Button goBackwardBtn;  // 后退
    private Button modelSwitchBtn;  // 进入、退出 手动
 
    private Button bodySwitchBtn; // 底盘/货叉切换
    private Socket socket;
    private String AgvNo = "12";
    private RelativeLayout layout_bottom;
    private RelativeLayout layout_top;
    private RelativeLayout layout_single;
    private RelativeLayout layout_reset;
 
 
    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);
 
        // 按钮震动
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        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_rotatopn_left);
        rotatopnRightBtn = findViewById(R.id.btn_rotatopn_right);
        modelSwitchBtn = findViewById(R.id.btn_model_switch);
 
        bodySwitchBtn = findViewById(R.id.btn_body_switch);
 
        layout_bottom = findViewById(R.id.layout_bottom);
        layout_top = findViewById(R.id.layout_top);
        layout_single = findViewById(R.id.layout_single);
        layout_reset = findViewById(R.id.layout_reset);
 
        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); // 发送消息到客户端
                }
 
 
 
 
 
                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;
 
        }
    }
 
    private boolean isVisble = true;
 
    // 货叉等按钮
    public void switchover(View view) {
        if (isVisble) {
            layout_bottom.setVisibility(View.GONE);
            layout_top.setVisibility(View.VISIBLE);
            bodySwitchBtn.setText("货叉");
        } else {
            layout_bottom.setVisibility(View.VISIBLE);
            layout_top.setVisibility(View.GONE);
            bodySwitchBtn.setText("底盘");
        }
        isVisble = !isVisble;
 
        layout_single.setVisibility(View.GONE);
        layout_reset.setVisibility(View.GONE);
 
    }
 
    // 单轴使能按钮
    public void switchSingle(View view) {
        layout_bottom.setVisibility(View.GONE);
        layout_top.setVisibility(View.GONE);
        layout_single.setVisibility(View.VISIBLE);
        layout_reset.setVisibility(View.GONE);
    }
 
    // 复位按钮
    public void switchRest(View view) {
        layout_bottom.setVisibility(View.GONE);
        layout_top.setVisibility(View.GONE);
        layout_single.setVisibility(View.GONE);
        layout_reset.setVisibility(View.VISIBLE);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //SocketManager.getInstance().disconnect();
        //SocketManager.getInstance().stopServer();
        //socketManager.stopServer(); // 停止服务器
    }
}