Commit c86a727b authored by 马乐's avatar 马乐

1.修改日志方式

2.优化tcp通信
parent 2144d4c6
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:name=".MyApp" android:name=".MyApp"
android:allowBackup="true" android:allowBackup="true"
......
...@@ -34,4 +34,9 @@ object Settings { ...@@ -34,4 +34,9 @@ object Settings {
* */ * */
var token: String by PreferenceDelegate("token", "") var token: String by PreferenceDelegate("token", "")
/**
* 心跳周期
* */
var heartbeatInterval:Long by PreferenceDelegate("heartbeatInterval", 2)
} }
\ No newline at end of file
package com.waytous.anticollision.tcp package com.waytous.anticollision.tcp
import com.blankj.utilcode.util.DeviceUtils import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.LogUtils
import com.waytous.anticollision.BuildConfig
import com.waytous.anticollision.config.DeviceConfig import com.waytous.anticollision.config.DeviceConfig
import com.waytous.anticollision.config.Settings import com.waytous.anticollision.config.Settings
import com.waytous.anticollision.listener.SessionListener import com.waytous.anticollision.listener.SessionListener
...@@ -187,7 +189,11 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo ...@@ -187,7 +189,11 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo
SignInStatus.SignOut SignInStatus.SignOut
} }
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
loge("登录线程被中断,被中断线程:${Thread.currentThread().name},中断线程:${e.printStackTrace()}") if (BuildConfig.DEBUG) {
LogUtils.e("登录线程被中断,被中断线程:${Thread.currentThread().name},中断线程:${e.printStackTrace()}")
} else {
LogUtils.file("登录线程被中断,被中断线程:${Thread.currentThread().name},中断线程:${e.printStackTrace()}")
}
return SignInStatus.SignOut return SignInStatus.SignOut
} finally { } finally {
mOperateLock.unlock() mOperateLock.unlock()
...@@ -285,7 +291,7 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo ...@@ -285,7 +291,7 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo
::sendPing, ::sendPing,
0, 0,
Schedule.tcpReadIntervalSecs, Schedule.tcpReadIntervalSecs,
TimeUnit.SECONDS TimeUnit.MINUTES
) )
} }
...@@ -318,20 +324,32 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo ...@@ -318,20 +324,32 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo
private fun doSendMessage(name: String) = if (buffs.isNotEmpty()) { private fun doSendMessage(name: String) = if (buffs.isNotEmpty()) {
val byteArray = ByteArray(buffs[0].readableBytes()) val byteArray = ByteArray(buffs[0].readableBytes())
buffs[0].readBytes(byteArray) buffs[0].readBytes(byteArray)
logd("【$name】:${HexUtil.dump(byteArray)}") if (BuildConfig.DEBUG) {
LogUtils.d("【$name】:${HexUtil.dump(byteArray)}")
} else {
LogUtils.file("【$name】:${HexUtil.dump(byteArray)}")
}
tcpManager.send(byteArray) tcpManager.send(byteArray)
synchronized(buffs) { synchronized(buffs) {
buffs.removeAt(0) buffs.removeAt(0)
} }
true true
} else { } else {
loge("【$name】:buffs is empty,Message encode failed!") if (BuildConfig.DEBUG) {
LogUtils.e("【$name】:buffs is empty,Message encode failed!")
} else {
LogUtils.file("【$name】:buffs is empty,Message encode failed!")
}
false false
} }
override fun onDisconnect(error: Error) { override fun onDisconnect(error: Error) {
stopHeartbeat() stopHeartbeat()
logd(error.reason) if (BuildConfig.DEBUG) {
LogUtils.e(error.reason)
} else {
LogUtils.file(error.reason)
}
} }
override fun onSignUp(payload: AbstractToStringJoiner) { override fun onSignUp(payload: AbstractToStringJoiner) {
...@@ -349,15 +367,27 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo ...@@ -349,15 +367,27 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo
when (codec.result) { when (codec.result) {
B8001.RESULT_SUCCESSFUL -> { B8001.RESULT_SUCCESSFUL -> {
deviceStatus.set(DeviceStatus.Registered) deviceStatus.set(DeviceStatus.Registered)
loge("设备注册成功!") if (BuildConfig.DEBUG) {
LogUtils.d("设备注册成功!")
} else {
LogUtils.file("设备注册成功!")
}
} }
B8001.RESULT_FAILED -> { B8001.RESULT_FAILED -> {
deviceStatus.set(DeviceStatus.Unregistered) deviceStatus.set(DeviceStatus.Unregistered)
loge("设备注册失败!") if (BuildConfig.DEBUG) {
LogUtils.e("设备注册失败!")
} else {
LogUtils.file("设备注册失败!")
}
} }
B8001.RESULT_WRONG -> { B8001.RESULT_WRONG -> {
deviceStatus.set(DeviceStatus.Unregistered) deviceStatus.set(DeviceStatus.Unregistered)
loge("设备注册消息有误!") if (BuildConfig.DEBUG) {
LogUtils.e("设备注册消息有误!")
} else {
LogUtils.file("设备注册消息有误!")
}
} }
} }
} }
...@@ -367,32 +397,60 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo ...@@ -367,32 +397,60 @@ internal class Session : ConnectListener, SyncMessageListener<AbstractToStringJo
if (deviceStatus.get() == DeviceStatus.Authenticating) { if (deviceStatus.get() == DeviceStatus.Authenticating) {
deviceStatus.set(DeviceStatus.Authenticated) deviceStatus.set(DeviceStatus.Authenticated)
startHeartbeat() startHeartbeat()
loge("设备鉴权成功!") if (BuildConfig.DEBUG) {
LogUtils.d("设备鉴权成功!")
} else {
LogUtils.file("设备鉴权成功!")
}
} else { } else {
deviceStatus.set(DeviceStatus.UnAuthenticated) deviceStatus.set(DeviceStatus.UnAuthenticated)
loge("设备鉴权失败!") if (BuildConfig.DEBUG) {
LogUtils.e("设备鉴权失败!")
} else {
LogUtils.file("设备鉴权失败!")
}
} }
} }
B8001.RESULT_FAILED -> { B8001.RESULT_FAILED -> {
deviceStatus.set(DeviceStatus.UnAuthenticated) deviceStatus.set(DeviceStatus.UnAuthenticated)
loge("设备鉴权失败!") if (BuildConfig.DEBUG) {
LogUtils.e("设备鉴权失败!")
} else {
LogUtils.file("设备鉴权失败!")
}
} }
B8001.RESULT_WRONG -> { B8001.RESULT_WRONG -> {
deviceStatus.set(DeviceStatus.UnAuthenticated) deviceStatus.set(DeviceStatus.UnAuthenticated)
loge("设备鉴权消息有误!") if (BuildConfig.DEBUG) {
LogUtils.e("设备鉴权消息有误!")
} else {
LogUtils.file("设备鉴权消息有误!")
}
} }
} }
} }
0x0002 -> { 0x0002 -> {
when (codec.result) { when (codec.result) {
B8001.RESULT_SUCCESSFUL -> { B8001.RESULT_SUCCESSFUL -> {
logd("心跳响应成功") if (BuildConfig.DEBUG) {
LogUtils.d("心跳响应成功!")
} else {
LogUtils.file("心跳响应成功!")
}
} }
B8001.RESULT_FAILED -> { B8001.RESULT_FAILED -> {
loge("心跳响应失败!") if (BuildConfig.DEBUG) {
LogUtils.e("心跳响应失败!")
} else {
LogUtils.file("心跳响应失败!")
}
} }
B8001.RESULT_WRONG -> { B8001.RESULT_WRONG -> {
loge("心跳消息有误!") if (BuildConfig.DEBUG) {
LogUtils.e("心跳消息有误!")
} else {
LogUtils.file("心跳消息有误!")
}
} }
} }
} }
......
...@@ -2,8 +2,6 @@ package com.waytous.anticollision.tcp ...@@ -2,8 +2,6 @@ package com.waytous.anticollision.tcp
import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.LogUtils
import com.waytous.anticollision.BuildConfig import com.waytous.anticollision.BuildConfig
import com.waytous.anticollision.utils.logd
import io.github.toggery.jt808.codec.Codec
import io.github.toggery.jt808.codec.Message import io.github.toggery.jt808.codec.Message
import io.github.toggery.jt808.codec.MessageMetadata import io.github.toggery.jt808.codec.MessageMetadata
import io.github.toggery.jt808.messagebody.AbstractToStringJoiner import io.github.toggery.jt808.messagebody.AbstractToStringJoiner
...@@ -27,7 +25,11 @@ class SyncParser(private val syncMessageListener: SyncMessageListener<AbstractTo ...@@ -27,7 +25,11 @@ class SyncParser(private val syncMessageListener: SyncMessageListener<AbstractTo
fun parse(buf: ByteBuf){ fun parse(buf: ByteBuf){
try { try {
val message: Message<AbstractToStringJoiner> = Message.decode(buf, MessageMetadata.outbounds(), DefaultAttributeMap()) val message: Message<AbstractToStringJoiner> = Message.decode(buf, MessageMetadata.outbounds(), DefaultAttributeMap())
logd("【解析】:$message") if (BuildConfig.DEBUG) {
LogUtils.d("【解析】:$message")
} else {
LogUtils.file("【解析】:$message")
}
when(val payload:Any = message.body){ when(val payload:Any = message.body){
is B8100 -> syncMessageListener.onSignUp(payload) is B8100 -> syncMessageListener.onSignUp(payload)
is B8001 -> syncMessageListener.onCommonResponse(payload) is B8001 -> syncMessageListener.onCommonResponse(payload)
......
...@@ -279,7 +279,8 @@ internal class TcpManager( ...@@ -279,7 +279,8 @@ internal class TcpManager(
} }
private fun closeSocket() { private fun closeSocket() {
socket.close() socket.shutdownInput()
socket.shutdownOutput()
} }
/** /**
......
package com.waytous.anticollision.utils package com.waytous.anticollision.utils
import com.blankj.utilcode.util.LogUtils
import com.waytous.anticollision.BuildConfig
/** /**
* 连接状态 * 连接状态
* */ * */
...@@ -16,23 +13,3 @@ enum class ConnectStatus { ...@@ -16,23 +13,3 @@ enum class ConnectStatus {
enum class SignInStatus { enum class SignInStatus {
SignOut, SignIn SignOut, SignIn
} }
fun logd(message:String){
if (BuildConfig.DEBUG) {
if (BuildConfig.DEBUG) {
LogUtils.d(message)
} else {
LogUtils.file(message)
}
}
}
fun loge(message:String){
if (BuildConfig.DEBUG) {
if (BuildConfig.DEBUG) {
LogUtils.e(message)
} else {
LogUtils.file(message)
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment