package com.basic.security.manager;
|
|
|
import android.database.Cursor;
|
import android.database.sqlite.SQLiteDatabase;
|
import android.os.SystemClock;
|
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.manager.impl.cblite.DeviceManager;
|
import com.basic.security.manager.impl.cblite.RuleManager;
|
import com.basic.security.manager.impl.cblite.UserManager;
|
import com.basic.security.manager.impl.erlang.ErlangProcess;
|
import com.basic.security.manager.impl.erlang.NodeNameManager;
|
import com.basic.security.manager.impl.sqlite.FeatureManager;
|
import com.basic.security.manager.impl.sqlite.SlBaseManager;
|
import com.basic.security.manager.impl.sqlite.SlDeviceManager;
|
import com.basic.security.model.Device;
|
import com.basic.security.model.ModelAdapter;
|
import com.basic.security.model.Person;
|
import com.basic.security.utils.Constants;
|
import com.basic.security.utils.IpUtils;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
public class SystemInitManager {
|
|
public static void initTablesAndDefaultValues() {
|
BaseApplication.getApplication().executorService.execute(() -> {
|
createTablesInBaseDatabase();
|
createTablesInCacheDatabase();
|
createTablesInSyncDatabase();
|
initDefaultValues();
|
FeatureManager.initCppFeatureMap();
|
OfficeDeviceManager.initOfficeDevice();
|
});
|
}
|
|
public static void initDefaultValues() {
|
try {
|
String deviceId = SlDeviceManager.getDeviceId();
|
ModelAdapter adminIdentity = SlBaseManager.findById("identity", "adminIdentity");
|
if (adminIdentity == null) {
|
adminIdentity = new ModelAdapter("adminIdentity");
|
adminIdentity.setString("table", "identity");
|
adminIdentity.setString("name", "管理员");
|
adminIdentity.setString("auto_init", "1");
|
adminIdentity.setString("device_id", DeviceManager.getDeviceId());
|
adminIdentity.setString("company_id", DeviceManager.getCompareId());
|
}
|
RuleManager.saveRule(adminIdentity);
|
|
ModelAdapter staffIdentity = SlBaseManager.findById("identity", "staffIdentity");
|
if (staffIdentity == null) {
|
staffIdentity = new ModelAdapter("staffIdentity");
|
staffIdentity.setString("table", "identity");
|
staffIdentity.setString("name", "老师");
|
staffIdentity.setString("device_id", DeviceManager.getDeviceId());
|
staffIdentity.setString("company_id", DeviceManager.getCompareId());
|
}
|
RuleManager.saveRule(staffIdentity);
|
|
ModelAdapter blackListIdentity = SlBaseManager.findById("identity", "blackListIdentity");
|
if (blackListIdentity == null) {
|
blackListIdentity = new ModelAdapter("blackListIdentity");
|
blackListIdentity.setString("table", "identity");
|
blackListIdentity.setString("name", "内部人员");
|
blackListIdentity.setString("device_id", DeviceManager.getDeviceId());
|
blackListIdentity.setString("company_id", DeviceManager.getCompareId());
|
}
|
RuleManager.saveRule(blackListIdentity);
|
|
ModelAdapter admin_person = SlBaseManager.findById("person", deviceId + "admin_person");
|
if (admin_person == null) {
|
admin_person = new ModelAdapter(deviceId + "admin_person")
|
.setString("name", "系统管理员")
|
.setString("sign_up_status", "已注册")
|
.setString(Person.sign_up_rule, "允许通行")
|
.setString("verify_status", "不需要验证身份证")
|
.setString("sign_up_method", "系统初始化分配")
|
.setString("sign_up_time", System.currentTimeMillis() + "")
|
.setString("device_id", DeviceManager.getDeviceId())
|
//是系统初始化默认设定的 不能删除这个person的管理员身份
|
.setString("auto_init", "1")
|
.setString("company_id", DeviceManager.getCompareId())
|
.setString("table", "person");
|
}
|
PersonManager.savePerson(admin_person);
|
|
ModelAdapter admin_user_table = SlBaseManager.findById("user", deviceId + "admin_user");
|
if (admin_user_table == null) {
|
admin_user_table = new ModelAdapter(deviceId + "admin_user")
|
.setString("table", "user")
|
.setString("name", "系统管理员")
|
.setString("person_id", admin_person.getId())
|
.setString("username", "admin")
|
.setString("auto_init", "1")
|
.setString("password", "admin123")
|
.setString("device_id", DeviceManager.getDeviceId())
|
.setString("company_id", DeviceManager.getCompareId());
|
}
|
UserManager.saveUser(admin_user_table);
|
|
ModelAdapter admin_person_identity = SlBaseManager.findById("person_identity", deviceId + "admin_person_identity");
|
if (admin_person_identity == null) {
|
admin_person_identity = new ModelAdapter(deviceId + "admin_person_identity")
|
.setString("table", "person_identity")
|
.setString("identity_id", adminIdentity.getId())
|
.setString("auto_init", "1")
|
.setString("person_id", admin_person.getId())
|
.setString("device_id", DeviceManager.getDeviceId())
|
.setString("company_id", DeviceManager.getCompareId());
|
}
|
PersonIdentityManager.savePersonIdentity(admin_person_identity);
|
|
BaseSettingManager.initBaseSetting();
|
|
ModelAdapter device = SlBaseManager.findById("device", deviceId + "device");
|
if (device == null) {
|
device = new ModelAdapter(deviceId + "device");
|
device.setString("device_id", SlDeviceManager.getDeviceId());
|
device.setString("ip", IpUtils.getIpAddress(BaseApplication.getApplication()));
|
device.setString(Device.gateway, IpUtils.getGateway(BaseApplication.getApplication()));
|
device.setString(Device.mask, IpUtils.getNetmask(BaseApplication.getApplication()));
|
device.setString("name", DeviceManager.getDeviceId());
|
device.setString("open_door_time", "5");
|
device.setString("no_face_delay", "10");
|
device.setString("table", "device");
|
}
|
SlBaseManager.save(device);
|
|
HintDoorAccessManager.initHintDoorAccess();
|
HintRecognizeManager.initHintRecognize();
|
|
HintSignUpManager.initHintSignUp();
|
|
// DatabaseManager.getDatabase(Constants.cacheDatabasePath).close();
|
// DatabaseManager.getDatabase(Constants.syncDatabasePath).close();
|
SystemClock.sleep(1000);
|
ErlangProcess.erlangProcessCanStart = true;
|
NodeNameManager.start();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void createTablesInCacheDatabase() {
|
try {
|
SQLiteDatabase cacheDatabase = SQLiteDatabase.openOrCreateDatabase(
|
Constants.cacheDatabasePath, null);
|
cacheDatabase.execSQL("CREATE TABLE\n" +
|
"IF NOT EXISTS SqlCache (\n" +
|
" uuid TEXT PRIMARY KEY,\n" +
|
" func TEXT DEFAULT NULL,\n" +
|
" sql TEXT DEFAULT NULL,\n" +
|
" create_time TEXT,\n" +
|
" create_by TEXT\n" +
|
")");
|
cacheDatabase.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_sql_cache ON SqlCache (uuid)");
|
cacheDatabase.execSQL("CREATE TABLE\n" +
|
"IF NOT EXISTS sql_cache_device (\n" +
|
" sql_cache_id TEXT ,\n" +
|
" receive_device_id TEXT, \n" +
|
" CONSTRAINT idx_sql_cache_device UNIQUE (sql_cache_id, receive_device_id)" +
|
")");
|
cacheDatabase.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void createTablesInBaseDatabase() {
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS base_setting (open_door_password TEXT, open_door_password_enable TEXT,\"open_door_duration\" TEXT, \"auto_register_rule\" TEXT, \"auto_checked_time_rule_id\" TEXT, \"device_setting\" TEXT, \"relation_person\" TEXT, \"relation_device\" TEXT, \"device_name_ip\" TEXT,\"server_name_ip\" TEXT,\"no_face_delay\" TEXT,\"picture_rotation_angle\" TEXT,\"id\" TEXT, \"auto_mode\" TEXT, \"admin_mode\" TEXT, \"pass_mode\" TEXT, \"visitor_mode\" TEXT, \"admin_normal_verify\" TEXT, \"admin_normal_verify_phone\" TEXT, \"admin_normal_verify_idcard\" TEXT, \"admin_normal_verify_name\" TEXT, \"admin_idcard_verify\" TEXT, \"admin_idcard_verify_phone\" TEXT, \"back_to_home\" TEXT, \"image_play_interval\" TEXT, \"detect_multiple_face\" TEXT, \"face_ratio\" TEXT, \"face_angle\" TEXT, \"image_play_time\" TEXT, \"login_expire_long\" TEXT, \"auto_mode_verify_phone\" TEXT, \"auto_normal_verify\" TEXT, \"auto_normal_verify_phone\" TEXT, \"auto_normal_verify_idcard\" TEXT, \"auto_normal_verify_name\" TEXT, \"auto_idcard_verify\" TEXT, \"auto_idcard_verify_phone\" TEXT, \"auto_register_identity_id\" TEXT, \"auto_register_identity_name\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
try {
|
DatabaseManager.execSQL("ALTER TABLE base_setting ADD COLUMN normally_closed text");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
try {
|
DatabaseManager.execSQL("ALTER TABLE base_setting ADD COLUMN reboot_hour text");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
try {
|
DatabaseManager.execSQL("ALTER TABLE base_setting ADD COLUMN reboot_hour_enable text");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_base_setting ON base_setting (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS business (\"id\" TEXT, \"business_name\" TEXT, \"create_time\" TEXT, \"only_allow\" TEXT, \"only_allow_start\" TEXT, \"only_allow_end\" TEXT, \"choose_from_time_rule\" TEXT, \"checked_time_rule_id\" TEXT, \"access_table_name\" TEXT,\"access_identity\" TEXT, \"access_name\" TEXT, \"access_hint\" TEXT, \"not_reached_time_hint\" TEXT, \"not_reached_time_remind\" TEXT, \"time_unit\" TEXT, \"confirm_pass_identity\" TEXT, \"confirm_pass_name\" TEXT, \"confirm_pass_hint\" TEXT, \"time_ids\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_business ON business (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS business_person (\"id\" TEXT, \"person_id\" TEXT, \"business_id\" TEXT, \"pass_model\" TEXT, \"contact_person\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_business_person ON business_person (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS confirm (\"id\" TEXT, \"from_device_id\" TEXT, \"from_person_id\" TEXT, \"from_time\" TEXT, \"valid_long\" TEXT, \"confirm_device_id\" TEXT, \"confirm_user_id\" TEXT, \"confirm_time\" TEXT, \"del_flag\" TEXT, \"company_id\" TEXT, \"table\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_confirm ON confirm (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS device (\"id\" TEXT, \"name\" TEXT, \"ip\" TEXT, \"mask\" TEXT, \"gateway\" TEXT, \"no_face_delay\" TEXT, \"address\" TEXT, \"server_ip\" TEXT, \"apply_device_ids\" TEXT, \"device_id\" TEXT, \"open_door_time\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_device ON device (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS hint_door_access (\"id\" TEXT, \"name\" TEXT, \"device_id\" TEXT, \"no_register\" TEXT, \"company_id\" TEXT, \"access_identity\" TEXT, \"access_name\" TEXT, \"access_hint\" TEXT, \"not_reached_time_hint\" TEXT, \"not_reached_time_remind\" TEXT, \"confirm_pass_identity\" TEXT, \"confirm_pass_name\" TEXT, \"confirm_pass_hint\" TEXT, \"refuse_pass_identity\" TEXT, \"refuse_pass_name\" TEXT, \"refuse_pass_hint\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_hint_door_access ON hint_door_access (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS hint_recognize_message (\"id\" TEXT, \"name\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"head_up\" TEXT, \"head_up_message\" TEXT, \"head_down\" TEXT, \"head_down_message\" TEXT, \"face_max\" TEXT, \"face_max_message\" TEXT, \"face_min\" TEXT, \"face_min_message\" TEXT, \"rotation_left\" TEXT, \"rotation_left_message\" TEXT, \"rotation_right\" TEXT, \"rotation_right_message\" TEXT, \"all_fit\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_hint_recognize_message ON hint_recognize_message (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS hint_sign_up (\"id\" TEXT, \"name\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"id_card_message\" TEXT, \"id_card_success\" TEXT, \"id_card_fail\" TEXT, \"edit_message\" TEXT, \"register_success\" TEXT, \"admin_logout\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_hint_sign_up ON hint_sign_up (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS identity (\"id\" TEXT, \"name\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_identity ON identity (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS org (\"id\" TEXT, \"pid\" TEXT, \"name\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_org ON org (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS person (\"camera_image_feature_path\" text, \"checked_time_rule_id\" TEXT, \"sign_up_rule\" TEXT,\"id\" TEXT, \"name\" TEXT, \"camera_image_path\" TEXT, \"camera_image_feature\" TEXT, \"camera_image_fastdfs_path\" TEXT, \"id_card_number\" TEXT, \"id_card_image_path\" TEXT, \"id_card_image_feature\" TEXT, \"type\" TEXT, \"phone\" TEXT, \"sign_up_time\" TEXT, \"sign_up_method\" TEXT, \"sign_up_status\" TEXT, \"verify_status\" TEXT, \"auto_init\" TEXT, \"update_time\" TEXT, \"del_flag\" TEXT, \"all_device\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"device_id\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_person ON person (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS person_identity (\"id\" TEXT, \"person_id\" TEXT, \"identity_id\" TEXT, \"del_flag\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_person_identity ON person_identity (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS temporary_person (\"id\" TEXT, \"save_time\" TEXT, \"ip\" TEXT, \"camera_image_path\" TEXT, \"camera_image_feature\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_temporary_person ON temporary_person (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS time (\"id\" TEXT, \"time_rule_name\" TEXT, \"week_json\" TEXT, \"date_json\" TEXT, \"is_use\" TEXT, \"del_flag\" TEXT, \"table\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_time ON time (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS user (\"id\" TEXT, \"person_id\" TEXT, \"username\" TEXT, \"password\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_user ON user (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS visit (\"id\" TEXT, \"person_id\" TEXT, \"person_name\" TEXT, \"device_name\" TEXT, \"visit_time\" TEXT, \"verify_result\" TEXT, \"exit_time\" TEXT, \"target_person_id\" TEXT, \"target_person_name\" TEXT, \"target_org_id\" TEXT, \"target_org_name\" TEXT, \"del_flag\" TEXT, \"person_id_number\" TEXT, \"person_phone\" TEXT, \"person_type\" TEXT, \"device_id\" TEXT, \"company_id\" TEXT, \"table\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_visit ON visit (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS business_apply_device (\"id\" TEXT, \"business_id\" TEXT, \"device_id\" TEXT, \"table\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_business_apply_device ON business_apply_device (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS OfficeDevice (\"id\" TEXT,\"deviceIp\" TEXT, \"deviceName\" TEXT, \"deviceId\" TEXT, \"company_id\" TEXT, \"ip\" TEXT, \"valid\" TEXT, \"del_flag\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_OfficeDevice ON OfficeDevice (id)");
|
DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS Log (\"id\" TEXT,\"message\" TEXT,\"createTime\" TEXT,\"table\" TEXT, PRIMARY KEY(id))");
|
DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_log ON Log (id)");
|
initTableColumnNames();
|
}
|
|
public static void createTablesInSyncDatabase() {
|
try {
|
SQLiteDatabase syncDatabase = SQLiteDatabase.openOrCreateDatabase(
|
Constants.syncDatabasePath, null);
|
syncDatabase.execSQL("CREATE TABLE IF NOT EXISTS 'device_info'(\"father_node\" text, \"table\" text ,uuid text PRIMARY KEY, node_id text, device_id text, cluster_id text, create_time text, update_time text, create_by text, del_flag text )");
|
syncDatabase.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_device_info ON device_info (uuid)");
|
syncDatabase.execSQL("CREATE TABLE IF NOT EXISTS 'cluster_info'(\"table\" text, cluster_id text PRIMARY KEY,cluster_name text,create_time text ,update_time text,create_by text,del_flag text )");
|
syncDatabase.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_cluster_info ON cluster_info (cluster_id)");
|
syncDatabase.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private static void initTableColumnNames() {
|
SQLiteDatabase mDataBase = DatabaseManager.getDatabase();
|
Cursor c = mDataBase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
|
List<String> tableNameList = new ArrayList<>();
|
if (c.moveToFirst()) {
|
while (!c.isAfterLast()) {
|
tableNameList.add(c.getString(0));
|
c.moveToNext();
|
}
|
}
|
c.close();
|
for (String tableName : tableNameList) {
|
Cursor dbCursor = mDataBase.query(tableName, null, null, null, null, null, null);
|
String[] columnNames = dbCursor.getColumnNames();
|
BaseManager.tableColumnNames.put(tableName, Arrays.asList(columnNames));
|
dbCursor.close();
|
}
|
}
|
/*device_id=1
|
company_id=1
|
couchbase_server_ip=192.168.1.133
|
port=5555*/
|
// public void initLocalSetting() {
|
// // 把本地设置写到SD卡中
|
// File file = new File("/sdcard/config.ini");
|
// if (!file.exists()) {
|
// CopyToSdcard.getInstance(this).copyAssetsToSD("config.ini", "/config.ini");
|
// }
|
//
|
// try {
|
// //这个判断语句使我们常用的:判断手机是否插入了SD卡和是否有访问权限,如果都有就返回true
|
// if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
// FileInputStream fis = new FileInputStream("/sdcard/config.ini");
|
// List<String> lines = IOUtils.readLines(fis);
|
// for (String line : lines) {
|
// String[] split = line.split("=");
|
// localSetting.put(split[0], split[1]);
|
// }
|
// DeviceManager.device_id = localSetting.get("device_id");
|
// DeviceManager.company_id = localSetting.get("company_id");
|
// DeviceManager.couchbase_server_ip = localSetting.get("couchbase_server_ip");
|
// DeviceManager.port = localSetting.get("port");
|
//
|
// }
|
//
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
//
|
// }
|
|
}
|