562935844@qq.com
2023-11-27 566ddb2ea03e2514de50f2ca861a2674f6e840ac
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//package com.hdl.sdk.ttl_sdk.bean;
//
//import android.os.Bundle;
//import android.view.View;
//import android.widget.Button;
//import android.widget.RelativeLayout;
//import android.widget.TextView;
//
////import com.google.gson.Gson;
////import com.google.gson.Gson;
//import com.google.gson.Gson;
//import com.hdl.sdk.ttl.Config.MCUConstants;
//import com.hdl.sdk.ttl.HDLDeviceManger.Core.Crc;
//import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
//import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.EventCode;
//import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.MCUFeedBackEvent;
//import com.hdl.sdk.ttl.Utils.HDLUtlis.HDLStringUtils;
//import com.hdl.sdk.ttl_sdk.R;
//import com.hdl.sdk.ttl_sdk.base.BaseActivity;
//import com.hdl.sdk.ttl_sdk.bean.ZigBeeCommand;
//import com.hdl.sdk.ttl_sdk.bean.ZigBeeCtrl;
//import com.hdl.sdk.ttl_sdk.bean.ZigBeeCtrl2;
//import com.hdl.sdk.ttl_sdk.bean.ZigBeeDataBean;
//import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
//
//import org.greenrobot.eventbus.Subscribe;
//import org.greenrobot.eventbus.ThreadMode;
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Timer;
//import java.util.TimerTask;
//
//public class ZigbeeActivity2 extends BaseActivity {
//    /**
//     * Topbar
//     */
//    private RelativeLayout topBarBack;
//    private TextView topBarTitle;
//    private Button btn_PassThrough, btn_PassThrough_close;
//    private Button btn_open_light, btn_open_switch,btn_open_switch2;
//
//
//    private TextView  tv_other_mes;
//    private Button btn_close;
//    private boolean bReceivingMode = false;//接收者模式
//
//
//
//
//
//    /**
//     * 复写isRegisterEventBus()  要注册使用EventBus,这里要设置返回true
//     *
//     * @return true
//     */
//    @Override
//    protected boolean isRegisterEventBus() {
//        return true;
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_zigbee);
//        initToolbar();
//        initView();
//        initOnClick();
//        getJson();
//        startHDLThread();
//        startSendHeartPacketTimer();
//    }
//
//
//
//    /**
//     * 初始化Toolbar
//     */
//    private void initToolbar() {
//        topBarBack = findViewById(R.id.ll_top_b_left);
//        setViewVisible(topBarBack);
//        topBarTitle = findViewById(R.id.tv_top_b_header_title);
//        topBarBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
//        topBarTitle.setText("Zigbee调试");
//    }
//
//    /**
//     * initView
//     */
//    private void initView() {
//        btn_PassThrough = findViewById(R.id.btn_PassThrough);
//        btn_PassThrough_close = findViewById(R.id.btn_PassThrough_close);
//        tv_other_mes = findViewById(R.id.tv_other_mes);
//
//        btn_close = findViewById(R.id.btn_close);
//
//        btn_open_light = findViewById(R.id.btn_open_light);
//        btn_open_switch = findViewById(R.id.btn_open_switch);
//
//        btn_open_switch2= findViewById(R.id.btn_open_switch2);
//
//    }
//
//    boolean dd = true;
//    /**
//     * initOnClick
//     */
//    private void initOnClick() {
//        btn_PassThrough.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////
////                SendCtrl222();
////                dd = !dd;
////                if(dd){
////                    SendGetDeviceList();
////                }else {
////                    SendCtrl222();
////                }
////                SendCtrl();
//                SendGetDeviceList();
////                SendCtrlLight();
////                SendCtrl();
////                SendCtrlLight();
//            }
//        });
//
//        btn_PassThrough_close.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                setHDLPassThroughOpen(false);
//                tv_other_mes.setText("关闭透传");
//
//            }
//        });
//
//        btn_close.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                cancelTimer();
//            }
//        });
//
//        btn_open_light.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                SendCtrlLight();
//            }
//        });
//
//        btn_open_switch.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                SendCtrl();
//            }
//        });
//
//
//        btn_open_switch2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                SendCtrl222();
//            }
//        });
//
//    }
//
//    /**
//     * 2019-8-9
//     * 发送透传参数
//     * 透传数据,不走bus协议,直接转发,不做数据格式处理
//     * mcuSendTransparentData接口调用一次以后,默认所有接收数据走透传
//     * 可以调用setHDLPassThroughOpen关闭透传数据
//     */
//    private void mcuSendTransparentData() {
//        startSendHeartPacketTimer();
//    }
//
//    /**
//     * 设置是否开启透传数据
//     * 要想直接接收透传数据,直接调用这个接口打开就可以,不走bus协议处理
//     * 不想透传的话,要记得设置回关闭状态
//     */
//    private void setHDLPassThroughOpen(Boolean bOpen){
//
//        HDLCommand.setHDLPassThroughOpen(bOpen);
//
//    }
//
//
////    private int HeartData = 0x2000;
////    private byte[]  HeartDataBytes = {(byte) (HeartData / 256),(byte) (HeartData %  256) };
//    private String sendDataStr = "";
//
//    private static int sendCount = 0;//发送序列号
////    private boolean bFrist = true;
//
//    private void SendHeartPacket(){
//        if(sendCount<255){
//            sendCount++;
//        }else {
//            sendCount = 0;
//        }
//
//        byte[] sendBytes = ZigBeeCommand.GetHeartbeatBytes(sendCount);
//        HDLCommand.mcuSendTransparentData(sendBytes);
//    }
//
//    private void SendGetDeviceList(){
////        if(bReceivingMode) return;
//
//
//        byte[] bytes = sendDataStr.getBytes();
//        byte[] sendBytes = ZigBeeCommand.GetSendBytes(bytes, ZigBeeCommand.COMMAND_SEND_DATA, sendCount);
//        HDLCommand.mcuSendTransparentData(sendBytes);
//
////        bReceivingMode = true;
//
//    }
//
//    int lenght = 1280;
//
//    private void SendCtrl222(){
//
////        byte[] sendBytes = new byte[]{
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////                0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
////        };
//
//        lenght ++;
//        HDLLog.Log("KNNN:send" + lenght);
//        byte[] sendBytes = new byte[lenght];
//        for(int i = 0;i <lenght;i++){
//            sendBytes[i] = 0x01;
//
//        }
//        Crc.ConCRC(sendBytes, sendBytes.length - 2);
////        byte[] sendBytes = ZigBeeCommand.GetSendBytes(jsonStr.getBytes(), ZigBeeCommand.COMMAND_SEND_DATA, sendCount);
//        HDLCommand.mcuSendTransparentData(sendBytes);
//        bReceivingMode = true;
//    }
//
//    private boolean kkkk = true;
//
//    private void SendCtrl(){
//        if(sendCount<255){
//            sendCount++;
//        }else {
//            sendCount = 0;
//        }
//        ZigBeeCtrl send = new ZigBeeCtrl();
//        send.Cluster_ID = 6;
//        send.Command = 2;
//        send.Epoint = 3;
//        send.SendMode = 2;
//        send.DeviceAddr = "000d6ffffe58d64c";
//        Gson gson = new Gson();
//        String jsonStr = gson.toJson(send); //转换成Json
//        byte[] sendBytes = ZigBeeCommand.GetSendBytes(jsonStr.getBytes(), ZigBeeCommand.COMMAND_SEND_DATA, sendCount);
//
//        HDLCommand.mcuSendTransparentData(sendBytes);
//        bReceivingMode = true;
//
//    }
//
//    private void SendCtrlLight(){
//        if(sendCount<255){
//            sendCount++;
//        }else {
//            sendCount = 0;
//        }
//        ZigBeeCtrl2 send = new ZigBeeCtrl2();
//        send.Cluster_ID = 8;
//        send.Command = 4;
//        send.Epoint = 8;
//        send.SendMode = 2;
//        send.DeviceAddr = "000d6ffffe58d491";
//        kkkk = !kkkk;
//        if(kkkk) {
//            send.Data.Level = 0;
//        }else {
//            send.Data.Level = 255;
//        }
//        send.Data.TransitionTime = 0;
//        Gson gson = new Gson();
//        String jsonStr = gson.toJson(send); //转换成Json
//        byte[] sendBytes = ZigBeeCommand.GetSendBytes(jsonStr.getBytes(), ZigBeeCommand.COMMAND_SEND_DATA, sendCount);
//        HDLCommand.mcuSendTransparentData(sendBytes);
//        bReceivingMode = true;
//
//
//    }
////
////    private boolean bGetData = false;
////    private boolean bBeginSendData = false;
//    // 定时器
//    private Timer mTimer = new Timer();
//    // 定时任务
//    private MyTimerTask mTask;
//
//    class MyTimerTask extends TimerTask{
//        @Override
//        public void run() {
//            // TODO Auto-generated method stub
//            // 要做的事情
//            if(!bReceivingMode) {
//                SendHeartPacket();
//            }
//        }
//
//    }
//
//    private void startSendHeartPacketTimer(){
//
//        if(mTimer == null){
//            mTimer = new Timer();
//        }
//        if(mTask != null){
//            mTask.cancel();
//
//        }
//        mTask = new MyTimerTask();
//        mTimer.schedule(mTask, 2000, 1500);
//    }
//
//    private void cancelTimer() {
//        if (mTimer != null) {
//            mTask.cancel();
//            mTimer.cancel();
//            mTimer = null;
//
//        }
//    }
//
//    /**
//     * ***********************************************************处理MCU回复数据***********************************************************
//     */
//    /**
//     * MCU控制回调Event
//     *
//     * @param event
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onMCUFeedBackEventMain(MCUFeedBackEvent event) {
////        if (event.getEventCode() == EventCode.FAILURE_TIMEOUT) {
////            if (TextUtils.isEmpty(event.getError())) {
////                showToast("MCU请求超时,请稍后再试");
////            } else {
////                showToast(event.getError());
////            }
////            return;
////        }
//
//        switch (event.getMCUDataBean().command) {
//            case MCUConstants.MCU_COMMAND_SEND_BACK:    //
//                //透传返回的数据
//                handleMCUPassThroughDataBack(event);
//                break;
//            default:
//                break;
//        }
//    }
//
//
//    /**
//     * 处理MCU透传回调数据
//     * 透传数据,不走bus协议,直接转发,不做数据格式处理
//     *
//     * @param event
//     */
//    private void handleMCUPassThroughDataBack(MCUFeedBackEvent event) {
//        if (event.getEventCode() != EventCode.SUCCESS) {
//            tv_other_mesSetText("MCU透传数据出错,返回数据异常");
//            return;
//        }
//
//        AddReceiveDatasList(event.getMCUDataBean().receiveBytes);
//    }
//
//    private void tv_other_mesSetText(String mes) {
//        tv_other_mes.setText(mes);
//        showToast(mes);
//    }
//
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        cancelTimer();
//        closeThread();
//    }
//
////    private class ZIGBEECommand{
////        private int Cluster_ID = 0;
////        private int Command = 93;
////        private ZIGBEECommand(int Cluster_ID, int Command){
////            this.Cluster_ID = Cluster_ID;
////            this.Command = Command;
////        }
////    }
//
//    private void getJson(){
//        try {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("Cluster_ID", 0);
//            jsonObject.put("Command", 93);
//            HDLLog.Log("getJson:" + jsonObject.toString());
//            sendDataStr =  jsonObject.toString();
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * 处理HDL Data
//     *
//     * @param receiveBytes
//     */
//    private void HandleMCUdata(byte[] receiveBytes) {
//        if (receiveBytes.length < 15) {
//            return;
//        }
//        //操作码
//        int command = (receiveBytes[8] & 0xFF) * 256 + (receiveBytes[9] & 0xFF);
//        int mId = receiveBytes[12] & 0xFF;
//
//        HDLLog.Log("KNNN:command" + command + "   Mid:"+mId);
//        String receiveString = HDLStringUtils.ByteArrToHex(receiveBytes, 0, receiveBytes.length);
//
////        tv_other_mes.setText("收到的透传数据:" + receiveString);
//        HDLLog.Log("收到的透传数据:length: "+  receiveBytes.length+"  DATA: " + receiveString);
//
//
//        switch (command){
//            case ZigBeeCommand.COMMAND_BACK_CLOSE_RECEIVING_MODE:
//                bReceivingMode = false;
//                setTextView("没: ");
//                break;
//            case ZigBeeCommand.COMMAND_BACK_OPEN_RECEIVING_MODE:
//                bReceivingMode = true;
//                setTextView("有缓存数据:");
//                break;
//            case ZigBeeCommand.COMMAND_SEND_DATA_BACK:
////                //数据长度
////                int addDataLength = (receiveBytes[14] & 0xFF) * 256 + (receiveBytes[13] & 0xFF);
////                //附加数据
////                byte[] usefulBytes = new byte[addDataLength];
////                //复制附加数据
////                System.arraycopy(receiveBytes, 15, usefulBytes, 0, addDataLength);
////                //参数
////                ZigBeeDataBean mZigBeeDataBean = new ZigBeeDataBean();
////                mZigBeeDataBean.command = command;
////                mZigBeeDataBean.mID = mId;
////                mZigBeeDataBean.receiveBytes = usefulBytes;
//
//                if((receiveBytes[13] & 0xFF) == 0xF8){
//                    setTextView("发送数据成功");
//                }else{
//                    setTextView("发送数据失败");
//                }
//                break;
//            case ZigBeeCommand.COMMAND_RECEIVE_DATA:
//                //数据长度
//                int addDataLength2 = (receiveBytes[14] & 0xFF) * 256 + (receiveBytes[13] & 0xFF);
//                HDLLog.Log("addDataLength2: "+  addDataLength2);
//
//                //附加数据
//                byte[] usefulBytes2 = new byte[addDataLength2];
//                //复制附加数据
//                System.arraycopy(receiveBytes, 15, usefulBytes2, 0, addDataLength2);
//                //参数
//                ZigBeeDataBean mZigBeeDataBean2 = new ZigBeeDataBean();
//                mZigBeeDataBean2.command = command;
//                mZigBeeDataBean2.mID = mId;
//                mZigBeeDataBean2.receiveBytes = usefulBytes2;
//                sendReceiveDataSuccess(mId);//透传JSON数据返回(魔镜->STM32)
//                getJsonData(mZigBeeDataBean2);
//                break;
//        }
//
//
//
//
//    }
//
//    private void getJsonData(ZigBeeDataBean mZigBeeDataBean){
////        String receiveString = HDLStringUtils.ByteArrToHex(mZigBeeDataBean.receiveBytes, 0, mZigBeeDataBean.receiveBytes.length);
////        String receiveString =new Gson().toJson(mZigBeeDataBean.receiveBytes);
//
//        String receiveString = HDLStringUtils.byte2String(mZigBeeDataBean.receiveBytes);
//
////        Log.d(TAG,new Gson().toJson(mZigBeeDataBean.receiveBytes));
//
//        setTextView("GET JSON: "+receiveString );
//        //回复:透传JSON数据返回(魔镜->STM32)
//    }
//
//
//    /**
//     * 透传JSON数据返回(魔镜->STM32)
//     * 0XF8成功 0XF5失败
//     * @param mID
//     */
//    private void sendReceiveDataSuccess(int mID){
//        byte[] sendBytes = ZigBeeCommand.GetReplyBytes(mID, true);
//        HDLCommand.mcuSendTransparentData(sendBytes);
//    }
//
//
//
//    private void setTextView(final String mes){
//
//
//        runOnUiThread(new Runnable() {
//            public void run() {
//                tv_other_mes.append(mes + "\n");
//            }
//        });
//
//        HDLLog.Log("KNNN:" + mes);
//    }
//
//
//
//
//
//
//
//
//
//    /**
//     * 启动线程
//     */
//    private  void startHDLThread() {
//        mHDLRead485DatasThread = new HDLRead485DatasThread();
//        mHDLRead485DatasThread.start();
//    }
//    /**
//     * 启动线程
//     */
//    private  void closeThread() {
//        if (mHDLRead485DatasThread != null) mHDLRead485DatasThread.interrupt();
//    }
//
//    private HDLRead485DatasThread mHDLRead485DatasThread;
//    private  boolean bOpenSuccess= true;
//    private List<Byte> mReceiveDatasList = new ArrayList<Byte>();//485接收数据队列
//
//    /**
//     *
//     * @param receiveBytes
//     */
//    private  void AddReceiveDatasList(byte[] receiveBytes) {
//        for (int i = 0; i < receiveBytes.length; i++) {
//            mReceiveDatasList.add(receiveBytes[i]);
//        }
//    }
//
//    /**
//     * 485数据接收线程
//     *
//     * @author 2019年06月26日
//     */
//    private  class HDLRead485DatasThread extends Thread {
//
//        @Override
//        public void run() {
//            super.run();
//            HDLLog.Log("ZigBee接收线程 开启");
//            while (bOpenSuccess) {
//                int size = 0;
//                try {
//                    Thread.sleep(10);
//                    while (0 < mReceiveDatasList.size()) {
////                        HDLLog.I("485arraylist.size():" + mReceive485DatasList.size());
//                        if (mReceiveDatasList.size() < 3) break;//2019-07-02
//                        if (mReceiveDatasList.size() > 3000) { //数据缓存太多错误情况下 清空一次
//                            mReceiveDatasList.clear();
//                            break;//2019-07-24
//                        }
//                        int len = -1;
//                        int startIndex = 0;
//                        for (startIndex = 0; startIndex < mReceiveDatasList.size(); startIndex++) {
//                            if ((mReceiveDatasList.get(startIndex) & 0xFF) == 0xAA
//                                    && (mReceiveDatasList.get(startIndex + 1) & 0xFF) == 0xAA) {
////                                len = (mReceiveDatasList.get(startIndex + 2) & 0xFF) * 256 + (mReceiveDatasList.get(startIndex + 3) & 0xFF) + 5;
//                                len =  (mReceiveDatasList.get(startIndex + 2) & 0xFF) * 256 + (mReceiveDatasList.get(startIndex + 3) & 0xFF) + 2;
//                                break;
//                            }
//                        }
//                        if (len == -1 || mReceiveDatasList.size() < len) break;
//
////                        HDLLog.Log("startIndex:" + startIndex + "   LEN:" + len);
//                        for (int i = 0; i < startIndex; i++) {
//                            if (mReceiveDatasList.size() < 1) break;//2019-07-02
//                            mReceiveDatasList.remove(0);
//                        }
//                        byte[] tempBytes = new byte[len];
//                        for (int i = 0; i < tempBytes.length; i++) {
//                            if (mReceiveDatasList.size() < 1) break;//2019-07-02
//
//                            tempBytes[i] = mReceiveDatasList.get(0);
//                            mReceiveDatasList.remove(0);
//                        }
//                        HandleMCUdata(tempBytes);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                   HDLLog.Log("error:" + e.getMessage());
//                }
//            }
//            HDLLog.Log("485接收线程 关闭");
//        }
//    }
//}