add status in ttn handlers
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2023-12-28 15:48:51 +01:00
parent b374b7f49d
commit b938d48c7f
7 changed files with 40 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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