package com.hdl.sdk.hdl_sdk.activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.hdl.sdk.hdl_core.Config.Configuration; import com.hdl.sdk.hdl_core.HDLAppliances.HDLAirCondition.Parser.AirCtrlParser; import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.AppliancesInfo; import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.DevicesData; import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLCommand; import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLDeviceManager; import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HandleSearch; import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.BgmInfoEvent; import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.DevicesInfoEvent; import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.ThirdPartyBgmInfoEvent; import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.WarningInfoEvent; import com.hdl.sdk.hdl_sdk.R; import com.hdl.sdk.hdl_sdk.utlis.HDLLog; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { private Button btn, btn2; private Button btn_get_all,btn_on_all, btn_off_all,btn_getlocal,btn_getlocal_add; private TextView tv; private EditText editText; private List devicesDatas; private List listString = new ArrayList<>(); private ArrayAdapter adapter; private ProgressDialog proDia; private ListView listView; private List airList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HDLCommand.init(this); // /**配置是否开启SDK打印日志,默认为打开*/ // HDLCommand.setHDLLogOpen(false);// if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); } initView(); setOnClick(); // hideTestButton(); } private void initView() { btn = findViewById(R.id.btn); btn2 = findViewById(R.id.get); btn_get_all = findViewById(R.id.btn_get_all); btn_on_all = findViewById(R.id.btn_on_all); btn_off_all = findViewById(R.id.btn_off_all); btn_getlocal = findViewById(R.id.btn_getlocal); btn_getlocal_add = findViewById(R.id.btn_getlocal_add); tv = findViewById(R.id.tv); editText = findViewById(R.id.edt); editText.setText("172.168.188.100"); adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, listString); listView = findViewById(R.id.listView1); proDia = new ProgressDialog(MainActivity.this); proDia.setTitle("正在获取数据..."); proDia.setMessage("请耐心等待"); proDia.onStart(); listView.setAdapter(adapter); } private void setOnClick() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Intent intent = new Intent(); intent.setClass(MainActivity.this, AppliancesActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("Appliances", (Serializable) devicesDatas.get(position).getAppliancesInfoList()); intent.putExtras(bundle); MainActivity.this.startActivity(intent); } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { HDLCommand.getHomeDevices(MainActivity.this); proDia.show(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isIP(editText.getText().toString().trim())) { HDLCommand.getRcuDevices(MainActivity.this, editText.getText().toString().trim()); proDia.show(); } else { Toast.makeText(MainActivity.this, "请输入正确格式Ip地址", Toast.LENGTH_SHORT).show(); } } }); btn_get_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getAllAirList(); } }); btn_on_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendAll(true); } }); btn_off_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendAll(false); } }); btn_getlocal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearListView(); getLocalDevicesDataList(); } }); btn_getlocal_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(AddDevicesManuallyActivity.class); } }); } public void startActivity(Class clazz) { Intent intent = new Intent(this, clazz); startActivity(intent); } /** * 读取和加载本地数据 */ private void getLocalDevicesDataList() { devicesDatas = HDLDeviceManager.getLocalDevicesDataList(); if (devicesDatas.size() > 0) { updateDeviceListView(); HandleSearch.refreshAllDevicesStateAndRemarks();//如果本地有数据,可以调用该方法,遍历发送指令,刷新所有设备的备注和状态 } else { showToast("本地数据为空"); } } /** * 刷新设备列表数据 */ private void updateDeviceListView() { int countAll = 0; for (DevicesData devicesData : devicesDatas) { countAll += devicesData.getAppliancesInfoList().size(); } tv.setText("总共模块数:" + devicesDatas.size() + " 总共回路数:" + countAll); HDLLog.I("获取数据成功:总共模块数:" + devicesDatas.size() + " 总共回路数:" + countAll); 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(); } /** * 清空数据并刷新列表 */ private void clearListView() { if (devicesDatas != null) devicesDatas.clear(); if (listString != null) listString.clear(); adapter.notifyDataSetChanged(); } @Override protected void onDestroy() { //关闭Socket接收 super.onDestroy(); HDLCommand.release(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onDevicesInfoEventMain(DevicesInfoEvent event) { listString.clear(); proDia.dismiss(); if (!event.isSuccess()) { Toast.makeText(MainActivity.this, "搜索超时,请重新再试", Toast.LENGTH_SHORT).show(); tv.setText("搜索超时,请重新再试"); return; } devicesDatas = event.getDesDataList(); int countAll = 0; for (DevicesData devicesData : devicesDatas) { countAll += devicesData.getAppliancesInfoList().size(); } tv.setText("总共模块数:" + event.getDesDataList().size() + " 总共回路数:" + countAll); 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(); } // 已废除,场景数据已经和常规设备合并。若有需求需要区分,请根据设备类型 // 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 onRcuIpListEventMain(RcuIpListEvent event) { // for (int i = 0; i < event.getRcuIpList().size(); i++) { // Log.i("djl", "用户收到rcuIp" + event.getRcuIpList().get(i)); // } // //调用 //// HDLCommand.getRcuDevices(String rcuIp); // } @Subscribe(threadMode = ThreadMode.MAIN) public void onWarningEventMain(WarningInfoEvent event) { String warningType = event.getWarningType(); Toast.makeText(MainActivity.this, warningType, Toast.LENGTH_SHORT).show(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onBgmInfoEventMain(BgmInfoEvent event) { int eventType = event.getType(); } public static boolean isIP(String str) { // 匹配 1 // String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; // 匹配 2 String regex = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"; // 匹配1 和匹配2均可实现Ip判断的效果 Pattern pattern = Pattern.compile(regex); return pattern.matcher(str).matches(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onBgmInfoEventMain(ThirdPartyBgmInfoEvent event) { byte[] eventType = event.getBytes(); } private void hideTestButton(){ btn_get_all.setVisibility(View.GONE); btn_on_all.setVisibility(View.GONE); btn_off_all.setVisibility(View.GONE); } private void getAllAirList(){ airList.clear(); if(devicesDatas == null || devicesDatas.size() == 0) { showToast("没有设备"); return; } for (int j = 0; j < devicesDatas.size(); j++) { DevicesData mDevicesData = devicesDatas.get(j); List appliancesInfos = mDevicesData.getAppliancesInfoList(); for (int i = 0; i < appliancesInfos.size(); i++) { if (appliancesInfos.get(i).getBigType() == Configuration.AIR_BIG_TYPE) { airList.add(appliancesInfos.get(i)); } } } String mes = "共获得:" + airList.size() + " 台空调"; showToast(mes); } private void sendAll(Boolean bOn){ if(airList == null || airList.size() == 0) { showToast("没有设备"); return; } for (int j = 0; j < airList.size(); j++) { AppliancesInfo info = airList.get(j); if(bOn){ HDLCommand.airCtrl(info, AirCtrlParser.airSwich, AirCtrlParser.airOn); }else { HDLCommand.airCtrl(info, AirCtrlParser.airSwich, AirCtrlParser.airOff); } } } private void showToast(String text) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } }