a
554325746@qq.com
2019-07-15 e6a8bdd23d6f5cb46ac650285829573280edcdf6
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
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();
        }
    }
}