JLChen
2020-03-16 7a951ecdc084ea8643e29e1ade59c1877fd7fbe5
app/src/main/java/com/hdl/sdk/hdl_sdk/activity/MainActivity.java
@@ -19,11 +19,15 @@
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_core.Util.NetUtil.NetWorkUtil;
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;
@@ -37,7 +41,8 @@
public class MainActivity extends AppCompatActivity {
    private Button btn, btn2;
    private Button btn_get_all,btn_on_all, btn_off_all;
    private Button btn_get_all,btn_on_all, btn_off_all,btn_getlocal,btn_getlocal_add;
    private Button btn_setting;
    private TextView tv;
    private EditText editText;
    private List<DevicesData> devicesDatas;
@@ -51,6 +56,7 @@
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NetWorkUtil.setIsGetWifiBroadCast(true);
        HDLCommand.init(this);
//        /**配置是否开启SDK打印日志,默认为打开*/
//        HDLCommand.setHDLLogOpen(false);//
@@ -72,7 +78,11 @@
        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);
        btn_setting  = findViewById(R.id.btn_setting);
        tv = findViewById(R.id.tv);
        editText = findViewById(R.id.edt);
        editText.setText("172.168.188.100");
@@ -142,8 +152,84 @@
                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);
            }
        });
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SettingActivity.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() {