From b938d48c7fa34d1698740ac52b0fcd7058281f68 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 28 Dec 2023 15:48:51 +0100 Subject: [PATCH] add status in ttn handlers --- migration/migrate-level.py | 13 +++++++++++-- .../ttn/models/draginoLdds75/draginoLdds75.go | 19 ++++++++++++++----- .../models/draginoLmds200/draginoLmds200.go | 11 ++++++++++- .../ttn/models/draginoLse01/draginoLse01.go | 2 +- .../emuProfIILoRaCfg1/emuProfIILoRaCfg1.go | 2 +- .../rawPayloadPrinter/rawPayloadPrinter.go | 2 +- src/udi/handlers/ttn/ttn.go | 3 ++- 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/migration/migrate-level.py b/migration/migrate-level.py index 10113bb..84cd70f 100644 --- a/migration/migrate-level.py +++ b/migration/migrate-level.py @@ -4,8 +4,10 @@ from loguru import logger try: srcConn = psycopg2.connect(database="level_monitoring_berresheim") srcConn.autocommit = False + destConn = psycopg2.connect(database="udi-berresheim") + destConn.autocommit = False - with srcConn.cursor() as srcCur: + with srcConn.cursor() as srcCur, destConn.cursor() as destCur: srcCur.execute("select time, application_name, raw_level, level, status, battery from measurement_t") for srcObj in srcCur: timestamp = srcObj[0] @@ -21,9 +23,16 @@ try: destApplication = "de-hottis-level-monitoring" destDevice = "eui-a84041a2c18341d6" destAttributes = '{"ApplicationId":"de-hottis-level-monitoring", "DeviceType":"dragino-ldds75", "Hint": "Migrated"}' - destValues = '{"Battery":{"unit":"V","label":"Battery","value":' + str(battery) + ',"variable":"Voltage"}, "Distance":{"unit":mm","label":"Distance","variable":"Level","value":' + str(rawLevel) + '}, "CorrectedDistance":{"unit":"mm", "label":"CorrectedDistance", "variable":"Level","value":' + str(level) + '}}' + destValues = '{"Battery":{"unit":"V","label":"Battery","value":' + str(battery) + ',"variable":"Voltage"}, "Distance":{"unit":"mm","label":"Distance","variable":"Level","value":' + str(rawLevel) + '}, "CorrectedDistance":{"unit":"mm", "label":"CorrectedDistance", "variable":"Level","value":' + str(level) + '}}' logger.info(f"{destTime=}, {destApplication=}, {destDevice=}, {destAttributes=}, {destValues=}") + + destCur.execute("insert into measurements (time, application, device, attributes, values) values(%s, %s, %s, %s, %s)", + (destTime, destApplication, destDevice, destAttributes, destValues)) + destConn.commit() finally: if srcConn: srcConn.close() + if destConn: + destConn.close() + diff --git a/src/udi/handlers/ttn/models/draginoLdds75/draginoLdds75.go b/src/udi/handlers/ttn/models/draginoLdds75/draginoLdds75.go index b96b7f6..13e6ffd 100644 --- a/src/udi/handlers/ttn/models/draginoLdds75/draginoLdds75.go +++ b/src/udi/handlers/ttn/models/draginoLdds75/draginoLdds75.go @@ -2,7 +2,7 @@ package draginoLdds75 import ( "fmt" - "log" + // "log" "strings" "strconv" "encoding/json" @@ -26,7 +26,7 @@ type message struct { TempC_DS18B20 string `json:"TempC_DS18B20"` } -func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error { +func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error { if fPort != 2 { return fmt.Errorf("Unexpected fPort %d", fPort) } @@ -55,11 +55,20 @@ func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]dat Unit: "mm", Value: distance, } + + if distance == 20 { + (*attributes)["Status"] = "invalid value" + } else if distance == 0 { + (*attributes)["Status"] = "no sensor detected" + } else { + (*attributes)["Status"] = "Ok" + } + groundLevelI, exists := device.Attributes["GroundLevel"] groundLevelS, ok := groundLevelI.(string) groundLevel, err3 := strconv.Atoi(groundLevelS) if exists && err3 == nil && ok { - log.Println("add corrected distance") + //log.Println("add corrected distance") correctedDistance := groundLevel - distance (*variables)["CorrectedDistance"] = database.VariableType { Label: "CorrectedDistance", @@ -67,11 +76,11 @@ func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]dat Unit: "mm", Value: correctedDistance, } - } else { + } /* else { log.Printf("no ground level: %s %s %s", exists, err3, ok) log.Printf("Device: %s", device) log.Printf("Attributes: %s", device.Attributes) - } + } */ return nil } diff --git a/src/udi/handlers/ttn/models/draginoLmds200/draginoLmds200.go b/src/udi/handlers/ttn/models/draginoLmds200/draginoLmds200.go index cd9707e..201fb7c 100644 --- a/src/udi/handlers/ttn/models/draginoLmds200/draginoLmds200.go +++ b/src/udi/handlers/ttn/models/draginoLmds200/draginoLmds200.go @@ -26,7 +26,7 @@ type message struct { Dis2 int `json:"dis2"` } -func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error { +func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error { if fPort != 2 { return fmt.Errorf("Unexpected fPort %d", fPort) } @@ -55,6 +55,15 @@ func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]dat Unit: "mm", Value: distance2, } + + if distance1 == 2 { + (*attributes)["Status"] = "invalid value" + } else if distance1 == 1 { + (*attributes)["Status"] = "no sensor detected" + } else { + (*attributes)["Status"] = "Ok" + } + groundLevelI, exists := device.Attributes["GroundLevel"] groundLevelS, ok := groundLevelI.(string) groundLevel, err3 := strconv.Atoi(groundLevelS) diff --git a/src/udi/handlers/ttn/models/draginoLse01/draginoLse01.go b/src/udi/handlers/ttn/models/draginoLse01/draginoLse01.go index 211cc13..afd3ec3 100644 --- a/src/udi/handlers/ttn/models/draginoLse01/draginoLse01.go +++ b/src/udi/handlers/ttn/models/draginoLse01/draginoLse01.go @@ -24,7 +24,7 @@ type message struct { Water_SOIL string `json:"water_SOIL"` } -func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error { +func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error { if fPort != 2 { return fmt.Errorf("Unexpected fPort %d", fPort) } diff --git a/src/udi/handlers/ttn/models/emuProfIILoRaCfg1/emuProfIILoRaCfg1.go b/src/udi/handlers/ttn/models/emuProfIILoRaCfg1/emuProfIILoRaCfg1.go index 6c0cb3f..e148497 100644 --- a/src/udi/handlers/ttn/models/emuProfIILoRaCfg1/emuProfIILoRaCfg1.go +++ b/src/udi/handlers/ttn/models/emuProfIILoRaCfg1/emuProfIILoRaCfg1.go @@ -177,7 +177,7 @@ type emuMessage1 struct { -func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *database.Device) error { +func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error { //log.Printf("Parse input: %d, %s", fPort, decodedPayload) switch fPort { case 1: diff --git a/src/udi/handlers/ttn/models/rawPayloadPrinter/rawPayloadPrinter.go b/src/udi/handlers/ttn/models/rawPayloadPrinter/rawPayloadPrinter.go index a0dea57..d6d8b88 100644 --- a/src/udi/handlers/ttn/models/rawPayloadPrinter/rawPayloadPrinter.go +++ b/src/udi/handlers/ttn/models/rawPayloadPrinter/rawPayloadPrinter.go @@ -9,7 +9,7 @@ import ( ) -func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, device *database.Device) error { +func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error { if fPort != 2 { return fmt.Errorf("Unexpected fPort %d", fPort) } diff --git a/src/udi/handlers/ttn/ttn.go b/src/udi/handlers/ttn/ttn.go index 209d5c2..7396bd6 100644 --- a/src/udi/handlers/ttn/ttn.go +++ b/src/udi/handlers/ttn/ttn.go @@ -134,7 +134,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) { //log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier) - var parser func(int, []byte, string, *map[string]database.VariableType, *database.Device) error + var parser func(int, []byte, string, *map[string]database.VariableType, *map[string]interface{}, *database.Device) error switch device.DeviceType.ModelIdentifier { case "emu-prof-ii-lora-cfg1": parser = emuProfIILoRaCfg1.Parse @@ -156,6 +156,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) { uplinkMessage.UplinkMessage.DecodedPayload.Payload, uplinkMessage.UplinkMessage.FrmPayload, &(measurement.Values), + &(measurement.Attributes), device) if err3 != nil { self.Lost("Model parser failed", err3, message)