desc:有无网络通过socket连接公网8.8.8.8DNS服务来判断,通过收发包的快慢模拟信号变化

main
xiaowusky 1 year ago
parent 841f27e6de
commit d855210a2e

@ -10,6 +10,7 @@ import com.yinuo.safetywatcher.watcher.bean.GetRtspUrlResponse
import com.yinuo.safetywatcher.watcher.bean.LastTimeResponse import com.yinuo.safetywatcher.watcher.bean.LastTimeResponse
import com.yinuo.safetywatcher.watcher.bean.TrafficReqInfo import com.yinuo.safetywatcher.watcher.bean.TrafficReqInfo
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.SimHelper
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
@ -23,50 +24,62 @@ import retrofit2.http.Query
class DevicesApi : BaseObserve<DevicesApi.Api>(Api::class.java) { class DevicesApi : BaseObserve<DevicesApi.Api>(Api::class.java) {
fun heartBeat(sn: String) { fun heartBeat(sn: String) {
observe(api.heartBeat(sn), object : RequestNoResultCallBack<BaseResponse>() { if (SimHelper.isNetOk()) {
override fun onError(error: String?) { observe(api.heartBeat(sn), object : RequestNoResultCallBack<BaseResponse>() {
} override fun onError(error: String?) {
}
override fun onComplete() {
} override fun onComplete() {
}) }
})
}
} }
fun getPushUrl(sn: String, callBack: RequestResultCallBack<GetRtspUrlResponse>) { fun getPushUrl(sn: String, callBack: RequestResultCallBack<GetRtspUrlResponse>) {
observe(api.getPushUrl(sn), callBack) if (SimHelper.isNetOk()) {
observe(api.getPushUrl(sn), callBack)
}
} }
fun updateTrafficInfo() { fun updateTrafficInfo() {
val trafficReqInfo = TrafficReqInfo() if (SimHelper.isNetOk()) {
observe(api.updateTrafficInfo(trafficReqInfo), null) val trafficReqInfo = TrafficReqInfo()
observe(api.updateTrafficInfo(trafficReqInfo), null)
}
} }
suspend fun uploadGasData( suspend fun uploadGasData(
gasList: List<GasReqBean> = emptyList() gasList: List<GasReqBean> = emptyList()
): BaseResponse? { ): BaseResponse? {
try { if (SimHelper.isNetOk()) {
val gasReq = GasRequest(detections = gasList) try {
return api.uploadGasData(gasReq) val gasReq = GasRequest(detections = gasList)
} catch (e: Exception) { return api.uploadGasData(gasReq)
e.printStackTrace() } catch (e: Exception) {
e.printStackTrace()
}
} }
return null return null
} }
suspend fun getDetectionLastTime(): LastTimeResponse? { suspend fun getDetectionLastTime(): LastTimeResponse? {
try { if (SimHelper.isNetOk()) {
return api.getDetectionLastTime(LztekUtil.getSn()) try {
} catch (e: Exception) { return api.getDetectionLastTime(LztekUtil.getSn())
e.printStackTrace() } catch (e: Exception) {
e.printStackTrace()
}
} }
return null return null
} }
suspend fun getVideoLastTime(): LastTimeResponse? { suspend fun getVideoLastTime(): LastTimeResponse? {
try { if (SimHelper.isNetOk()) {
return api.getVideoLastTime(LztekUtil.getSn()) try {
} catch (e: Exception) { return api.getVideoLastTime(LztekUtil.getSn())
e.printStackTrace() } catch (e: Exception) {
e.printStackTrace()
}
} }
return null return null
} }

@ -9,6 +9,11 @@ import com.yinuo.safetywatcher.watcher.bean.GetRtspUrlResponse
import com.yinuo.safetywatcher.watcher.constant.HEARTBEAT_TIME import com.yinuo.safetywatcher.watcher.constant.HEARTBEAT_TIME
import com.yinuo.safetywatcher.watcher.net.DevicesApi import com.yinuo.safetywatcher.watcher.net.DevicesApi
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import java.net.SocketAddress
private const val ACTION_START = "com.yinuo.safetywatcher.watcher.services.action.Heartbeat" private const val ACTION_START = "com.yinuo.safetywatcher.watcher.services.action.Heartbeat"
@ -33,6 +38,7 @@ class HeartbeatService : IntentService("HeartbeatService") {
@SuppressLint("HardwareIds") @SuppressLint("HardwareIds")
private fun handleActionStart(intent: Intent) { private fun handleActionStart(intent: Intent) {
heartConter++ heartConter++
SimHelper.setNetStatus(isOnline())
// 心跳 // 心跳
devicesApi.heartBeat(LztekUtil.getSn()) devicesApi.heartBeat(LztekUtil.getSn())
// 获取推流地址 // 获取推流地址
@ -78,4 +84,17 @@ class HeartbeatService : IntentService("HeartbeatService") {
context.startService(intent) context.startService(intent)
} }
} }
fun isOnline(): Boolean {
return try {
val timeoutMs = 1500
val sock = Socket()
val sockaddr: SocketAddress = InetSocketAddress("8.8.8.8", 53)
sock.connect(sockaddr, timeoutMs)
sock.close()
true
} catch (e: IOException) {
false
}
}
} }

