设备告警日志功能逻辑
This commit is contained in:
@@ -6,7 +6,10 @@ import androidx.recyclerview.widget.RecyclerView
|
|||||||
import com.example.defenseapplication.bean.DeviceAlarmBean
|
import com.example.defenseapplication.bean.DeviceAlarmBean
|
||||||
import com.example.defenseapplication.databinding.ItemAlarmLogBinding
|
import com.example.defenseapplication.databinding.ItemAlarmLogBinding
|
||||||
|
|
||||||
class AlarmLogAdapter(private val items: List<DeviceAlarmBean>) : RecyclerView.Adapter<AlarmLogAdapter.ViewHolder>() {
|
class AlarmLogAdapter(
|
||||||
|
private val items: List<DeviceAlarmBean>,
|
||||||
|
private val onItemClick: (DeviceAlarmBean) -> Unit
|
||||||
|
) : RecyclerView.Adapter<AlarmLogAdapter.ViewHolder>() {
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
val binding = ItemAlarmLogBinding.inflate(
|
val binding = ItemAlarmLogBinding.inflate(
|
||||||
@@ -23,10 +26,14 @@ class AlarmLogAdapter(private val items: List<DeviceAlarmBean>) : RecyclerView.A
|
|||||||
|
|
||||||
override fun getItemCount(): Int = items.size
|
override fun getItemCount(): Int = items.size
|
||||||
|
|
||||||
class ViewHolder(private val binding: ItemAlarmLogBinding) : RecyclerView.ViewHolder(binding.root) {
|
inner class ViewHolder(private val binding: ItemAlarmLogBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||||
fun bind(item: DeviceAlarmBean) {
|
fun bind(item: DeviceAlarmBean) {
|
||||||
binding.tvTitle.text = item.categoryName
|
binding.tvTitle.text = item.categoryName
|
||||||
binding.tvTime.text = item.createTime
|
binding.tvTime.text = item.createTime
|
||||||
|
|
||||||
|
binding.root.setOnClickListener {
|
||||||
|
onItemClick(item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
private var isLightOn = false
|
private var isLightOn = false
|
||||||
private var deviceId: String? = null
|
private var deviceId: String? = null
|
||||||
private var isLoading = false
|
private var isLoading = false
|
||||||
|
private var fromLiveVideo = false
|
||||||
|
private var passedVideoUrl: String? = null
|
||||||
|
private var passedSelectedId: String? = null
|
||||||
|
private var passedCreateTime: String? = null
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -53,6 +57,10 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
.init()
|
.init()
|
||||||
|
|
||||||
deviceId = intent.getStringExtra("device_id")
|
deviceId = intent.getStringExtra("device_id")
|
||||||
|
fromLiveVideo = intent.getBooleanExtra("from_live_video", false)
|
||||||
|
passedVideoUrl = intent.getStringExtra("video_url")
|
||||||
|
passedSelectedId = intent.getStringExtra("selected_id")
|
||||||
|
passedCreateTime = intent.getStringExtra("create_time")
|
||||||
calendar = Calendar.getInstance()
|
calendar = Calendar.getInstance()
|
||||||
updateDateDisplay()
|
updateDateDisplay()
|
||||||
initVideoPlayer()
|
initVideoPlayer()
|
||||||
@@ -77,9 +85,14 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private fun initVideoPlayer() {
|
private fun initVideoPlayer() {
|
||||||
val videoPlayerView = binding.exoPlayContextId
|
val videoPlayerView = binding.exoPlayContextId
|
||||||
|
val videoUri = if (fromLiveVideo && !passedVideoUrl.isNullOrEmpty()) {
|
||||||
|
passedVideoUrl!!
|
||||||
|
} else {
|
||||||
|
"https://media.w3.org/2010/05/sintel/trailer.mp4"
|
||||||
|
}
|
||||||
|
|
||||||
exoPlayerManager = VideoPlayerManager.Builder(VideoPlayerManager.TYPE_PLAY_GESTURE, videoPlayerView)
|
exoPlayerManager = VideoPlayerManager.Builder(VideoPlayerManager.TYPE_PLAY_GESTURE, videoPlayerView)
|
||||||
.setPlayUri("https://media.w3.org/2010/05/sintel/trailer.mp4")
|
.setPlayUri(videoUri)
|
||||||
.setPosition(0)
|
.setPosition(0)
|
||||||
.addVideoInfoListener(object : VideoInfoListener {
|
.addVideoInfoListener(object : VideoInfoListener {
|
||||||
override fun onPlayStart(p0: Long) {
|
override fun onPlayStart(p0: Long) {
|
||||||
@@ -87,7 +100,6 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLoadingChanged() {
|
override fun onLoadingChanged() {
|
||||||
// 视频加载状态变化
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlayerError(p0: com.google.android.exoplayer2.ExoPlaybackException?) {
|
override fun onPlayerError(p0: com.google.android.exoplayer2.ExoPlaybackException?) {
|
||||||
@@ -99,11 +111,9 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun isPlaying(p0: Boolean) {
|
override fun isPlaying(p0: Boolean) {
|
||||||
// p0 表示当前是否正在播放
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onRepeatModeChanged(p0: Int) {
|
fun onRepeatModeChanged(p0: Int) {
|
||||||
// 循环模式改变时的回调
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.create()
|
.create()
|
||||||
@@ -145,10 +155,49 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initRecyclerView() {
|
private fun initRecyclerView() {
|
||||||
adapter = AlarmLogAdapter(alarmList)
|
adapter = AlarmLogAdapter(alarmList) { item ->
|
||||||
|
if (!item.video.isNullOrEmpty()) {
|
||||||
|
playVideo(item.video!!)
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "该告警记录没有视频", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
binding.alarmListRecyclerView.layoutManager = LinearLayoutManager(this)
|
binding.alarmListRecyclerView.layoutManager = LinearLayoutManager(this)
|
||||||
binding.alarmListRecyclerView.adapter = adapter
|
binding.alarmListRecyclerView.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun playVideo(videoUrl: String) {
|
||||||
|
exoPlayerManager.onDestroy()
|
||||||
|
|
||||||
|
exoPlayerManager = VideoPlayerManager.Builder(VideoPlayerManager.TYPE_PLAY_GESTURE, binding.exoPlayContextId)
|
||||||
|
.setPlayUri(videoUrl)
|
||||||
|
.setPosition(0)
|
||||||
|
.addVideoInfoListener(object : VideoInfoListener {
|
||||||
|
override fun onPlayStart(p0: Long) {
|
||||||
|
Log.d("AlarmLogActivity", "视频开始播放")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLoadingChanged() {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPlayerError(p0: com.google.android.exoplayer2.ExoPlaybackException?) {
|
||||||
|
Toast.makeText(this@AlarmLogActivity, "播放失败", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPlayEnd() {
|
||||||
|
Log.d("AlarmLogActivity", "播放结束")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isPlaying(p0: Boolean) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onRepeatModeChanged(p0: Int) {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.create()
|
||||||
|
|
||||||
|
exoPlayerManager.startPlayer()
|
||||||
|
}
|
||||||
|
|
||||||
private fun fetchAlarmList() {
|
private fun fetchAlarmList() {
|
||||||
val currentDeviceId = deviceId ?: return
|
val currentDeviceId = deviceId ?: return
|
||||||
@@ -297,6 +346,7 @@ class AlarmLogActivity : AppCompatActivity() {
|
|||||||
.setTextColorCenter(Color.parseColor("#09C2BD"))//选中颜色
|
.setTextColorCenter(Color.parseColor("#09C2BD"))//选中颜色
|
||||||
.setLineSpacingMultiplier(2.5f)//行间距倍数
|
.setLineSpacingMultiplier(2.5f)//行间距倍数
|
||||||
.setItemVisibleCount(5)//可见项数量
|
.setItemVisibleCount(5)//可见项数量
|
||||||
|
.setDate(calendar) // 设置默认选中日期为tvDate显示的日期
|
||||||
.build()
|
.build()
|
||||||
timePicker.show()
|
timePicker.show()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,9 +45,11 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
private var alarmList: MutableList<DeviceAlarmBean> = mutableListOf()
|
private var alarmList: MutableList<DeviceAlarmBean> = mutableListOf()
|
||||||
private var alarmAdapter: AlarmLogHorizontalAdapter? = null
|
private var alarmAdapter: AlarmLogHorizontalAdapter? = null
|
||||||
private var currentPage = 1
|
private var currentPage = 1
|
||||||
|
private var totalPage = 1
|
||||||
private val pageSize = 10
|
private val pageSize = 10
|
||||||
private var hasMore = true
|
private var hasMore = true
|
||||||
private var isLoading = false
|
private var isLoading = false
|
||||||
|
private var isFirstLoad = true
|
||||||
|
|
||||||
private val requestPermissionLauncher = registerForActivityResult(
|
private val requestPermissionLauncher = registerForActivityResult(
|
||||||
ActivityResultContracts.RequestPermission()
|
ActivityResultContracts.RequestPermission()
|
||||||
@@ -130,6 +132,9 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
val intent = Intent(this, AlarmLogActivity::class.java)
|
val intent = Intent(this, AlarmLogActivity::class.java)
|
||||||
intent.putExtra("device_id", deviceId)
|
intent.putExtra("device_id", deviceId)
|
||||||
intent.putExtra("selected_id", messageBean.id)
|
intent.putExtra("selected_id", messageBean.id)
|
||||||
|
intent.putExtra("video_url", messageBean.video)
|
||||||
|
intent.putExtra("create_time", messageBean.createTime)
|
||||||
|
intent.putExtra("from_live_video", true)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
binding.alarmLogRecyclerView.adapter = alarmAdapter
|
binding.alarmLogRecyclerView.adapter = alarmAdapter
|
||||||
@@ -139,8 +144,7 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
super.onScrolled(recyclerView, dx, dy)
|
super.onScrolled(recyclerView, dx, dy)
|
||||||
if (dx > 0 && !isLoading && hasMore) {
|
if (dx > 0 && !isLoading && hasMore) {
|
||||||
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
|
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
|
||||||
val totalItemCount = layoutManager.itemCount
|
if (lastVisibleItemPosition == alarmList.size - 1) {
|
||||||
if (lastVisibleItemPosition >= totalItemCount - 1) {
|
|
||||||
loadMoreAlarms()
|
loadMoreAlarms()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,6 +155,10 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
private fun loadMoreAlarms() {
|
private fun loadMoreAlarms() {
|
||||||
if (!hasMore || isLoading) return
|
if (!hasMore || isLoading) return
|
||||||
currentPage++
|
currentPage++
|
||||||
|
if (currentPage > totalPage) {
|
||||||
|
hasMore = false
|
||||||
|
return
|
||||||
|
}
|
||||||
fetchAlarmList(currentPage)
|
fetchAlarmList(currentPage)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +195,7 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
binding.xq.setOnClickListener {
|
binding.xq.setOnClickListener {
|
||||||
val intent = Intent(this, AlarmLogActivity::class.java)
|
val intent = Intent(this, AlarmLogActivity::class.java)
|
||||||
intent.putExtra("device_id", deviceId)
|
intent.putExtra("device_id", deviceId)
|
||||||
|
intent.putExtra("from_live_video", true)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -220,17 +229,25 @@ class LiveVideoActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
totalPage = if (response.total % pageSize == 0) {
|
||||||
|
response.total / pageSize
|
||||||
|
} else {
|
||||||
|
response.total / pageSize + 1
|
||||||
|
}
|
||||||
|
Log.d("LiveVideoActivity", "总页数: $totalPage")
|
||||||
|
|
||||||
|
isFirstLoad = false
|
||||||
|
|
||||||
if (page == 1) {
|
if (page == 1) {
|
||||||
alarmList.clear()
|
alarmList.clear()
|
||||||
hasMore = true
|
hasMore = totalPage > 1
|
||||||
Log.d("LiveVideoActivity", "第一页,已清空列表")
|
Log.d("LiveVideoActivity", "第一页,已清空列表")
|
||||||
}
|
}
|
||||||
|
|
||||||
alarmList.addAll(newData)
|
alarmList.addAll(newData)
|
||||||
Log.d("LiveVideoActivity", "已添加 ${newData.size} 条数据,列表总条数: ${alarmList.size}")
|
Log.d("LiveVideoActivity", "已添加 ${newData.size} 条数据,列表总条数: ${alarmList.size}")
|
||||||
alarmAdapter?.notifyDataSetChanged()
|
alarmAdapter?.notifyDataSetChanged()
|
||||||
Log.d("LiveVideoActivity", "已通知适配器刷新")
|
Log.d("LiveVideoActivity", "已通知适配器刷新")
|
||||||
hasMore = newData.size >= pageSize
|
|
||||||
Log.d("LiveVideoActivity", "是否有更多数据: $hasMore")
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("LiveVideoActivity", "请求异常: ${e.message}", e)
|
Log.e("LiveVideoActivity", "请求异常: ${e.message}", e)
|
||||||
|
|||||||
Reference in New Issue
Block a user