Skip to content

Commit 4250239

Browse files
committed
增加检测设备是否在线信令实现
1 parent 5cb3ce2 commit 4250239

File tree

2 files changed

+89
-79
lines changed

2 files changed

+89
-79
lines changed

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/preview/VideoPreviewActivity.kt

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,15 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
200200
private fun checkDeviceState() {
201201
Log.d(tag, "====检测设备状态===")
202202
launch(Dispatchers.IO) {
203-
val command = "action=user_define&channel=${presenter.getChannel()}&cmd=device_state"
204-
val retContent = XP2P.postCommandRequestSync(
205-
"${presenter.getProductId()}/${presenter.getDeviceName()}",
206-
command.toByteArray(), command.toByteArray().size.toLong(), 1 * 1000 * 1000
207-
) ?: ""
208-
Log.d(tag, "device_state back content:$retContent")
209-
210-
if (retContent.isEmpty()) {
203+
getDeviceStatus("${presenter.getProductId()}/${presenter.getDeviceName()}") { isOnline, msg ->
211204
launch(Dispatchers.Main) {
212-
restartService()
213-
}
214-
} else {
215-
launch(Dispatchers.Main) {
216-
isRestart = false
217-
delegateHttpFlv()
205+
Toast.makeText(this@VideoPreviewActivity, msg, Toast.LENGTH_SHORT).show()
206+
if (isOnline) {
207+
isRestart = false
208+
delegateHttpFlv()
209+
} else {
210+
restartService()
211+
}
218212
}
219213
}
220214
}
@@ -248,18 +242,18 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
248242
}
249243

250244
override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
251-
Log.d(tag,"onSurfaceTextureAvailable")
245+
Log.d(tag, "onSurfaceTextureAvailable")
252246
surface?.let {
253247
this.surface = Surface(surface)
254248
player.setSurface(this.surface)
255249
}
256250
}
257251

