//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接收线程 关闭");
|
// }
|
// }
|
//}
|