DeteMin
2020-03-31 77c62e023d2dc31200fc696158df84b3aee90ee7
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
package com.basic.security.fragment;
 
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
 
import com.basic.security.R;
import com.basic.security.base.BaseApplication;
import com.basic.security.base.BaseFragment;
import com.basic.security.manager.impl.erlang.ClusterManager;
import com.basic.security.manager.impl.erlang.ErlangConnection;
import com.basic.security.manager.impl.erlang.SqliteManager;
import com.basic.security.utils.Constants;
import com.basic.security.utils.ToastUtil;
import com.basic.security.widget.RoundView;
 
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@EFragment(R.layout.fragment_cluster_setting)
public class ClusterSettingFragment extends BaseFragment {
 
    @ViewById
    Button cluster_a, cluster_b, cluster_c, cluster_plus, confirm_save, cancel_save, cluster_add, cluster_exit;
    @ViewById
    RadioButton only_allow, no_limit;
    @ViewById
    EditText cluster_name, cluster_cookie, cluster_ip;
    @ViewById
    RoundView round_view;
    @ViewById
    LinearLayout ll_cluster_ip;
    @ViewById
    ListView lv_device_list;
    private DeviceListAdapter deviceListAdapter;
    private List<Map<String, String>> deviceList = new ArrayList<>();
    private boolean isCreateCluster = true;
 
    @AfterViews
    public void afterViews() {
//        lv_device_list.addHeaderView(View.inflate(getContext(), R.layout.cluster_setting_list_head, null));
 
    }
 
    public void queryDeviceList() {
        BaseApplication.getApplication().executorService.execute(() -> {
            deviceList = SqliteManager.queryFromSync(ErlangConnection.getLocalConnection(),
                    "SELECT\n" +
                            "    a.father_node,\n" +
                            "    a.\"table\",\n" +
                            "    a.uuid,\n" +
                            "    a.node_id,\n" +
                            "    a.device_id,\n" +
                            "    a.cluster_id,\n" +
                            "    a.create_time,\n" +
                            "    a.update_time,\n" +
                            "    a.create_by,\n" +
                            "    a.del_flag\n" +
                            "FROM\n" +
                            "    device_info a\n" +
                            "WHERE a.del_flag = 0\n" +
                            "and a.device_id = '" + Constants.deviceId + "'"
            );
            queryDeviceListReturn();
        });
 
    }
 
    @UiThread
    public void queryDeviceListReturn() {
        if (deviceListAdapter == null) {
            deviceListAdapter = new DeviceListAdapter();
            lv_device_list.setAdapter(deviceListAdapter);
        }
//        System.out.println("deviceList.size" + deviceList.size());
        deviceListAdapter.notifyDataSetChanged();
    }
 
    @Override
    public void show() {
        super.show();
        isCreateCluster = true;
        ll_cluster_ip.setVisibility(View.GONE);
        deviceListAdapter = new DeviceListAdapter();
        lv_device_list.setAdapter(deviceListAdapter);
        deviceList.clear();
        queryDeviceList();
 
 
    }
 
    @Click
    public void cluster_plus() {
//        clearEditText();
        isCreateCluster = true;
        ll_cluster_ip.setVisibility(View.GONE);
 
    }
 
