package com.basic.security.manager;
|
|
import android.database.Cursor;
|
|
import com.basic.security.dao.DatabaseManager;
|
import com.basic.security.dao.SqliteManager;
|
import com.basic.security.model.Alarm;
|
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
public class AlarmManager extends SqliteManager {
|
|
public static List<Map<String, String>> sqliteAlarmList = new ArrayList<>();
|
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
public static Map<String, String> findById(String id) {
|
Cursor cursor = null;
|
try {
|
cursor = DatabaseManager.getDatabase().rawQuery("select * from alarm where id = '" + id + "'", null);
|
if (cursor.moveToFirst()) {
|
return cursorToModelAdapter(cursor, "alarm");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null)
|
cursor.close();
|
}
|
return null;
|
}
|
|
public static void renameKeys(List<Map<String, String>> listApiModel) {
|
for (Map<String, String> apiModel : listApiModel) {
|
for (Map.Entry<String, String> entry : apiModel.entrySet()) {
|
String value = entry.getValue();
|
if (value != null) {
|
if (value.startsWith("\"")) {
|
value = value.substring(1);
|
}
|
if (value.endsWith("\"")) {
|
value = value.substring(0, value.length() - 1);
|
}
|
apiModel.put(entry.getKey(), value);
|
}
|
}
|
}
|
for (Map<String, String> apiModel : listApiModel) {
|
apiModel.put("table", "alarm");
|
apiModel.put("alarmTime", apiModel.get("picDate"));
|
apiModel.put("alarmPerson", "");
|
|
String sdkType = apiModel.get("sdkType");
|
if (sdkType != null) {
|
apiModel.put("alarmType", apiModel.remove("sdkType"));
|
if (sdkType.equals("人脸")) {
|
apiModel.put("property", apiModel.get("gender") + " " + apiModel.get("ageDescription") + " " + apiModel.get("race"));
|
apiModel.put("alarmPerson",
|
apiModel.remove("tableName")+" "+ apiModel.remove("idcard"));
|
}
|
}
|
apiModel.put("alarmVideo", apiModel.get("videoNum"));
|
String picDate = apiModel.get("picDate");
|
if (picDate != null) {
|
apiModel.put("alarmPicture", apiModel.remove("picDate"));
|
}
|
apiModel.put("alarmPicture",
|
apiModel.get("picSmUrl")
|
);
|
apiModel.put("alarmLargePicture",
|
apiModel.get("picMaxUrl")
|
);
|
String picAddress = apiModel.get("picAddress");
|
if (picAddress != null) {
|
apiModel.put("alarmAddress", apiModel.remove("picAddress"));
|
}
|
}
|
}
|
|
public static List<Map<String, String>> findAlarmList() {
|
List<Map<String, String>> dbSqliteAlarmList = findList("select * from alarm where 1=1 and closeAlarm='false' order by alarmTime desc ");
|
int maxAlarm = 1000;
|
sqliteAlarmList.clear();
|
if (dbSqliteAlarmList.size() > maxAlarm) {
|
for (int i = 0; i < dbSqliteAlarmList.size(); i++) {
|
if (i < maxAlarm) {
|
sqliteAlarmList.add(dbSqliteAlarmList.get(i));
|
} else {
|
try {
|
DatabaseManager.execSQL("delete from alarm where id='" + dbSqliteAlarmList.get(i).get("id") + "'");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
} else {
|
sqliteAlarmList.addAll(dbSqliteAlarmList);
|
}
|
long keepStartTime = SettingManager.getKeepStartTime();
|
for (Map<String, String> alarm : sqliteAlarmList) {
|
String alarmVideo = alarm.get("alarmVideo");
|
String alarmPicture = alarm.get("alarmPicture");
|
String alarmLargePicture = alarm.get("alarmLargePicture");
|
String alarmTimeStr = alarm.get(Alarm.alarmTime);
|
long alarmTime = new Date().getTime();
|
if (alarmTimeStr != null && alarmTimeStr.length() > "yyyy-MM-dd HH:mm:ss".length()) {
|
alarmTimeStr = alarmTimeStr.substring(0, "yyyy-MM-dd HH:mm:ss".length());
|
}
|
try {
|
alarmTime = sdf.parse(alarmTimeStr).getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if (alarmVideo != null && alarmVideo.startsWith("http:")) {
|
alarm.put("alarmVideoValid", "true");
|
} else {
|
alarm.put("alarmVideoValid", "false");
|
}
|
if (alarmPicture != null && alarmPicture.startsWith("http:")) {
|
alarm.put("alarmPictureValid", "true");
|
} else {
|
alarm.put("alarmPictureValid", "false");
|
}
|
if (alarmLargePicture != null && alarmLargePicture.startsWith("http:")) {
|
alarm.put("alarmLargePictureValid", "true");
|
} else {
|
alarm.put("alarmLargePictureValid", "false");
|
}
|
if (alarmTime < keepStartTime ) {
|
alarm.put("canClose", "true");
|
} else {
|
alarm.put("canClose", "false");
|
}
|
}
|
return sqliteAlarmList;
|
}
|
|
public static int saveRemoteAlarmListToSqlite(List<Map<String, String>> newRemoteAlarmList) {
|
int savedCount = 0;
|
for (Map<String, String> newRemoteAlarm : newRemoteAlarmList) {
|
if (findById(newRemoteAlarm.get("id")) == null) {
|
newRemoteAlarm.put("table", "alarm");
|
newRemoteAlarm.put("closeAlarm", "false");
|
newRemoteAlarm.put("mute", "false");
|
newRemoteAlarm.put("createTime", new Date().getTime()+"");
|
save(newRemoteAlarm);
|
savedCount++;
|
}
|
}
|
return savedCount;
|
}
|
|
public static void deleteAlarm(Map<String, String> alarm) {
|
try {
|
DatabaseManager.execSQL("delete from alarm where id='" + alarm.get("id") + "'");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|