//package com.hdl.sdk.ttl_sdk.activity; // // //import android.Manifest; //import android.app.Activity; //import android.content.Intent; //import android.net.Uri; //import android.os.Bundle; //import android.support.annotation.NonNull; //import android.text.TextUtils; //import android.util.Log; //import android.view.View; //import android.widget.AdapterView; //import android.widget.ArrayAdapter; //import android.widget.Button; //import android.widget.RelativeLayout; //import android.widget.Spinner; //import android.widget.TextView; // //import com.hdl.sdk.ttl.Config.MCUConstants; //import com.hdl.sdk.ttl.HDLDeviceManger.Bean.MCUConfigurationBean; //import com.hdl.sdk.ttl.HDLDeviceManger.Bean.MCUDataBean; //import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand; //import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLSerialPortCore; //import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.EventCode; //import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.MCUFeedBackEvent; //import com.hdl.sdk.ttl.HDLDeviceManger.HDLListener.IMcuOtaListener; //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.utlis.HDLLog; //import com.hdl.sdk.ttl_sdk.utlis.HDLUriUtils; //import com.hdl.sdk.ttl_sdk.utlis.HDLUtlis; // //import org.greenrobot.eventbus.EventBus; //import org.greenrobot.eventbus.Subscribe; //import org.greenrobot.eventbus.ThreadMode; // //import java.io.File; //import java.io.FileInputStream; //import java.io.FileNotFoundException; //import java.io.IOException; //import java.util.List; //import java.util.Timer; //import java.util.TimerTask; // //import pub.devrel.easypermissions.AppSettingsDialog; //import pub.devrel.easypermissions.EasyPermissions; // ///** // * MCU协议调试页面 // */ //public class MCUActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, IMcuOtaListener { // /** // * Topbar // */ // private RelativeLayout topBarBack; // private TextView topBarTitle; // private Button btn_packet_loss, btn_read_config, btn_write_config, btn_update, btn_restart, btn_PassThrough, btn_PassThrough_close; // private Spinner sp_type, sp_Baudrate, sp_DataBit, sp_CheckBit, sp_StopBit; // private TextView tv_select_bin, tv_config_mes, tv_update_mes, tv_other_mes; // // private MCUConfigurationBean mMCUConfigurationBean; // private static final String[] mcuTypeItems = {"Buspro", "zigbee", "KNX", "Modbus", "禁选", "自定义"}; // private static final String[] mcuBaudrateItems = {"1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "250000"}; // private static final String[] mcuDataItems = {"8", "9"}; // private static final String[] mcuCheckItems = {"无", "奇", "偶"}; // private static final String[] mcuStopItems = {"0.5bit", "1bit", "2bit", "1.5bit"}; // private boolean bGetMCUConfiguration = false; // // private byte[] upgradeFileDatas;//升级文件数据 // private static final int SELECT_FILE_CODE = 10; // private static final int PERMISSION = 101; // private boolean bPermission = false; // private static IMcuOtaListener mIMcuOtaListener; // private boolean bUpdating = 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_mcu); // // initToolbar(); // // initView(); // initOnClick(); // getEasyPermissions(); // initData(); // // HDLSerialPortCore.setIMcuOtaListener(this);//设置监听 // } // // @Override // protected void onDestroy() { // super.onDestroy(); // HDLSerialPortCore.removeIMcuOtaListener(); // } // // /** // * 初始化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("MCU 协议调试"); // } // // /** // * initView // */ // private void initView() { // btn_packet_loss = findViewById(R.id.btn_packet_loss); // btn_read_config = findViewById(R.id.btn_read_config); // btn_write_config = findViewById(R.id.btn_write_config); // btn_update = findViewById(R.id.btn_update); // btn_restart = findViewById(R.id.btn_restart); // tv_other_mes = findViewById(R.id.tv_other_mes); // tv_select_bin = findViewById(R.id.tv_select_bin); // tv_config_mes = findViewById(R.id.tv_config_mes); // tv_update_mes = findViewById(R.id.tv_update_mes); // // sp_type = findViewById(R.id.spinner_type); // sp_Baudrate = findViewById(R.id.spinner_baudrate); // sp_DataBit = findViewById(R.id.spinner_dataBit); // sp_CheckBit = findViewById(R.id.spinner_checkBit); // sp_StopBit = findViewById(R.id.spinner_stopBit); // // btn_PassThrough = findViewById(R.id.btn_PassThrough); // btn_PassThrough_close = findViewById(R.id.btn_PassThrough_close); // // mMCUConfigurationBean = new MCUConfigurationBean(); // } // // /** // * initOnClick // */ // private void initOnClick() { // // btn_read_config.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // HDLCommand.mcuReadConfiguration(); // } // }); // // btn_write_config.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // HDLCommand.mcuWriteConfiguration(mMCUConfigurationBean.getMCUConfigurationSendBytes()); // } // }); // // // btn_packet_loss.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // HDLCommand.mcuGetDetectPacketLossStatus(); // } // }); // // tv_select_bin.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // selectBin(); // } // }); // // btn_update.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // // if (upgradeFileDatas == null) { // showToast("请先选择升级文件"); // return; // } // // HDLCommand.mcuRequestUpgradeWithFile(upgradeFileDatas);//传入升级文件并开始升级 // } // }); // // btn_restart.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // HDLCommand.mcuSendRestart(); // } // }); // // // /** // * 发送透传参数 ,不走bus协议 // */ // btn_PassThrough.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // mcuSendTransparentData(); // // } // }); // // btn_PassThrough_close.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // setHDLPassThroughOpen(false); // tv_other_mes.setText("关闭透传"); // // } // }); // // ArrayAdapter sp_typeAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcuTypeItems); // sp_typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // sp_type.setAdapter(sp_typeAdapter); // sp_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { // public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // if (arg2 == 4) { // //选择了禁选 // showToast("该项不能选择!"); // sp_type.setSelection(mMCUConfigurationBean.getMcuAgreementType()); // } else { // mMCUConfigurationBean.setMcuAgreementType(arg2); // } // // } // // public void onNothingSelected(AdapterView arg0) { // // // } // }); // // // ArrayAdapter sp_BaudrateAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcuBaudrateItems); // sp_BaudrateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // sp_Baudrate.setAdapter(sp_BaudrateAdapter); // sp_Baudrate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { // public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // mMCUConfigurationBean.setMcuBaudrate(arg2); // } // // public void onNothingSelected(AdapterView arg0) { // // // } // }); // // // ArrayAdapter sp_DataBitAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcuDataItems); // sp_DataBitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // sp_DataBit.setAdapter(sp_DataBitAdapter); // sp_DataBit.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { // public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // mMCUConfigurationBean.setMcuDataBit(arg2); // } // // public void onNothingSelected(AdapterView arg0) { // } // }); // // // ArrayAdapter sp_CheckBitAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcuCheckItems); // sp_CheckBitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // sp_CheckBit.setAdapter(sp_CheckBitAdapter); // sp_CheckBit.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { // public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // mMCUConfigurationBean.setMcuCheckBit(arg2); // } // // public void onNothingSelected(AdapterView arg0) { // // } // }); // // ArrayAdapter sp_StopBitAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcuStopItems); // sp_StopBitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // sp_StopBit.setAdapter(sp_StopBitAdapter); // sp_StopBit.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { // public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // mMCUConfigurationBean.setMcuStopBit(arg2); //// showToast("选择的是" + mcuStopItems[arg2]); // } // // public void onNothingSelected(AdapterView arg0) { // // // } // }); // // } // // // // // /** // * initData // */ // private void initData() { // setEnabledConfigBtn(); // // } // // private void setEnabledConfigBtn() { // btn_write_config.setEnabled(bGetMCUConfiguration); // } // // private void updateMCUConfiguration(MCUConfigurationBean mMCUConfigurationBean) { // this.mMCUConfigurationBean = mMCUConfigurationBean; // sp_type.setSelection(mMCUConfigurationBean.getMcuAgreementType(), true); // sp_Baudrate.setSelection(mMCUConfigurationBean.getMcuBaudrate(), true); // sp_DataBit.setSelection(mMCUConfigurationBean.getMcuDataBit(), true); // sp_CheckBit.setSelection(mMCUConfigurationBean.getMcuCheckBit(), true); // sp_StopBit.setSelection(mMCUConfigurationBean.getMcuStopBit(), true); // // } // // private void resetMCUConfiguration() { // sp_type.setSelection(0, true); // sp_Baudrate.setSelection(0, true); // sp_DataBit.setSelection(0, true); // sp_CheckBit.setSelection(0, true); // sp_StopBit.setSelection(0, true); // // } // // /** // * ***********************************************************处理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; // case MCUConstants.MCU_READ_CONFIGURATION_BACK:// 读配置返回 // case MCUConstants.MCU_WRITE_CONFIGURATION_BACK:// 写配置信息返回 // handleMCUConfigurationBack(event); // break; // case MCUConstants.MCU_DETECT_PACKET_LOSS_STATUS_BACK:// 检测丢包状态返回 // //待处理 // handleDetectPacketLossStatusBack(event); // break; // case MCUConstants.MCU_REQUEST_UPGRADE_BACK: // 请求升级返回 // handleRequestUpgradeBack(event); // break; //// case MCUConstants.MCU_SEND_UPGRADE_DATA_BACK: // 发送升级数据返回 //// handleSendUpgradeDataBack(event); //// break; // case MCUConstants.MCU_RESTART_BACK: //0x86 重启返回 // handleRestartBack(event); // break; // default: // break; // } // } // // /** // * 处理MCU透传回调数据 // * 透传数据,不走bus协议,直接转发,不做数据格式处理 // * // * @param event // */ // private void handleMCUPassThroughDataBack(MCUFeedBackEvent event) { // if (event.getEventCode() != EventCode.SUCCESS) { // tv_other_mesSetText("MCU透传数据出错,返回数据异常"); // return; // } // String receiveString = HDLStringUtils.ByteArrToHex(event.getMCUDataBean().receiveBytes, 0, event.getMCUDataBean().receiveBytes.length); // // tv_other_mes.setText("收到的透传数据:" + receiveString); // HDLLog.Log("收到的透传数据:" + receiveString); // } // // /** // * 处理MCU读写配置返回数据 // * // * @param event // */ // private void handleMCUConfigurationBack(MCUFeedBackEvent event) { // if (event.getEventCode() != EventCode.SUCCESS) { // tv_config_mesSetText("MCU读写配置出错,返回数据异常"); // return; // } // tv_config_mesSetText("成功获取配置"); // bGetMCUConfiguration = true; // setEnabledConfigBtn(); // updateMCUConfiguration(event.getMCUDataBean().getMCUConfigurationBean()); // } // // /** // * 处理MCU丢包检测返回数据 // * // * @param event // */ // private void handleDetectPacketLossStatusBack(MCUFeedBackEvent event) { // if (event.getEventCode() != EventCode.SUCCESS) { // tv_other_mesSetText("MCU丢包检测出错,返回数据异常"); // return; // } // // String receiveString = HDLStringUtils.ByteArrToHex(event.getMCUDataBean().receiveBytes, 0, event.getMCUDataBean().receiveBytes.length); // HDLLog.Log("丢包检测返回数据:" + receiveString); // tv_other_mesSetText("丢包检测返回数据:" + receiveString); // } // // /** // * 处理MCU请求重启返回数据 // * // * @param event // */ // private void handleRestartBack(MCUFeedBackEvent event) { // if (event.getEventCode() != EventCode.SUCCESS) { // showToast("MCU请求重启返回错误,重启失败"); // return; // } // if (bUpdating) { // bUpdating = false; // tv_update_mes.setText("MCU重启成功,升级完成!"); // tv_other_mesSetText("MCU重启成功,升级完成!"); // } else { // tv_other_mesSetText("MCU重启成功!"); // } // } // // /** // * 处理MCU请求升级返回数据 // * // * @param event // */ // private void handleRequestUpgradeBack(MCUFeedBackEvent event) { // if (event.getEventCode() != EventCode.SUCCESS) { // tv_update_mesSetText("MCU请求升级出错"); // return; // } // bUpdating = true; // tv_update_mesSetText("MCU请求升级成功,开始升级..."); // } // //// //// private void handleSendUpgradeDataBack(MCUFeedBackEvent event) { //// if (event.getEventCode() != EventCode.SUCCESS) { //// showToast("MCU发送升级数据出错"); //// return; //// } ////// 注意,当文件地址为0xF5F5F5F5的时候,代表发生错误, ////// 升级包不对,会退出升级模式。当文件地址为0xF8F8F8F8,代表成功,成功后会重启,监听到重启完毕,才代表完成。 //// //// showToast("收到回复,继续发送数据"); //// //// } // // /** // * ***********************************************************选择升级文件*********************************************************** // */ // private void selectBin() { //// {".bin", "application/octet-stream"} // Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //// intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。 // intent.setType("application/octet-stream");//设置类型,我这里是任意类型,任意后缀的可以这样写。 {".bin", "application/octet-stream"} // intent.addCategory(Intent.CATEGORY_OPENABLE); // startActivityForResult(intent, SELECT_FILE_CODE); // } // // protected void onActivityResult(int requestCode, int resultCode, Intent data) { // if (resultCode == Activity.RESULT_OK) { // if (requestCode == SELECT_FILE_CODE) { // if (data != null) { // Uri fileUri = data.getData(); // if (fileUri != null) { // getFileFromUri(fileUri); // } // } // // } // } // } // // /** // * 根据uri 读取升级固件 // * // * @param mUri // */ // private void getFileFromUri(Uri mUri) { // String path = mUri.toString(); // if (!HDLUtlis.checkIsBinFile(path)) { // tv_select_bin.setText(getString(R.string.select_bin_frist)); // upgradeFileDatas = null; // btn_update.setEnabled(false); // showToast("升级固件格式不对,请选择.bin后缀的升级文件"); // return; // } // btn_update.setEnabled(true); // tv_select_bin.setText(path); // File file = HDLUriUtils.uri2File(this, mUri); // FileInputStream fis = null; // try { // fis = new FileInputStream(file); // int length = fis.available(); // upgradeFileDatas = new byte[length]; // fis.read(upgradeFileDatas); // fis.close(); // showToast("upgradeFileDatas 长度:" + upgradeFileDatas.length); // // // //打印接受数据 // com.hdl.sdk.ttl.Utils.LogUtils.HDLLog.I("receiveBytes HandleMCUdata: " + HDLStringUtils.ByteArrToHex(upgradeFileDatas, 0, upgradeFileDatas.length)); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // // /** // * ***********************************************************权限管理*********************************************************** // */ // private void getEasyPermissions() { // /** // * 6.0+系统动态权限申请需要 // */ // String[] params = new String[]{ // Manifest.permission.WRITE_EXTERNAL_STORAGE, // }; // if (EasyPermissions.hasPermissions(this, params)) { // bPermission = true; // } else { // EasyPermissions.requestPermissions(this, "应用需要权限才能安全运行", PERMISSION, params); // } // } // // @Override // public void onPermissionsGranted(int requestCode, List perms) { // Log.i("hdlll", "onPermissionsGranted permsSize:" + perms.size()); // if (requestCode == PERMISSION) { // if (!perms.isEmpty()) { // if (perms.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // //全部权限授权通过才正常打开APP // bPermission = true; // } // } // } // // } // // @Override // public void onPermissionsDenied(int requestCode, List perms) { // switch (requestCode) { // case PERMISSION: // //引导用户跳转到设置界面 // new AppSettingsDialog.Builder(this).setTitle("温馨提示").setRationale("无法正常启动APP,您需要授予权限!").build().show(); // break; // } // } // // @Override // public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { // super.onRequestPermissionsResult(requestCode, permissions, grantResults); // EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); // } // // // /** // * ***********************************************************固件升级 回调*********************************************************** // */ // @Override // public void onSuccess() { // sendRestartTimer();//升级10S超时提示 // HDLLog.Log("IMcuOtaListener onSuccess"); // runOnUiThread(new Runnable() { //要在主线程更新UI // public void run() { // tv_update_mesSetText("升级成功,等待重启..."); // } // }); // } // // @Override // public void onFailure(int code, final String error) { // HDLLog.Log("IMcuOtaListener onFailure"); // runOnUiThread(new Runnable() { // public void run() { // tv_update_mesSetText("升级失败: " + error); // } // }); // // } // // @Override // public void onProgress(final int progress) { // HDLLog.Log("IMcuOtaListener onProgress:" + progress); // runOnUiThread(new Runnable() { // public void run() { // tv_update_mes.setText("升级进度:" + progress + "%"); // } // }); // // } // // /** // * TextView 显示信息并且showToast提示 // * // * @param mes // */ // private void tv_update_mesSetText(String mes) { // tv_update_mes.setText(mes); // showToast(mes); // } // // private void tv_other_mesSetText(String mes) { // tv_other_mes.setText(mes); // showToast(mes); // } // // private void tv_config_mesSetText(String mes) { // tv_config_mes.setText(mes); // showToast(mes); // } // // // /** // * 升级完成后判断 是否重启成功 设置10S超时等待 // */ // private void sendRestartTimer() { // Timer restartTimer = new Timer(); // restartTimer.schedule(new TimerTask() { // @Override // public void run() { // if (bUpdating) { // runOnUiThread(new Runnable() { // public void run() { // tv_update_mesSetText("升级完成,MCU重启超时!"); // } // }); // } // } // }, 10000);//10s超时限制 // } // // // // // /** // * 2019-8-9 // * 发送透传参数 // * 透传数据,不走bus协议,直接转发,不做数据格式处理 // * mcuSendTransparentData接口调用一次以后,默认所有接收数据走透传 // * 可以调用setHDLPassThroughOpen关闭透传数据 // */ // private void mcuSendTransparentData() { // byte[] sendBytes = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; // HDLCommand.mcuSendTransparentData(sendBytes); // } // // /** // * 设置是否开启透传数据 // * 要想直接接收透传数据,直接调用这个接口打开就可以,不走bus协议处理 // * 不想透传的话,要记得设置回关闭状态 // */ // private void setHDLPassThroughOpen(Boolean bOpen){ // HDLCommand.setHDLPassThroughOpen(bOpen); // // } // //}