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
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.HDLDeviceManger.Bean.DevicesData;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLCommand;
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 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 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;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.hdl.sdk.hdl_sdk.R.layout.activity_main);
        HDLCommand.init(this);
//        /**配置是否开启SDK打印日志,默认为打开*/
//        HDLCommand.setHDLLogOpen(false);//
 
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
 
        initView();
        setOnClick();
 
 
    }
 
    private void initView() {
        btn = findViewById(com.hdl.sdk.hdl_sdk.R.id.btn);
        btn2 = findViewById(com.hdl.sdk.hdl_sdk.R.id.get);
        tv = findViewById(com.hdl.sdk.hdl_sdk.R.id.tv);
        editText = findViewById(com.hdl.sdk.hdl_sdk.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(com.hdl.sdk.hdl_sdk.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();
                }
 
 
            }
        });
    }
 
 
    @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();
 
 
    }
 
 
}