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> 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> 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 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 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; } } }