258-
open fun speakAble(able: Boolean): Boolean {
252+
private fun speakAble(able: Boolean): Boolean {
259253
App.data.accessInfo?.let { accessInfo ->
260254
if (able) {
261-
var command = Command.getNvrIpcStatus(presenter.getChannel(), 0)
262-
var repStatus = XP2P.postCommandRequestSync(
255+
val command = Command.getNvrIpcStatus(presenter.getChannel(), 0)
256+
val repStatus = XP2P.postCommandRequestSync(
263257
"${accessInfo.productId}/${presenter.getDeviceName()}",
264258
command.toByteArray(), command.toByteArray().size.toLong(), 2 * 1000 * 1000
265259
) ?: ""
@@ -314,7 +308,7 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
314308
radio_record.setOnCheckedChangeListener { buttonView, isChecked ->
315309
if (isChecked) {
316310
filePath = CommonUtils.generateFileDefaultPath()
317-
var ret = player.startRecord(filePath)
311+
val ret = player.startRecord(filePath)
318312
if (ret != 0) {
319313
ToastDialog(
320314
this,
@@ -330,7 +324,7 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
330324
}
331325
}
332326
radio_playback.setOnClickListener {
333-
var dev = DevInfo()
327+
val dev = DevInfo()
334328
dev.DeviceName = presenter.getDeviceName()
335329
VideoPlaybackActivity.startPlaybackActivity(this@VideoPreviewActivity, dev)
336330
}
@@ -548,7 +542,7 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
548542
}
549543

550544
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
551-
Log.d(tag,"onSurfaceTextureSizeChanged")
545+
Log.d(tag, "onSurfaceTextureSizeChanged")
552546
val layoutParams = v_preview.layoutParams
553547
if (orientationV) {
554548
layoutParams.width = (player.videoWidth * (screenWidth * 16 / 9)) / player.videoHeight
@@ -561,7 +555,7 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
561555
}
562556

563557
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
564-
Log.d(tag,"onSurfaceTextureDestroyed")
558+
Log.d(tag, "onSurfaceTextureDestroyed")
565559
return false
566560
}
567561

@@ -682,7 +676,7 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
682676
}
683677
}
684678

685-
private fun getDeviceStatus(id: String?): Int {
679+
private fun getDeviceStatus(id: String?, block: ((Boolean, String) -> Unit)? = null) {
686680
var command: ByteArray? = null
687681
when (tv_video_quality.text.toString()) {
688682
getString(R.string.video_quality_high_str) -> {
@@ -711,50 +705,23 @@ class VideoPreviewActivity : VideoPreviewBaseActivity(), EventView,
711705
// 405 connect number too many
712706
// 406 current command don't support
713707
// 407 device process error
708+
var deviceState: Int = -1
709+
var msg: String = ""
714710
if (deviceStatuses.isNotEmpty()) {
715-
runOnUiThread {
716-
when (deviceStatuses[0].status) {
717-
0 -> Toast.makeText(this, "设备状态正常", Toast.LENGTH_SHORT).show()
718-
1 -> Toast.makeText(
719-
this,
720-
"设备状态异常, 拒绝请求: $reponse",
721-
Toast.LENGTH_SHORT
722-
).show()
723-
724-
404 -> Toast.makeText(
725-
this,
726-
"设备状态异常, error request message: $reponse",
727-
Toast.LENGTH_SHORT
728-
).show()
729-
730-
405 -> Toast.makeText(
731-
this,
732-
"设备状态异常, connect number too many: $reponse",
733-
Toast.LENGTH_SHORT
734-
).show()
735-
736-
406 -> Toast.makeText(
737-
this,
738-
"设备状态异常, current command don't support: $reponse",
739-
Toast.LENGTH_SHORT
740-
).show()
741-
742-
407 -> Toast.makeText(
743-
this,
744-
"设备状态异常, device process error: $reponse",
745-
Toast.LENGTH_SHORT
746-
).show()
747-
}
711+
msg = when (deviceStatuses[0].status) {
712+
0 -> "设备状态正常"
713+
404 -> "设备状态异常, error request message: $reponse"
714+
405 -> "设备状态异常, connect number too many: $reponse"
715+
406 -> "设备状态异常, current command don't support: $reponse"
716+
407 -> "设备状态异常, device process error: $reponse"
717+
else -> "设备状态异常, 拒绝请求: $reponse"
748718
}
749-
return deviceStatuses[0].status
719+
deviceState = deviceStatuses[0].status
750720
} else {
751-
runOnUiThread {
752-
Toast.makeText(this, "获取设备状态失败", Toast.LENGTH_SHORT).show()
753-
}
754-
return -1
721+
msg = "获取设备状态失败"
755722
}
723+
block?.invoke(deviceState == 0, msg)
756724
}
757-
return -1
758725
}
759726

760727
private val mHandler = MyHandler(this)

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/preview/VideoTestActivity.kt

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.tencent.iot.explorer.link.demo.video.playback.VideoPlaybackActivity
3030
import com.tencent.iot.explorer.link.demo.video.utils.ListOptionsDialog
3131
import com.tencent.iot.explorer.link.demo.video.utils.TipToastDialog
3232
import com.tencent.iot.explorer.link.demo.video.utils.ToastDialog
33+
import com.tencent.iot.video.link.entity.DeviceStatus
3334
import com.tencent.iot.video.link.util.audio.AudioRecordUtil
3435
import com.tencent.xnet.XP2P
3536
import com.tencent.xnet.XP2PAppConfig
@@ -182,22 +183,16 @@ class VideoTestActivity : VideoBaseActivity(), XP2PCallback, CoroutineScope by M
182183
private fun checkDeviceState() {
183184
Log.d(tag, "====检测设备状态===")
184185
launch(Dispatchers.IO) {
185-
val command = "action=user_define&channel=${channel}&cmd=device_state"
186-
val retContent = XP2P.postCommandRequestSync(
187-
"${productId}/${deviceName}",
188-
command.toByteArray(), command.toByteArray().size.toLong(), 1 * 1000 * 1000
189-
) ?: ""
190-
Log.d(tag, "device_state back content:$retContent")
191-
192-
// if (retContent.isEmpty()) {
193-
// launch(Dispatchers.Main) {
194-
// restartService()
195-
// }
196-
// } else {
197-
launch(Dispatchers.Main) {
198-
delegateHttpFlv()
186+
getDeviceStatus("${productId}/${deviceName}") { isOnline, msg ->
187+
launch(Dispatchers.Main) {
188+
Toast.makeText(this@VideoTestActivity, msg, Toast.LENGTH_SHORT).show()
189+
if (isOnline) {
190+
delegateHttpFlv()
191+
} else {
192+
restartService()
193+
}
194+
}
199195
}
200-
// }
201196
}
202197
}
203198

@@ -527,8 +522,8 @@ class VideoTestActivity : VideoBaseActivity(), XP2PCallback, CoroutineScope by M
527522
connectTime = System.currentTimeMillis() - connectStartTime
528523
if (event == 1004) {
529524
Log.e(tag, "====event === 1004")
530-
// checkDeviceState()
531-
delegateHttpFlv()
525+
checkDeviceState()
526+
// delegateHttpFlv()
532527
}
533528
} else if (event == 1010) {
534529
Log.e(tag, "====event === 1010, 校验失败,info撞库防止串流: $msg")
@@ -603,6 +598,54 @@ class VideoTestActivity : VideoBaseActivity(), XP2PCallback, CoroutineScope by M
603598
override fun onInfoAudioPcmData(p0: IMediaPlayer?, p1: ByteArray?, p2: Int) {
604599
}
605600

601+
private fun getDeviceStatus(id: String?, block: ((Boolean, String) -> Unit)? = null) {
602+
var command: ByteArray? = null
603+
when (tv_video_quality.text.toString()) {
604+
getString(R.string.video_quality_high_str) -> {
605+
command =
606+
"action=inner_define&channel=0&cmd=get_device_st&type=live&quality=super".toByteArray()
607+
}
608+
609+
getString(R.string.video_quality_medium_str) -> {
610+
command =
611+
"action=inner_define&channel=0&cmd=get_device_st&type=live&quality=high".toByteArray()
612+
}
613+
614+
getString(R.string.video_quality_low_str) -> {
615+
command =
616+
"action=inner_define&channel=0&cmd=get_device_st&type=live&quality=standard".toByteArray()
617+
}
618+
}
619+
val reponse =
620+
XP2P.postCommandRequestSync(id, command, command!!.size.toLong(), 2 * 1000 * 1000)
621+
if (!TextUtils.isEmpty(reponse)) {
622+
val deviceStatuses: List<DeviceStatus> =
623+
JSONArray.parseArray(reponse, DeviceStatus::class.java)
624+
// 0 接收请求
625+
// 1 拒绝请求
626+
// 404 error request message
627+
// 405 connect number too many
628+
// 406 current command don't support
629+
// 407 device process error
630+
var deviceState: Int = -1
631+
var msg: String = ""
632+
if (deviceStatuses.isNotEmpty()) {
633+
msg = when (deviceStatuses[0].status) {
634+
0 -> "设备状态正常"
635+
404 -> "设备状态异常, error request message: $reponse"
636+
405 -> "设备状态异常, connect number too many: $reponse"
637+
406 -> "设备状态异常, current command don't support: $reponse"
638+
407 -> "设备状态异常, device process error: $reponse"
639+
else -> "设备状态异常, 拒绝请求: $reponse"
640+
}
641+
deviceState = deviceStatuses[0].status
642+
} else {
643+
msg = "获取设备状态失败"
644+
}
645+
block?.invoke(deviceState == 0, msg)
646+
}
647+
}
648+
606649
private val mHandler = MyHandler(this)
607650

608651
private class MyHandler(activity: VideoTestActivity) : Handler() {

0 commit comments

Comments
 (0)