diff --git a/src/main/kotlin/dev/asdf00/visionfive/hc/gpio/Gpio.kt b/src/main/kotlin/dev/asdf00/visionfive/hc/gpio/Gpio.kt index 802978e..4ef8517 100644 --- a/src/main/kotlin/dev/asdf00/visionfive/hc/gpio/Gpio.kt +++ b/src/main/kotlin/dev/asdf00/visionfive/hc/gpio/Gpio.kt @@ -83,7 +83,7 @@ fun usingPins(vararg pins: Pair, body: (Array) -> Unit) { recUsingPins(i + 1, pins, pinIds, body) } } else { - body(Array(pins.size) { pins[it]!! }) + body(pins as Array) } } recUsingPins(0, arrayOfNulls(pins.size), pins, body) diff --git a/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt b/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt index 4155ab8..da8fdbd 100644 --- a/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt +++ b/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt @@ -17,13 +17,18 @@ import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse import java.nio.charset.StandardCharsets +import java.nio.file.Files import java.time.Duration import java.util.* +import kotlin.collections.HashMap +import kotlin.io.path.Path import kotlin.math.max const val DEBUG = true +const val SENSOR_ID = "sensorId.txt" + fun main() { val forecastQuery = Thread { whileNotInterrupted { @@ -332,19 +337,33 @@ fun main() { } } + val allowedSensor = if (Files.exists(SENSOR_ID.toPath())) + Files.readString(SENSOR_ID.toPath()).let { if (it.endsWith("\n")) it.substring(0, it.length - 1) else it } + else { + print("Please provide the sensor ID: ") + val id = readln().lowercase() + Files.writeString(SENSOR_ID.toPath(), id) + id + } + val tempServer = buildWebServer(8088) { // data collection endpoint("temp") handler@{ exchange -> - exchange.requestURI.query.split("&").forEach { - if (it.startsWith("t=")) { - it.substring(2).toDoubleOrNull()?.let { - GlobalDataStore.temperature = it + exchange.requestURI.query?.qToMap()?.let { + if (it.containsKey("temp")) { + if (allowedSensor == it["id"]?.lowercase()) { + GlobalDataStore.temperature = it["temp"]?.toDoubleOrNull() ?: run { + exchange.replyCat(400) + return@handler + } GlobalDataStore.tempUpdated = Date() exchange.replyCat(200) - } ?: run { - exchange.replyCat(400) + return@handler + } + else { + exchange.replyCat(403) + return@handler } - return@handler } } exchange.replyCat(400) @@ -402,3 +421,16 @@ fun whileNotInterrupted(body: () -> Unit) { Thread.currentThread().interrupt() } } + +fun String.qToMap(): Map? { + val map = HashMap() + split("&").forEach { + val eq = it.indexOf('=') + if (eq < 0) + return null + map.put(it.substring(0, eq), it.substring(eq + 1)) + } + return map +} + +fun String.toPath() = Path(this) \ No newline at end of file diff --git a/src/main/kotlin/dev/asdf00/visionfive/hc/server/WebServerBuilder.kt b/src/main/kotlin/dev/asdf00/visionfive/hc/server/WebServerBuilder.kt index 09c1dbf..4b1d677 100644 --- a/src/main/kotlin/dev/asdf00/visionfive/hc/server/WebServerBuilder.kt +++ b/src/main/kotlin/dev/asdf00/visionfive/hc/server/WebServerBuilder.kt @@ -24,7 +24,13 @@ fun buildWebServer(port: Int, rootPath: String = "", executor: Executor = Execut throw IllegalStateException("Invalid path '$curPath' encountered while building server!") } for (ep in endpoints) { - server.createContext("$curPath/${ep.path}", ep.code) + server.createContext("$curPath/${ep.path}") { + try { + ep.code(it) + } catch (e: Exception) { + it.replyCat(500) + } + } } for (inner in innerPaths) { inner.collect(curPath) diff --git a/src/test/kotlin/Sender.kt b/src/test/kotlin/Sender.kt index eb68f77..e0f6a80 100644 --- a/src/test/kotlin/Sender.kt +++ b/src/test/kotlin/Sender.kt @@ -5,7 +5,6 @@ import java.net.http.HttpResponse fun main() { val client = HttpClient.newHttpClient() - // val req = HttpRequest.newBuilder(URI.create("http://192.168.178.71:8030/temp?t=34.5")).GET().build() - val req = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8030/temp?t=34.5")).GET().build() + val req = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8088/temp?hum=62&temp=22.88&id=shellyht-123456")).GET().build() println("response: ${client.send(req, HttpResponse.BodyHandlers.ofString()).statusCode()}") } \ No newline at end of file