package com.basic.security.manager.erlang; import android.os.SystemClock; import android.text.TextUtils; import com.basic.security.base.BaseApplication; import com.basic.security.utils.Constants; import com.ericsson.otp.erlang.OtpConnection; import java.util.ArrayList; import java.util.List; import java.util.Map; public class DeviceManager { public static List> getLocalDevice() { return getDeviceList(Constants.erlangLocalNode, "", Constants.erlangLocalNode); } public static List> getOtherDeviceList() { return getDeviceList(Constants.erlangLocalNode, Constants.erlangLocalNode, ""); } public static String getInsertSql(Map deviceInFatherNode) { String deviceInfoInFatherNodeInsertSql = "INSERT INTO `main`.`device_info` (\n" + " `father_node`,\n" + " `table`,\n" + " `uuid`,\n" + " `node_id`,\n" + " `device_id`,\n" + " `cluster_id`,\n" + " `create_time`,\n" + " `update_time`,\n" + " `create_by`,\n" + " `del_flag`\n" + ")\n" + "VALUES\n" + " (\n" + " '" + deviceInFatherNode.get("father_node") + "',\n" + " '" + deviceInFatherNode.get("table") + "',\n" + " '" + deviceInFatherNode.get("uuid") + "',\n" + " '" + deviceInFatherNode.get("node_id") + "',\n" + " '" + deviceInFatherNode.get("device_id") + "',\n" + " '" + deviceInFatherNode.get("cluster_id") + "',\n" + " '" + deviceInFatherNode.get("create_time") + "',\n" + " '" + deviceInFatherNode.get("update_time") + "',\n" + " '" + deviceInFatherNode.get("create_by") + "',\n" + " '" + deviceInFatherNode.get("del_flag") + "'\n" + " );"; return deviceInfoInFatherNodeInsertSql; } public static List> getDeviceList(String nodeName, String notNodeId, String isNodeId) { String andNotNodeId = ""; if (!TextUtils.isEmpty(notNodeId)) { andNotNodeId = " and a.node_id != '" + notNodeId + "' "; } String andIsNodeId = ""; if (!TextUtils.isEmpty(isNodeId)) { andIsNodeId = " and a.node_id = '" + isNodeId + "' "; } List> deviceListInFatherNode = SqliteManager.queryFromSync(ErlangConnection.getRemoteConnection(nodeName), "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 " + " where 1=1 " + andNotNodeId + andIsNodeId ); return deviceListInFatherNode; } public static List getInsertDeviceSqlList(String nodeName, String notNodeId) { return getInsertDeviceSqlList(nodeName, notNodeId, ""); } public static List getInsertDeviceSqlList(String nodeName, String notNodeId, String isNodeId) { List nodeIdlList = new ArrayList<>(); List insertDeviceSqlList = new ArrayList<>(); List> deviceListInFatherNode = getDeviceList(nodeName, notNodeId, isNodeId); for (Map deviceInFatherNode : deviceListInFatherNode) { for (Map.Entry entry : deviceInFatherNode.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (value == null || "NULL".equalsIgnoreCase(value) || "undefined".equalsIgnoreCase(value)) { deviceInFatherNode.put(key, ""); } } // 添加了if判断是为了解决集群中有node_id为null以及有重复设备id的bug if (deviceInFatherNode.get("node_id") != null && !nodeIdlList.contains(deviceInFatherNode.get("node_id"))) { nodeIdlList.add(deviceInFatherNode.get("node_id")); insertDeviceSqlList.add(getInsertSql(deviceInFatherNode)); } } return insertDeviceSqlList; } public static void sendLocalDeviceInfoToOtherDevices(List> localDeviceList) { String localDeviceInsertSql = getInsertSql(localDeviceList.get(0)); List> otherDeviceList = getOtherDeviceList(); for (Map otherDevice : otherDeviceList) { String nodeId = otherDevice.get("node_id"); SqliteManager.executeInSync(ErlangConnection.getRemoteConnection(nodeId), localDeviceInsertSql); } } public static void downloadDeviceInfoFromOtherDevices(String otherNodeName) { List insertDeviceSqlList = DeviceManager.getInsertDeviceSqlList(otherNodeName, ""); for (String inserDeviceSql : insertDeviceSqlList) { SqliteManager.executeInSync(ErlangConnection.getLocalConnection(), inserDeviceSql); } } public static void syncDeviceInfoAtAppStart() { BaseApplication.getApplication().executorService.execute(() -> { while (true) { try { OtpConnection connection = ErlangConnection.getLocalConnection(); if (connection != null && connection.isConnected()) { List> localDeviceList = getLocalDevice(); if (localDeviceList.size() > 0) { for (Map localDevice : localDeviceList) { String fatherNode = localDevice.get("father_node"); if (fatherNode != null && fatherNode.length() > 5) { downloadDeviceInfoFromOtherDevices(fatherNode); } } sendLocalDeviceInfoToOtherDevices(localDeviceList); break; } } } catch (Exception e) { e.printStackTrace(); } SystemClock.sleep(2 * 1000); } }); } }