@ -14,6 +14,7 @@ import com.yinuo.safetywatcher.watcher.bean.GasReqBean
import com.yinuo.safetywatcher.watcher.constant.GAS_CLOUD_UPLOAD_SIZE_ONCE import com.yinuo.safetywatcher.watcher.constant.GAS_CLOUD_UPLOAD_SIZE_ONCE
import com.yinuo.safetywatcher.watcher.net.DevicesApi import com.yinuo.safetywatcher.watcher.net.DevicesApi
import com.yinuo.safetywatcher.watcher.net.UploadFileApi import com.yinuo.safetywatcher.watcher.net.UploadFileApi
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.showToast import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -52,6 +53,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip)) showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener return@setOnClickListener
} }
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog() showLoadingDialog()
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
uploadSensorData() uploadSensorData()
@ -63,6 +68,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip)) showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener return@setOnClickListener
} }
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog() showLoadingDialog()
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
uploadVideo() uploadVideo()
@ -74,6 +83,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip)) showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener return@setOnClickListener
} }
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog() showLoadingDialog()
uploadOnece() uploadOnece()
} }

@ -95,17 +95,21 @@ class CommonTopBar : LinearLayout {
private val simCallback = object : SimHelper.OnSimLevelCallback { private val simCallback = object : SimHelper.OnSimLevelCallback {
override fun onEnable(enable: Boolean) { override fun onEnable(enable: Boolean) {
Log.i(this@CommonTopBar.javaClass.name, "simCallback onEnable = $enable") Log.i(this@CommonTopBar.javaClass.name, "simCallback onEnable = $enable")
if (enable) { mBinding?.fourG?.post {
mBinding?.fourG?.visibility = VISIBLE if (enable) {
} else { mBinding?.fourG?.visibility = VISIBLE
mBinding?.fourG?.visibility = GONE } else {
mBinding?.fourG?.visibility = GONE
}
} }
} }
override fun onLevel(level: Int) { override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "simCallback onLevel = $level") Log.i(this@CommonTopBar.javaClass.name, "simCallback onLevel = $level")
val showLevel = (level + 1) * 20 mBinding?.fourG?.post {
mBinding?.fourG?.setImageLevel(showLevel) val showLevel = (level + 1) * 20
mBinding?.fourG?.setImageLevel(showLevel)
}
} }
} }

@ -6,12 +6,14 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.TrafficStats
import android.telephony.PhoneStateListener import android.telephony.PhoneStateListener
import android.telephony.SignalStrength import android.telephony.SignalStrength
import android.telephony.SubscriptionInfo import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import android.util.Log import android.util.Log
import com.common.commonlib.utils.LogUtils
object SimHelper { object SimHelper {
@ -36,8 +38,8 @@ object SimHelper {
} }
fun init(context: Context) { fun init(context: Context) {
watchSimState(context) // watchSimState(context)
watchSimRssi(context) // watchSimRssi(context)
} }
private fun watchSimRssi(context: Context) { private fun watchSimRssi(context: Context) {
@ -106,4 +108,42 @@ object SimHelper {
} }
return null return null
} }
private var isNetOK = false
fun setNetStatus(status: Boolean) {
LogUtils.w("setNetStatus $status")
isNetOK = status
val netSpeed = getNetSpeed()
callbacks.forEach {
it.onEnable(status)
it.onLevel(netSpeed)
}
}
fun isNetOk(): Boolean {
return isNetOK
}
var lastTime = 0L
var lastTxBytes = 0L;
var lastRxBytes = 0L;
private fun getNetSpeed(): Int {
val totalTxBytes = TrafficStats.getTotalTxBytes()
val totalRxBytes = TrafficStats.getTotalRxBytes()
val currentTimeMillis = System.currentTimeMillis()
val timeOffset = currentTimeMillis - lastTime
val rxOffset = totalRxBytes - lastRxBytes
val txOffset = totalTxBytes - lastTxBytes
Log.i(
"setNetStatus",
"txbyte = $totalTxBytes rxbyte = $totalRxBytes timeOffset = $timeOffset"
)
lastTime = currentTimeMillis
lastRxBytes = totalRxBytes
lastTxBytes = totalTxBytes
return ((rxOffset + txOffset) / timeOffset).toInt()
}
} }

@ -90,4 +90,6 @@
<string name="sensor_status_on">通道开启</string> <string name="sensor_status_on">通道开启</string>
<string name="sensor_status_off">通道关闭</string> <string name="sensor_status_off">通道关闭</string>
<string name="net_off">无网络连接,请稍后再试!</string>
</resources> </resources>

Loading…
Cancel
Save