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