feat(api): 新增V2版本接口支持并集成至网络请求客户端
- 新增 ApiServiceV2 接口,支持人脸缓存、增量数据、设备配置等接口 - ApiClient 中新增 apiServiceV2 对象,复用 Retrofit 实例,调整超时时间至60秒 - DeviceInitActivity 改用 netViewModelV2 进行人脸缓存全量拉取及设备配置读取 - BaseActivity 新增对人脸增量同步接口调用,调用成功后刷新本地识别缓存 - BaseActivity 新增清空本地人脸库功能,异步清理数据库并刷新识别缓存 - FaceApi 增加清空本地人脸库接口,重写数据库操作逻辑 - 升级 lib_face 模块 Room 数据库版本,新增字段以支持多标识人脸实体扩展 - FaceEntity 实体扩展会员编号、用户ID、人脸ID、会员标识、更新时间等字段 - FaceDao 新增按人脸ID查询和删除接口,支持多重人脸数据操作 - 优化网络层 OkHttpClient 配置,简化拦截器写法及日志级别判断 - 升级 lib_face 模块支持 arm64-v8a 架构,增强兼容性 - 规范模块间依赖关系,统一版本管理及包路径声明 - 完善 CLAUDE.md 文档,补充项目架构、模块划分与开发流程说明
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"version": 3,
|
||||
"artifactType": {
|
||||
"type": "APK",
|
||||
"kind": "Directory"
|
||||
},
|
||||
"applicationId": "com.sw.take.plate",
|
||||
"variantName": "release",
|
||||
"elements": [
|
||||
{
|
||||
"type": "SINGLE",
|
||||
"filters": [],
|
||||
"attributes": [],
|
||||
"versionCode": 1,
|
||||
"versionName": "1.1",
|
||||
"outputFile": "zncpg_1.1-release.apk"
|
||||
}
|
||||
],
|
||||
"elementType": "File",
|
||||
"baselineProfiles": [
|
||||
{
|
||||
"minApi": 28,
|
||||
"maxApi": 30,
|
||||
"baselineProfiles": [
|
||||
"baselineProfiles/1/zncpg_1.1-release.dm"
|
||||
]
|
||||
},
|
||||
{
|
||||
"minApi": 31,
|
||||
"maxApi": 2147483647,
|
||||
"baselineProfiles": [
|
||||
"baselineProfiles/0/zncpg_1.1-release.dm"
|
||||
]
|
||||
}
|
||||
],
|
||||
"minSdkVersionForDexing": 24
|
||||
}
|
||||
@@ -28,13 +28,19 @@ object GlobalData {
|
||||
|
||||
var appId = "Hkz1rBk6PZXbS8KwKr67K2eZtsz8bRoMHLg64bUdgCZj"
|
||||
var sdkKey = "AabXs3sHM8UhhE7oCGf4LVMLrdkGb1nJNksbjjTdVt7k"
|
||||
var activeKey = "085F-118G-Q3Y3-WXXX"//"085F-118G-Q3J6-35UX" //"085F-118G-Q391-53YL"
|
||||
var activeKey = "85Q1-1216-X3DH-QYTJ"//"085F-118G-Q3J6-35UX" //"085F-118G-Q391-53YL"
|
||||
|
||||
/**
|
||||
* 具体业务baseurl
|
||||
*/
|
||||
// var appBaseUrl: String = "https://dev.yixiong-tech.com:8081"
|
||||
var appBaseUrl: String = "https://dev.yixiong-tech.com:8081"
|
||||
var appBaseUrl: String = ""
|
||||
|
||||
/**
|
||||
* 具体业务 BaseUrl
|
||||
*/
|
||||
const val LOCAL_BASE_URL: String = "http://192.168.10.101:24801"
|
||||
const val TEST_BASE_URL: String = "https://dev.yixiong-tech.com:8081"
|
||||
const val PROD_BASE_URL: String = "https://platform-api.uat.shuziweidao.com"
|
||||
|
||||
/**
|
||||
* 设备id
|
||||
@@ -62,4 +68,5 @@ object GlobalKey {
|
||||
const val KEY_FIRST_RUN = "firstRun"
|
||||
const val KEY_USER_INFO = "userInfoKey"
|
||||
const val PARAM_EQUIPMENT_INFO = "equipmentUserInfo"
|
||||
const val KEY_BASE_URL = "baseUrlKey"
|
||||
}
|
||||
@@ -17,11 +17,9 @@ import androidx.core.view.WindowCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.sw.inbound.utils.DateTimeUtils
|
||||
import com.sw.plate.utils.Base64
|
||||
import com.sw.plate.utils.ScanGunKeyEventHelper
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import com.sw.plate.utils.arcface.facedb.FaceDatabase
|
||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
||||
import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel
|
||||
import com.sw.plate.utils.comn.SerialApi
|
||||
import com.sw.plate.utils.comn.SerialPortManager
|
||||
@@ -30,17 +28,18 @@ import com.sw.platecabinet.member.databinding.ItemTitleTimeBinding
|
||||
import com.sw.platecabinet.dialog.BalanceNotEnoughDialog
|
||||
import com.sw.platecabinet.ext.setClickListeners
|
||||
import com.sw.platecabinet.model.response.EquipmentUserInfo
|
||||
import com.sw.platecabinet.model.response.UserFaceModel
|
||||
import com.sw.platecabinet.utils.IntervalExecutor
|
||||
import com.sw.platecabinet.utils.PermissionHelper
|
||||
import com.sw.platecabinet.utils.SpTool
|
||||
import com.sw.platecabinet.view.CustomLoadingDialog
|
||||
import com.sw.platecabinet.viewmodel.NetViewModelV2
|
||||
import com.sw.platecabinet.viewmodel.SettingViewModel
|
||||
import com.sw.platecabinet.viewmodel.UserViewModel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
@@ -61,6 +60,9 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
||||
// 用户对应的viewModel
|
||||
protected val viewModel by viewModels<UserViewModel>()
|
||||
|
||||
// V2 网络请求 viewModel(人脸缓存/增量同步/设备配置)
|
||||
val netViewModelV2 by viewModels<NetViewModelV2>()
|
||||
|
||||
private var startTime: Long = 0
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -84,8 +86,6 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
||||
registerKeyEvent()
|
||||
val durationTime = System.currentTimeMillis() - startTime
|
||||
Timber.d("启动时间:$durationTime")
|
||||
|
||||
startFaceTask()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -396,6 +396,9 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
||||
// private val dealyMillis = 10 * 60 * 1000L
|
||||
private val initialDelay = 5 * 60 * 1000L
|
||||
private val dealyMillis = 5 * 60 * 1000L
|
||||
|
||||
val recognizeViewModel by viewModels<RecognizeViewModel>()
|
||||
|
||||
fun startFaceTask() {
|
||||
faceTaskJob =
|
||||
intervalExecutor.startIntervalTaskWithInitialDelay(initialDelay, dealyMillis) {
|
||||
@@ -403,70 +406,33 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private var faceTimestamp = 0L
|
||||
|
||||
/**
|
||||
* 人脸增量同步(V2,同步逻辑在 NetViewModelV2 内部处理)
|
||||
*/
|
||||
private fun getFaceIncrementList(pageNo: Int = 1) {
|
||||
val timestamp = SpTool.getLastFaceTimestamp()
|
||||
if (timestamp == 0L) {
|
||||
return
|
||||
}
|
||||
settingViewModel.getFaceIncrementList(
|
||||
netViewModelV2.getFaceIncrementList(
|
||||
pageNo = pageNo,
|
||||
timestamp = timestamp
|
||||
) { list ->
|
||||
runOnUiThread {
|
||||
if (pageNo == 1 && list.isEmpty()) {
|
||||
//未查询到增量数据
|
||||
return@runOnUiThread
|
||||
}
|
||||
updateFaceData(list)
|
||||
// if (list.size >= settingViewModel.PAGE_SIZE) {
|
||||
// faceTimestamp = list.last().faceUpdateTimestamp?:0
|
||||
// taskPageNo++
|
||||
// getFaceIncrementList()
|
||||
// return@runOnUiThread
|
||||
// }
|
||||
if (list.isNotEmpty()) {
|
||||
faceTimestamp = list.last().faceUpdateTimestamp?:0
|
||||
}
|
||||
SpTool.setLastFaceTimestamp(faceTimestamp)
|
||||
}
|
||||
) {
|
||||
recognizeViewModel.refreshFaceList()
|
||||
}
|
||||
}
|
||||
|
||||
private val recognizeViewModel by viewModels<RecognizeViewModel>()
|
||||
private fun updateFaceData(list: List<UserFaceModel>) {
|
||||
Thread {
|
||||
val faceList = mutableListOf<FaceEntity>()
|
||||
try {
|
||||
list.forEach { model ->
|
||||
if (model.faceDeleted == true) {
|
||||
//删除数据
|
||||
FaceDatabase.getInstance(this).faceDao().deleteFaceById(model.userId)
|
||||
} else {
|
||||
//保存数据
|
||||
val faceEntity = FaceEntity(
|
||||
model.userId,
|
||||
null,
|
||||
Base64.decode(model.faceFeatureStr)
|
||||
).also {
|
||||
it.userType = "1"
|
||||
}
|
||||
faceList.add(faceEntity)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
try {
|
||||
if (faceList.isNotEmpty()) {
|
||||
FaceDatabase.getInstance(this).faceDao().insert(faceList)
|
||||
}
|
||||
recognizeViewModel.refreshFaceList()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}.start()
|
||||
/**
|
||||
* 清空本地人脸库
|
||||
*/
|
||||
fun clearAllFace(block: () -> Unit) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val faceDao = FaceDatabase.getInstance(this@BaseActivity).faceDao()
|
||||
faceDao.deleteAll()
|
||||
faceDao.resetId()
|
||||
recognizeViewModel.refreshFaceList()
|
||||
withContext(Dispatchers.Main) { block() }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,11 @@
|
||||
package com.sw.platecabinet.activity
|
||||
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.activity.viewModels
|
||||
import com.sw.plate.utils.AppUtil
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import com.sw.platecabinet.GlobalData
|
||||
import com.sw.platecabinet.member.databinding.ActivityDeviceInitBinding
|
||||
import com.sw.platecabinet.member.databinding.ItemTitleTimeBinding
|
||||
import com.sw.platecabinet.ext.gone
|
||||
import com.sw.platecabinet.ext.invisible
|
||||
import com.sw.platecabinet.viewmodel.SettingViewModel
|
||||
import com.sw.platecabinet.utils.SpTool
|
||||
|
||||
/**
|
||||
* 设备初始化界面
|
||||
@@ -26,48 +21,45 @@ class DeviceInitActivity : BaseActivity<ActivityDeviceInitBinding>() {
|
||||
}
|
||||
|
||||
override fun initialize() {
|
||||
// val isSuccess = deviceViewModel.checkEquipmentInfo()
|
||||
// if (isSuccess) {
|
||||
// goLoginActivity()
|
||||
// return
|
||||
// }
|
||||
// binding.ivQrCode.setImageBitmap(
|
||||
// QRCodeUtil.generateQRCode(
|
||||
// content = GlobalData.deviceId,
|
||||
// size = 200
|
||||
// )
|
||||
// )
|
||||
// binding.btnInit.setOnClickListener {
|
||||
// deviceViewModel.getDeviceToken()
|
||||
// }
|
||||
// binding.btnInit.gone()
|
||||
// binding.ivQrCode.invisible()
|
||||
// binding.root.postDelayed({
|
||||
// goLoginActivity()
|
||||
// }, 1000)
|
||||
// showWaitingDialog("加载中……")
|
||||
// viewModel.getDeviceConfig { deviceConfig ->
|
||||
// runOnUiThread {
|
||||
// if (deviceConfig == null) {
|
||||
// hideWaitingDialog()
|
||||
// ToastUtils.showToast("获取设备配置数据失败")
|
||||
// hideWaitingDialog()
|
||||
// goLoginActivity()
|
||||
// return@runOnUiThread
|
||||
// }
|
||||
// GlobalData.appId = deviceConfig.arcsoftAppId ?: ""
|
||||
// GlobalData.sdkKey = deviceConfig.arcsoftSdkKey ?: ""
|
||||
// GlobalData.activeKey = deviceConfig.arcsoftActiveKey ?: ""
|
||||
// binding.root.postDelayed({
|
||||
// hideWaitingDialog()
|
||||
// goLoginActivity()
|
||||
// }, 500)
|
||||
// }
|
||||
// }
|
||||
showWaitingDialog("加载中……")
|
||||
// 首次启动先全量拉取人脸缓存,后续走增量同步
|
||||
if (SpTool.getFirstGetFace()) {
|
||||
netViewModelV2.getUserFaceCache { status, msg ->
|
||||
SpTool.setFirstGetFace(false)
|
||||
getDeviceConfig()
|
||||
}
|
||||
} else {
|
||||
getDeviceConfig()
|
||||
}
|
||||
}
|
||||
|
||||
binding.root.postDelayed({
|
||||
goLoginActivity()
|
||||
}, 500)
|
||||
/**
|
||||
* 获取设备配置(V2),并初始化虹软 SDK 激活参数
|
||||
*/
|
||||
private fun getDeviceConfig() {
|
||||
netViewModelV2.getDeviceConfig(onSuccess = { deviceConfig ->
|
||||
runOnUiThread {
|
||||
if (deviceConfig == null) {
|
||||
hideWaitingDialog()
|
||||
ToastUtils.showToast("获取设备配置数据失败")
|
||||
goLoginActivity()
|
||||
return@runOnUiThread
|
||||
}
|
||||
GlobalData.appId = deviceConfig.arcsoftAppId ?: ""
|
||||
GlobalData.sdkKey = deviceConfig.arcsoftSdkKey ?: ""
|
||||
GlobalData.activeKey = deviceConfig.arcsoftActiveKey ?: ""
|
||||
binding.root.postDelayed({
|
||||
hideWaitingDialog()
|
||||
goLoginActivity()
|
||||
}, 500)
|
||||
}
|
||||
}, onFailure = { errMsg ->
|
||||
runOnUiThread {
|
||||
hideWaitingDialog()
|
||||
ToastUtils.showToast(errMsg ?: "接口异常")
|
||||
goLoginActivity()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun registerDataChange() {
|
||||
|
||||
@@ -24,11 +24,13 @@ import com.sw.plate.utils.arcface.PreviewConfig
|
||||
import com.sw.plate.utils.arcface.camera.CameraListener
|
||||
import com.sw.plate.utils.arcface.camera.DualCameraHelper
|
||||
import com.sw.plate.utils.arcface.face.constants.LivenessType
|
||||
import com.sw.plate.utils.arcface.face.model.CompareResult
|
||||
import com.sw.plate.utils.arcface.face.model.FacePreviewInfo
|
||||
import com.sw.plate.utils.arcface.face.model.RecognizeConfiguration
|
||||
import com.sw.plate.utils.arcface.faceserver.FaceServer
|
||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
||||
import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel
|
||||
import com.sw.platecabinet.utils.Debouncer
|
||||
import com.sw.platecabinet.member.R
|
||||
import com.sw.platecabinet.member.databinding.ActivityLoginFaceBinding
|
||||
import com.sw.platecabinet.member.databinding.ItemTitleTimeBinding
|
||||
@@ -45,7 +47,6 @@ import timber.log.Timber
|
||||
*/
|
||||
class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
ViewTreeObserver.OnGlobalLayoutListener {
|
||||
private val recognizeViewModel by viewModels<RecognizeViewModel>()
|
||||
private var countDownTimer: CountDownTimer? = null
|
||||
|
||||
private val CAMERA_PERMISSION_REQUEST_CODE = 100
|
||||
@@ -73,16 +74,17 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
|
||||
override fun initialize() {
|
||||
instance = this
|
||||
viewModel.activeEngine()
|
||||
// V2 引擎激活(appId/sdkKey/activeKey 来自 V2 设备配置)
|
||||
netViewModelV2.activeEngine()
|
||||
|
||||
initCountTime()
|
||||
initArcViewModel()
|
||||
initArcView()
|
||||
openRectInfoDraw = true
|
||||
recognizeViewModel.setDrawRectInfoTextValue(true)
|
||||
//viewModel.generateToken()
|
||||
// viewModel.activeEngine()
|
||||
viewModel.getUserFaceCache()
|
||||
|
||||
//开启人脸增量数据定时任务(V2)
|
||||
startFaceTask()
|
||||
|
||||
binding.llToPwd.setOnClickListener {
|
||||
val intent = Intent(this, LoginByPwdActivity::class.java)
|
||||
@@ -171,9 +173,12 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
Timber.i("recognizeNotice observe notice = $notice")
|
||||
})
|
||||
|
||||
recognizeViewModel.recognizeUserId.observe(this, Observer { userId: String? ->
|
||||
Timber.i("recognizeUserId observe userId = $userId")
|
||||
viewModel.getUserInfoById(memberId = userId)
|
||||
recognizeViewModel.recognizeUserId.observe(this, Observer { result: CompareResult? ->
|
||||
Timber.i(
|
||||
"recognizeUserId observe userId=${result?.faceEntity?.userName}," +
|
||||
"similar=${result?.similar},userType=${result?.faceEntity?.userType}"
|
||||
)
|
||||
loadFaceRecognizeResult(result)
|
||||
})
|
||||
|
||||
recognizeViewModel.drawRectInfoText.observe(this, Observer { info ->
|
||||
@@ -187,6 +192,47 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
recognizeViewModel.getCompareResultList().getValue()
|
||||
}
|
||||
|
||||
/**
|
||||
* 识别阈值,默认值为0.8
|
||||
*/
|
||||
private val faceSuccessThreshold by lazy { ConfigUtil.getRecognizeThreshold(this) }
|
||||
|
||||
/**
|
||||
* 识别成功后开柜(查询用户信息)防抖,防止重复触发
|
||||
*/
|
||||
private val loginDebouncer = Debouncer(3000)
|
||||
|
||||
/**
|
||||
* 处理识别结果:达到阈值走登录流程,未达阈值仅记录(本版本无采集模式)
|
||||
*/
|
||||
private fun loadFaceRecognizeResult(result: CompareResult?) {
|
||||
val similar = result?.similar ?: return
|
||||
|
||||
Timber.i(
|
||||
"loadFaceRecognizeResult 阈值=$faceSuccessThreshold, similar=$similar"
|
||||
)
|
||||
if (similar >= faceSuccessThreshold) {
|
||||
val userId = result.faceEntity?.userName
|
||||
recognizeSuccess(userId)
|
||||
return
|
||||
}
|
||||
// 未达阈值:暂不处理(无采集模式),等待下一次识别
|
||||
}
|
||||
|
||||
/**
|
||||
* 识别成功:防抖后查询用户信息,走开柜门业务流程
|
||||
*/
|
||||
private fun recognizeSuccess(userId: String?) {
|
||||
if (userId.isNullOrBlank()) {
|
||||
Timber.i("recognizeSuccess 识别成功但 userId 为空,忽略")
|
||||
return
|
||||
}
|
||||
Timber.i("recognizeSuccess userId=$userId")
|
||||
loginDebouncer.debounce {
|
||||
viewModel.getUserInfoById(memberId = userId)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Timber.d("onDestroy")
|
||||
instance = null
|
||||
@@ -392,14 +438,24 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
}
|
||||
|
||||
private fun resumeCamera() {
|
||||
isRecognition = true
|
||||
if (rgbCameraHelper != null && rgbCameraHelper!!.isStopped) {
|
||||
rgbCameraHelper!!.start()
|
||||
val helper = rgbCameraHelper
|
||||
if (helper != null && helper.isStopped) {
|
||||
isRecognition = true
|
||||
helper.start()
|
||||
} else {
|
||||
// 相机未停止时,发送黑帧清除 ViewModel 内部识别缓存,防止恢复后使用旧数据
|
||||
recognizeViewModel.onPreviewFrame(emptyFrame, true)
|
||||
binding.dualCameraFaceRectView.clearFaceInfo()
|
||||
binding.root.postDelayed({
|
||||
isRecognition = true
|
||||
}, 500)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// 清空上次识别结果和 FaceHelper 内部状态,防止短时间内再次识别无法触发
|
||||
recognizeViewModel.resetFaceState()
|
||||
resumeCamera()
|
||||
viewModel.resetUserInfo()
|
||||
countDownTimer?.let {
|
||||
@@ -416,10 +472,26 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
||||
|
||||
private fun pauseCamera() {
|
||||
isRecognition = false
|
||||
|
||||
recognizeViewModel.onPreviewFrame(ByteArray(1382400), true)
|
||||
recognizeViewModel.onPreviewFrame(emptyFrame, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造符合分辨率的 NV21 格式全黑帧
|
||||
* @param width 图像宽度
|
||||
* @param height 图像高度
|
||||
* @return 合法的全黑帧字节数组
|
||||
*/
|
||||
private fun createBlackNV21Frame(width: Int, height: Int): ByteArray {
|
||||
val frameSize = width * height
|
||||
val nv21Data = ByteArray(frameSize * 3 / 2) // NV21 格式长度 = 宽×高×1.5
|
||||
// Y 分量(亮度)设为 0(全黑),UV 分量设为 128(默认值)
|
||||
nv21Data.fill(0, 0, frameSize)
|
||||
nv21Data.fill(128.toByte(), frameSize, nv21Data.size)
|
||||
return nv21Data
|
||||
}
|
||||
|
||||
private val emptyFrame = createBlackNV21Frame(1280, 720)
|
||||
|
||||
override fun handleLoginSuccess(equipmentUserInfo: EquipmentUserInfo, isAdmin: Boolean) {
|
||||
super.handleLoginSuccess(equipmentUserInfo, isAdmin)
|
||||
// goInitActivity()
|
||||
|
||||
@@ -21,6 +21,28 @@ data class DeviceConfig(
|
||||
var arcsoftActiveKey: String? = null
|
||||
)
|
||||
|
||||
/**
|
||||
* V2 设备配置(人脸 5.0 体系)
|
||||
*/
|
||||
data class DeviceConfigV2(
|
||||
/** 业务服务器 BASE URL */
|
||||
val appPackageUrl: String? = null,
|
||||
/** 食堂/餐厅名称 */
|
||||
val canteenName: String? = null,
|
||||
/** 食堂/餐厅 ID(后续所有业务 API 的 restId) */
|
||||
val canteenId: String? = null,
|
||||
/** 虹软人脸 SDK App ID */
|
||||
val arcsoftAppId: String? = null,
|
||||
/** 虹软人脸 SDK Key */
|
||||
val arcsoftSdkKey: String? = null,
|
||||
/** 虹软人脸 SDK Active Key */
|
||||
val arcsoftActiveKey: String? = null,
|
||||
/** MQTT 客户端服务器 IP */
|
||||
val clientServerIp: String? = null,
|
||||
/** 智慧食堂服务器 IP(MQTT 端口) */
|
||||
val zhstServerIp: String? = null
|
||||
)
|
||||
|
||||
data class CodeMsg(
|
||||
val code: String? = "",
|
||||
val msg: String? = ""
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sw.platecabinet.model.request
|
||||
|
||||
data class FeatureBody(
|
||||
val url:String,
|
||||
val featureChar:String
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sw.platecabinet.model.request
|
||||
|
||||
/**
|
||||
* 通用 ID 请求体
|
||||
*
|
||||
* 用于按用户 id(餐盘号)查询或上报类的接口,例如:
|
||||
* - P-09a 取餐盘(/neglect/pickup/plate-pickup)
|
||||
*
|
||||
* @property id 用户 id(餐盘号 = userId)
|
||||
*/
|
||||
data class IdDTO(
|
||||
val id: Long
|
||||
)
|
||||
@@ -2,7 +2,6 @@ package com.sw.platecabinet.model.response
|
||||
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
@@ -10,21 +9,20 @@ import kotlinx.parcelize.Parcelize
|
||||
*/
|
||||
@Parcelize
|
||||
data class UserFaceModel(
|
||||
// @SerializedName("faceFeature")
|
||||
// val faceFeature: String? = "",
|
||||
// @SerializedName("faceFeatureString")
|
||||
// val faceFeatureString: String? = "",
|
||||
// @SerializedName("faceType")
|
||||
// val faceType: String? = "",
|
||||
// @SerializedName("userFaceId")
|
||||
// val userFaceId: String? = "",
|
||||
// @SerializedName("userId")
|
||||
// val userId: String? = "",
|
||||
// @SerializedName("updateTimeStamp")
|
||||
// val updateTimeStamp: Long = 0,
|
||||
|
||||
val userId: String? = "",
|
||||
val faceFeatureStr: String? = "",
|
||||
val faceUpdateTimestamp:Long?=null,
|
||||
val faceDeleted: Boolean?=false
|
||||
val faceUpdateTimestamp: Long? = null,
|
||||
/**
|
||||
* 人脸删除标识
|
||||
*/
|
||||
val faceDeleted: Boolean? = false,
|
||||
/**
|
||||
* 会员编号
|
||||
*/
|
||||
val cardNo: String? = null,
|
||||
|
||||
/**
|
||||
* 是否会员
|
||||
*/
|
||||
val member: Boolean? = null
|
||||
) : Parcelable
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.sw.platecabinet.model.response
|
||||
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
* 用户人脸信息
|
||||
*/
|
||||
@Parcelize
|
||||
data class UserFaceModelV2(
|
||||
val userFaceId: String? = null,
|
||||
val userId: String? = null,
|
||||
val faceFeature: String? = null,
|
||||
val faceFeatureStr: String? = null,
|
||||
val faceFeatureString: String? = null,
|
||||
val faceUpdateTimestamp: Long? = null,
|
||||
val cardNo: String? = null,
|
||||
val member: Boolean? = false,
|
||||
val faceDeleted: Boolean? = false,
|
||||
val personType: String? = null
|
||||
) : Parcelable {
|
||||
fun resolveFeatureStr(): String? {
|
||||
return faceFeature?.takeIf { it.isNotEmpty() }
|
||||
?: faceFeatureStr?.takeIf { it.isNotEmpty() }
|
||||
?: faceFeatureString?.takeIf { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
fun toFaceEntity(): FaceEntity {
|
||||
val featureBase64 = resolveFeatureStr()
|
||||
return FaceEntity(
|
||||
userId,
|
||||
android.util.Base64.decode(featureBase64, android.util.Base64.DEFAULT),
|
||||
personType,
|
||||
cardNo,
|
||||
userId,
|
||||
userFaceId,
|
||||
member ?: false,
|
||||
faceUpdateTimestamp ?: 0L
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.sw.platecabinet.network
|
||||
import com.sw.platecabinet.GlobalData
|
||||
import com.sw.platecabinet.MyApp
|
||||
import com.sw.platecabinet.network.api.ApiService
|
||||
import com.sw.platecabinet.network.api.ApiServiceV2
|
||||
import com.sw.platecabinet.network.interceptor.RequestInterceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
@@ -12,36 +13,33 @@ import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object ApiClient {
|
||||
//private const val BASE_URL = "https://vip.shuziweidao.com"
|
||||
// private const val DEVICE_BASE_URL = "http://device.shuziweidao.com:8889/"
|
||||
private var DEVICE_BASE_URL = GlobalData.appBaseUrl
|
||||
// private const val BASE_URL = "http://192.168.1.8:9092"
|
||||
private const val TIME_OUT = 30L // 超时时间(秒)
|
||||
|
||||
val okHttpClient = OkHttpClient.Builder()
|
||||
private const val TIME_OUT = 60L // 超时时间(秒)
|
||||
|
||||
private val okHttpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
|
||||
.readTimeout(TIME_OUT, TimeUnit.SECONDS)
|
||||
.writeTimeout(TIME_OUT, TimeUnit.SECONDS)
|
||||
.addNetworkInterceptor(HttpLoggingInterceptor(logger = {
|
||||
Timber.d("okhttp logger ==>${it}")
|
||||
}).apply {
|
||||
level = if (MyApp.DEBUG) {
|
||||
HttpLoggingInterceptor.Level.BODY
|
||||
} else {
|
||||
HttpLoggingInterceptor.Level.NONE
|
||||
}
|
||||
.addNetworkInterceptor(HttpLoggingInterceptor { Timber.d("okhttp ==>${it}") }.apply {
|
||||
level = if (MyApp.DEBUG) HttpLoggingInterceptor.Level.BODY
|
||||
else HttpLoggingInterceptor.Level.NONE
|
||||
})
|
||||
.addInterceptor(RequestInterceptor())
|
||||
.build()
|
||||
|
||||
private val retrofit = Retrofit.Builder()
|
||||
.baseUrl(DEVICE_BASE_URL)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
// .addCallAdapterFactory(CoroutineCallAdapterFactory()) // 协程适配器
|
||||
.build()
|
||||
private val retrofit by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(GlobalData.appBaseUrl)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
}
|
||||
|
||||
val apiService: ApiService by lazy {
|
||||
retrofit.create(ApiService::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
val apiServiceV2: ApiServiceV2 by lazy {
|
||||
retrofit.create(ApiServiceV2::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.sw.platecabinet.network.api
|
||||
|
||||
import com.sw.platecabinet.GlobalData
|
||||
import com.sw.platecabinet.model.DeviceConfigV2
|
||||
import com.sw.platecabinet.model.request.FeatureBody
|
||||
import com.sw.platecabinet.model.request.IdDTO
|
||||
import com.sw.platecabinet.model.response.ApiResponse
|
||||
import com.sw.platecabinet.model.response.UserFaceModel
|
||||
import com.sw.platecabinet.model.response.UserFaceModelV2
|
||||
import okhttp3.MultipartBody
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.Field
|
||||
import retrofit2.http.FormUrlEncoded
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Multipart
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Part
|
||||
import retrofit2.http.Url
|
||||
|
||||
interface ApiServiceV2 {
|
||||
/**
|
||||
* 获取人脸缓存数据
|
||||
*/
|
||||
@POST
|
||||
suspend fun getUserFaceCache(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/common/face/page",
|
||||
@Body param: Map<String, Int>
|
||||
): ApiResponse<List<UserFaceModelV2>>
|
||||
|
||||
/**
|
||||
* 获取人脸增量数据
|
||||
*/
|
||||
@POST
|
||||
suspend fun getFaceIncrementList(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/common/face/increment",
|
||||
@Body param: Map<String, Long>
|
||||
): ApiResponse<List<UserFaceModelV2>?>
|
||||
|
||||
/**
|
||||
* 获取设备配置数据
|
||||
*/
|
||||
@GET
|
||||
suspend fun getDeviceConfig(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/pickup/device/config"
|
||||
): ApiResponse<DeviceConfigV2?>
|
||||
|
||||
/**
|
||||
* 添加人脸数据
|
||||
*/
|
||||
@POST
|
||||
suspend fun addUserFace(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/user/add-by-face",
|
||||
@Body faceData: FeatureBody
|
||||
): ApiResponse<UserFaceModelV2?>
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
@Multipart
|
||||
@POST
|
||||
suspend fun uploadImage(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/upload",
|
||||
@Part file: MultipartBody.Part,
|
||||
): ApiResponse<String?>
|
||||
|
||||
/**
|
||||
* 取餐盘(P-09a):记录用户开始就餐时刻
|
||||
* 调用一次即 upsert 到主单 plate_pickup_time,是 P-09 提交就餐记录的强前置。
|
||||
* @param param IdDTO,id 为用户 id(餐盘号 = userId)
|
||||
* @return ApiResponse<String?>,data 为主单 recordNo
|
||||
*/
|
||||
@POST
|
||||
suspend fun platePickup(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/pickup/plate-pickup",
|
||||
@Body param: IdDTO
|
||||
): ApiResponse<String?>
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.sw.platecabinet.repository
|
||||
|
||||
import com.sw.platecabinet.model.DeviceConfigV2
|
||||
import com.sw.platecabinet.model.request.FeatureBody
|
||||
import com.sw.platecabinet.model.request.IdDTO
|
||||
import com.sw.platecabinet.model.response.ApiResponse
|
||||
import com.sw.platecabinet.model.response.UserFaceModel
|
||||
import com.sw.platecabinet.model.response.UserFaceModelV2
|
||||
import com.sw.platecabinet.network.api.ApiServiceV2
|
||||
import com.sw.platecabinet.utils.FileUtils
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* 远程数据处理
|
||||
*/
|
||||
class RemoteRepositoryV2 constructor(
|
||||
private val apiService: ApiServiceV2
|
||||
) : BaseRepository() {
|
||||
|
||||
/**
|
||||
* 获取人脸数据
|
||||
*/
|
||||
suspend fun getUserFaceCache(
|
||||
pageNum: Int,
|
||||
pageSize: Int = 100,
|
||||
): ApiResponse<List<UserFaceModelV2>> {
|
||||
return safeApiCall {
|
||||
apiService.getUserFaceCache(
|
||||
param = mapOf(
|
||||
"pageNum" to pageNum, "pageSize" to pageSize
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun getFaceIncrementList(
|
||||
pageNum: Long, pageSize: Long = 100L, timestamp: Long
|
||||
): ApiResponse<List<UserFaceModelV2>?> {
|
||||
return safeApiCall {
|
||||
apiService.getFaceIncrementList(
|
||||
param = mapOf(
|
||||
"pageNum" to pageNum, "pageSize" to pageSize, "timestamp" to timestamp
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getDeviceConfig(): ApiResponse<DeviceConfigV2?> {
|
||||
return safeApiCall { apiService.getDeviceConfig() }
|
||||
}
|
||||
|
||||
suspend fun addUserFace(faceData: String, imageUrl: String): ApiResponse<UserFaceModelV2?> {
|
||||
return safeApiCall {
|
||||
apiService.addUserFace(
|
||||
faceData = FeatureBody(
|
||||
url = imageUrl, featureChar = faceData
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun uploadImage(file: File): ApiResponse<String?> {
|
||||
return safeApiCall {
|
||||
val part = FileUtils.genRequestPart(file)
|
||||
if (part == null) {
|
||||
ApiResponse(code = "-1", msg = "解析图片失败")
|
||||
} else {
|
||||
apiService.uploadImage(file = part)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取餐盘(P-09a):上报用户开始就餐时刻
|
||||
* @param userId 用户 id(餐盘号 = userId)
|
||||
* @return ApiResponse<String?>,data 为主单 recordNo
|
||||
*/
|
||||
suspend fun platePickup(userId: Long): ApiResponse<String?> {
|
||||
return safeApiCall { apiService.platePickup(param = IdDTO(id = userId)) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package com.sw.platecabinet.utils
|
||||
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.MediaStore
|
||||
import androidx.annotation.RequiresApi
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import kotlin.io.copyTo
|
||||
import kotlin.io.outputStream
|
||||
import kotlin.io.use
|
||||
import kotlin.text.equals
|
||||
|
||||
object FileUtils {
|
||||
/**
|
||||
* 从Uri获取File
|
||||
* example: file:///data/user/0/com.sw.inbound/cache/IMG_17515262353556856678814444882273.jpg
|
||||
*/
|
||||
private fun getFileFromUri(context: Context, uri: Uri): File? {
|
||||
Timber.d("getFileFromUri uri = ${uri.scheme}")
|
||||
return when (uri.scheme) {
|
||||
"file" -> File(uri.path ?: return null)
|
||||
"content" -> {
|
||||
try {
|
||||
val inputStream = context.contentResolver.openInputStream(uri) ?: return null
|
||||
val cacheDir = context.cacheDir
|
||||
val file = File.createTempFile(
|
||||
"upload_${System.currentTimeMillis()}",
|
||||
".jpg",
|
||||
cacheDir
|
||||
)
|
||||
file.outputStream().use { output ->
|
||||
inputStream.copyTo(output)
|
||||
}
|
||||
file
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过uri生成http请求体
|
||||
*/
|
||||
fun genRequestPart(context: Context, imageUri: Uri): MultipartBody.Part? {
|
||||
Timber.d("genRequestPart imageUri = $imageUri")
|
||||
// 1. 从Uri获取文件
|
||||
val file = getFileFromUri(context, imageUri)
|
||||
return genRequestPart(file)
|
||||
}
|
||||
fun genRequestPart(file: File?): MultipartBody.Part? {
|
||||
if (file == null) {
|
||||
Timber.e("getFileFromUri file is null")
|
||||
return null
|
||||
}
|
||||
// 2. 创建请求体
|
||||
val requestFile = file
|
||||
.asRequestBody("application/octet-stream".toMediaTypeOrNull())
|
||||
val imagePart = MultipartBody.Part.createFormData(
|
||||
"file",
|
||||
file.name,
|
||||
requestFile
|
||||
)
|
||||
return imagePart
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Uri删除文件
|
||||
* @param context 上下文
|
||||
* @param uri 文件Uri
|
||||
* @return Boolean 是否删除成功
|
||||
*/
|
||||
fun deleteFileWithUri(context: Context, uri: Uri): Boolean {
|
||||
Timber.d("deleteFileWithUri uri = ${uri.scheme}")
|
||||
return when {
|
||||
// 1. 处理 content:// 类型的Uri (MediaStore)
|
||||
uri.scheme.equals("content", ignoreCase = true) -> {
|
||||
deleteContentUriFile(context, uri)
|
||||
}
|
||||
// 2. 处理 file:// 类型的Uri
|
||||
uri.scheme.equals("file", ignoreCase = true) -> {
|
||||
deleteFileUriFile(uri)
|
||||
}
|
||||
// 3. 其他情况尝试直接解析路径
|
||||
else -> {
|
||||
deleteFileFromPath(uri.path ?: return false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除Content Uri文件
|
||||
private fun deleteContentUriFile(context: Context, uri: Uri): Boolean {
|
||||
Timber.d("deleteContentUriFile uri = ${uri.scheme}")
|
||||
return try {
|
||||
context.contentResolver.delete(uri, null, null) > 0
|
||||
} catch (e: SecurityException) {
|
||||
// Android 10+需要特殊处理
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
deleteMediaStoreFile(context, uri)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// Android 10+删除MediaStore文件
|
||||
@RequiresApi(Build.VERSION_CODES.Q)
|
||||
private fun deleteMediaStoreFile(context: Context, uri: Uri): Boolean {
|
||||
Timber.d("deleteMediaStoreFile uri = ${uri.scheme}")
|
||||
val contentResolver = context.contentResolver
|
||||
val projection = arrayOf(MediaStore.MediaColumns._ID)
|
||||
|
||||
return try {
|
||||
contentResolver.query(uri, projection, null, null, null)?.use { cursor ->
|
||||
if (cursor.moveToFirst()) {
|
||||
val id =
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID))
|
||||
val contentUri = ContentUris.withAppendedId(uri, id)
|
||||
contentResolver.delete(contentUri, null, null) > 0
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} ?: false
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// 删除File Uri文件
|
||||
private fun deleteFileUriFile(uri: Uri): Boolean {
|
||||
Timber.d("deleteFileUriFile uri = $uri")
|
||||
return try {
|
||||
File(uri.path ?: return false).delete()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// 直接通过路径删除文件
|
||||
private fun deleteFileFromPath(path: String): Boolean {
|
||||
Timber.d("deleteFileFromPath path = $path")
|
||||
return try {
|
||||
File(path).delete()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.sw.platecabinet.utils;
|
||||
|
||||
import com.sw.inbound.utils.SPUtil;
|
||||
import com.sw.platecabinet.GlobalKey;
|
||||
import com.sw.platecabinet.MyApp;
|
||||
|
||||
public class SpTool {
|
||||
|
||||
public static final String LAST_FACE_TIMESTAMP = "faceTimestamp";
|
||||
public static final String IS_FIRST_GET_FACE = "isFirstGetFace";
|
||||
|
||||
public static long getLastFaceTimestamp() {
|
||||
return SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").get(LAST_FACE_TIMESTAMP, 0L);
|
||||
@@ -15,6 +17,26 @@ public class SpTool {
|
||||
SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").put(LAST_FACE_TIMESTAMP, timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否首次拉取人脸缓存(Kotlin 侧以 SpTool.firstGetFace 属性语法访问)
|
||||
*/
|
||||
public static boolean getFirstGetFace() {
|
||||
return SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").get(IS_FIRST_GET_FACE, true);
|
||||
}
|
||||
|
||||
public static void setFirstGetFace(boolean value) {
|
||||
SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").put(IS_FIRST_GET_FACE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务服务器 BaseUrl(Kotlin 侧以 SpTool.baseUrl 属性语法访问)
|
||||
*/
|
||||
public static String getBaseUrl() {
|
||||
return SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").get(GlobalKey.KEY_BASE_URL, "");
|
||||
}
|
||||
|
||||
public static void setBaseUrl(String value) {
|
||||
SPUtil.Companion.getInstance(MyApp.getContext(), "default_sp").put(GlobalKey.KEY_BASE_URL, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
package com.sw.platecabinet.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.arcsoft.face.ErrorInfo
|
||||
import com.sw.inbound.utils.SPUtil
|
||||
import com.sw.plate.App
|
||||
import com.sw.plate.utils.Base64
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import com.sw.plate.utils.arcface.FaceApi
|
||||
import com.sw.plate.utils.arcface.facedb.FaceDatabase
|
||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
||||
import com.sw.platecabinet.GlobalData
|
||||
import com.sw.platecabinet.GlobalKey
|
||||
import com.sw.platecabinet.model.DeviceConfigV2
|
||||
import com.sw.platecabinet.model.response.UserFaceModel
|
||||
import com.sw.platecabinet.model.response.UserFaceModelV2
|
||||
import com.sw.platecabinet.network.ApiClient
|
||||
import com.sw.platecabinet.repository.RemoteRepositoryV2
|
||||
import com.sw.platecabinet.utils.SpTool
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* 用户viewmodel
|
||||
*/
|
||||
class NetViewModelV2 : ViewModel() {
|
||||
|
||||
companion object {
|
||||
const val PAGE_SIZE = 100
|
||||
}
|
||||
|
||||
private val faceApi: FaceApi = FaceApi()
|
||||
private val repository = RemoteRepositoryV2(ApiClient.apiServiceV2)
|
||||
|
||||
|
||||
//fun getUserFace(
|
||||
// pageNo: Int,
|
||||
// pageSize: Int,
|
||||
// callback: (Boolean, List<UserFaceModelV2>) -> Unit
|
||||
//) {
|
||||
// viewModelScope.launch {
|
||||
// val response = repository.getUserFaceCache(pageNum = pageNo, pageSize = pageSize)
|
||||
// if (response.isSuccess()) callback(true, response.data ?: emptyList())
|
||||
// else callback(false, emptyList())
|
||||
// }
|
||||
//}
|
||||
|
||||
/**
|
||||
* 获取人脸数据
|
||||
*/
|
||||
fun getUserFaceCache(
|
||||
pageNo: Int = 1,
|
||||
pageSize: Int = PAGE_SIZE,
|
||||
callback: (Boolean, String?) -> Unit = { _, _ -> }
|
||||
) {
|
||||
var currentPageNo = pageNo
|
||||
viewModelScope.launch {
|
||||
val response = repository.getUserFaceCache(pageNo, pageSize)
|
||||
if (response.isSuccess().not()) {
|
||||
callback(false, response.msg)
|
||||
return@launch
|
||||
}
|
||||
// 获取成功一次后缓存状态
|
||||
SPUtil.getInstance().put(GlobalKey.KEY_FIRST_RUN, true)
|
||||
val list = response.data ?: emptyList()
|
||||
if (pageNo == 1 && list.isEmpty()) {
|
||||
callback(false, "暂无数据")
|
||||
return@launch
|
||||
}
|
||||
val faceEntityList = list.mapNotNull { face ->
|
||||
val featureBase64 = face.resolveFeatureStr()
|
||||
if (featureBase64.isNullOrBlank()) null
|
||||
else {
|
||||
val featureData = base64ToByteArray(featureBase64)
|
||||
if (featureData == null) null
|
||||
else face.toFaceEntity()
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Default) {
|
||||
if (currentPageNo == 1) {
|
||||
//第一页数据删除本地数据库,同时重置时间戳
|
||||
faceApi.clearFaceData()
|
||||
faceTimestamp = 0L
|
||||
}
|
||||
faceApi.updateFaceData(pageNo, faceEntityList)
|
||||
if (list.size >= pageSize) {
|
||||
// 取当前页最大时间戳,与已累积的比较取最大值
|
||||
val pageMaxTimestamp = maxFaceTimestamp(list)
|
||||
if (pageMaxTimestamp > faceTimestamp) {
|
||||
faceTimestamp = pageMaxTimestamp
|
||||
}
|
||||
currentPageNo++
|
||||
getUserFaceCache(
|
||||
pageNo = currentPageNo, callback = callback
|
||||
)
|
||||
return@withContext
|
||||
}
|
||||
if (list.isNotEmpty()) {
|
||||
// 最后一页:取当前页最大时间戳,与已累积的比较取最大值
|
||||
val pageMaxTimestamp = maxFaceTimestamp(list)
|
||||
if (pageMaxTimestamp > faceTimestamp) {
|
||||
faceTimestamp = pageMaxTimestamp
|
||||
}
|
||||
}
|
||||
SpTool.setLastFaceTimestamp(faceTimestamp)
|
||||
callback(true, "查询完成")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var faceTimestamp = 0L
|
||||
|
||||
|
||||
/**
|
||||
* 激活人脸识别引擎
|
||||
*/
|
||||
fun activeEngine() {
|
||||
Timber.d("activeEngine")
|
||||
|
||||
faceApi.activeEngine(
|
||||
App.getContext(),
|
||||
GlobalData.appId,
|
||||
GlobalData.sdkKey,
|
||||
GlobalData.activeKey,
|
||||
object : FaceApi.ActiveCallback {
|
||||
override fun onSuccess(activeCode: Int) {
|
||||
Timber.d("activeEngine activeCode = $activeCode")
|
||||
// ThreadUtils.launch {
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
when (activeCode) {
|
||||
ErrorInfo.MOK -> {
|
||||
ToastUtils.showToast("激活引擎成功")
|
||||
}
|
||||
|
||||
ErrorInfo.MERR_ASF_ALREADY_ACTIVATED -> {
|
||||
// ToastUtils.showToast("引擎已激活,无需再次激活")
|
||||
}
|
||||
|
||||
else -> {
|
||||
ToastUtils.showToast("激活引擎失败($activeCode)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFail(e: Exception?) {
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
ToastUtils.showToast("激活引擎异常,${e?.message}")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fun getDeviceConfig(onSuccess: (DeviceConfigV2?) -> Unit, onFailure: (String?) -> Unit) {
|
||||
// Timber.tag(TAG).d("getDeviceConfig")
|
||||
viewModelScope.launch {
|
||||
val response = repository.getDeviceConfig()
|
||||
if (response.isSuccess()) {
|
||||
onSuccess(response.data)
|
||||
} else {
|
||||
onFailure(response.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增量获取人脸数据(支持分页递归,取所有页中的最大时间戳)
|
||||
*/
|
||||
fun getFaceIncrementList(
|
||||
pageNo: Int = 1,
|
||||
pageSize: Int = PAGE_SIZE,
|
||||
timestamp: Long,
|
||||
refreshFaceList: () -> Unit
|
||||
) {
|
||||
var currentPageNo = pageNo
|
||||
viewModelScope.launch {
|
||||
val response = repository.getFaceIncrementList(
|
||||
pageNum = currentPageNo.toLong(),
|
||||
pageSize = pageSize.toLong(),
|
||||
timestamp = timestamp
|
||||
)
|
||||
if (response.isSuccess().not()) {
|
||||
return@launch
|
||||
}
|
||||
withContext(Dispatchers.IO) {
|
||||
val list = response.data ?: emptyList()
|
||||
updateFaceData(list)
|
||||
// 取当前页最大时间戳,与已累积的比较取最大值
|
||||
val pageMaxTimestamp = maxFaceTimestamp(list)
|
||||
if (pageMaxTimestamp > faceTimestamp) {
|
||||
faceTimestamp = pageMaxTimestamp
|
||||
}
|
||||
if (list.size >= pageSize) {
|
||||
// 还有下一页,继续递归拉取
|
||||
currentPageNo++
|
||||
getFaceIncrementList(
|
||||
pageNo = currentPageNo,
|
||||
pageSize = pageSize,
|
||||
timestamp = timestamp,
|
||||
refreshFaceList = refreshFaceList
|
||||
)
|
||||
return@withContext
|
||||
}
|
||||
// 所有页拉取完毕,保存最终时间戳(只升不降)
|
||||
val storedTimestamp = SpTool.getLastFaceTimestamp()
|
||||
if (faceTimestamp > storedTimestamp) {
|
||||
SpTool.setLastFaceTimestamp(faceTimestamp)
|
||||
}
|
||||
try {
|
||||
refreshFaceList()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateFaceData(list: List<UserFaceModelV2>) {
|
||||
try {
|
||||
list.forEach { model ->
|
||||
if (model.faceDeleted == true) {
|
||||
//删除数据
|
||||
// 仅删除 userFaceId 对应的单条人脸记录,避免误删该用户名下所有特征
|
||||
val userFaceId = model.userFaceId
|
||||
if (!userFaceId.isNullOrEmpty()) {
|
||||
faceApi.deleteByUserFaceId(userFaceId)
|
||||
}
|
||||
} else {
|
||||
// 优先按 userFaceId 精确判重,避免特征字段不一致导致重复入库
|
||||
val userFaceId = model.userFaceId
|
||||
if (!userFaceId.isNullOrEmpty() && faceApi.queryByUserFaceId(userFaceId) != null) {
|
||||
// 本地已存在该 userFaceId,跳过插入
|
||||
return@forEach
|
||||
}
|
||||
// userFaceId 为空时,回退到特征比较去重(修正字段来源,保证与入库一致)
|
||||
val featureStr = model.resolveFeatureStr()
|
||||
val existList = faceApi.queryAllByUserName(model.userId)
|
||||
val alreadyExists = existList.any { entity ->
|
||||
featureStr != null && Base64.encode(entity.featureData) == featureStr
|
||||
}
|
||||
if (!alreadyExists) {
|
||||
faceApi.insert(model.toFaceEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加人脸数据
|
||||
*/
|
||||
fun addUserFace(faceData: String, imageFile: File, block: (Boolean, UserFaceModelV2?) -> Unit) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val imageResp = repository.uploadImage(imageFile)
|
||||
val imageUrl = imageResp.data ?: ""
|
||||
if (imageUrl.isBlank()) {
|
||||
block(false, null)
|
||||
return@launch
|
||||
}
|
||||
val faceResp = repository.addUserFace(faceData, imageUrl)
|
||||
if (faceResp.isSuccess().not()) {
|
||||
block(false, null)
|
||||
return@launch
|
||||
}
|
||||
block(true, faceResp.data)
|
||||
} catch (e: Exception) {
|
||||
// 网络或解析异常时同样回调失败,避免 block 永不触发导致上层无反馈
|
||||
e.printStackTrace()
|
||||
block(false, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取餐盘上报(P-09a):记录用户开始就餐时刻,作为后续就餐记录(P-09)的强前置。
|
||||
* 失败仅回调通知,不阻断吐盘等业务主流程。
|
||||
*
|
||||
* @param userId 用户 id(餐盘号 = userId)
|
||||
* @param callback 上报结果回调:Boolean 是否成功,String? 成功为 recordNo、失败为错误信息
|
||||
*/
|
||||
fun platePickup(
|
||||
userId: Long,
|
||||
callback: (Boolean, String?) -> Unit = { _, _ -> }
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response = repository.platePickup(userId)
|
||||
if (response.isSuccess()) {
|
||||
callback(true, response.data)
|
||||
} else {
|
||||
callback(false, response.msg)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
callback(false, e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun base64ToByteArray(base64: String?): ByteArray? {
|
||||
base64 ?: return null
|
||||
return try {
|
||||
Base64.decode(base64)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从人脸数据列表中取出最大的 faceUpdateTimestamp(跳过 null 值)
|
||||
*/
|
||||
private fun maxFaceTimestamp(list: List<UserFaceModelV2>): Long {
|
||||
return list.mapNotNull { it.faceUpdateTimestamp }.maxOrNull() ?: 0L
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user