Maa HTTP server

This commit is contained in:
iguigui
2022-05-27 18:12:13 +08:00
parent 43f878a356
commit dfab6b5606
4 changed files with 64 additions and 31 deletions

View File

@@ -34,7 +34,7 @@ data class WsResponse(
sealed class BaseData
@Serializable
data class GetVersion(
data class GetVersionResponse(
@SerialName("version")
val version: String
) : BaseData()
@@ -49,7 +49,7 @@ data class ConnectResponse(
@Serializable
data class ListInstance(
data class ListInstanceResponse(
@SerialName("list")
val list: List<MaaInstanceInfo>
) : BaseData()
@@ -62,21 +62,50 @@ data class MaaInstanceInfo(
val host: String,
@SerialName("adbPath")
val adbPath: String,
@SerialName("uuid")
val uuid: String,
@SerialName("status")
val status: Int,
) : BaseData()
@Serializable
data class AppendTask(
data class AppendTaskResponse(
@SerialName("id")
val id: Int
val id: String,
@SerialName("taskId")
val taskId: Int
) : BaseData()
@Serializable
data class SetTaskParamsResponse(
@SerialName("id")
val id: String,
@SerialName("result")
val result: Boolean
) : BaseData()
@Serializable
data class StartResponse(
@SerialName("id")
val id: String,
@SerialName("result")
val result: Boolean
) : BaseData()
@Serializable
data class StopResponse(
@SerialName("id")
val id: String,
@SerialName("result")
val result: Boolean
) : BaseData()
@Serializable
data class CallBackLog(
@SerialName("instanceId")
val instanceId: String,
@SerialName("id")
val id: String,
@SerialName("logId")
val logId: Long,
@SerialName("msg")

View File

@@ -2,12 +2,12 @@ package com.iguigui.maaj.routing
import com.iguigui.maaj.dto.*
import com.iguigui.maaj.service.MaaService
import com.iguigui.maaj.service.MaaService.appendTask
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
fun Application.httpRouting() {
@@ -16,48 +16,48 @@ fun Application.httpRouting() {
route("/v1") {
get("/getVersion") {
val version = MaaService.getVersion()
call.respond(GetVersion(version).wapperToResponse())
call.respond(GetVersionResponse(version).wapperToResponse())
}
post("/connect") {
with(call.receive<ConnectRequest>()) {
val connect = MaaService.connect(adbPath, host, detailJson)
call.respond(HttpResponse(connect.toJsonElement()))
call.respond(connect.wapperToResponse())
}
}
post("/appendTask") {
with(call.receive<AppendTaskRequest>()) {
MaaService.appendTask(
val result = appendTask(
id,
type,
params.jsonObject.toString()
)
call.respond(AppendTaskResponse(id, result).wapperToResponse())
}
call.respond(EmptyBaseData.wapperToResponse())
}
post("/setTaskParams") {
with(call.receive<SetTaskParamsRequest>()) {
MaaService.setTaskParams(
val result = MaaService.setTaskParams(
id,
type,
taskId,
params.jsonObject.toString()
)
call.respond(SetTaskParamsResponse(id, result).wapperToResponse())
}
call.respond(EmptyBaseData.wapperToResponse())
}
post("/start") {
val start = call.receive<Start>()
MaaService.start(start.id)
call.respond(EmptyBaseData.wapperToResponse())
val result = MaaService.start(start.id)
call.respond(StartResponse(start.id, result).wapperToResponse())
}
post("/stop") {
val stop = call.receive<Stop>()
MaaService.stop(stop.id)
call.respond(EmptyBaseData.wapperToResponse())
val result = MaaService.stop(stop.id)
call.respond(StartResponse(stop.id, result).wapperToResponse())
}
get("/listInstance") {
val list = MaaService.listInstance()
call.respond(ListInstance(list).wapperToResponse())
call.respond(ListInstanceResponse(list).wapperToResponse())
}
}
}

View File

@@ -39,6 +39,8 @@ class MaaInstance(
lateinit var pointer: Pointer
var uuid = ""
var status = 0
var logQueue: Queue<CallBackLog> = LinkedBlockingQueue(1000)

View File

@@ -26,17 +26,19 @@ object MaaService {
fun connect(adbPath: String, host: String, detailJson: String): ConnectResponse {
val id = sha1(host)
if (instancePool.containsKey(id)) {
return ConnectResponse(id,true)
synchronized(id) {
if (instancePool.containsKey(id)) {
return ConnectResponse(id, true)
}
val maaInstance = MaaInstance(meoAssistant, host, adbPath, host, detailJson)
maaInstance.pointer = meoAssistant.AsstCreateEx(maaInstance, maaInstance.id)
val connect = maaInstance.connect()
if (!connect) {
return ConnectResponse("", false)
}
instancePool.putIfAbsent(id, maaInstance)
}
val maaInstance = MaaInstance(meoAssistant, host, adbPath, host, detailJson)
maaInstance.pointer = meoAssistant.AsstCreateEx(maaInstance, maaInstance.id)
val connect = maaInstance.connect()
if (!connect) {
return ConnectResponse("",false)
}
instancePool.putIfAbsent(id, maaInstance)
return ConnectResponse(id,true)
return ConnectResponse(id, true)
}
fun appendTask(host: String, type: String, detailJson: String) =
@@ -46,9 +48,9 @@ object MaaService {
fun setTaskParams(host: String, type: String, taskId: Int, detailJson: String) =
instancePool[host]?.setTaskParams(type, taskId, detailJson) ?: false
fun start(host: String) = instancePool[host]?.start()
fun start(host: String) = instancePool[host]?.start() ?: false
fun stop(host: String) = instancePool[host]?.stop()
fun stop(host: String) = instancePool[host]?.stop() ?: false
fun getVersion(): String = meoAssistant.AsstGetVersion()
@@ -69,7 +71,7 @@ object MaaService {
}
fun listInstance(): List<MaaInstanceInfo> =
instancePool.values.map { e -> MaaInstanceInfo(e.id, e.host, e.id, e.status) }.toList()
instancePool.values.map { e -> MaaInstanceInfo(e.id, e.host, e.adbPath, e.uuid, e.status) }.toList()
}