@@ -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 )
0 commit comments