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
f848c1da
Commit
f848c1da
authored
Mar 07, 2023
by
马乐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.注册初步验证OK
2.鉴权初步验证OK 3.心跳OK
parent
02a36148
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
187 additions
and
135 deletions
+187
-135
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+3
-0
MainActivity.kt
app/src/main/java/com/waytous/anticollision/MainActivity.kt
+13
-0
MyApp.kt
app/src/main/java/com/waytous/anticollision/MyApp.kt
+9
-3
Session.kt
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
+85
-59
SyncMessageListener.kt
...java/com/waytous/anticollision/tcp/SyncMessageListener.kt
+4
-3
SyncParser.kt
...src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
+11
-7
TcpManager.kt
...src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
+39
-38
HomeFragment.kt
...in/java/com/waytous/anticollision/ui/home/HomeFragment.kt
+20
-19
PreferenceDelegate.kt
...ava/com/waytous/anticollision/utils/PreferenceDelegate.kt
+3
-3
fragment_home.xml
app/src/main/res/layout/fragment_home.xml
+0
-3
No files found.
app/src/main/AndroidManifest.xml
View file @
f848c1da
...
...
@@ -3,7 +3,9 @@
xmlns:tools=
"http://schemas.android.com/tools"
>
<uses-permission
android:name=
"android.permission.READ_PRIVILEGED_PHONE_STATE"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<application
android:name=
".MyApp"
android:allowBackup=
"true"
android:dataExtractionRules=
"@xml/data_extraction_rules"
android:fullBackupContent=
"@xml/backup_rules"
...
...
@@ -11,6 +13,7 @@
android:label=
"@string/app_name"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:supportsRtl=
"true"
android:usesCleartextTraffic=
"true"
android:theme=
"@style/Theme.HTAnticollision"
tools:targetApi=
"31"
>
<activity
...
...
app/src/main/java/com/waytous/anticollision/MainActivity.kt
View file @
f848c1da
...
...
@@ -8,7 +8,11 @@ import androidx.navigation.ui.setupWithNavController
import
com.blankj.utilcode.util.Utils
import
com.google.android.material.bottomnavigation.BottomNavigationView
import
com.gyf.immersionbar.ImmersionBar
import
com.waytous.anticollision.config.DeviceConfig
import
com.waytous.anticollision.databinding.ActivityMainBinding
import
com.waytous.anticollision.tcp.Session
import
io.github.toggery.jt808.messagebody.B8001
import
kotlin.concurrent.thread
class
MainActivity
:
AppCompatActivity
()
{
...
...
@@ -25,6 +29,15 @@ class MainActivity : AppCompatActivity() {
binding
=
ActivityMainBinding
.
inflate
(
layoutInflater
)
setContentView
(
binding
.
root
)
initNavView
()
DeviceConfig
.
province
=
11
DeviceConfig
.
city
=
0
DeviceConfig
.
phoneNumber
=
"18801011111"
DeviceConfig
.
HostConfig
.
host
=
"192.168.9.48"
DeviceConfig
.
HostConfig
.
port
=
6608
val
session
=
Session
()
thread
{
session
.
login
()
}
}
private
fun
initNavView
(){
...
...
app/src/main/java/com/waytous/anticollision/MyApp.kt
View file @
f848c1da
...
...
@@ -5,10 +5,9 @@ import com.blankj.utilcode.util.LogUtils
import
com.blankj.utilcode.util.SPStaticUtils
import
com.blankj.utilcode.util.SPUtils
import
com.blankj.utilcode.util.Utils
import
kotlin.properties.Delegates
object
MyApp
:
Application
()
{
lateinit
var
instance
:
MyApp
class
MyApp
:
Application
()
{
override
fun
onCreate
()
{
super
.
onCreate
()
...
...
@@ -17,4 +16,10 @@ object MyApp: Application() {
LogUtils
.
getConfig
().
globalTag
=
"Waytous"
SPStaticUtils
.
setDefaultSPUtils
(
SPUtils
.
getInstance
(
"settings"
))
}
companion
object
{
//情况一:声明可空的属性
private
var
instance
:
MyApp
by
Delegates
.
notNull
()
fun
instance
()
=
instance
}
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
View file @
f848c1da
...
...
@@ -4,15 +4,9 @@ import com.blankj.utilcode.util.DeviceUtils
import
com.waytous.anticollision.config.DeviceConfig
import
com.waytous.anticollision.config.Settings
import
com.waytous.anticollision.listener.SessionListener
import
com.waytous.anticollision.utils.NamedThreadFactory
import
com.waytous.anticollision.utils.SignInStatus
import
com.waytous.anticollision.utils.logd
import
com.waytous.anticollision.utils.loge
import
com.waytous.anticollision.utils.*
import
io.github.toggery.jt808.codec.*
import
io.github.toggery.jt808.messagebody.B0100
import
io.github.toggery.jt808.messagebody.B0102
import
io.github.toggery.jt808.messagebody.B8001
import
io.github.toggery.jt808.messagebody.HexUtil
import
io.github.toggery.jt808.messagebody.*
import
io.netty.buffer.ByteBuf
import
io.netty.buffer.UnpooledByteBufAllocator
import
io.netty.util.DefaultAttributeMap
...
...
@@ -36,14 +30,17 @@ internal enum class DeviceStatus {
* 未注册
* */
Unregistered
,
/**
* 注册中
* */
Registering
,
/**
* 已册中
* */
Registered
,
/**
* 未鉴权
* */
...
...
@@ -60,13 +57,13 @@ internal enum class DeviceStatus {
Authenticated
}
internal
class
Schedule
{
internal
class
Schedule
{
companion
object
{
const
val
tcpReadIntervalSecs
:
Long
=
2
// 2 seconds
const
val
tcpConnectTimeoutSecs
=
40
// 40 seconds
const
val
signInTimeoutSecs
:
Long
=
60
// 60 seconds
const
val
heartbeatInterval
=
120
// 120 seconds
companion
object
{
const
val
tcpReadIntervalSecs
:
Long
=
2
// 2 seconds
const
val
tcpConnectTimeoutSecs
=
40
// 40 seconds
const
val
signInTimeoutSecs
:
Long
=
60
// 60 seconds
const
val
heartbeatInterval
=
120
// 120 seconds
}
}
...
...
@@ -74,7 +71,7 @@ internal class Schedule{
* jt808网络会话管理
* @author male
* */
internal
class
Session
:
ConnectListener
,
SyncMessageListener
<
Codec
<*>
>
{
internal
class
Session
:
ConnectListener
,
SyncMessageListener
<
AbstractToStringJoiner
>
{
/**
* 消息内容解析器
...
...
@@ -117,10 +114,10 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
}
private
val
scheduler
by
lazy
{
Executors
.
newScheduledThreadPool
(
1
,
NamedThreadFactory
(
"Heartbeat"
))
Executors
.
newScheduledThreadPool
(
1
,
NamedThreadFactory
(
"Heartbeat"
))
}
private
var
scheduleFutureTask
:
ScheduledFuture
<*>?
=
null
private
var
scheduleFutureTask
:
ScheduledFuture
<*>?
=
null
private
val
mOperateLock
by
lazy
{
ReentrantLock
()
...
...
@@ -145,13 +142,13 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 设备尝试登录
* */
private
fun
tryLogin
(
block
:
()->
Boolean
)
{
private
fun
tryLogin
(
block
:
()
->
Boolean
)
{
var
retryTimes
=
0
var
success
:
Boolean
var
success
:
Boolean
do
{
success
=
block
()
retryTimes
++
}
while
(
retryTimes
<
CONNECT_MAX_RETRY
&&
success
)
}
while
(
retryTimes
<
CONNECT_MAX_RETRY
&&
!
success
)
if
(!
success
)
{
handleDisconnect
(
Error
.
JT808EncodeError
)
}
...
...
@@ -160,9 +157,9 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 错误处理逻辑
* */
private
fun
handleDisconnect
(
error
:
Error
){
when
(
error
)
{
Error
.
IOError
,
Error
.
StreamClosed
,
Error
.
Timeout
,
Error
.
NotConnected
->
{
private
fun
handleDisconnect
(
error
:
Error
)
{
when
(
error
)
{
Error
.
IOError
,
Error
.
StreamClosed
,
Error
.
Timeout
,
Error
.
NotConnected
->
{
}
else
->
{}
...
...
@@ -172,11 +169,11 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 设备登录
* */
fun
login
():
SignInStatus
{
fun
login
():
SignInStatus
{
mOperateLock
.
lock
()
try
{
deviceStatus
.
set
(
DeviceStatus
.
Registering
)
tcpManager
.
connect
(
DeviceConfig
.
HostConfig
.
host
,
DeviceConfig
.
HostConfig
.
port
,
Schedule
.
tcpConnectTimeoutSecs
)
tcpManager
.
connect
(
DeviceConfig
.
HostConfig
.
host
,
DeviceConfig
.
HostConfig
.
port
,
Schedule
.
tcpConnectTimeoutSecs
)
return
if
(
mOperateCondition
.
await
(
Schedule
.
signInTimeoutSecs
,
TimeUnit
.
SECONDS
))
{
when
(
deviceStatus
.
get
())
{
DeviceStatus
.
Authenticated
->
{
...
...
@@ -189,10 +186,10 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
}
else
{
SignInStatus
.
SignOut
}
}
catch
(
e
:
InterruptedException
)
{
}
catch
(
e
:
InterruptedException
)
{
loge
(
"登录线程被中断,被中断线程:${Thread.currentThread().name},中断线程:${e.printStackTrace()}"
)
return
SignInStatus
.
SignOut
}
finally
{
}
finally
{
mOperateLock
.
unlock
()
}
}
...
...
@@ -200,8 +197,8 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 添加监听器
* */
fun
addSessionListener
(
listener
:
SessionListener
)
{
synchronized
(
listeners
){
fun
addSessionListener
(
listener
:
SessionListener
)
{
synchronized
(
listeners
)
{
listeners
.
add
(
listener
)
}
}
...
...
@@ -209,26 +206,50 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 删除监听器
* */
fun
removeSessionListener
(
listener
:
SessionListener
)
{
synchronized
(
listeners
){
fun
removeSessionListener
(
listener
:
SessionListener
)
{
synchronized
(
listeners
)
{
listeners
.
remove
(
listener
)
}
}
private
fun
notifyConnectStatus
(
status
:
ConnectStatus
)
{
if
(
deviceStatus
.
get
()
!=
DeviceStatus
.
Authenticated
)
{
return
}
synchronized
(
listeners
)
{
listeners
.
forEach
{
it
.
onConnectStatusChanged
(
status
)
}
}
}
private
fun
notifySignInStatus
(
status
:
SignInStatus
,
error
:
Error
)
{
synchronized
(
listeners
)
{
listeners
.
forEach
{
if
(
status
==
SignInStatus
.
SignIn
)
{
it
.
onUserSignIn
()
}
else
{
it
.
onUserSignOut
(
error
)
}
}
}
}
/**
* 设备鉴权
* */
private
fun
doAuthenticate
():
Boolean
{
private
fun
doAuthenticate
():
Boolean
{
val
b0102
=
B0102
().
apply
{
token
=
Settings
.
token
}
val
request
=
Message
.
of
(
0
x010
0
,
b0102
).
apply
{
val
request
=
Message
.
of
(
0
x010
2
,
b0102
).
apply
{
simNo
=
DeviceConfig
.
phoneNumber
}
Message
.
encode
(
request
,
MessageMetadata
.
inbounds
(),
attributeMap
,
UnpooledByteBufAllocator
.
DEFAULT
::
buffer
,
buffs
::
add
)
deviceStatus
.
set
(
DeviceStatus
.
Authenticating
)
return
doSendMessage
(
"设备鉴权"
)
}
...
...
@@ -259,14 +280,19 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 开始心跳
* */
private
fun
startHeartbeat
(){
scheduleFutureTask
=
scheduler
.
scheduleAtFixedRate
(
::
sendPing
,
0
,
Schedule
.
tcpReadIntervalSecs
,
TimeUnit
.
SECONDS
)
private
fun
startHeartbeat
()
{
scheduleFutureTask
=
scheduler
.
scheduleAtFixedRate
(
::
sendPing
,
0
,
Schedule
.
tcpReadIntervalSecs
,
TimeUnit
.
SECONDS
)
}
/**
* 结束心跳
* */
private
fun
stopHeartbeat
(){
private
fun
stopHeartbeat
()
{
if
(
scheduleFutureTask
?.
isDone
==
false
)
{
scheduleFutureTask
?.
cancel
(
true
)
}
...
...
@@ -275,7 +301,7 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 发送心跳
* */
private
fun
sendPing
(){
private
fun
sendPing
()
{
val
request
=
Message
.
of
(
0
x0002
).
apply
{
simNo
=
DeviceConfig
.
phoneNumber
}
...
...
@@ -289,12 +315,12 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
/**
* 发送心跳
* */
private
fun
doSendMessage
(
name
:
String
)
=
if
(
buffs
.
isNotEmpty
())
{
private
fun
doSendMessage
(
name
:
String
)
=
if
(
buffs
.
isNotEmpty
())
{
val
byteArray
=
ByteArray
(
buffs
[
0
].
readableBytes
())
buffs
[
0
].
readBytes
(
byteArray
)
logd
(
"【$name】:${HexUtil.dump(byteArray)}"
)
tcpManager
.
send
(
byteArray
)
synchronized
(
buffs
){
synchronized
(
buffs
)
{
buffs
.
removeAt
(
0
)
}
true
...
...
@@ -308,64 +334,64 @@ internal class Session : ConnectListener, SyncMessageListener<Codec<*>> {
logd
(
error
.
reason
)
}
override
fun
onSignUp
(
data
:
Codec
<*>
)
{
override
fun
onSignUp
(
payload
:
AbstractToStringJoiner
)
{
Settings
.
deviceId
=
DeviceUtils
.
getUniqueDeviceId
()
Settings
.
isRegistered
=
true
Settings
.
token
=
(
data
as
B8100Codec
).
newInstance
(
).
token
Settings
.
token
=
(
payload
as
B8100
).
token
deviceStatus
.
set
(
DeviceStatus
.
Registered
)
tryLogin
(
::
doAuthenticate
)
}
override
fun
onCommonResponse
(
data
:
Codec
<*>
)
{
val
codec
=
(
data
as
B8001Codec
).
newInstance
(
)
override
fun
onCommonResponse
(
payload
:
AbstractToStringJoiner
)
{
val
codec
=
(
payload
as
B8001
)
when
(
codec
.
replyId
)
{
0
x0100
->{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
0
x0100
->
{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
deviceStatus
.
set
(
DeviceStatus
.
Registered
)
loge
(
"设备注册成功!"
)
}
B8001
.
RESULT_FAILED
->
{
B8001
.
RESULT_FAILED
->
{
deviceStatus
.
set
(
DeviceStatus
.
Unregistered
)
loge
(
"设备注册失败!"
)
}
B8001
.
RESULT_WRONG
->
{
B8001
.
RESULT_WRONG
->
{
deviceStatus
.
set
(
DeviceStatus
.
Unregistered
)
loge
(
"设备注册消息有误!"
)
}
}
}
0
x0102
->{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
0
x0102
->
{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
if
(
deviceStatus
.
get
()
==
DeviceStatus
.
Authenticating
)
{
deviceStatus
.
set
(
DeviceStatus
.
Authenticated
)
startHeartbeat
()
loge
(
"设备鉴权成功!"
)
}
else
{
}
else
{
deviceStatus
.
set
(
DeviceStatus
.
UnAuthenticated
)
loge
(
"设备鉴权失败!"
)
}
}
B8001
.
RESULT_FAILED
->
{
B8001
.
RESULT_FAILED
->
{
deviceStatus
.
set
(
DeviceStatus
.
UnAuthenticated
)
loge
(
"设备鉴权失败!"
)
}
B8001
.
RESULT_WRONG
->
{
B8001
.
RESULT_WRONG
->
{
deviceStatus
.
set
(
DeviceStatus
.
UnAuthenticated
)
loge
(
"设备鉴权消息有误!"
)
}
}
}
0
x0002
->
{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
0
x0002
->
{
when
(
codec
.
result
)
{
B8001
.
RESULT_SUCCESSFUL
->
{
logd
(
"心跳响应成功"
)
}
B8001
.
RESULT_FAILED
->
{
B8001
.
RESULT_FAILED
->
{
loge
(
"心跳响应失败!"
)
}
B8001
.
RESULT_WRONG
->
{
B8001
.
RESULT_WRONG
->
{
loge
(
"心跳消息有误!"
)
}
}
...
...
app/src/main/java/com/waytous/anticollision/tcp/SyncMessageListener.kt
View file @
f848c1da
...
...
@@ -3,17 +3,17 @@ package com.waytous.anticollision.tcp
/**
* 消息同步监听器
* */
interface
SyncMessageListener
<
T
>
{
interface
SyncMessageListener
<
in
T
>
{
/**
* 设备注册
* @param data
* */
fun
onSignUp
(
data
:
T
)
fun
onSignUp
(
payload
:
T
)
/**
* 平台通用应答
* @param data
* */
fun
onCommonResponse
(
data
:
T
)
fun
onCommonResponse
(
payload
:
T
)
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
View file @
f848c1da
...
...
@@ -3,7 +3,12 @@ package com.waytous.anticollision.tcp
import
com.blankj.utilcode.util.LogUtils
import
com.waytous.anticollision.BuildConfig
import
com.waytous.anticollision.utils.logd
import
io.github.toggery.jt808.codec.*
import
io.github.toggery.jt808.codec.Codec
import
io.github.toggery.jt808.codec.Message
import
io.github.toggery.jt808.codec.MessageMetadata
import
io.github.toggery.jt808.messagebody.AbstractToStringJoiner
import
io.github.toggery.jt808.messagebody.B8001
import
io.github.toggery.jt808.messagebody.B8100
import
io.netty.buffer.ByteBuf
import
io.netty.util.DefaultAttributeMap
import
io.netty.util.ReferenceCountUtil
...
...
@@ -13,7 +18,7 @@ import io.netty.util.ReferenceCountUtil
*
* @author male
* */
class
SyncParser
(
private
val
syncMessageListener
:
SyncMessageListener
<
Codec
<*>
>)
{
class
SyncParser
(
private
val
syncMessageListener
:
SyncMessageListener
<
AbstractToStringJoiner
>)
{
/**
* 解析字节流
...
...
@@ -21,13 +26,12 @@ class SyncParser(private val syncMessageListener: SyncMessageListener<Codec<*>>)
* */
fun
parse
(
buf
:
ByteBuf
){
try
{
val
message
:
Message
<
Codec
<*>
>
=
Message
.
decode
(
buf
,
MessageMetadata
.
outbounds
(),
DefaultAttributeMap
())
val
message
:
Message
<
AbstractToStringJoiner
>
=
Message
.
decode
(
buf
,
MessageMetadata
.
outbounds
(),
DefaultAttributeMap
())
logd
(
"【解析】:$message"
)
when
(
val
codec
:
Codec
<*>
=
message
.
body
){
is
B8
001Codec
->
syncMessageListener
.
onCommonResponse
(
codec
)
is
B8
100Codec
->
syncMessageListener
.
onSignUp
(
codec
)
when
(
val
payload
:
Any
=
message
.
body
){
is
B8
100
->
syncMessageListener
.
onSignUp
(
payload
)
is
B8
001
->
syncMessageListener
.
onCommonResponse
(
payload
)
}
LogUtils
.
d
(
message
.
toString
())
}
catch
(
e
:
Exception
)
{
if
(
BuildConfig
.
DEBUG
)
{
LogUtils
.
e
(
"parse received data error:${e.message}"
)
...
...
app/src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
View file @
f848c1da
...
...
@@ -142,10 +142,12 @@ internal class TcpManager(
}
val
(
errCode
,
length
)
=
connection
.
receive
()
if
(
errCode
==
Error
.
NOError
)
{
val
buf
=
UnpooledByteBufAllocator
.
DEFAULT
.
buffer
(
length
)
buf
.
writeBytes
(
connection
.
buffer
,
0
,
length
)
connectListener
.
onDataReceived
(
buf
)
connection
.
buffer
.
fill
(
0
,
0
,
length
-
1
)
if
(
length
>
0
)
{
val
buf
=
UnpooledByteBufAllocator
.
DEFAULT
.
buffer
(
length
)
buf
.
writeBytes
(
connection
.
buffer
,
0
,
length
)
connectListener
.
onDataReceived
(
buf
)
connection
.
buffer
.
fill
(
0
,
0
,
length
-
1
)
}
receiveExecutor
.
execute
{
receive
(
timeout
)
}
}
else
{
mCancel
.
set
(
true
)
...
...
@@ -212,40 +214,39 @@ internal class TcpManager(
* @param timeout
* @return Error
* */
fun
connect
(
host
:
String
,
port
:
Int
,
timeout
:
Int
,
receiveInterval
:
Int
=
2
):
Error
=
try
{
receiveLock
.
lock
()
sendLock
.
lock
()
var
errorCode
:
Error
=
Error
.
NOError
if
(
socket
.
isConnected
&&
connectStatus
.
get
()
>
State
.
Disconnected
)
{
errorCode
=
Error
.
NOError
}
else
{
closeSocket
()
fun
connect
(
host
:
String
,
port
:
Int
,
timeout
:
Int
,
receiveInterval
:
Int
=
2
):
Error
{
try
{
receiveLock
.
lock
()
sendLock
.
lock
()
if
(
socket
.
isConnected
&&
connectStatus
.
get
()
>
State
.
Disconnected
)
{
return
Error
.
NOError
}
connectStatus
.
set
(
State
.
Connecting
)
socket
.
soTimeout
=
receiveInterval
socket
.
connect
(
InetSocketAddress
(
host
,
port
),
if
(
timeout
>
0
)
timeout
else
DEFAULT_TIMEOUT
)
inputStream
=
socket
.
getInputStream
()
outputStream
=
socket
.
getOutputStream
()
connectStatus
.
set
(
State
.
Connected
)
return
Error
.
NOError
}
catch
(
e
:
IOException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
return
Error
.
IOError
}
catch
(
e
:
SocketTimeoutException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
return
Error
.
Timeout
}
catch
(
e
:
IllegalArgumentException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
return
Error
.
InvalidParam
}
catch
(
e
:
Exception
)
{
connectStatus
.
set
(
State
.
Disconnected
)
return
Error
.
ServerUnknownError
}
finally
{
sendLock
.
unlock
()
receiveLock
.
unlock
()
}
connectStatus
.
set
(
State
.
Connecting
)
socket
.
soTimeout
=
receiveInterval
socket
.
connect
(
InetSocketAddress
(
host
,
port
),
if
(
timeout
>
0
)
timeout
else
DEFAULT_TIMEOUT
)
inputStream
=
socket
.
getInputStream
()
outputStream
=
socket
.
getOutputStream
()
connectStatus
.
set
(
State
.
Connected
)
errorCode
}
catch
(
e
:
IOException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
IOError
}
catch
(
e
:
SocketTimeoutException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
Timeout
}
catch
(
e
:
IllegalArgumentException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
InvalidParam
}
catch
(
e
:
Exception
)
{
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
ServerUnknownError
}
finally
{
sendLock
.
unlock
()
receiveLock
.
unlock
()
}
/**
...
...
@@ -292,7 +293,7 @@ internal class TcpManager(
Error
.
NotConnected
,
0
)
if
(
inputStream
.
available
()
<
0
)
return
ReceivedResult
(
Error
.
NOError
,
0
)
if
(
inputStream
.
available
()
<
=
0
)
return
ReceivedResult
(
Error
.
NOError
,
0
)
val
len
=
inputStream
.
read
(
buffer
)
return
if
(
len
>
0
)
{
ReceivedResult
(
Error
.
NOError
,
len
)
...
...
app/src/main/java/com/waytous/anticollision/ui/home/HomeFragment.kt
View file @
f848c1da
...
...
@@ -37,31 +37,31 @@ class HomeFragment : Fragment() {
_binding
=
FragmentHomeBinding
.
inflate
(
inflater
,
container
,
false
)
val
root
:
View
=
binding
.
root
//
val mapView: MapView = binding.mapView
val
mapView
=
MapView
(
this
.
context
!!
)
val
mapView
:
MapView
=
binding
.
mapView
//
val mapView = MapView(this.context!!)
mapView
.
getMapboxMap
().
setCamera
(
CameraOptions
.
Builder
()
.
center
(
Point
.
fromLngLat
(
LATITUDE
,
LONGITUDE
))
//
.center(
//
Point.fromLngLat(
//
LATITUDE,
//
LONGITUDE
//
))
.
zoom
(
ZOOM
).
build
()
)
mapView
.
getMapboxMap
().
loadStyle
(
style
(
Style
.
MAPBOX_STREET
S
){
mapView
.
getMapboxMap
().
loadStyle
(
style
(
Style
.
OUTDOOR
S
){
+
geoJsonSource
(
GEOJSON_SOURCE_ID
)
{
url
(
"multiple_geometry_example.geojson"
)
}
+
lineLayer
(
"linelayer"
,
GEOJSON_SOURCE_ID
)
{
lineCap
(
LineCap
.
ROUND
)
lineJoin
(
LineJoin
.
ROUND
)
lineOpacity
(
0.7
)
lineWidth
(
8.0
)
lineColor
(
"#888"
)
url
(
"asset://multiple_geometry_example.geojson"
)
}
// +lineLayer("linelayer", GEOJSON_SOURCE_ID) {
// lineCap(LineCap.ROUND)
// lineJoin(LineJoin.ROUND)
// lineOpacity(0.7)
// lineWidth(8.0)
// lineColor("#888")
// }
})
return
mapView
return
root
}
override
fun
onDestroyView
()
{
...
...
@@ -71,8 +71,8 @@ class HomeFragment : Fragment() {
companion
object
{
private
const
val
GEOJSON_SOURCE_ID
=
"geo-json"
private
const
val
LATITUDE
=
-
122.483696
private
const
val
LONGITUDE
=
3
7.83381
8
private
const
val
LATITUDE
=
-
77.0911931991577
private
const
val
LONGITUDE
=
3
8.9565388617423
8
private
const
val
ZOOM
=
14.0
}
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/utils/PreferenceDelegate.kt
View file @
f848c1da
...
...
@@ -8,9 +8,9 @@ import kotlin.reflect.KProperty
class
PreferenceDelegate
<
T
>(
private
val
name
:
String
,
private
val
default
:
T
,
private
val
prefName
:
String
=
"settings"
)
:
ReadWriteProperty
<
Any
?,
T
>
{
private
val
prefs
:
SharedPreferences
by
lazy
{
MyApp
.
instance
.
applicationContext
.
getSharedPreferences
(
prefName
,
Context
.
MODE_PRIVATE
)
}
private
val
prefs
:
SharedPreferences
=
MyApp
.
instance
().
applicationContext
.
getSharedPreferences
(
prefName
,
Context
.
MODE_PRIVATE
)
@Synchronized
override
fun
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<*>):
T
{
...
...
app/src/main/res/layout/fragment_home.xml
View file @
f848c1da
...
...
@@ -11,9 +11,6 @@
android:id=
"@+id/mapView"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
mapbox:mapbox_cameraTargetLat=
"40.7128"
mapbox:mapbox_cameraTargetLng=
"-74.0060"
mapbox:mapbox_cameraZoom=
"9.0"
mapbox:mapbox_resourcesAccessToken=
"@string/map_box_public_key"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
...
...
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