desc:云端同步

main
xiaowusky 2 years ago
parent 50f9742817
commit b880de0928

@ -13,7 +13,7 @@ import com.common.commonlib.net.bean.BaseResponse
import com.common.commonlib.net.callback.RequestResultCallBack import com.common.commonlib.net.callback.RequestResultCallBack
import com.yinuo.library.vlc.PushHelper import com.yinuo.library.vlc.PushHelper
import com.yinuo.library.vlc.TxtOverlay import com.yinuo.library.vlc.TxtOverlay
import com.yinuo.safetywatcher.watcher.net.api.UploadFileApi import com.yinuo.safetywatcher.watcher.net.UploadFileApi
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog
import com.yinuo.safetywatcher.watcher.utils.DateUtils import com.yinuo.safetywatcher.watcher.utils.DateUtils
import com.yinuo.safetywatcher.xls.SimpleCellValue import com.yinuo.safetywatcher.xls.SimpleCellValue

@ -1,5 +1,7 @@
package com.yinuo.safetywatcher.watcher.bean package com.yinuo.safetywatcher.watcher.bean
import com.common.commonlib.db.entity.Gas
data class GasReqBean( data class GasReqBean(
val gasName: String = "", val gasName: String = "",
val gasValue: String = "", val gasValue: String = "",
@ -8,4 +10,14 @@ data class GasReqBean(
val sensorName: String = "", val sensorName: String = "",
val status: String = "", val status: String = "",
val alarmName: String = "" val alarmName: String = ""
) ) {
constructor(gas: Gas) : this(
gasName = gas.gasName,
gasValue = gas.gasValue.toString(),
gasUnit = gas.unit,
reportTime = gas.time.toString(),
sensorName = "",
status = "",
alarmName = ""
)
}

@ -1,6 +1,5 @@
package com.yinuo.safetywatcher.watcher.net package com.yinuo.safetywatcher.watcher.bean
import com.yinuo.safetywatcher.watcher.bean.GasReqBean
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
data class GasRequest( data class GasRequest(

@ -1,4 +1,4 @@
package com.yinuo.safetywatcher.watcher.net package com.yinuo.safetywatcher.watcher.bean
import com.common.commonlib.net.bean.BaseResponse import com.common.commonlib.net.bean.BaseResponse

@ -1,3 +0,0 @@
package com.yinuo.safetywatcher.watcher.bean
data class HistoryData(val time:Long, val sensor:String)

@ -0,0 +1,5 @@
package com.yinuo.safetywatcher.watcher.bean
import com.common.commonlib.net.bean.BaseResponse
data class LastTimeResponse(val data: Long) : BaseResponse()

@ -1,3 +0,0 @@
package com.yinuo.safetywatcher.watcher.bean
data class WarnData(val time:Long, val error:String)

@ -1,4 +1,4 @@
package com.yinuo.safetywatcher.watcher.net.api package com.yinuo.safetywatcher.watcher.net
import com.common.commonlib.net.BaseObserve import com.common.commonlib.net.BaseObserve
import com.common.commonlib.net.bean.BaseResponse import com.common.commonlib.net.bean.BaseResponse
@ -6,8 +6,10 @@ import com.common.commonlib.net.callback.RequestNoResultCallBack
import com.common.commonlib.net.callback.RequestResultCallBack import com.common.commonlib.net.callback.RequestResultCallBack
import com.yinuo.safetywatcher.watcher.bean.GasReqBean import com.yinuo.safetywatcher.watcher.bean.GasReqBean
import com.yinuo.safetywatcher.watcher.bean.TrafficReqInfo import com.yinuo.safetywatcher.watcher.bean.TrafficReqInfo
import com.yinuo.safetywatcher.watcher.net.GasRequest import com.yinuo.safetywatcher.watcher.bean.GasRequest
import com.yinuo.safetywatcher.watcher.net.GetRtspUrlResponse import com.yinuo.safetywatcher.watcher.bean.GetRtspUrlResponse
import com.yinuo.safetywatcher.watcher.bean.LastTimeResponse
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.GET import retrofit2.http.GET
@ -44,6 +46,13 @@ class DevicesApi : BaseObserve<DevicesApi.Api>(Api::class.java) {
observe(api.uploadGasData(gasReq), null) observe(api.uploadGasData(gasReq), null)
} }
fun getDetectionLastTime(callBack: RequestResultCallBack<LastTimeResponse>) {
observe(api.getDetectionLastTime(LztekUtil.getSn()), callBack)
}
fun getVideoLastTime(callBack: RequestResultCallBack<LastTimeResponse>) {
observe(api.getVideoLastTime(LztekUtil.getSn()), callBack)
}
interface Api { interface Api {
@Headers("baseurl:host") @Headers("baseurl:host")
@ -62,5 +71,13 @@ class DevicesApi : BaseObserve<DevicesApi.Api>(Api::class.java) {
@Headers("baseurl:host") @Headers("baseurl:host")
@POST("/api/device/addDetection") @POST("/api/device/addDetection")
fun uploadGasData(@Body body: GasRequest): Observable<BaseResponse> fun uploadGasData(@Body body: GasRequest): Observable<BaseResponse>
@Headers("baseurl:host")
@GET("/api/device/getDetectionLastTime")
fun getDetectionLastTime(@Query("deviceSn") sn: String): Observable<LastTimeResponse>
@Headers("baseurl:host")
@GET("/api/device/getVideoLastTime")
fun getVideoLastTime(@Query("deviceSn") sn: String): Observable<LastTimeResponse>
} }
} }

@ -1,4 +1,4 @@
package com.yinuo.safetywatcher.watcher.net.api package com.yinuo.safetywatcher.watcher.net
import com.common.commonlib.net.BaseObserve import com.common.commonlib.net.BaseObserve
import com.common.commonlib.net.bean.BaseResponse import com.common.commonlib.net.bean.BaseResponse

@ -5,8 +5,8 @@ import android.app.IntentService
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import com.common.commonlib.net.callback.RequestResultCallBack import com.common.commonlib.net.callback.RequestResultCallBack
import com.yinuo.safetywatcher.watcher.net.GetRtspUrlResponse import com.yinuo.safetywatcher.watcher.bean.GetRtspUrlResponse
import com.yinuo.safetywatcher.watcher.net.api.DevicesApi import com.yinuo.safetywatcher.watcher.net.DevicesApi
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil

@ -1,3 +1,5 @@
@file:OptIn(DelicateCoroutinesApi::class)
package com.yinuo.safetywatcher.watcher.ui package com.yinuo.safetywatcher.watcher.ui
import android.view.View import android.view.View
@ -6,8 +8,12 @@ import com.yinuo.safetywatcher.TestUtils
import com.yinuo.safetywatcher.databinding.ActivityCloudBinding import com.yinuo.safetywatcher.databinding.ActivityCloudBinding
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
import com.common.commonlib.db.DBUtils import com.common.commonlib.db.DBUtils
import com.yinuo.safetywatcher.watcher.net.api.UploadFileApi import com.common.commonlib.net.callback.RequestResultCallBack
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog import com.yinuo.library.vlc.utils.LogUtils
import com.yinuo.safetywatcher.watcher.bean.GasReqBean
import com.yinuo.safetywatcher.watcher.bean.LastTimeResponse
import com.yinuo.safetywatcher.watcher.net.DevicesApi
import com.yinuo.safetywatcher.watcher.net.UploadFileApi
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -21,6 +27,10 @@ class CloudActivity : NoOptionsActivity() {
UploadFileApi() UploadFileApi()
} }
private val devicesApi by lazy {
DevicesApi()
}
override fun getTopBarTitle(): String? { override fun getTopBarTitle(): String? {
return getString(R.string.cloud) return getString(R.string.cloud)
} }
@ -52,32 +62,72 @@ class CloudActivity : NoOptionsActivity() {
} }
private fun uploadVideo() { private fun uploadVideo() {
//TODO 1.拿到云端最新数据时间 // 1.拿到云端最新数据时间
val cloudTime = 0L devicesApi.getVideoLastTime(object : RequestResultCallBack<LastTimeResponse>() {
// 2. 拿到待上传文件列表 override fun onResult(result: LastTimeResponse) {
val fileList = getFileList(cloudTime) if (result.isOk()) {
// 上传 val cloudTime = result.data
fileList?.forEach { // 2. 拿到待上传文件列表 上传
uploadFile(it) getFileListAndUpload(cloudTime)
} } else {
// TODO 删除 LogUtils.e("getVideoLastTime error code = ${result.code}")
uploadFile("") }
}
override fun onError(error: String?) {
LogUtils.e(error)
}
})
} }
@OptIn(DelicateCoroutinesApi::class)
private fun uploadSensorData() { private fun uploadSensorData() {
// 1.拿到云端最新数据时间
devicesApi.getVideoLastTime(object : RequestResultCallBack<LastTimeResponse>() {
override fun onResult(result: LastTimeResponse) {
if (result.isOk()) {
val cloudTime = result.data
// 2. 查询本地数据库中该时间之后的数据
// 3. 上传数据给服务器
getSensorDataUpload(cloudTime)
} else {
LogUtils.e("getVideoLastTime error code = ${result.code}")
}
}
override fun onError(error: String?) {
LogUtils.e(error)
}
})
}
private fun getSensorDataUpload(cloudTime: Long) {
GlobalScope.launch { GlobalScope.launch {
//TODO 1.拿到云端最新数据时间
val cloudTime = 0L
// 2. 查询本地数据库中该时间之后的数据
val gasList = DBUtils.gasDao().getAllBySyncFlagOrTime(false, cloudTime) val gasList = DBUtils.gasDao().getAllBySyncFlagOrTime(false, cloudTime)
// 3. 上传数据给服务器 val tempList = arrayListOf<GasReqBean>()
gasList.forEach {
tempList.add(GasReqBean(it))
if (tempList.size == 10) {
val upLoadList = arrayListOf<GasReqBean>()
upLoadList.addAll(tempList)
tempList.clear()
devicesApi.uploadGasData(upLoadList)
}
}
if (tempList.isNotEmpty()) {
devicesApi.uploadGasData(tempList)
}
} }
} }
private fun getFileList(cloudTime: Long): List<String>? { private fun getFileListAndUpload(cloudTime: Long) {
return null GlobalScope.launch {
val videoDao = DBUtils.videoDao()
val videoList = videoDao.getAllByTime(cloudTime, System.currentTimeMillis())
// 上传
videoList?.forEach {
uploadFile(it.path)
}
}
} }
private fun uploadFile(path: String) { private fun uploadFile(path: String) {

@ -56,6 +56,9 @@ class HomeActivity : NoOptionsActivity() {
itemRecovery.setOnClickListener { itemRecovery.setOnClickListener {
mBinding.tvWarn.visibility = View.GONE mBinding.tvWarn.visibility = View.GONE
} }
itemCamera.setOnClickListener {
cameraSwitch.performClick()
}
cameraSwitch.setOnCheckedChangeListener { buttonView, isChecked -> cameraSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) mRender?.startRecording() else mRender?.stopRecording() if (isChecked) mRender?.startRecording() else mRender?.stopRecording()
} }

@ -50,6 +50,6 @@ object LztekUtil {
fun setRtspUrl(str: String?) { fun setRtspUrl(str: String?) {
this.rtspUrl = str this.rtspUrl = str
// rtspUrl?.let { PushHelper.setPushUrl(it) } rtspUrl?.let { PushHelper.setPushUrl(it) }
} }
} }

@ -115,6 +115,7 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/item_camera"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/_88dp" android:layout_height="@dimen/_88dp"
android:focusable="true" android:focusable="true"

@ -11,14 +11,10 @@ import kotlinx.parcelize.Parcelize
*/ */
@Parcelize @Parcelize
open class BaseResponse : Parcelable { open class BaseResponse : Parcelable {
var errorCode: Int = 0 var code: Int = 0
var errorMessage: String = "" var msg: String = ""
fun getMErrorCode(): Int { fun isOk(): Boolean {
return errorCode return code == 200
}
fun getMErrorMessage(): String {
return errorMessage
} }
} }

Loading…
Cancel
Save