Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
HTAnticollision
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
马乐
HTAnticollision
Commits
c86a727b
Commit
c86a727b
authored
Mar 13, 2023
by
马乐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.修改日志方式
2.优化tcp通信
parent
2144d4c6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
43 deletions
+88
-43
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+2
-0
Settings.kt
...rc/main/java/com/waytous/anticollision/config/Settings.kt
+6
-0
Session.kt
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
+73
-15
SyncParser.kt
...src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
+5
-3
TcpManager.kt
...src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
+2
-1
Extensions.kt
...c/main/java/com/waytous/anticollision/utils/Extensions.kt
+0
-24
No files found.
app/src/main/AndroidManifest.xml
View file @
c86a727b
...
@@ -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"
...
...
app/src/main/java/com/waytous/anticollision/config/Settings.kt
View file @
c86a727b
...
@@ -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
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
View file @
c86a727b
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
.
SECOND
S
TimeUnit
.
MINUTE
S
)
)
}
}
...
@@ -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
(
"设备鉴权消息有误!"
)
}
}
}
}
}
}
}
0
x0002
->
{
0
x0002
->
{
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
(
"心跳消息有误!"
)
}
}
}
}
}
}
}
...
...
app/src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
View file @
c86a727b
...
@@ -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
)
...
...
app/src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
View file @
c86a727b
...
@@ -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
()
}
}
/**
/**
...
...
app/src/main/java/com/waytous/anticollision/utils/Extensions.kt
View file @
c86a727b
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment