fixed temp collection

This commit is contained in:
00asdf 2025-05-04 19:52:59 +02:00
parent eca3762cbb
commit e1ddbde0da
4 changed files with 48 additions and 11 deletions

View File

@ -83,7 +83,7 @@ fun usingPins(vararg pins: Pair<Int, Boolean>, body: (Array<GpioPin>) -> Unit) {
recUsingPins(i + 1, pins, pinIds, body)
}
} else {
body(Array(pins.size) { pins[it]!! })
body(pins as Array<GpioPin>)
}
}
recUsingPins(0, arrayOfNulls(pins.size), pins, body)

View File

@ -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<String, String>? {
val map = HashMap<String, String>()
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)

View File

@ -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)

View File

@ -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()}")
}