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