DebugHttpServer: Mercury BLE-эндпоинты (/mercury/status|connect|disconnect|screen/off|screen/on)
This commit is contained in:
@@ -115,6 +115,32 @@ object DebugHttpServer {
|
||||
code = 400
|
||||
}
|
||||
}
|
||||
// --- Mercury BLE (полевой тест либы rayneo-mercury) ---
|
||||
method == "GET" && path.startsWith("/mercury/status") -> {
|
||||
val s = MercuryBridge.state.value
|
||||
response = """{"connected":${s.connected},"glasses":${s.glassesName?.let { "\"$it\"" } ?: "null"},"status":${jsonEscape(s.statusText)},"error":${s.lastError?.let { jsonEscape(it) } ?: "null"}}"""
|
||||
code = 200
|
||||
}
|
||||
method == "POST" && path.startsWith("/mercury/connect") -> {
|
||||
val ok = kotlinx.coroutines.runBlocking { MercuryBridge.connectFirst() }
|
||||
response = """{"ok":$ok,"connected":${MercuryBridge.state.value.connected},"status":${jsonEscape(MercuryBridge.state.value.statusText)}}"""
|
||||
code = 200
|
||||
}
|
||||
method == "POST" && path.startsWith("/mercury/disconnect") -> {
|
||||
kotlinx.coroutines.runBlocking { MercuryBridge.disconnect() }
|
||||
response = """{"ok":true}"""
|
||||
code = 200
|
||||
}
|
||||
method == "POST" && path.startsWith("/mercury/screen/off") -> {
|
||||
val ok = kotlinx.coroutines.runBlocking { MercuryBridge.screenOff() }
|
||||
response = """{"ok":$ok,"status":${jsonEscape(MercuryBridge.state.value.statusText)}}"""
|
||||
code = 200
|
||||
}
|
||||
method == "POST" && path.startsWith("/mercury/screen/on") -> {
|
||||
val ok = kotlinx.coroutines.runBlocking { MercuryBridge.screenOn() }
|
||||
response = """{"ok":$ok,"status":${jsonEscape(MercuryBridge.state.value.statusText)}}"""
|
||||
code = 200
|
||||
}
|
||||
else -> {
|
||||
response = """{"error":"нет такого пути: $method $path"}"""
|
||||
code = 404
|
||||
@@ -139,6 +165,9 @@ object DebugHttpServer {
|
||||
return regex.find(body)?.groupValues?.get(1)
|
||||
}
|
||||
|
||||
private fun jsonEscape(s: String): String =
|
||||
s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
|
||||
|
||||
private fun reason(code: Int): String = when (code) {
|
||||
200 -> "OK"
|
||||
400 -> "Bad Request"
|
||||
|
||||
Reference in New Issue
Block a user