From 72757b5ae7885861c8ee4cdc4b4e9cdf85fb43aa Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 12 Jul 2024 11:16:37 +0200 Subject: [PATCH] add hottis threeway thermometer --- src/udi/config-test-ttn.json | 2 +- .../hottisThreeWayThermometer.go | 74 +++++++++++++++++++ src/udi/handlers/ttn/ttn.go | 3 + 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/udi/handlers/ttn/models/hottisThreeWayThermometer/hottisThreeWayThermometer.go diff --git a/src/udi/config-test-ttn.json b/src/udi/config-test-ttn.json index 78ff7a3..b8229a9 100644 --- a/src/udi/config-test-ttn.json +++ b/src/udi/config-test-ttn.json @@ -1,7 +1,7 @@ { "mqtt": { "broker": "ssl://eu1.cloud.thethings.network:8883", - "username": "de-hottis-lora-test1@ttn", + "username": "de-hottis-saerbeck-monitoring@ttn", "tlsEnable": "true" }, "topicMappings": [ diff --git a/src/udi/handlers/ttn/models/hottisThreeWayThermometer/hottisThreeWayThermometer.go b/src/udi/handlers/ttn/models/hottisThreeWayThermometer/hottisThreeWayThermometer.go new file mode 100644 index 0000000..a1f7a14 --- /dev/null +++ b/src/udi/handlers/ttn/models/hottisThreeWayThermometer/hottisThreeWayThermometer.go @@ -0,0 +1,74 @@ +package hottisThreeWayThermometer + +import ( + "log" + "fmt" + "bytes" + "encoding/base64" + "encoding/binary" + "udi/database" +) + +type hottisThreeWayThermometerValues struct { + Status uint8 +// Battery uint16 + SensorAddress1 uint64 + Value1 int32 + SensorAddress2 uint64 + Value2 int32 + SensorAddress3 uint64 + Value3 int32 +} + + +func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error { + if fPort != 2 { + return fmt.Errorf("Unexpected fPort %d", fPort) + } + + b, err := base64.StdEncoding.DecodeString(frmPayload) + if err != nil { + return fmt.Errorf("Unable to base64-decode payload: %v", err) + } + + var values hottisThreeWayThermometerValues + err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values) + if err != nil { + return fmt.Errorf("Unable to cast into struct: %v", err) + } + + var value1 float32 = float32(values.Value1) / 128.0 + var value2 float32 = float32(values.Value2) / 128.0 + var value3 float32 = float32(values.Value3) / 128.0 + + //log.Printf("Status: %d, Battery: %d", values.Status, values.Battery); + log.Printf("Status: %d", values.Status); + log.Printf("Sensor1: Addr: %d, Value: %f", values.SensorAddress1, value1); + log.Printf("Sensor2: Addr: %d, Value: %f", values.SensorAddress2, value2); + log.Printf("Sensor3: Addr: %d, Value: %f", values.SensorAddress3, value3); + + (*variables)["Temperature1"] = database.VariableType { + Label: "Temperature1", + Variable: "Temperature", + Unit: "°C", + Value: value1, + } + (*variables)["Temperature2"] = database.VariableType { + Label: "Temperature2", + Variable: "Temperature", + Unit: "°C", + Value: value2, + } + (*variables)["Temperature3"] = database.VariableType { + Label: "Temperature3", + Variable: "Temperature", + Unit: "°C", + Value: value3, + } + + (*attributes)["Status"] = values.Status + (*attributes)["SensorAddress1"] = values.SensorAddress1 + (*attributes)["SensorAddress2"] = values.SensorAddress2 + (*attributes)["SensorAddress3"] = values.SensorAddress3 + return nil +} diff --git a/src/udi/handlers/ttn/ttn.go b/src/udi/handlers/ttn/ttn.go index eb0ea3a..5731fd7 100644 --- a/src/udi/handlers/ttn/ttn.go +++ b/src/udi/handlers/ttn/ttn.go @@ -14,6 +14,7 @@ import ( "udi/handlers/ttn/models/draginoLsn50" "udi/handlers/ttn/models/rawPayloadPrinter" "udi/handlers/ttn/models/hottisScd30" + "udi/handlers/ttn/models/hottisThreeWayThermometer" "udi/database" ) @@ -152,6 +153,8 @@ func (self *TTNHandler) Handle(message handler.MessageT) { parser = rawPayloadPrinter.Parse case "hottis-scd30": parser = hottisScd30.Parse + case "hottis-threeway-thermometer": + parser = hottisThreeWayThermometer.Parse default: self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message) return