a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.basic.security.manager;
 
import android.os.SystemClock;
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.fragment.GuestModeFragment;
import com.basic.security.model.Guest;
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Outdoor;
import com.basic.security.model.Person;
import com.basic.security.utils.FaceId;
 
import org.apache.commons.io.FileUtils;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
public class GuestManager extends BaseManager {
    public static boolean cameraOneEnable = false;
    public static boolean cameraTwoEnable = false;
    static long deleteMoreThanLastTime = 0;
    static String cameraTitle = "";
    static int guestListCameraType = 1;
    static boolean isEnable = false;
    static ModelAdapter outdoor = null;
    static int prevCameraCount = 2;
 
    public static void saveGuest(ModelAdapter guest) {
        save(guest);
        deleteMoreThan(guest, 50);
    }
 
    public static ModelAdapter findGuestListRecentOne() {
        return findOne("select * from guest  order by " + Guest.create_time + " desc limit 1");
    }
 
    public static List<ModelAdapter> findGuestList(String cameraId) {
        List<ModelAdapter> guestList = findList("select * from guest where 1=1 " +
//                " and " + Guest.camera_id + "='" + cameraId + "'"+
                " order by " + Guest.create_time + " desc limit 50");
        List<ModelAdapter> guestToRemoveList = new ArrayList<>();
        for (ModelAdapter guest : guestList) {
            byte[] cameraImageFeature = guest.getBlob(Guest.camera_image_feature);
            if (TextUtils.isEmpty(guest.getString(Guest.person_id))
                    && (cameraImageFeature == null || cameraImageFeature.length < 10)) {
                guestToRemoveList.add(guest);
            }
        }
        for (ModelAdapter guestToRemove : guestToRemoveList) {
            deleteGuest(guestToRemove);
            guestList.remove(guestToRemove);
        }
        return guestList;
    }
 
    public static void deleteMoreThan(ModelAdapter guest, int rows) {
        if (System.currentTimeMillis() - deleteMoreThanLastTime > 10000) {
            String sql = "select * from guest where " +
                    Guest.camera_id + "='" + guest.getString(Guest.camera_id) + "'" +
                    " order by create_time desc limit " + rows + ",1000000 ";
            List<ModelAdapter> modelAdapterList = findList(sql);
            for (ModelAdapter modelAdapter : modelAdapterList) {
                deleteGuestAttachment(modelAdapter);
            }
            deleteMoreThanLastTime = System.currentTimeMillis();
        }
    }
 
    public static void deleteGuestAttachment(ModelAdapter modelAdapter) {
        try {
            File camera_image_pathFile = new File(ModelAdapter.getAttachmentPath(modelAdapter.getId(), Person.camera_image_path, Guest.tableName));
            if (camera_image_pathFile.exists()) {
                FileUtils.forceDelete(camera_image_pathFile);
            }
        } catch (Exception e) {
            System.out.println("GuestManager.deleteGuestAttachment " + e.getMessage());
        }
        deletePhysically(modelAdapter);
    }
 
    public static void deleteGuest(ModelAdapter checkedGuest) {
        deleteGuestAttachment(checkedGuest);
    }
 
    public static void deleteSavedGuest(byte[] cameraImageFeature) {
        if (cameraImageFeature != null && cameraImageFeature.length > 0) {
            BaseApplication.getApplication().executorService.execute(() -> {
                try {
                    int deleteCount = 1;
                    List<ModelAdapter> guestList = findList("select * from guest");
                    for (ModelAdapter guest : guestList) {
                        byte[] dbFeature = guest.getBlob(Guest.camera_image_feature);
                        float score = FaceId.instance.compareFeature(cameraImageFeature, dbFeature);
                        if (score >= 70 || dbFeature == null || dbFeature.length < 10) {
                            deleteGuest(guest);
                            deleteCount++;
                        }
                    }
                    if (deleteCount > 0) {
                        BaseApplication.getApplication().activity.fragment_guest_mode.reloadCameraList();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }
 
    public static void enableCamera() {
        enableCamera(guestListCameraType, isEnable, outdoor);
    }
 
    public static void enableCamera(int guestListCameraType, boolean isEnable, ModelAdapter outdoor) {
        GuestManager.guestListCameraType = guestListCameraType;
        GuestManager.isEnable = isEnable;
        GuestManager.outdoor = outdoor;
        try {
            GuestModeFragment guestModeFragment = BaseApplication.getApplication().activity.fragment_guest_mode;
            cameraTitle = "";
            if (outdoor != null) {
                cameraTitle = outdoor.getString(Outdoor.outdoor_device_name);
            }
            if (guestListCameraType == 1) {
//                if (guestModeFragment.camera1Title != null) {
//                    GuestManager.cameraOneEnable = isEnable;
//                    BaseApplication.getApplication().activity.runOnUiThread(() -> {
//
//                        if (isEnable) {
//                            if (!TextUtils.isEmpty(cameraTitle)) {
//                                guestModeFragment.camera1Title.setVisibility(View.VISIBLE);
//                                guestModeFragment.camera1Title.setText(cameraTitle);
//                            } else {
//                                guestModeFragment.camera1Title.setVisibility(View.GONE);
//                            }
//                        } else {
//                            guestModeFragment.camera1Title.setVisibility(View.GONE);
//                        }
//                    });
//
//                }
                guestModeFragment.reloadCameraOneList();
            }
            if (guestListCameraType == 2) {
//                if (guestModeFragment.camera2Title != null) {
//                    GuestManager.cameraTwoEnable = isEnable;
//                    BaseApplication.getApplication().activity.runOnUiThread(() -> {
//                        if (isEnable) {
//                            if (!TextUtils.isEmpty(cameraTitle)) {
//                                guestModeFragment.camera2Title.setVisibility(View.VISIBLE);
//                                guestModeFragment.camera2Title.setText(cameraTitle);
//                            } else {
//                                guestModeFragment.camera2Title.setVisibility(View.GONE);
//                            }
//                        } else {
//                            guestModeFragment.camera2Title.setVisibility(View.GONE);
//                        }
//                    });
//                    guestModeFragment.reloadCameraTwoList();
//                }
                guestModeFragment.reloadCameraTwoList();
            }
        } catch (Exception e) {
            System.out.println("GuestManager.enableCamera " + e.getMessage());
        }
    }
 
    public static void removePersonId(String personId) {
        if (!TextUtils.isEmpty(personId)) {
            ModelAdapter person = PersonManager.findPersonById(personId);
            if (person != null) {
                String idCardNumber = person.getString(Person.id_card_number);
                if (!TextUtils.isEmpty(idCardNumber)) {
                    DatabaseManager.getDatabase().execSQL("delete from " + Guest.tableName + " where " + Guest.id_card_number + "='" + idCardNumber + "'");
                }
            }
            DatabaseManager.getDatabase().execSQL("delete from " + Guest.tableName + " where " + Guest.person_id + "='" + personId + "'");
            BaseApplication.getApplication().activity.fragment_guest_mode.reloadCameraList();
            SystemClock.sleep(1 * 1000);
            BaseApplication.getApplication().activity.fragment_guest_mode.initGuest(null);
        }
    }
 
    public static void updateGuest(ModelAdapter checkedGuest, ModelAdapter newPersonAsGuest) {
        try {
            checkedGuest.setString(Guest.person_id, newPersonAsGuest.getString(Person.id));
            checkedGuest.setString(Guest.name, newPersonAsGuest.getString(Person.name));
            save(checkedGuest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static boolean twoCameras() {
        return false;
    }
}