fixed temp collection
This commit is contained in:
parent
eca3762cbb
commit
e1ddbde0da
|
|
@ -83,7 +83,7 @@ fun usingPins(vararg pins: Pair<Int, Boolean>, body: (Array<GpioPin>) -> Unit) {
|
||||||
recUsingPins(i + 1, pins, pinIds, body)
|
recUsingPins(i + 1, pins, pinIds, body)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body(Array(pins.size) { pins[it]!! })
|
body(pins as Array<GpioPin>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
recUsingPins(0, arrayOfNulls(pins.size), pins, body)
|
recUsingPins(0, arrayOfNulls(pins.size), pins, body)
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,18 @@ import java.net.http.HttpClient
|
||||||
import java.net.http.HttpRequest
|
import java.net.http.HttpRequest
|
||||||
import java.net.http.HttpResponse
|
import java.net.http.HttpResponse
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
|
import java.nio.file.Files
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.collections.HashMap
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
const val DEBUG = true
|
const val DEBUG = true
|
||||||
|
|
||||||
|
const val SENSOR_ID = "sensorId.txt"
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val forecastQuery = Thread {
|
val forecastQuery = Thread {
|
||||||
whileNotInterrupted {
|
whileNotInterrupted {
|
||||||
|
|
@ -332,20 +337,34 @@ 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) {
|
val tempServer = buildWebServer(8088) {
|
||||||
// data collection
|
// data collection
|
||||||
endpoint("temp") handler@{ exchange ->
|
endpoint("temp") handler@{ exchange ->
|
||||||
exchange.requestURI.query.split("&").forEach {
|
exchange.requestURI.query?.qToMap()?.let {
|
||||||
if (it.startsWith("t=")) {
|
if (it.containsKey("temp")) {
|
||||||
it.substring(2).toDoubleOrNull()?.let {
|
if (allowedSensor == it["id"]?.lowercase()) {
|
||||||
GlobalDataStore.temperature = it
|
GlobalDataStore.temperature = it["temp"]?.toDoubleOrNull() ?: run {
|
||||||
|
exchange.replyCat(400)
|
||||||
|
return@handler
|
||||||
|
}
|
||||||
GlobalDataStore.tempUpdated = Date()
|
GlobalDataStore.tempUpdated = Date()
|
||||||
exchange.replyCat(200)
|
exchange.replyCat(200)
|
||||||
} ?: run {
|
|
||||||
exchange.replyCat(400)
|
|
||||||
}
|
|
||||||
return@handler
|
return@handler
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
exchange.replyCat(403)
|
||||||
|
return@handler
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exchange.replyCat(400)
|
exchange.replyCat(400)
|
||||||
}
|
}
|
||||||
|
|
@ -402,3 +421,16 @@ fun whileNotInterrupted(body: () -> Unit) {
|
||||||
Thread.currentThread().interrupt()
|
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)
|
||||||
|
|
@ -24,7 +24,13 @@ fun buildWebServer(port: Int, rootPath: String = "", executor: Executor = Execut
|
||||||
throw IllegalStateException("Invalid path '$curPath' encountered while building server!")
|
throw IllegalStateException("Invalid path '$curPath' encountered while building server!")
|
||||||
}
|
}
|
||||||
for (ep in endpoints) {
|
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) {
|
for (inner in innerPaths) {
|
||||||
inner.collect(curPath)
|
inner.collect(curPath)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import java.net.http.HttpResponse
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val client = HttpClient.newHttpClient()
|
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:8088/temp?hum=62&temp=22.88&id=shellyht-123456")).GET().build()
|
||||||
val req = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8030/temp?t=34.5")).GET().build()
|
|
||||||
println("response: ${client.send(req, HttpResponse.BodyHandlers.ofString()).statusCode()}")
|
println("response: ${client.send(req, HttpResponse.BodyHandlers.ofString()).statusCode()}")
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user