parse sensor json values
This commit is contained in:
parent
6c0711acd5
commit
a1020b80d2
5 changed files with 46 additions and 14 deletions
|
|
@ -1,9 +1,5 @@
|
|||
import envoy
|
||||
import glaml
|
||||
import gleam/dict
|
||||
import gleam/erlang/node
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
import gleam/option.{type Option, None, Some}
|
||||
import gleam/result
|
||||
import simplifile
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import gleam/bit_array
|
||||
import gleam/erlang/process
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
import gleam/otp/actor
|
||||
import sensors
|
||||
|
|
@ -17,11 +17,13 @@ fn handle_message(
|
|||
state: State,
|
||||
update: mqtt.Update,
|
||||
) -> actor.Next(State, mqtt.Update) {
|
||||
echo update
|
||||
case parse_mqtt_update(update) {
|
||||
Ok(reading) -> {
|
||||
Ok(readings) -> {
|
||||
// Publish to all subscribers
|
||||
echo readings
|
||||
list.each(state.subscribers, fn(subscriber) {
|
||||
process.send(subscriber, reading)
|
||||
list.each(readings, fn(reading) { process.send(subscriber, reading) })
|
||||
})
|
||||
actor.continue(state)
|
||||
}
|
||||
|
|
@ -29,12 +31,20 @@ fn handle_message(
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_mqtt_update(update: mqtt.Update) -> Result(sensors.SensorReading, Nil) {
|
||||
io.println("GOT MQTT MESSAGE!")
|
||||
echo update
|
||||
// TODO: Implement parsing logic based on your MQTT topic/payload structure
|
||||
// For now, return an error
|
||||
Error(Nil)
|
||||
fn parse_mqtt_update(
|
||||
update: mqtt.Update,
|
||||
) -> Result(List(sensors.SensorReading), Nil) {
|
||||
case update {
|
||||
mqtt.ReceivedMessage(topic, payload, _) -> {
|
||||
let assert Ok(payload_str) = bit_array.to_string(payload)
|
||||
case topic {
|
||||
"homeassistant/sensor/bws/node1/state1" ->
|
||||
Ok(sensors.parse_topic_1(payload_str))
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
}
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import gleam/dynamic/decode
|
||||
import gleam/float
|
||||
import gleam/io
|
||||
import gleam/json
|
||||
|
||||
pub type Sensor {
|
||||
Temperature
|
||||
|
|
@ -25,7 +27,28 @@ pub fn sensor_name(sensor: Sensor) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
// homeassistant/sensor/bws/node1/state1
|
||||
pub const topic_1: String = "homeassistant/sensor/bws/node1/state1"
|
||||
|
||||
type Topic1Value {
|
||||
Topic1Value(temp: Float, humidity: Float, pressure: Float)
|
||||
}
|
||||
|
||||
/// Parses a JSON string into a list of `SensorReading` values.
|
||||
/// example input string: {"temp":18.84,"humidity":28.690811,"pressure":944.67}
|
||||
pub fn parse_topic_1(json_string: String) -> List(SensorReading) {
|
||||
let decoder = {
|
||||
use temp <- decode.field("temp", decode.float)
|
||||
use humidity <- decode.field("humidity", decode.float)
|
||||
use pressure <- decode.field("pressure", decode.float)
|
||||
decode.success(Topic1Value(temp:, humidity:, pressure:))
|
||||
}
|
||||
let assert Ok(decoded_val) = json.parse(from: json_string, using: decoder)
|
||||
[
|
||||
SensorReading(sensor: Temperature, value: decoded_val.temp),
|
||||
SensorReading(sensor: Humidity, value: decoded_val.humidity),
|
||||
SensorReading(sensor: Pressure, value: decoded_val.pressure),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn print_sensor_reading(reading: SensorReading) -> Nil {
|
||||
let sensor_name = sensor_name(reading.sensor)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue