[2.13.0_feat]消息盒子数据中心管理各类消息
This commit is contained in:
@@ -1,55 +1,91 @@
|
||||
package com.mogo.eagle.core.function.msgbox
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Looper
|
||||
import com.mogo.eagle.core.data.msgbox.MsgBoxBean
|
||||
import com.mogo.eagle.core.data.msgbox.MsgBoxType
|
||||
import com.mogo.eagle.core.data.msgbox.MsgCategory
|
||||
import com.mogo.eagle.core.data.msgbox.*
|
||||
import com.mogo.eagle.core.data.notice.NoticeNormalData
|
||||
import com.mogo.eagle.core.data.report.ReportEntity
|
||||
import com.mogo.eagle.core.function.call.msgbox.CallerMsgBoxListenerManager
|
||||
import com.mogo.eagle.core.function.msgbox.db.MsgBoxDb
|
||||
import com.mogo.eagle.core.function.msgbox.db.MsgBoxInfo
|
||||
import com.mogo.eagle.core.utilcode.kotlin.lifeCycleScope
|
||||
import com.mogo.eagle.core.utilcode.util.GsonUtils
|
||||
import com.mogo.eagle.core.utilcode.util.Utils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
object DataManager {
|
||||
|
||||
// private val msgBoxMap: EnumMap<MsgBoxType, MutableList<MsgBoxBean>> = EnumMap(MsgBoxType::class.java)
|
||||
|
||||
// 消失时间5000ms
|
||||
const val DISMISS_TIME = 5000L
|
||||
|
||||
private val notifyList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 当天缓存的通知消息
|
||||
*/
|
||||
private val cacheNotifyList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
private val sysInfoList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 当天缓存的系统消息
|
||||
*/
|
||||
private val cacheSysInfoList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
private val recordBagList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
@Volatile
|
||||
private var isUpdate = false
|
||||
/**
|
||||
* 当天缓存的录包消息
|
||||
*/
|
||||
private val cacheRecordList by lazy {
|
||||
mutableListOf<MsgBoxBean>()
|
||||
}
|
||||
|
||||
private val scope by lazy {
|
||||
Utils.getApp().lifeCycleScope
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消给录制的Bag包打标签
|
||||
*/
|
||||
private val removedRecordMap by lazy {
|
||||
HashMap<String, Any>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储时保证按时序排列
|
||||
*/
|
||||
fun saveMsg(bean: MsgBoxBean) {
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread) {
|
||||
scope.launch(Dispatchers.Default) {
|
||||
realSaveMsg(bean)
|
||||
scope.launch {
|
||||
withContext(Dispatchers.Default) {
|
||||
realSaveMsg(bean)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
realSaveMsg(bean)
|
||||
}
|
||||
}
|
||||
|
||||
fun realSaveMsg(bean: MsgBoxBean) {
|
||||
private fun realSaveMsg(bean: MsgBoxBean) {
|
||||
val type = bean.type
|
||||
bean.timestamp = System.currentTimeMillis()
|
||||
when(type) {
|
||||
bean.bean2Json = GsonUtils.toJson(bean)
|
||||
when (type) {
|
||||
MsgBoxType.V2X, MsgBoxType.OBU, MsgBoxType.NOTICE, MsgBoxType.OPERATION -> {
|
||||
synchronized(this) {
|
||||
notifyList.add(bean)
|
||||
@@ -76,20 +112,170 @@ object DataManager {
|
||||
* 通知消息:V2X、云公告、运营信息
|
||||
*/
|
||||
fun getNotifyData(): List<MsgBoxBean> {
|
||||
return notifyList
|
||||
return cacheNotifyList
|
||||
}
|
||||
|
||||
/**
|
||||
* 工控机Report信息
|
||||
*/
|
||||
fun getSysInfoData(): List<MsgBoxBean> {
|
||||
return sysInfoList
|
||||
return cacheSysInfoList
|
||||
}
|
||||
|
||||
/**
|
||||
* 录包信息
|
||||
*/
|
||||
fun getRecordBagData(): List<MsgBoxBean> {
|
||||
return recordBagList
|
||||
return cacheRecordList
|
||||
}
|
||||
|
||||
fun removeRecordInfo(key: String, value: Any) {
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread) {
|
||||
scope.launch {
|
||||
withContext(Dispatchers.Default) {
|
||||
removedRecordMap[key] = value
|
||||
}
|
||||
}
|
||||
} else {
|
||||
removedRecordMap[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地数据库中查询数据
|
||||
*/
|
||||
fun queryAllMessages(context: Context) {
|
||||
scope.launch {
|
||||
getCacheMessages(context)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getCacheMessages(context: Context): List<MsgBoxBean> = withContext(Dispatchers.IO) {
|
||||
return@withContext MsgBoxDb.getDb(context)
|
||||
.monitorDao()
|
||||
.getAllCachedMessages()
|
||||
.map { msgInfo ->
|
||||
val json = msgInfo.bean2Json
|
||||
when (msgInfo.obj2JsonType) {
|
||||
MsgBoxType.V2X.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.V2X,
|
||||
GsonUtils.fromJson(json, V2XMsg::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheNotifyList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
MsgBoxType.OBU.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.OBU,
|
||||
GsonUtils.fromJson(json, V2XMsg::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheNotifyList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
MsgBoxType.OPERATION.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.OPERATION,
|
||||
GsonUtils.fromJson(json, OperationMsg::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheNotifyList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
MsgBoxType.REPORT.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.REPORT,
|
||||
GsonUtils.fromJson(json, ReportEntity::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheSysInfoList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
MsgBoxType.RECORD.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.RECORD,
|
||||
GsonUtils.fromJson(json, RecordBagMsg::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheRecordList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
MsgBoxType.NOTICE.ordinal -> {
|
||||
return@map MsgBoxBean(
|
||||
MsgBoxType.NOTICE,
|
||||
GsonUtils.fromJson(json, NoticeNormalData::class.java)
|
||||
).apply {
|
||||
withContext(Dispatchers.Main) {
|
||||
cacheNotifyList.add(this@apply)
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
return@map MsgBoxBean(MsgBoxType.V2X, V2XMsg())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储到本地数据库
|
||||
*/
|
||||
fun saveAllMessages2Disk(context: Context) {
|
||||
synchronized(this) {
|
||||
val msgInfoList = ArrayList<MsgBoxInfo>()
|
||||
if (notifyList.isNotEmpty()) {
|
||||
notifyList.forEach {
|
||||
msgInfoList.add(MsgBoxInfo(it.bean2Json, it.type.ordinal, it.timestamp))
|
||||
}
|
||||
notifyList.clear()
|
||||
}
|
||||
|
||||
if (sysInfoList.isNotEmpty()) {
|
||||
sysInfoList.forEach {
|
||||
msgInfoList.add(MsgBoxInfo(it.bean2Json, it.type.ordinal, it.timestamp))
|
||||
}
|
||||
sysInfoList.clear()
|
||||
}
|
||||
|
||||
if (recordBagList.isNotEmpty()) {
|
||||
recordBagList.forEach {
|
||||
var recordKey = ""
|
||||
if (it.bean is RecordBagMsg) {
|
||||
recordKey = (it.bean as RecordBagMsg).key.toString()
|
||||
}
|
||||
// 用户未处理的Bag包才存本地
|
||||
if (!removedRecordMap.contains(recordKey)) {
|
||||
msgInfoList.add(MsgBoxInfo(it.bean2Json, it.type.ordinal, it.timestamp))
|
||||
} else {// 用户已处理的Bag包
|
||||
removedRecordMap.remove(recordKey)
|
||||
}
|
||||
}
|
||||
recordBagList.clear()
|
||||
}
|
||||
|
||||
if (msgInfoList.isNotEmpty()) {
|
||||
MsgBoxDb.getDb(context)
|
||||
.monitorDao()
|
||||
.insertMessages(*msgInfoList.toTypedArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun delMsgBoxBean(context: Context, msgBoxBean: MsgBoxBean) {
|
||||
scope.launch {
|
||||
withContext(Dispatchers.Default) {
|
||||
val msgBoxInfo = MsgBoxInfo(msgBoxBean.bean2Json, msgBoxBean.type.ordinal, msgBoxBean.timestamp)
|
||||
MsgBoxDb.getDb(context)
|
||||
.monitorDao()
|
||||
.deleteMsg(msgBoxInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.mogo.eagle.core.data.constants.MogoServicePaths.PATH_MSG_BOX_MODULE
|
||||
import com.mogo.eagle.core.data.msgbox.MsgBoxBean
|
||||
import com.mogo.eagle.core.function.api.msgbox.IMsgBoxProvider
|
||||
import com.mogo.eagle.core.function.msgbox.DataManager.saveAllMessages2Disk
|
||||
|
||||
@Route(path = PATH_MSG_BOX_MODULE)
|
||||
class MsgBoxProvider : IMsgBoxProvider {
|
||||
@@ -13,25 +14,46 @@ class MsgBoxProvider : IMsgBoxProvider {
|
||||
get() = "MsgBoxProvider"
|
||||
|
||||
override fun init(context: Context) {
|
||||
context
|
||||
Thread {
|
||||
while (true) {
|
||||
try {
|
||||
saveAllMessages2Disk(context)
|
||||
Thread.sleep(3000)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
override fun saveMsg(bean: MsgBoxBean) {
|
||||
DataManager.saveMsg(bean)
|
||||
}
|
||||
|
||||
override fun getNotifyData(): List<MsgBoxBean> {
|
||||
override fun getCachedNotifyData(): List<MsgBoxBean> {
|
||||
return DataManager.getNotifyData()
|
||||
}
|
||||
|
||||
override fun getSysInfoData(): List<MsgBoxBean> {
|
||||
override fun getCachedSysInfoData(): List<MsgBoxBean> {
|
||||
return DataManager.getSysInfoData()
|
||||
}
|
||||
|
||||
override fun getRecordBagData(): List<MsgBoxBean> {
|
||||
override fun getCachedRecordBagData(): List<MsgBoxBean> {
|
||||
return DataManager.getRecordBagData()
|
||||
}
|
||||
|
||||
override fun removeRecordInfo(key: String, value: Any) {
|
||||
return DataManager.removeRecordInfo(key, value)
|
||||
}
|
||||
|
||||
override fun deleteBoxBean(context: Context, msgBoxBean: MsgBoxBean) {
|
||||
DataManager.delMsgBoxBean(context, msgBoxBean)
|
||||
}
|
||||
|
||||
override fun getDismissTime(): Long {
|
||||
return DataManager.DISMISS_TIME
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
}
|
||||
|
||||
@@ -5,5 +5,14 @@ import androidx.room.*
|
||||
@Dao
|
||||
interface MsgBoxDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun saveMsg(info: MsgBoxInfo)
|
||||
fun insertMessage(info: MsgBoxInfo)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertMessages(vararg info: MsgBoxInfo)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteMsg(info: MsgBoxInfo)
|
||||
|
||||
@Query("SELECT * FROM t_msg_box")
|
||||
fun getAllCachedMessages(): List<MsgBoxInfo>
|
||||
}
|
||||
@@ -6,10 +6,13 @@ import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "t_msg_box")
|
||||
data class MsgBoxInfo(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "id")
|
||||
val uuid: Long = 0,
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "obj_json")
|
||||
val bean2Json: String,
|
||||
|
||||
@ColumnInfo(name = "obj_str")
|
||||
val objString: String
|
||||
@ColumnInfo(name = "json_obj_type")
|
||||
val obj2JsonType: Int = 0,
|
||||
|
||||
@ColumnInfo(name = "time_stamp")
|
||||
val timeStamp: Long = 0
|
||||
)
|
||||
Reference in New Issue
Block a user