    // 保存按钮
    @Click
    void confirm_save() {
        String cluster_name = this.cluster_name.getText().toString().trim();
        String cluster_cookie = this.cluster_cookie.getText().toString().trim();
        String cluster_ip = this.cluster_ip.getText().toString().trim();
        if (TextUtils.isEmpty(cluster_name)) {
            ToastUtil.show("请输入集群名称");
            return;
        }
        if (TextUtils.isEmpty(cluster_cookie)) {
            ToastUtil.show("请输入集群cookie");
            return;
        }
        if (!isCreateCluster) {
            if (TextUtils.isEmpty(cluster_ip)) {
                ToastUtil.show("请输入集群ip");
                return;
            }
 
            class TempJoin {
                void joinClu() {
//                    OtpConnection otpConnection = ErlangConnection.getRemoteConnection(cluster_ip);
//                    try {
//                        Thread.sleep(200);
//                        if(otpConnection== null || !otpConnection.isConnected()){
//                            ToastUtil.show("集群IP有误无法连接到该集群!");
//                        }else{
//                            // 保存加入集群逻辑
//                            ClusterManager.joinCluster(cluster_ip, cluster_cookie, cluster_name);
//                            try {
//                                updateUI();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
 
                    // 保存加入集群逻辑
                    BaseApplication.getApplication().executorService.execute(() -> {
                        ClusterManager.joinCluster(cluster_ip, cluster_cookie, cluster_name);
                        updateUI();
                    });
                }
 
                @UiThread
                void updateUI() {
                    try {
                        Thread.sleep(500);
                        ToastUtil.show("加入集群成功!");
                        clearEditText();
                        show();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
 
//                    mainActivity().showFragment(mainActivity().fragment_cluster_setting);
                }
            }
            new TempJoin().joinClu();
        } else {
            class TempCreate {
                void createClu() {
                    BaseApplication.getApplication().executorService.execute(() -> {
                        // 保存创建集群逻辑
                        //                    System.out.println(System.currentTimeMillis()+"创建集群的时间!!");
                        List<Map<String, String>> list = new ArrayList<>();
                        list = ClusterManager.queryClusterByDevice();
                        if (list.size() != 0) {
                            ToastUtil.show("该设备已经创建集群!");
                        } else {
                            ClusterManager.createCluster(cluster_cookie, cluster_name, "1");
                            try {
                                updateUI();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    });
 
                }
 
                @UiThread
                void updateUI() throws InterruptedException {
                    Thread.sleep(500);
//                    System.out.println(System.currentTimeMillis()+"进入updateUI的时间!!");
                    ToastUtil.show("创建集群成功!");
                    show();
//                    mainActivity().showFragment(mainActivity().fragment_cluster_setting);
                }
            }
            new TempCreate().createClu();
        }
 
    }
 
    // 取消按钮
    @Click
    void cancel_save() {
 
    }
 
    @Click
    public void cluster_add() {
        isCreateCluster = false;
//        clearEditText();
        ll_cluster_ip.setVisibility(View.VISIBLE);
    }
 
    //   private synchronized void f1( String deviceId){
//        ClusterManager.exitCluster(deviceId);
//    }
    @Click
    public void cluster_exit() {
        Map<String, String> device = deviceList.get(0);
        if (!device.isEmpty()) {
            String deviceId = device.get("device_id");
            try {
                ToastUtil.show("退出集群成功!");
                class Temp {
                    void deleteBackGround(String deviceId) {
                        BaseApplication.getApplication().executorService.execute(() -> {
                            ClusterManager.phyDelDeviceInfo(deviceId);
                            try {
                                showUiThread();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        });
                    }
 
                    @UiThread
                    void showUiThread() throws InterruptedException {
                        Thread.sleep(500);
                        clearEditText();
                        show();
                    }
                }
                new Temp().deleteBackGround(deviceId);
//                ExecutorService service = Executors.newSingleThreadExecutor();
//                service.submit(() -> {
//                    ClusterManager.exitCluster(deviceId);
//                });
//                service.submit(() -> {
//                    show();
//                    ToastUtil.show("退出集群成功!");
//                });
//                service.shutdown();
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void clearEditText() {
        cluster_name.setText("");
        cluster_cookie.setText("");
        cluster_ip.setText("");
    }
 
    private class DeviceListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return deviceList == null ? 0 : deviceList.size();
        }
 
        @Override
        public Object getItem(int position) {
            return deviceList.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.cluster_setting_list_item, null);
                viewHolder.deviceID = convertView.findViewById(R.id.tv_cluster_device_id);
                viewHolder.IP = convertView.findViewById(R.id.tv_cluster_ip);
                viewHolder.fatherClusterName = convertView.findViewById(R.id.tv_cluster_father_name);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Map<String, String> device = deviceList.get(position);
 
            viewHolder.deviceID.setText(device.get("device_id"));
            viewHolder.IP.setText(device.get("node_id"));
            viewHolder.fatherClusterName.setText(device.get("father_node"));
            return convertView;
        }
 
        class ViewHolder {
            TextView deviceID;
            TextView IP;
            TextView fatherClusterName;
        }
    }
 
}