a
554325746@qq.com
2020-01-02 e52632329ef8342a2f692bf58184fc54db3d2b4c
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
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);
            }
        });
    }
 
}