a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
package com.basic.security.fragment;
 
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
 
import com.basic.security.base.BaseFragment;
import com.basic.security.manager.ClusterSerfSyncManager;
import com.basic.security.manager.ClusterSettingManager;
import com.basic.security.model.ClusterSetting;
import com.basic.security.model.ModelAdapter;
import com.basic.security.utils.Constants;
import com.basic.security.utils.ResolutionAdaptation;
import com.basic.security.utils.ToastUtil;
import com.basic.security.widget.ClusterRoundView;
 
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.TextChange;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@EFragment
public class ClusterFragment 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_id, cluster_cookie, cluster_ip, node_id, node_name;
    @ViewById
    ClusterRoundView round_view;
    @ViewById
    ListView lv_device_list;
    private DeviceListAdapter deviceListAdapter;
    private List<Map<String, String>> deviceList = new ArrayList<>();
    private boolean isCreateCluster = true;
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(ResolutionAdaptation.fragment_cluster1(), container, false);
    }
 
    @AfterViews
    public void afterViews() {
        lv_device_list.addHeaderView(View.inflate(getContext(), com.basic.security.utils.ResolutionAdaptation.cluster_setting_list_head(), null));
    }
 
    public void queryDeviceList() {
    }
 
    @UiThread
    public void queryDeviceListReturn() {
        if (deviceListAdapter == null) {
            deviceListAdapter = new DeviceListAdapter();
            lv_device_list.setAdapter(deviceListAdapter);
        }
        deviceListAdapter.notifyDataSetChanged();
    }
 
    @UiThread
    public void show() {
        super.show();
        ModelAdapter cluster = ClusterSettingManager.getCluster();
        cluster_name.setText(cluster.getString(ClusterSetting.cluster_name));
        cluster_id.setText(cluster.getString(ClusterSetting.cluster_id));
        String password = cluster.getString(ClusterSetting.password);
        if (!TextUtils.isEmpty(password)) {
            try {
//                password = new String(Base64.decode(password, Base64.NO_WRAP), "UTF8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cluster_cookie.setText(password);
        cluster_ip.setText(cluster.getString(ClusterSetting.other_node_ip));
        node_id.setText(cluster.getString(ClusterSetting.node_id));
        node_name.setText(cluster.getString(ClusterSetting.node_name));
        isCreateCluster = true;
        deviceListAdapter = new DeviceListAdapter();
        lv_device_list.setAdapter(deviceListAdapter);
    }
 
    @Click
    public void cluster_plus() {
        isCreateCluster = true;
    }
 
    @Click
    void confirm_save() {
        ModelAdapter cluster = ClusterSettingManager.cluster;
        String cluster_name = this.cluster_name.getText().toString().trim();
        String cluster_id = this.cluster_id.getText().toString().trim();
        String password = this.cluster_cookie.getText().toString().trim();
        String cluster_ip = this.cluster_ip.getText().toString().trim();
        String node_id = this.node_id.getText().toString().trim();
        String node_name = this.node_name.getText().toString().trim();
        if (TextUtils.isEmpty(node_id)) {
            ToastUtil.show("请输入节点ID");
            return;
        }
        if (TextUtils.isEmpty(node_name)) {
            ToastUtil.show("请输入节点名称");
            return;
        }
        if (TextUtils.isEmpty(cluster_name)) {
            ToastUtil.show("请输入集群名称");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show("请输入集群密码");
            return;
        }
        try {
//            password = Base64.encodeToString(password.getBytes("UTF-8"), Base64.NO_WRAP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cluster.setString(ClusterSetting.cluster_name, cluster_name);
        cluster.setString(ClusterSetting.cluster_id, cluster_id);
        cluster.setString(ClusterSetting.password, password);
        cluster.setString(ClusterSetting.other_node_ip, cluster_ip);
        cluster.setString(ClusterSetting.exit, Constants.FALSE);
        ClusterSettingManager.saveCluster(cluster);
        ToastUtil.show("操作成功");
        synchronized (ClusterSerfSyncManager.serfQueueLock) {
            ClusterSerfSyncManager.serfQueue.add("1");
            ClusterSerfSyncManager.serfQueueLock.notify();
        }
    }
 
    @Click
    void cancel_save() {
    }
 
    @Click
    public void cluster_add() {
        confirm_save();
        isCreateCluster = false;
    }
 
    @TextChange(com.basic.security.utils.RUtils.R_id_cluster_ip)
    public void cluster_ip(TextView tv, CharSequence text) {
        if (TextUtils.isEmpty(text)) {
            cluster_add.setText("创建集群");
        } else {
            cluster_add.setText("加入集群");
        }
    }
 
    @Click
    public void cluster_exit() {
        ClusterSettingManager.cluster.setTrue(ClusterSetting.exit);
        ClusterSettingManager.saveCluster(ClusterSettingManager.cluster);
        updateDeviceList(new ArrayList<>());
        synchronized (ClusterSerfSyncManager.serfQueueLock) {
            ClusterSerfSyncManager.serfQueue.add("exit");
            ClusterSerfSyncManager.serfQueueLock.notify();
        }
        ToastUtil.show("退出集群成功");
    }
 
    @UiThread
    public void updateDeviceList(List<Map<String, String>> availableNodes) {
        try {
            if (deviceList != null && deviceListAdapter != null) {
                if (availableNodes != null) {
                    if (availableNodes.size() == 0 && deviceList.size() == 0) {
                        return;
                    }
                    for (Map<String, String> availableNode : availableNodes) {
                        String nodeIp = availableNode.get("nodeAddress");
                        if (!TextUtils.isEmpty(nodeIp)) {
                            nodeIp = nodeIp.replace(":30190", "");
                        } else {
                            nodeIp = "";
                        }
                        availableNode.put("device_id", availableNode.get("nodeID"));
                        availableNode.put("node_ip", nodeIp);
                        availableNode.put("father_node", "");
                    }
                    deviceList = availableNodes;
                    deviceListAdapter.notifyDataSetChanged();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Click
    public void search_cluster() {
//        synchronized (ClusterSerfSyncManager.serfQueueLock) {
//            ClusterSerfSyncManager.serfQueue.add("search");
//            ClusterSerfSyncManager.serfQueueLock.notify();
//        }
    }
 
    private class DeviceListAdapter extends BaseAdapter {
        public int getCount() {
            return deviceList == null ? 0 : deviceList.size();
        }
 
        public Object getItem(int position) {
            return deviceList.get(position);
        }
 
        public long getItemId(int position) {
            return position;
        }
 
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(getContext(), com.basic.security.utils.ResolutionAdaptation.cluster_setting_list_item(), null);
                viewHolder.deviceID = convertView.findViewById(com.basic.security.utils.RUtils.R_id_tv_cluster_device_id);
                viewHolder.IP = convertView.findViewById(com.basic.security.utils.RUtils.R_id_tv_cluster_ip);
                viewHolder.fatherClusterName = convertView.findViewById(com.basic.security.utils.RUtils.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_ip"));
            viewHolder.fatherClusterName.setText(device.get("father_node"));
            return convertView;
        }
 
        class ViewHolder {
            TextView deviceID;
            TextView IP;
            TextView fatherClusterName;
        }
    }
}