Commit f848c1da authored by 马乐's avatar 马乐

1.注册初步验证OK

2.鉴权初步验证OK 3.心跳OK
parent 02a36148
......@@ -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
......
......@@ -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(){
......
......@@ -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
......@@ -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
......@@ -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 B8001Codec -> syncMessageListener.onCommonResponse(codec)
is B8100Codec -> syncMessageListener.onSignUp(codec)
when(val payload:Any = message.body){
is B8100 -> syncMessageListener.onSignUp(payload)
is B8001 -> syncMessageListener.onCommonResponse(payload)
}
LogUtils.d(message.toString())
} catch (e: Exception) {
if (BuildConfig.DEBUG) {
LogUtils.e("parse received data error:${e.message}")
......
......@@ -142,10 +142,12 @@ internal class TcpManager(
}
val (errCode, length) = connection.receive()
if (errCode == Error.NOError) {
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,14 +214,12 @@ internal class TcpManager(
* @param timeout
* @return Error
* */
fun connect(host: String, port: Int, timeout: Int, receiveInterval: Int = 2): Error = try {
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()
return Error.NOError
}
connectStatus.set(State.Connecting)
socket.soTimeout = receiveInterval
......@@ -230,23 +230,24 @@ internal class TcpManager(
inputStream = socket.getInputStream()
outputStream = socket.getOutputStream()
connectStatus.set(State.Connected)
errorCode
return Error.NOError
} catch (e: IOException) {
connectStatus.set(State.Disconnected)
Error.IOError
return Error.IOError
} catch (e: SocketTimeoutException) {
connectStatus.set(State.Disconnected)
Error.Timeout
return Error.Timeout
} catch (e: IllegalArgumentException) {
connectStatus.set(State.Disconnected)
Error.InvalidParam
return Error.InvalidParam
} catch (e: Exception) {
connectStatus.set(State.Disconnected)
Error.ServerUnknownError
return 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)
......
......@@ -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_STREETS){
mapView.getMapboxMap().loadStyle(style(Style.OUTDOORS){
+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 = 37.833818
private const val LATITUDE = -77.0911931991577
private const val LONGITUDE = 38.95653886174238
private const val ZOOM = 14.0
}
}
\ No newline at end of file
......@@ -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 {
......
......@@ -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"
......
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