JLChen
2019-11-26 d6d95564b016c281bfa2e8c2454db8d71083e858
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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<DevicesData> devicesDatas;
    private List<String> listString = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private ProgressDialog proDia;
    private ListView listView;
    private List<AppliancesInfo> 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<String>(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<AppliancesInfo> 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();
    }
}