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.TrafficReqInfo
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import io.reactivex.rxjava3.core.Observable
import retrofit2.http.Body
import retrofit2.http.GET
@ -23,50 +24,62 @@ import retrofit2.http.Query
class DevicesApi : BaseObserve<DevicesApi.Api>(Api::class.java) {
fun heartBeat(sn: String) {
observe(api.heartBeat(sn), object : RequestNoResultCallBack<BaseResponse>() {
override fun onError(error: String?) {
}
override fun onComplete() {
}
})
if (SimHelper.isNetOk()) {
observe(api.heartBeat(sn), object : RequestNoResultCallBack<BaseResponse>() {
override fun onError(error: String?) {
}
override fun onComplete() {
}
})
}
}
fun getPushUrl(sn: String, callBack: RequestResultCallBack<GetRtspUrlResponse>) {
observe(api.getPushUrl(sn), callBack)
if (SimHelper.isNetOk()) {
observe(api.getPushUrl(sn), callBack)
}
}
fun updateTrafficInfo() {
val trafficReqInfo = TrafficReqInfo()
observe(api.updateTrafficInfo(trafficReqInfo), null)
if (SimHelper.isNetOk()) {
val trafficReqInfo = TrafficReqInfo()
observe(api.updateTrafficInfo(trafficReqInfo), null)
}
}
suspend fun uploadGasData(
gasList: List<GasReqBean> = emptyList()
): BaseResponse? {
try {
val gasReq = GasRequest(detections = gasList)
return api.uploadGasData(gasReq)
} catch (e: Exception) {
e.printStackTrace()
if (SimHelper.isNetOk()) {
try {
val gasReq = GasRequest(detections = gasList)
return api.uploadGasData(gasReq)
} catch (e: Exception) {
e.printStackTrace()
}
}
return null
}
suspend fun getDetectionLastTime(): LastTimeResponse? {
try {
return api.getDetectionLastTime(LztekUtil.getSn())
} catch (e: Exception) {
e.printStackTrace()
if (SimHelper.isNetOk()) {
try {
return api.getDetectionLastTime(LztekUtil.getSn())
} catch (e: Exception) {
e.printStackTrace()
}
}
return null
}
suspend fun getVideoLastTime(): LastTimeResponse? {
try {
return api.getVideoLastTime(LztekUtil.getSn())
} catch (e: Exception) {
e.printStackTrace()
if (SimHelper.isNetOk()) {
try {
return api.getVideoLastTime(LztekUtil.getSn())
} catch (e: Exception) {
e.printStackTrace()
}
}
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.net.DevicesApi
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"
@ -33,6 +38,7 @@ class HeartbeatService : IntentService("HeartbeatService") {
@SuppressLint("HardwareIds")
private fun handleActionStart(intent: Intent) {
heartConter++
SimHelper.setNetStatus(isOnline())
// 心跳
devicesApi.heartBeat(LztekUtil.getSn())
// 获取推流地址
@ -78,4 +84,17 @@ class HeartbeatService : IntentService("HeartbeatService") {
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.net.DevicesApi
import com.yinuo.safetywatcher.watcher.net.UploadFileApi
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
@ -52,6 +53,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener
}
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog()
GlobalScope.launch(Dispatchers.IO) {
uploadSensorData()
@ -63,6 +68,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener
}
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog()
GlobalScope.launch(Dispatchers.IO) {
uploadVideo()
@ -74,6 +83,10 @@ class CloudActivity : NoOptionsActivity() {
showToast(getString(R.string.repeat_click_sync_tip))
return@setOnClickListener
}
if (!SimHelper.isNetOk()){
showToast(getString(R.string.net_off))
return@setOnClickListener
}
showLoadingDialog()
uploadOnece()
}

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

@ -6,12 +6,14 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.TrafficStats
import android.telephony.PhoneStateListener
import android.telephony.SignalStrength
import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import android.util.Log
import com.common.commonlib.utils.LogUtils
object SimHelper {
@ -36,8 +38,8 @@ object SimHelper {
}
fun init(context: Context) {
watchSimState(context)
watchSimRssi(context)
// watchSimState(context)
// watchSimRssi(context)
}
private fun watchSimRssi(context: Context) {
@ -106,4 +108,42 @@ object SimHelper {
}
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_off">通道关闭</string>
<string name="net_off">无网络连接,请稍后再试!</string>
</resources>

Loading…
Cancel
Save