package com.hdl.sdk.ttl_sdk.activity;
|
|
import android.app.AlertDialog;
|
import android.app.ProgressDialog;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.RecyclerView;
|
import android.text.TextUtils;
|
import android.view.View;
|
import android.widget.Button;
|
import android.widget.EditText;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.hdl.sdk.ttl.Config.Configuration;
|
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.DevicesData;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLDeviceManager;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLSerialPortCore;
|
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLTtlSdk;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.BgmInfoEvent;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DevicesInfoEvent;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DiscoverNewDevicesEvent;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.UpdateRemarkFeedBackEvent;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.ThirdPartyBgmInfoEvent;
|
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.WarningInfoEvent;
|
import com.hdl.sdk.ttl.Utils.HDLUtlis.HDLStringUtils;
|
import com.hdl.sdk.ttl_sdk.R;
|
import com.hdl.sdk.ttl_sdk.adapter.HDLMainListAdapter;
|
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
|
//import com.hdl.sdk.ttl_sdk.bean.ZigbeeActivity2;
|
import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
|
import com.hdl.sdk.ttl_sdk.utlis.HDLWarningType;
|
|
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.ThreadMode;
|
|
import java.io.IOException;
|
import java.io.Serializable;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import static com.hdl.sdk.ttl_sdk.HDLApplication.HDL_BAUDRATE;
|
import static com.hdl.sdk.ttl_sdk.HDLApplication.HDL_UART_PATH;
|
|
/**
|
* Modify by JLChen on 2019/6/27
|
*/
|
public class MainActivity extends BaseActivity {
|
|
// @BindView(R.id.tv_version)
|
// TextView tvVersion;
|
// @BindView(R.id.btn_search)
|
// Button btnSearch;
|
// @BindView(R.id.btn_close)
|
// Button btnClose;
|
// @BindView(R.id.btn_open)
|
// Button btnOpen;
|
// @BindView(R.id.tv_result)
|
// TextView tvResult;
|
// @BindView(R.id.listView_devices)
|
// ListView listViewDevices;
|
|
/**
|
* Topbar
|
*/
|
// private RelativeLayout topBarBack;
|
private TextView topBarTitle;
|
private TextView tvVersion;
|
private Button btnSearch;
|
private Button btnGetLocal;
|
private Button btnClose;
|
private Button btnOpen;
|
private Button btnSetting;
|
private Button btnMCU, btn_ZigBee;
|
private Button btnAdd;
|
private Button btn_sendtest;
|
|
private TextView tvResult;
|
private RecyclerView mRecyclerView;
|
|
private List<DevicesData> mDevicesDataList;
|
private List<String> mAllDevicesList = new ArrayList<>();//搜索到的设备的备注名
|
|
private ProgressDialog mProgressDialog;
|
// private static String HDL_PATH_NAME = "/dev/ttyS2"; //串口设备路径名
|
// private static int HDL_BAUDRATE = 115200; //波特率
|
private Boolean bOpen = false;
|
private int remarkId = 0;
|
private String remarkStr = "";
|
private HDLMainListAdapter mHDLMainListAdapter;
|
|
|
/**
|
* 复写isRegisterEventBus() 要注册使用EventBus,这里要设置返回true
|
*
|
* @return true
|
*/
|
@Override
|
protected boolean isRegisterEventBus() {
|
return true;
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_main);
|
// ButterKnife.bind(this);
|
|
initView();
|
initRecyclerView();
|
initOnClick();
|
initData();
|
|
}
|
|
|
private void initView() {
|
topBarTitle = findViewById(R.id.tv_top_b_header_title);
|
tvVersion = findViewById(R.id.tv_version);
|
btnSearch = findViewById(R.id.btn_search);
|
btnGetLocal = findViewById(R.id.btn_getlocal);
|
btnClose = findViewById(R.id.btn_close);
|
btnOpen = findViewById(R.id.btn_open);
|
btnSetting = findViewById(R.id.btn_setting);
|
tvResult = findViewById(R.id.tv_result);
|
btnMCU = findViewById(R.id.btn_mcu);
|
btnAdd = findViewById(R.id.btn_add_device);
|
btn_sendtest = findViewById(R.id.btn_sendtest);
|
|
btn_ZigBee = findViewById(R.id.btn_ZigBee);
|
|
mProgressDialog = new ProgressDialog(MainActivity.this);
|
mProgressDialog.setTitle("正在获取数据...");
|
mProgressDialog.setMessage("请耐心等待");
|
mProgressDialog.onStart();
|
|
}
|
|
/**
|
* 初始化RecyclerView
|
*/
|
private void initRecyclerView() {
|
mRecyclerView = findViewById(R.id.listView_devices);
|
// 定义一个线性布局管理器
|
LinearLayoutManager manager = new LinearLayoutManager(this);
|
// 设置布局管理器
|
mRecyclerView.setLayoutManager(manager);
|
// 设置adapter
|
mHDLMainListAdapter = new HDLMainListAdapter(this, mAllDevicesList);
|
mRecyclerView.setAdapter(mHDLMainListAdapter);
|
|
mHDLMainListAdapter.setOnItemClickLitener(new HDLMainListAdapter.OnItemClickLitener() {
|
@Override
|
public void onItemClick(int position) {
|
if (!bOpen) {
|
showToast("请先打开串口!");
|
return;
|
}
|
Intent intent = new Intent();
|
intent.setClass(MainActivity.this, AppliancesListActivity.class);
|
Bundle bundle = new Bundle();
|
bundle.putSerializable("Appliances", (Serializable) mDevicesDataList.get(position).getAppliancesInfoList());
|
intent.putExtras(bundle);
|
MainActivity.this.startActivity(intent);
|
}
|
|
@Override
|
public void onItemLongClick(int position) {
|
remarkId = position;
|
String mInfo = mDevicesDataList.get(remarkId).getRemark();
|
showInPutView(mDevicesDataList.get(remarkId).getSourceSubnetID(), mDevicesDataList.get(remarkId).getSourceDeviceID(), mInfo);
|
|
}
|
});
|
|
|
}
|
|
private void initOnClick() {
|
btnSearch.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
/**全部重新搜索,清空原设备列表数据*/
|
clearListView();
|
HDLCommand.getHomeDevices();
|
mProgressDialog.show();
|
}
|
});
|
|
btnGetLocal.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
clearListView();
|
getLocalDevicesDataList();
|
}
|
});
|
|
btnClose.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
HDLSerialPortCore.closeSerialPort();
|
updateButtonEnable();
|
|
}
|
});
|
|
btnOpen.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
HDLSerialPortCore.openSerialPort(HDL_UART_PATH, HDL_BAUDRATE);
|
updateButtonEnable();
|
|
}
|
});
|
|
btnSetting.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
startActivity(SettingActivity.class);
|
|
|
// addSecurityModuleDevice();
|
// HDLDeviceManager.clearAllDeviceList();
|
}
|
});
|
|
btnMCU.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
// startActivity(MCUActivity.class);
|
}
|
});
|
|
btnAdd.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
startActivity(AddDevicesManuallyNEWActivity.class);
|
|
// byte[] remarkByte = new byte[]{
|
// (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
// (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
// (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
// (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,};
|
// String remarkStr = HDLStringUtils.byte2String(remarkByte);
|
// HDLLog.Log("remarkStr回路:" + remarkStr);
|
}
|
});
|
|
btn_ZigBee.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
// startActivity(ZigbeeActivity2.class);
|
|
|
}
|
});
|
|
btn_sendtest.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
startActivity(SendTestActivity.class);
|
}
|
});
|
|
}
|
|
private void initData() {
|
topBarTitle.setText("HDL TTL Demo");
|
updateButtonEnable();
|
}
|
|
/**
|
* 判断串口是否打开成功,并刷新按钮状态
|
*/
|
private void updateButtonEnable() {
|
bOpen = HDLSerialPortCore.getIsSerialPortOpen();
|
btnSearch.setEnabled(bOpen);
|
btnGetLocal.setEnabled(bOpen);
|
btnClose.setEnabled(bOpen);
|
btnOpen.setEnabled(!bOpen);
|
btnMCU.setEnabled(bOpen);
|
btnAdd.setEnabled(bOpen);
|
btn_sendtest.setEnabled(bOpen);
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
HDLTtlSdk.release(); //关闭串口
|
}
|
|
|
private void showInPutView(final int mSubnetID, final int mDeviceID, String mText) {
|
final EditText modifyRemark = new EditText(this);
|
modifyRemark.setText(mText);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
builder.setTitle("修改备注").setIcon(android.R.drawable.ic_dialog_info).setView(modifyRemark)
|
.setNegativeButton("取消", null);
|
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
remarkStr = modifyRemark.getText().toString();
|
updateModuleRemark(mSubnetID, mDeviceID, remarkStr);
|
}
|
});
|
builder.show();
|
}
|
|
private void updateModuleRemark(int mSubnetID, int mDeviceID, String remarkStr) {
|
if (TextUtils.isEmpty(remarkStr) || TextUtils.isEmpty(remarkStr)) {
|
showToast("内容不能为空");
|
return;
|
}
|
byte[] remarkByte = HDLStringUtils.stringtoBytes(remarkStr);
|
if (remarkByte.length > 20) {
|
showToast("备注名不能超过20字节");
|
} else {
|
//修改模块备注接口,
|
HDLCommand.updateModuleRemark(mSubnetID, mDeviceID, remarkByte);
|
HDLLog.Log("kkkkkkkkk:" + remarkStr);
|
}
|
}
|
|
/**
|
* 刷新设备列表数据
|
*/
|
private void updateDeviceListView() {
|
int countAll = 0;
|
for (DevicesData devicesData : mDevicesDataList) {
|
countAll += devicesData.getAppliancesInfoList().size();
|
}
|
tvResult.setText("总共模块数:" + mDevicesDataList.size() + " 总共回路数:" + countAll);
|
HDLLog.Log("获取数据成功:总共模块数:" + mDevicesDataList.size() + " 总共回路数:" + countAll);
|
|
for (int i = 0; i < mDevicesDataList.size(); i++) {
|
if (TextUtils.isEmpty(mDevicesDataList.get(i).getRemark())) {
|
mAllDevicesList.add("暂无备注");
|
} else {
|
mAllDevicesList.add(mDevicesDataList.get(i).getRemark());
|
}
|
}
|
mHDLMainListAdapter.notifyDataSetChanged();
|
}
|
|
/**
|
* 读取和加载本地数据
|
*/
|
private void getLocalDevicesDataList() {
|
mDevicesDataList = HDLDeviceManager.getLocalDevicesDataList();
|
if (mDevicesDataList.size() > 0) {
|
updateDeviceListView();
|
} else {
|
showToast("本地数据为空");
|
}
|
}
|
|
/**
|
* 清空数据并刷新列表
|
*/
|
private void clearListView() {
|
if (mAllDevicesList != null) mAllDevicesList.clear();
|
if (mDevicesDataList != null) mDevicesDataList.clear();
|
|
mHDLMainListAdapter.notifyDataSetChanged();
|
}
|
|
/**
|
* ====================EventBus 订阅事件 ====================
|
*/
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
public void onDevicesInfoEventMain(DevicesInfoEvent event) {
|
mAllDevicesList.clear();
|
mProgressDialog.dismiss();
|
if (!event.isSuccess()) {
|
Toast.makeText(MainActivity.this, "搜索超时,请重新再试", Toast.LENGTH_SHORT).show();
|
tvResult.setText("搜索超时,请重新再试");
|
return;
|
}
|
mDevicesDataList = event.getDesDataList();
|
updateDeviceListView();
|
}
|
|
|
// 已废除,场景数据已经和常规设备合并。若有需求需要区分,请根据设备类型
|
// 12、13 为场景 TYPE_LOGIC_MODULE、TYPE_GLOBAL_LOGIC_MODULE
|
// @Subscribe(threadMode = ThreadMode.MAIN)
|
// public void onSceneInfoEventMain(SceneInfoEvent event) {
|
// proDia.dismiss();
|
// listString.clear();
|
// if (!event.isSuccess()) {
|
// Toast.makeText(MainActivity.this, "搜索超时,请重新再试", Toast.LENGTH_SHORT).show();
|
// tv.setText("搜索超时,请重新再试");
|
// return;
|
// }
|
// devicesDatas = event.getDesDataList();
|
// tv.setText("总共模块数 : " + event.getDesDataList().size());
|
//
|
// for (int i = 0; i < devicesDatas.size(); i++) {
|
// if (TextUtils.isEmpty(devicesDatas.get(i).getRemark())) {
|
// listString.add("暂无备注");
|
// } else {
|
// listString.add(devicesDatas.get(i).getRemark());
|
// }
|
// }
|
// adapter.notifyDataSetChanged();
|
// }
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
public void onWarningEventMain(WarningInfoEvent event) {
|
String warningType = HDLWarningType.getWarningType(event.getWarningTypeId());
|
Toast.makeText(MainActivity.this, warningType, Toast.LENGTH_SHORT).show();
|
}
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
public void onRemarkUpdateFeedBackEventMain(UpdateRemarkFeedBackEvent event) {
|
if (event.isSuccess()) {
|
showToast("修改成功");
|
mDevicesDataList.get(remarkId).setRemark(remarkStr);
|
mAllDevicesList.set(remarkId, remarkStr);
|
HDLDeviceManager.devicesDataList.get(remarkId).setRemark(remarkStr);
|
mHDLMainListAdapter.notifyDataSetChanged();
|
} else {
|
showToast("修改失败");
|
}
|
|
}
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
public void onDiscoverNewDevicesEventMain(DiscoverNewDevicesEvent event) {
|
HDLLog.Log("发现新设备:" + event.getnewDevicesData().getRemark());
|
}
|
|
|
|
|
@SuppressWarnings("unused")
|
private void closePhone(Context context, String[] shutdown){
|
try {
|
Process process = Runtime.getRuntime().exec(shutdown);
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
|
}
|