zhangzengfei
2020-03-31 0ce893695d32ab686f9e2309509e80c6feb0d380
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
package com.basic.security.manager.erlang;
 
import android.os.SystemClock;
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.manager.BaseManager;
import com.basic.security.manager.DatabaseManager;
import com.basic.security.manager.DeviceManager;
import com.basic.security.manager.DeviceSettingManager;
import com.basic.security.utils.Base64Util;
import com.basic.security.utils.Constants;
import com.basic.security.utils.FaceId;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpErlangBinary;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangString;
 
import org.apache.commons.io.FileUtils;
 
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class SqlCacheDeviceManager {
 
    public static void downloadSqlCacheList() {
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                List<Map<String, String>> deviceList = SqliteManager.queryFromSync(ErlangConnection.getLocalConnection(), "select father_node,node_id from device_info ");
                String father_node = "";
                for (Map<String, String> device : deviceList) {
                    father_node = device.get("father_node");
                    if (father_node != null && father_node.length() > 4) {
                        break;
                    }
                }
                boolean hasFatherNode = false;
                for (Map<String, String> device : deviceList) {
                    String node_id = device.get("node_id");
                    if (node_id.equals(father_node)) {
                        hasFatherNode = true;
                        break;
                    }
                }
                if (!hasFatherNode) {
                    Map<String, String> fatherNode = new HashMap<>();
                    fatherNode.put("node_id", father_node);
                    deviceList.add(fatherNode);
                }
 
                for (Map<String, String> device : deviceList) {
                    try {
                        String node_id = device.get("node_id");
                        String newNodeName = NodeNameManager.getNodeNameByNodeName(node_id);
                        if (!TextUtils.isEmpty(newNodeName) && newNodeName.length() > 5 && !newNodeName.equals(node_id)) {
                            SqliteManager.executeInSync(ErlangConnection.getLocalConnection(), "update device_info set node_id='" +
                                    newNodeName + "' where node_id='" + node_id + "'");
                        }
                        String fatherNode = device.get("father_node");
                        if (fatherNode != null && fatherNode.length() > 5) {
                            String newFatherNode = NodeNameManager.getNodeNameByNodeName(fatherNode);
                            if (!TextUtils.isEmpty(newFatherNode) && newFatherNode.length() > 5 && !fatherNode.equals(newFatherNode)) {
                                SqliteManager.executeInSync(ErlangConnection.getLocalConnection(), "update device_info set father_node='" +
                                        newFatherNode + "' where father_node='" + fatherNode + "'");
                            }
                        }
                        String remoteDeviceId = node_id.split("@", -1)[0];
                        if (!DeviceManager.getDeviceId().equals(remoteDeviceId)) {
                            List<Map<String, String>> sqlCacheDeviceList = SqliteManager.queryFromCache(ErlangConnection.getRemoteConnection(node_id),
                                    "SELECT DISTINCT\n" +
                                            "    a.uuid,\n" +
                                            "    a.sql,\n" +
                                            "    a.create_by\n" +
                                            "FROM\n" +
                                            "    SqlCache a\n" +
                                            "WHERE\n" +
                                            "    a.uuid NOT IN (\n" +
                                            "        SELECT\n" +
                                            "            b.sql_cache_id\n" +
                                            "        FROM\n" +
                                            "            sql_cache_device b\n" +
                                            "        WHERE\n" +
                                            "            1 = 1\n" +
                                            "        AND b.receive_device_id LIKE '" + DeviceManager.getDeviceId() + "@%'\n" +
                                            "    )\n" +
                                            "ORDER BY\n" +
                                            "    a.create_time"
                            );
                            for (Map<String, String> sqlCacheDevice : sqlCacheDeviceList) {
                                String uuid = sqlCacheDevice.get("uuid");
                                String create_by = sqlCacheDevice.get("create_by");
                                String sql = sqlCacheDevice.get("sql").replace("INSERT INTO", "INSERT OR REPLACE INTO");
                                sql = new String(sql.getBytes("iso8859-1"));
                                if (sql.contains("device_info") || sql.contains("cluster_info")) {
                                    sql = sql.replaceAll("undefined", "");
                                    SqliteManager.executeInSync(ErlangConnection.getLocalConnection(), sql);
                                } else {
                                    if (sql.contains("temporary_person")) {
                                        System.out.println("1");
                                    }
                                    DatabaseManager.execSQL(sql);
                                    // 在此处判断是否是人员信息,是,判断是否在本设备中同步,(FeatureManager)
                                    // 不同步,移除特征,并且在查询人脸时不显示
                                    // FaceId.instance.removeFeatureFromDb();
                                    // 同步,添加特征
                                    if (sql.contains("temporary_person")) {
                                        System.out.println("");
                                    }
                                    if (sql.contains("INSERT OR REPLACE INTO person (")) {
                                        Map<String, String> tableKeyValueMap = BaseManager.parseSql(sql);
                                        byte[] bytes = Base64Util.featureBase642Bytes(tableKeyValueMap.get("camera_image_feature"));
                                        String id = tableKeyValueMap.get("id");
                                        if (bytes != null && !TextUtils.isEmpty(id)) {
                                            FaceId.instance.addFeatureToDb(id, bytes);
                                            if (tableKeyValueMap.containsKey("all_device")) {
                                                String all_device = tableKeyValueMap.get("all_device");
                                                String[] split = all_device.split(",");
                                                if (!Arrays.asList(split).contains(DeviceSettingManager.getDeviceName())) {
                                                    FaceId.instance.removeFeatureFromDb(tableKeyValueMap.get("id"));
                                                }
                                            }
                                        }
                                    }
                                    String imagePath = getImagePath(sql, Constants.attachmentPath + "person_camera_image_path",
                                            "/sdcard/security_attachment/temporary_person_camera_image_path_"
                                    );
                                    if (!TextUtils.isEmpty(imagePath)) {
                                        if (!new File(imagePath).exists()) {
                                            downloadAttachment(create_by, imagePath);
                                        }
                                    }
                                }
                                SqliteManager.executeConfirmInCache(ErlangConnection.getRemoteConnection(create_by),
                                        "INSERT OR REPLACE INTO sql_cache_device(sql_cache_id, receive_device_id) VALUES( " +
                                                "'" + uuid + "'," + "'" + Constants.erlangLocalNode + "'" + ")");
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("downloadSqlCacheList " + e.getMessage());
                    }
                }
                SystemClock.sleep(5 * 1000);
            }
        });
    }
 
    private static String getImagePath(String sql, String... imagePathPatternArray) {
        String imagePath = "";
        for (String imagePathPattern : imagePathPatternArray) {
            if (sql.contains(imagePathPattern)) {
                sql = sql.substring(sql.indexOf(imagePathPattern));
                sql = sql.substring(0, sql.indexOf("',"));
                imagePath = sql;
                break;
            }
        }
        return imagePath;
    }
 
    public static void downloadAttachment(String fromNode, String person_camera_image_path) {
        try {
            OtpConnection otpConnection = ErlangConnection.getRemoteConnection(fromNode);
            otpConnection.sendRPC("esqlite3Tool", "readAttachment", new OtpErlangObject[]{
                    new OtpErlangString(person_camera_image_path)
            });
            OtpErlangObject receiveRPC = otpConnection.receiveRPC();
            if (receiveRPC instanceof OtpErlangBinary) {
                OtpErlangBinary oeb = (OtpErlangBinary) receiveRPC;
                FileUtils.writeByteArrayToFile(new File(person_camera_image_path), oeb.binaryValue());
            }
        } catch (Exception e) {
            System.out.println("downloadAttachment " + e.getMessage());
        }
    }
 
}