diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt index 9d7c639..96a96b3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt @@ -1,14 +1,43 @@ package com.casic.br.operationsite.test.service +import android.app.NotificationChannel +import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.graphics.BitmapFactory import android.os.IBinder +import android.util.Base64 +import android.util.Log +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.test.R +import com.casic.br.operationsite.test.util.CurrentScene import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.websocket.WebSocketManager +import com.casic.br.operationsite.test.util.RuntimeCache +import com.casic.br.operationsite.test.view.GuardiansActivity +import com.casic.br.operationsite.test.view.SuppliesActivity +import com.pengxh.kt.lite.extensions.createImageFileDir +import com.pengxh.kt.lite.extensions.saveImage +import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.socket.web.OnWebSocketListener +import com.pengxh.kt.lite.utils.socket.web.WebSocketClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.Response +import okhttp3.WebSocket +import okio.ByteString +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale -class WebSocketService : Service() { +class WebSocketService : Service(), OnWebSocketListener { - private lateinit var socketManager: WebSocketManager + private val kTag = "WebSocketService" + private val notificationId = 10001 + private val webSocketClient by lazy { WebSocketClient(this) } + private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } + private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } + private var notificationBuilder: NotificationCompat.Builder? = null override fun onBind(intent: Intent?): IBinder? { return null @@ -16,19 +45,116 @@ override fun onCreate() { super.onCreate() - //初始化WebSocket - socketManager = WebSocketManager.Builder() - .setContext(this) - .setRemoteHost("ws://${LocaleConstant.AI_BASE_IP}:8765") - .needReconnect(true) - .setRetryIntervalTime(5000L) - .build() - socketManager.connect() + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "websocket_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for websocket connection service" + notificationManager.createNotificationChannel(channel) + notificationBuilder = NotificationCompat.Builder(this, "websocket_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("AI通讯服务连接中...") + .setContentText("为保证程序正常运行,请勿移除此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + //连接WebSocket + webSocketClient.start("ws://${LocaleConstant.AI_BASE_IP}:8765") + Log.d(kTag, "onStartCommand: WebSocketService") + return START_STICKY + } + + override fun onOpen(webSocket: WebSocket, response: Response) { + notificationBuilder?.setContentTitle("AI通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMessageResponse(webSocket: WebSocket, message: String) { + if (RuntimeCache.currentScene == null) { + return + } + + val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { + message.split(":")[1] + } else { + if (message.startsWith("alarm")) { + "" + } else { + message + } + } + + if (base64 != "") { + val bitmapArray = Base64.decode(base64, Base64.DEFAULT) + val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) + val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" + Log.d(kTag, "imageName: $imageName") + val imagePath = "/${createImageFileDir()}/$imageName" + bitmap.saveImage(imagePath) + + when (RuntimeCache.currentScene) { + CurrentScene.Supply -> { + val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + SuppliesActivity.weakReferenceHandler.sendMessage(msg) + } + + CurrentScene.Disclosure -> {} + + CurrentScene.Guardian -> { + val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + GuardiansActivity.weakReferenceHandler.sendMessage(msg) + } + + else -> { + CoroutineScope(Dispatchers.Main).launch { + "作业阶段错误,请注意流程".show(this@WebSocketService) + } + } + } + } + } + + override fun onMessageResponse(webSocket: WebSocket, bytes: ByteString) { + + } + + override fun onServerDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onClientDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onFailure(webSocket: WebSocket?, t: Throwable?) { + notificationBuilder?.setContentTitle("AI通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMaxRetryReached() { + } override fun onDestroy() { super.onDestroy() - //断开WebSocket - socketManager.close() + webSocketClient.stop() + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(kTag, "onDestroy: WebSocketService") } } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt index 9d7c639..96a96b3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt @@ -1,14 +1,43 @@ package com.casic.br.operationsite.test.service +import android.app.NotificationChannel +import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.graphics.BitmapFactory import android.os.IBinder +import android.util.Base64 +import android.util.Log +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.test.R +import com.casic.br.operationsite.test.util.CurrentScene import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.websocket.WebSocketManager +import com.casic.br.operationsite.test.util.RuntimeCache +import com.casic.br.operationsite.test.view.GuardiansActivity +import com.casic.br.operationsite.test.view.SuppliesActivity +import com.pengxh.kt.lite.extensions.createImageFileDir +import com.pengxh.kt.lite.extensions.saveImage +import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.socket.web.OnWebSocketListener +import com.pengxh.kt.lite.utils.socket.web.WebSocketClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.Response +import okhttp3.WebSocket +import okio.ByteString +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale -class WebSocketService : Service() { +class WebSocketService : Service(), OnWebSocketListener { - private lateinit var socketManager: WebSocketManager + private val kTag = "WebSocketService" + private val notificationId = 10001 + private val webSocketClient by lazy { WebSocketClient(this) } + private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } + private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } + private var notificationBuilder: NotificationCompat.Builder? = null override fun onBind(intent: Intent?): IBinder? { return null @@ -16,19 +45,116 @@ override fun onCreate() { super.onCreate() - //初始化WebSocket - socketManager = WebSocketManager.Builder() - .setContext(this) - .setRemoteHost("ws://${LocaleConstant.AI_BASE_IP}:8765") - .needReconnect(true) - .setRetryIntervalTime(5000L) - .build() - socketManager.connect() + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "websocket_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for websocket connection service" + notificationManager.createNotificationChannel(channel) + notificationBuilder = NotificationCompat.Builder(this, "websocket_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("AI通讯服务连接中...") + .setContentText("为保证程序正常运行,请勿移除此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + //连接WebSocket + webSocketClient.start("ws://${LocaleConstant.AI_BASE_IP}:8765") + Log.d(kTag, "onStartCommand: WebSocketService") + return START_STICKY + } + + override fun onOpen(webSocket: WebSocket, response: Response) { + notificationBuilder?.setContentTitle("AI通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMessageResponse(webSocket: WebSocket, message: String) { + if (RuntimeCache.currentScene == null) { + return + } + + val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { + message.split(":")[1] + } else { + if (message.startsWith("alarm")) { + "" + } else { + message + } + } + + if (base64 != "") { + val bitmapArray = Base64.decode(base64, Base64.DEFAULT) + val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) + val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" + Log.d(kTag, "imageName: $imageName") + val imagePath = "/${createImageFileDir()}/$imageName" + bitmap.saveImage(imagePath) + + when (RuntimeCache.currentScene) { + CurrentScene.Supply -> { + val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + SuppliesActivity.weakReferenceHandler.sendMessage(msg) + } + + CurrentScene.Disclosure -> {} + + CurrentScene.Guardian -> { + val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + GuardiansActivity.weakReferenceHandler.sendMessage(msg) + } + + else -> { + CoroutineScope(Dispatchers.Main).launch { + "作业阶段错误,请注意流程".show(this@WebSocketService) + } + } + } + } + } + + override fun onMessageResponse(webSocket: WebSocket, bytes: ByteString) { + + } + + override fun onServerDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onClientDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onFailure(webSocket: WebSocket?, t: Throwable?) { + notificationBuilder?.setContentTitle("AI通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMaxRetryReached() { + } override fun onDestroy() { super.onDestroy() - //断开WebSocket - socketManager.close() + webSocketClient.stop() + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(kTag, "onDestroy: WebSocketService") } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt deleted file mode 100644 index 03fa6b8..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import okhttp3.WebSocket - -interface IWebSocketListener { - fun onSocketOpened(webSocket: WebSocket) - - fun onMessageResponse(message: String) - - fun onSocketClosed(code: Int) - - fun onConnectFailed(t: Throwable) -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt index 9d7c639..96a96b3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt @@ -1,14 +1,43 @@ package com.casic.br.operationsite.test.service +import android.app.NotificationChannel +import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.graphics.BitmapFactory import android.os.IBinder +import android.util.Base64 +import android.util.Log +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.test.R +import com.casic.br.operationsite.test.util.CurrentScene import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.websocket.WebSocketManager +import com.casic.br.operationsite.test.util.RuntimeCache +import com.casic.br.operationsite.test.view.GuardiansActivity +import com.casic.br.operationsite.test.view.SuppliesActivity +import com.pengxh.kt.lite.extensions.createImageFileDir +import com.pengxh.kt.lite.extensions.saveImage +import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.socket.web.OnWebSocketListener +import com.pengxh.kt.lite.utils.socket.web.WebSocketClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.Response +import okhttp3.WebSocket +import okio.ByteString +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale -class WebSocketService : Service() { +class WebSocketService : Service(), OnWebSocketListener { - private lateinit var socketManager: WebSocketManager + private val kTag = "WebSocketService" + private val notificationId = 10001 + private val webSocketClient by lazy { WebSocketClient(this) } + private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } + private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } + private var notificationBuilder: NotificationCompat.Builder? = null override fun onBind(intent: Intent?): IBinder? { return null @@ -16,19 +45,116 @@ override fun onCreate() { super.onCreate() - //初始化WebSocket - socketManager = WebSocketManager.Builder() - .setContext(this) - .setRemoteHost("ws://${LocaleConstant.AI_BASE_IP}:8765") - .needReconnect(true) - .setRetryIntervalTime(5000L) - .build() - socketManager.connect() + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "websocket_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for websocket connection service" + notificationManager.createNotificationChannel(channel) + notificationBuilder = NotificationCompat.Builder(this, "websocket_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("AI通讯服务连接中...") + .setContentText("为保证程序正常运行,请勿移除此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + //连接WebSocket + webSocketClient.start("ws://${LocaleConstant.AI_BASE_IP}:8765") + Log.d(kTag, "onStartCommand: WebSocketService") + return START_STICKY + } + + override fun onOpen(webSocket: WebSocket, response: Response) { + notificationBuilder?.setContentTitle("AI通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMessageResponse(webSocket: WebSocket, message: String) { + if (RuntimeCache.currentScene == null) { + return + } + + val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { + message.split(":")[1] + } else { + if (message.startsWith("alarm")) { + "" + } else { + message + } + } + + if (base64 != "") { + val bitmapArray = Base64.decode(base64, Base64.DEFAULT) + val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) + val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" + Log.d(kTag, "imageName: $imageName") + val imagePath = "/${createImageFileDir()}/$imageName" + bitmap.saveImage(imagePath) + + when (RuntimeCache.currentScene) { + CurrentScene.Supply -> { + val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + SuppliesActivity.weakReferenceHandler.sendMessage(msg) + } + + CurrentScene.Disclosure -> {} + + CurrentScene.Guardian -> { + val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + GuardiansActivity.weakReferenceHandler.sendMessage(msg) + } + + else -> { + CoroutineScope(Dispatchers.Main).launch { + "作业阶段错误,请注意流程".show(this@WebSocketService) + } + } + } + } + } + + override fun onMessageResponse(webSocket: WebSocket, bytes: ByteString) { + + } + + override fun onServerDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onClientDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onFailure(webSocket: WebSocket?, t: Throwable?) { + notificationBuilder?.setContentTitle("AI通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMaxRetryReached() { + } override fun onDestroy() { super.onDestroy() - //断开WebSocket - socketManager.close() + webSocketClient.stop() + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(kTag, "onDestroy: WebSocketService") } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt deleted file mode 100644 index 03fa6b8..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import okhttp3.WebSocket - -interface IWebSocketListener { - fun onSocketOpened(webSocket: WebSocket) - - fun onMessageResponse(message: String) - - fun onSocketClosed(code: Int) - - fun onConnectFailed(t: Throwable) -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt deleted file mode 100644 index 3d045a7..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import android.util.Log -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import okhttp3.WebSocket -import okhttp3.WebSocketListener -import java.util.concurrent.TimeUnit - -class WebSocketClient { - private val kTag = "WebSocketClient" - private lateinit var webSocket: WebSocket - - fun connect(remoteHost: String, listener: IWebSocketListener) { - Log.d(kTag, "connect: $remoteHost") - val request = Request.Builder().url(remoteHost).build() - val httpClient by lazy { - OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .build() - } - httpClient.newWebSocket(request, object : WebSocketListener() { - override fun onOpen(webSocket: WebSocket, response: Response) { - super.onOpen(webSocket, response) - this@WebSocketClient.webSocket = webSocket - listener.onSocketOpened(webSocket) - } - - override fun onMessage(webSocket: WebSocket, text: String) { - super.onMessage(webSocket, text) - listener.onMessageResponse(text) - } - - override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { - super.onClosed(webSocket, code, reason) - listener.onSocketClosed(code) - } - - override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - super.onFailure(webSocket, t, response) - listener.onConnectFailed(t) - } - }) - httpClient.dispatcher.executorService.shutdown() - } - - fun disconnect() { - Log.d(kTag, "disconnect: 断开连接") - webSocket.close(1000, null) - } -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt index 9d7c639..96a96b3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt @@ -1,14 +1,43 @@ package com.casic.br.operationsite.test.service +import android.app.NotificationChannel +import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.graphics.BitmapFactory import android.os.IBinder +import android.util.Base64 +import android.util.Log +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.test.R +import com.casic.br.operationsite.test.util.CurrentScene import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.websocket.WebSocketManager +import com.casic.br.operationsite.test.util.RuntimeCache +import com.casic.br.operationsite.test.view.GuardiansActivity +import com.casic.br.operationsite.test.view.SuppliesActivity +import com.pengxh.kt.lite.extensions.createImageFileDir +import com.pengxh.kt.lite.extensions.saveImage +import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.socket.web.OnWebSocketListener +import com.pengxh.kt.lite.utils.socket.web.WebSocketClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.Response +import okhttp3.WebSocket +import okio.ByteString +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale -class WebSocketService : Service() { +class WebSocketService : Service(), OnWebSocketListener { - private lateinit var socketManager: WebSocketManager + private val kTag = "WebSocketService" + private val notificationId = 10001 + private val webSocketClient by lazy { WebSocketClient(this) } + private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } + private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } + private var notificationBuilder: NotificationCompat.Builder? = null override fun onBind(intent: Intent?): IBinder? { return null @@ -16,19 +45,116 @@ override fun onCreate() { super.onCreate() - //初始化WebSocket - socketManager = WebSocketManager.Builder() - .setContext(this) - .setRemoteHost("ws://${LocaleConstant.AI_BASE_IP}:8765") - .needReconnect(true) - .setRetryIntervalTime(5000L) - .build() - socketManager.connect() + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "websocket_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for websocket connection service" + notificationManager.createNotificationChannel(channel) + notificationBuilder = NotificationCompat.Builder(this, "websocket_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("AI通讯服务连接中...") + .setContentText("为保证程序正常运行,请勿移除此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + //连接WebSocket + webSocketClient.start("ws://${LocaleConstant.AI_BASE_IP}:8765") + Log.d(kTag, "onStartCommand: WebSocketService") + return START_STICKY + } + + override fun onOpen(webSocket: WebSocket, response: Response) { + notificationBuilder?.setContentTitle("AI通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMessageResponse(webSocket: WebSocket, message: String) { + if (RuntimeCache.currentScene == null) { + return + } + + val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { + message.split(":")[1] + } else { + if (message.startsWith("alarm")) { + "" + } else { + message + } + } + + if (base64 != "") { + val bitmapArray = Base64.decode(base64, Base64.DEFAULT) + val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) + val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" + Log.d(kTag, "imageName: $imageName") + val imagePath = "/${createImageFileDir()}/$imageName" + bitmap.saveImage(imagePath) + + when (RuntimeCache.currentScene) { + CurrentScene.Supply -> { + val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + SuppliesActivity.weakReferenceHandler.sendMessage(msg) + } + + CurrentScene.Disclosure -> {} + + CurrentScene.Guardian -> { + val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + GuardiansActivity.weakReferenceHandler.sendMessage(msg) + } + + else -> { + CoroutineScope(Dispatchers.Main).launch { + "作业阶段错误,请注意流程".show(this@WebSocketService) + } + } + } + } + } + + override fun onMessageResponse(webSocket: WebSocket, bytes: ByteString) { + + } + + override fun onServerDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onClientDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onFailure(webSocket: WebSocket?, t: Throwable?) { + notificationBuilder?.setContentTitle("AI通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMaxRetryReached() { + } override fun onDestroy() { super.onDestroy() - //断开WebSocket - socketManager.close() + webSocketClient.stop() + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(kTag, "onDestroy: WebSocketService") } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt deleted file mode 100644 index 03fa6b8..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import okhttp3.WebSocket - -interface IWebSocketListener { - fun onSocketOpened(webSocket: WebSocket) - - fun onMessageResponse(message: String) - - fun onSocketClosed(code: Int) - - fun onConnectFailed(t: Throwable) -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt deleted file mode 100644 index 3d045a7..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import android.util.Log -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import okhttp3.WebSocket -import okhttp3.WebSocketListener -import java.util.concurrent.TimeUnit - -class WebSocketClient { - private val kTag = "WebSocketClient" - private lateinit var webSocket: WebSocket - - fun connect(remoteHost: String, listener: IWebSocketListener) { - Log.d(kTag, "connect: $remoteHost") - val request = Request.Builder().url(remoteHost).build() - val httpClient by lazy { - OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .build() - } - httpClient.newWebSocket(request, object : WebSocketListener() { - override fun onOpen(webSocket: WebSocket, response: Response) { - super.onOpen(webSocket, response) - this@WebSocketClient.webSocket = webSocket - listener.onSocketOpened(webSocket) - } - - override fun onMessage(webSocket: WebSocket, text: String) { - super.onMessage(webSocket, text) - listener.onMessageResponse(text) - } - - override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { - super.onClosed(webSocket, code, reason) - listener.onSocketClosed(code) - } - - override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - super.onFailure(webSocket, t, response) - listener.onConnectFailed(t) - } - }) - httpClient.dispatcher.executorService.shutdown() - } - - fun disconnect() { - Log.d(kTag, "disconnect: 断开连接") - webSocket.close(1000, null) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt deleted file mode 100644 index 4eddb03..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt +++ /dev/null @@ -1,167 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import android.content.Context -import android.graphics.BitmapFactory -import android.os.Handler -import android.os.Looper -import android.util.Base64 -import android.util.Log -import com.casic.br.operationsite.test.util.CurrentScene -import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.RuntimeCache -import com.casic.br.operationsite.test.view.GuardiansActivity -import com.casic.br.operationsite.test.view.SuppliesActivity -import com.pengxh.kt.lite.extensions.createImageFileDir -import com.pengxh.kt.lite.extensions.saveImage -import com.pengxh.kt.lite.extensions.show -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import okhttp3.WebSocket -import java.text.SimpleDateFormat -import java.util.Date -import java.util.Locale - - -class WebSocketManager private constructor(builder: Builder) : IWebSocketListener { - - private val kTag = "WebSocketManager" - private val webSocketClient by lazy { WebSocketClient() } - private val retryHandler by lazy { Handler(Looper.getMainLooper()) } - private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } - private val context = builder.context - private val remoteHost = builder.remoteHost - private val need = builder.need - private val retryIntervalTime = builder.retryIntervalTime - private var isConnected = false - - class Builder { - lateinit var context: Context - lateinit var remoteHost: String - var need = true - var retryIntervalTime = 5000L - - fun setContext(context: Context): Builder { - this.context = context - return this - } - - /** - * ws://192.168.10.139:8765 - * */ - fun setRemoteHost(remoteHost: String): Builder { - this.remoteHost = remoteHost - return this - } - - fun needReconnect(need: Boolean): Builder { - this.need = need - return this - } - - fun setRetryIntervalTime(retryIntervalTime: Long): Builder { - this.retryIntervalTime = retryIntervalTime - return this - } - - fun build(): WebSocketManager { - return WebSocketManager(this) - } - } - - /** - * ws://192.168.10.139:8765 - * */ - fun connect() { - synchronized(this) { - CoroutineScope(Dispatchers.IO).launch { - if (!isConnected) { - webSocketClient.connect(remoteHost, this@WebSocketManager) - } else { - close() - } - } - } - } - - private val retryRunnable = Runnable { connect() } - - override fun onSocketOpened(webSocket: WebSocket) { - isConnected = true - CoroutineScope(Dispatchers.Main).launch { - "AI连接成功".show(context) - } - } - - override fun onMessageResponse(message: String) { - isConnected = true - if (RuntimeCache.currentScene == null) { - return - } - - val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { - message.split(":")[1] - } else { - if (message.startsWith("alarm")) { - "" - } else { - message - } - } - - if (base64 != "") { - val bitmapArray = Base64.decode(base64, Base64.DEFAULT) - val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) - val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" - Log.d(kTag, "imageName: $imageName") - val imagePath = "/${context.createImageFileDir()}/$imageName" - bitmap.saveImage(imagePath) - - when (RuntimeCache.currentScene) { - CurrentScene.Supply -> { - val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() - msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE - msg.obj = imagePath - SuppliesActivity.weakReferenceHandler.sendMessage(msg) - } - - CurrentScene.Disclosure -> {} - - CurrentScene.Guardian -> { - val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() - msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE - msg.obj = imagePath - GuardiansActivity.weakReferenceHandler.sendMessage(msg) - } - - else -> { - CoroutineScope(Dispatchers.Main).launch { - "作业阶段错误,请注意流程".show(context) - } - } - } - } - } - - override fun onSocketClosed(code: Int) { - isConnected = false - CoroutineScope(Dispatchers.Main).launch { - "AI连接关闭".show(context) - } - } - - override fun onConnectFailed(t: Throwable) { - isConnected = false - CoroutineScope(Dispatchers.Main).launch { - "AI连接失败,正在重连...".show(context) - } - if (need) { - retryHandler.postDelayed(retryRunnable, retryIntervalTime) - } - } - - fun close() { - webSocketClient.disconnect() - retryHandler.removeCallbacks(retryRunnable) - } -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb07476..c8488a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,7 +60,9 @@ - + diff --git a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt b/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt deleted file mode 100644 index 86cf991..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/model/Point.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.casic.br.operationsite.test.model - -data class Point(val x: Float, val y: Float) diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt index 337aaf1..40275e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/SocketConnectionService.kt @@ -25,7 +25,7 @@ override fun handleMessage(msg: Message): Boolean { if (!tcpClient.isRunning()) { - "数据通讯服务断开连接,指令发送失败".show(this) + "指令通讯服务断开连接,指令发送失败".show(this) return true } @@ -51,7 +51,7 @@ } private val kTag = "SocketService" - private val notificationId = 1 + private val notificationId = 10000 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null @@ -70,7 +70,7 @@ notificationManager.createNotificationChannel(channel) notificationBuilder = NotificationCompat.Builder(this, "socket_connection_service_channel") .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("数据通讯服务连接中...") + .setContentTitle("指令通讯服务连接中...") .setContentText("为保证程序正常运行,请勿移除此通知") .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 .setOngoing(true) @@ -84,28 +84,29 @@ if (weakReferenceHandler == null) { weakReferenceHandler = WeakReferenceHandler(this) } + //连接TCP tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) Log.d(kTag, "onStartCommand: SocketConnectionService") return START_STICKY } override fun onConnected() { - notificationBuilder?.setContentTitle("数据通讯服务已连接") - "数据通讯服务连接成功,现在可以下发指令了".show(this) + notificationBuilder?.setContentTitle("指令通讯服务已连接") + "指令通讯服务连接成功,现在可以下发指令了".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onDisconnected() { - notificationBuilder?.setContentTitle("数据通讯服务断开连接") - "数据通讯服务断开连接".show(this) + notificationBuilder?.setContentTitle("指令通讯服务断开连接") + "指令通讯服务断开连接".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } override fun onConnectFailed() { - notificationBuilder?.setContentTitle("数据通讯服务连接出错,开始重连") - "数据通讯服务连接出错,开始重连".show(this) + notificationBuilder?.setContentTitle("指令通讯服务连接出错,开始重连") + "指令通讯服务连接出错,开始重连".show(this) val notification = notificationBuilder?.build() notificationManager.notify(notificationId, notification) } diff --git a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt index 9d7c639..96a96b3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/service/WebSocketService.kt @@ -1,14 +1,43 @@ package com.casic.br.operationsite.test.service +import android.app.NotificationChannel +import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.graphics.BitmapFactory import android.os.IBinder +import android.util.Base64 +import android.util.Log +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.test.R +import com.casic.br.operationsite.test.util.CurrentScene import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.websocket.WebSocketManager +import com.casic.br.operationsite.test.util.RuntimeCache +import com.casic.br.operationsite.test.view.GuardiansActivity +import com.casic.br.operationsite.test.view.SuppliesActivity +import com.pengxh.kt.lite.extensions.createImageFileDir +import com.pengxh.kt.lite.extensions.saveImage +import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.socket.web.OnWebSocketListener +import com.pengxh.kt.lite.utils.socket.web.WebSocketClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.Response +import okhttp3.WebSocket +import okio.ByteString +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale -class WebSocketService : Service() { +class WebSocketService : Service(), OnWebSocketListener { - private lateinit var socketManager: WebSocketManager + private val kTag = "WebSocketService" + private val notificationId = 10001 + private val webSocketClient by lazy { WebSocketClient(this) } + private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } + private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } + private var notificationBuilder: NotificationCompat.Builder? = null override fun onBind(intent: Intent?): IBinder? { return null @@ -16,19 +45,116 @@ override fun onCreate() { super.onCreate() - //初始化WebSocket - socketManager = WebSocketManager.Builder() - .setContext(this) - .setRemoteHost("ws://${LocaleConstant.AI_BASE_IP}:8765") - .needReconnect(true) - .setRetryIntervalTime(5000L) - .build() - socketManager.connect() + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "websocket_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for websocket connection service" + notificationManager.createNotificationChannel(channel) + notificationBuilder = NotificationCompat.Builder(this, "websocket_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("AI通讯服务连接中...") + .setContentText("为保证程序正常运行,请勿移除此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + //连接WebSocket + webSocketClient.start("ws://${LocaleConstant.AI_BASE_IP}:8765") + Log.d(kTag, "onStartCommand: WebSocketService") + return START_STICKY + } + + override fun onOpen(webSocket: WebSocket, response: Response) { + notificationBuilder?.setContentTitle("AI通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMessageResponse(webSocket: WebSocket, message: String) { + if (RuntimeCache.currentScene == null) { + return + } + + val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { + message.split(":")[1] + } else { + if (message.startsWith("alarm")) { + "" + } else { + message + } + } + + if (base64 != "") { + val bitmapArray = Base64.decode(base64, Base64.DEFAULT) + val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) + val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" + Log.d(kTag, "imageName: $imageName") + val imagePath = "/${createImageFileDir()}/$imageName" + bitmap.saveImage(imagePath) + + when (RuntimeCache.currentScene) { + CurrentScene.Supply -> { + val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + SuppliesActivity.weakReferenceHandler.sendMessage(msg) + } + + CurrentScene.Disclosure -> {} + + CurrentScene.Guardian -> { + val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() + msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE + msg.obj = imagePath + GuardiansActivity.weakReferenceHandler.sendMessage(msg) + } + + else -> { + CoroutineScope(Dispatchers.Main).launch { + "作业阶段错误,请注意流程".show(this@WebSocketService) + } + } + } + } + } + + override fun onMessageResponse(webSocket: WebSocket, bytes: ByteString) { + + } + + override fun onServerDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onClientDisconnected(webSocket: WebSocket, code: Int, reason: String) { + notificationBuilder?.setContentTitle("AI通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onFailure(webSocket: WebSocket?, t: Throwable?) { + notificationBuilder?.setContentTitle("AI通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + override fun onMaxRetryReached() { + } override fun onDestroy() { super.onDestroy() - //断开WebSocket - socketManager.close() + webSocketClient.stop() + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(kTag, "onDestroy: WebSocketService") } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt deleted file mode 100644 index 03fa6b8..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/IWebSocketListener.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import okhttp3.WebSocket - -interface IWebSocketListener { - fun onSocketOpened(webSocket: WebSocket) - - fun onMessageResponse(message: String) - - fun onSocketClosed(code: Int) - - fun onConnectFailed(t: Throwable) -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt deleted file mode 100644 index 3d045a7..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketClient.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import android.util.Log -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import okhttp3.WebSocket -import okhttp3.WebSocketListener -import java.util.concurrent.TimeUnit - -class WebSocketClient { - private val kTag = "WebSocketClient" - private lateinit var webSocket: WebSocket - - fun connect(remoteHost: String, listener: IWebSocketListener) { - Log.d(kTag, "connect: $remoteHost") - val request = Request.Builder().url(remoteHost).build() - val httpClient by lazy { - OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .build() - } - httpClient.newWebSocket(request, object : WebSocketListener() { - override fun onOpen(webSocket: WebSocket, response: Response) { - super.onOpen(webSocket, response) - this@WebSocketClient.webSocket = webSocket - listener.onSocketOpened(webSocket) - } - - override fun onMessage(webSocket: WebSocket, text: String) { - super.onMessage(webSocket, text) - listener.onMessageResponse(text) - } - - override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { - super.onClosed(webSocket, code, reason) - listener.onSocketClosed(code) - } - - override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - super.onFailure(webSocket, t, response) - listener.onConnectFailed(t) - } - }) - httpClient.dispatcher.executorService.shutdown() - } - - fun disconnect() { - Log.d(kTag, "disconnect: 断开连接") - webSocket.close(1000, null) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt b/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt deleted file mode 100644 index 4eddb03..0000000 --- a/app/src/main/java/com/casic/br/operationsite/test/util/websocket/WebSocketManager.kt +++ /dev/null @@ -1,167 +0,0 @@ -package com.casic.br.operationsite.test.util.websocket - -import android.content.Context -import android.graphics.BitmapFactory -import android.os.Handler -import android.os.Looper -import android.util.Base64 -import android.util.Log -import com.casic.br.operationsite.test.util.CurrentScene -import com.casic.br.operationsite.test.util.LocaleConstant -import com.casic.br.operationsite.test.util.RuntimeCache -import com.casic.br.operationsite.test.view.GuardiansActivity -import com.casic.br.operationsite.test.view.SuppliesActivity -import com.pengxh.kt.lite.extensions.createImageFileDir -import com.pengxh.kt.lite.extensions.saveImage -import com.pengxh.kt.lite.extensions.show -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import okhttp3.WebSocket -import java.text.SimpleDateFormat -import java.util.Date -import java.util.Locale - - -class WebSocketManager private constructor(builder: Builder) : IWebSocketListener { - - private val kTag = "WebSocketManager" - private val webSocketClient by lazy { WebSocketClient() } - private val retryHandler by lazy { Handler(Looper.getMainLooper()) } - private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } - private val context = builder.context - private val remoteHost = builder.remoteHost - private val need = builder.need - private val retryIntervalTime = builder.retryIntervalTime - private var isConnected = false - - class Builder { - lateinit var context: Context - lateinit var remoteHost: String - var need = true - var retryIntervalTime = 5000L - - fun setContext(context: Context): Builder { - this.context = context - return this - } - - /** - * ws://192.168.10.139:8765 - * */ - fun setRemoteHost(remoteHost: String): Builder { - this.remoteHost = remoteHost - return this - } - - fun needReconnect(need: Boolean): Builder { - this.need = need - return this - } - - fun setRetryIntervalTime(retryIntervalTime: Long): Builder { - this.retryIntervalTime = retryIntervalTime - return this - } - - fun build(): WebSocketManager { - return WebSocketManager(this) - } - } - - /** - * ws://192.168.10.139:8765 - * */ - fun connect() { - synchronized(this) { - CoroutineScope(Dispatchers.IO).launch { - if (!isConnected) { - webSocketClient.connect(remoteHost, this@WebSocketManager) - } else { - close() - } - } - } - } - - private val retryRunnable = Runnable { connect() } - - override fun onSocketOpened(webSocket: WebSocket) { - isConnected = true - CoroutineScope(Dispatchers.Main).launch { - "AI连接成功".show(context) - } - } - - override fun onMessageResponse(message: String) { - isConnected = true - if (RuntimeCache.currentScene == null) { - return - } - - val base64 = if (RuntimeCache.currentScene == CurrentScene.Guardian) { - message.split(":")[1] - } else { - if (message.startsWith("alarm")) { - "" - } else { - message - } - } - - if (base64 != "") { - val bitmapArray = Base64.decode(base64, Base64.DEFAULT) - val bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size) - val imageName = "IMG_${timeFormat.format(Date())}_${(0..300).random()}.jpg" - Log.d(kTag, "imageName: $imageName") - val imagePath = "/${context.createImageFileDir()}/$imageName" - bitmap.saveImage(imagePath) - - when (RuntimeCache.currentScene) { - CurrentScene.Supply -> { - val msg = SuppliesActivity.weakReferenceHandler.obtainMessage() - msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE - msg.obj = imagePath - SuppliesActivity.weakReferenceHandler.sendMessage(msg) - } - - CurrentScene.Disclosure -> {} - - CurrentScene.Guardian -> { - val msg = GuardiansActivity.weakReferenceHandler.obtainMessage() - msg.what = LocaleConstant.WEBSOCKET_MESSAGE_CODE - msg.obj = imagePath - GuardiansActivity.weakReferenceHandler.sendMessage(msg) - } - - else -> { - CoroutineScope(Dispatchers.Main).launch { - "作业阶段错误,请注意流程".show(context) - } - } - } - } - } - - override fun onSocketClosed(code: Int) { - isConnected = false - CoroutineScope(Dispatchers.Main).launch { - "AI连接关闭".show(context) - } - } - - override fun onConnectFailed(t: Throwable) { - isConnected = false - CoroutineScope(Dispatchers.Main).launch { - "AI连接失败,正在重连...".show(context) - } - if (need) { - retryHandler.postDelayed(retryRunnable, retryIntervalTime) - } - } - - fun close() { - webSocketClient.disconnect() - retryHandler.removeCallbacks(retryRunnable) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/test/view/MainActivity.kt b/app/src/main/java/com/casic/br/operationsite/test/view/MainActivity.kt index 92eee74..08b09e3 100644 --- a/app/src/main/java/com/casic/br/operationsite/test/view/MainActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/test/view/MainActivity.kt @@ -140,9 +140,12 @@ override fun initOnCreate(savedInstanceState: Bundle?) { ActivityStackManager.addActivity(this) - - startService(Intent(this, SocketConnectionService::class.java)) - startService(Intent(this, WebSocketService::class.java)) + Intent(this, SocketConnectionService::class.java).apply { + startService(this) + } + Intent(this, WebSocketService::class.java).apply { + startService(this) + } getProjectListByPage()