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<Map<String, String>> getLocalDevice() {
|
return getDeviceList(Constants.erlangLocalNode, "", Constants.erlangLocalNode);
|
}
|
|
public static List<Map<String, String>> getOtherDeviceList() {
|
return getDeviceList(Constants.erlangLocalNode, Constants.erlangLocalNode, "");
|
}
|
|
public static String getInsertSql(Map<String, String> 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<Map<String, String>> 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<Map<String, String>> 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<String> getInsertDeviceSqlList(String nodeName, String notNodeId) {
|
return getInsertDeviceSqlList(nodeName, notNodeId, "");
|
}
|
|
public static List<String> getInsertDeviceSqlList(String nodeName, String notNodeId, String isNodeId) {
|
List<String> nodeIdlList = new ArrayList<>();
|
List<String> insertDeviceSqlList = new ArrayList<>();
|
List<Map<String, String>> deviceListInFatherNode = getDeviceList(nodeName, notNodeId, isNodeId);
|
for (Map<String, String> deviceInFatherNode : deviceListInFatherNode) {
|
for (Map.Entry<String, String> 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<Map<String, String>> localDeviceList) {
|
String localDeviceInsertSql = getInsertSql(localDeviceList.get(0));
|
List<Map<String, String>> otherDeviceList = getOtherDeviceList();
|
for (Map<String, String> otherDevice : otherDeviceList) {
|
String nodeId = otherDevice.get("node_id");
|
SqliteManager.executeInSync(ErlangConnection.getRemoteConnection(nodeId), localDeviceInsertSql);
|
}
|
}
|
|
public static void downloadDeviceInfoFromOtherDevices(String otherNodeName) {
|
List<String> 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<Map<String, String>> localDeviceList = getLocalDevice();
|
if (localDeviceList.size() > 0) {
|
for (Map<String, String> 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);
|
}
|
});
|
}
|
|
}
|