Maa WS接口实现

This commit is contained in:
guigui
2022-05-29 22:54:09 +08:00
parent 7638056261
commit c82611ea08
9 changed files with 150 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
package com.iguigui.maaj.dto
import com.iguigui.maaj.util.Json
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@@ -20,7 +21,7 @@ data class HttpResponse(
@Serializable
data class WsResponse(
@SerialName("version")
@SerialName("data")
val data: JsonElement,
@SerialName("command")
val command: String,

View File

@@ -13,7 +13,7 @@ import kotlinx.serialization.json.jsonObject
fun Application.httpRouting() {
routing {
route("/v1") {
route("/API/V1") {
get("/getVersion") {
val version = MaaService.getVersion()
call.respond(GetVersionResponse(version).wapperToHttpResponse())

View File

@@ -19,13 +19,13 @@ import kotlin.collections.LinkedHashSet
fun Application.wsRouting() {
routing {
webSocket("/v1") {
webSocket("/API/V1") {
println("Adding user!")
val connection = Connection(this)
addConnection(connection)
for (frame in incoming) {
frame as? Frame.Text ?: continue
try {
try {
for (frame in incoming) {
frame as? Frame.Text ?: continue
val receivedText = frame.readText()
val wsRequest = Json.decodeFromString(WsRequest.serializer(), receivedText)
var response: BaseData? = null
@@ -71,7 +71,7 @@ fun Application.wsRouting() {
"destroy" -> {
val destroyRequest =
Json.decodeFromJsonElement(DestroyRequest.serializer(), wsRequest.data)
MaaService.stop(destroyRequest.id)
MaaService.destroy(destroyRequest.id)
response = EmptyBaseData
}
"listInstance" -> {
@@ -83,13 +83,14 @@ fun Application.wsRouting() {
response?.let {
send(it.wapperToWsResponse(wsRequest.command, wsRequest.msgId).toJsonString())
}
} catch (e: Exception) {
e.printStackTrace()
println(e.message)
} finally {
println("Removing $connection!")
removeConnection(connection)
}
} catch (e: Exception) {
e.printStackTrace()
println(e.message)
} finally {
println("Removing $connection!")
removeConnection(connection)
}
}
}

View File

@@ -1,9 +1,6 @@
package com.iguigui.maaj.service
import com.iguigui.maaj.dto.CallBackLog
import com.iguigui.maaj.dto.ConnectResponse
import com.iguigui.maaj.dto.MaaInstanceInfo
import com.iguigui.maaj.dto.toJsonElement
import com.iguigui.maaj.dto.*
import com.iguigui.maaj.easySample.MeoAssistant
import com.sun.jna.Native
import io.ktor.websocket.*
@@ -37,16 +34,16 @@ object MaaService {
fun connect(adbPath: String, host: String, detailJson: String): ConnectResponse {
val id = sha1(host)
if (instancePool.containsKey(id)) {
return ConnectResponse(id, true)
}
val maaInstance = MaaInstance(meoAssistant, host, adbPath, host, detailJson, ::callBackLog)
// if (instancePool.containsKey(id)) {
// return ConnectResponse(id, true)
// }
val maaInstance = MaaInstance(meoAssistant, id, adbPath, host, detailJson, ::callBackLog)
maaInstance.pointer = meoAssistant.AsstCreateEx(maaInstance, maaInstance.id)
val connect = maaInstance.connect()
if (!connect) {
return ConnectResponse("", false)
}
instancePool.putIfAbsent(id, maaInstance)
instancePool[id] = maaInstance
return ConnectResponse(id, true)
}
@@ -82,13 +79,16 @@ object MaaService {
fun listInstance(): List<MaaInstanceInfo> =
instancePool.values.map { e -> MaaInstanceInfo(e.id, e.host, e.adbPath, e.uuid, e.status) }.toList()
fun destroy(id: String) = instancePool[id]?.destroy()
fun destroy(id: String) {
instancePool[id]?.destroy()
instancePool.remove(id)
}
@OptIn(DelicateCoroutinesApi::class)
fun callBackLog(message: CallBackLog) {
GlobalScope.launch(Dispatchers.IO) {
wsConnection.forEach { it.session.send(message.toJsonElement().toString()) }
wsConnection.forEach { it.session.send(message.wapperToWsResponse("callBack", 0).toJsonString()) }
}
}

View File

@@ -5,5 +5,6 @@ import kotlinx.serialization.json.Json
val Json by lazy {
Json {
ignoreUnknownKeys = true
encodeDefaults = true
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

View File

@@ -0,0 +1,13 @@
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>Maaj.log</file>
<append>true</append>
<encoder>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE"/>
</root>
<logger name="io.netty" level="INFO"/>
</configuration>

View File

@@ -7,6 +7,5 @@
<root level="trace">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/>
</configuration>

View File

@@ -0,0 +1,97 @@
package com.iguigui.maaj.routing;
import com.iguigui.maaj.configure
import com.iguigui.maaj.dto.GetVersionResponse
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.HttpStatusCode.Companion.OK
import io.ktor.server.testing.*
import org.junit.Assert
import org.junit.Before
import kotlin.test.Test
class HttpRoutingKtTest {
@Test
fun testGetApiV1Getversion() = testApplication {
application {
httpRouting()
}
client.get("/API/V1/getVersion").apply {
Assert.assertEquals(status,OK)
val body :GetVersionResponse = body()
println(body.version)
}
}
@Test
fun testPostApiV1Connect() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/connect").apply {
}
}
@Test
fun testPostApiV1Appendtask() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/appendTask").apply {
TODO("Please write your test here")
}
}
@Test
fun testPostApiV1Settaskparams() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/setTaskParams").apply {
TODO("Please write your test here")
}
}
@Test
fun testPostApiV1Start() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/start").apply {
TODO("Please write your test here")
}
}
@Test
fun testPostApiV1Stop() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/stop").apply {
TODO("Please write your test here")
}
}
@Test
fun testPostApiV1Destroy() = testApplication {
application {
httpRouting()
}
client.post("/API/V1/destroy").apply {
TODO("Please write your test here")
}
}
@Test
fun testGetApiV1Listinstance() = testApplication {
application {
httpRouting()
}
client.get("/API/V1/listInstance").apply {
TODO("Please write your test here")
}
}
}