Files
universal-data-ingest/src/udi/handlers/ttn/models/draginoLmds200/draginoLmds200.go
Wolfgang Hottgenroth b938d48c7f
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
add status in ttn handlers
2023-12-28 15:48:51 +01:00

91 lines
2.2 KiB
Go

package draginoLmds200
import (
"fmt"
"strconv"
"encoding/json"
"udi/database"
)
/*
"decoded_payload": {
"Bat": 3.082,
"DALARM_count": 0,
"Distance_alarm": 0,
"Interrupt_alarm": 0,
"dis1": 105,
"dis2": 201
},
*/
type message struct {
Bat float32 `json:"Bat"`
DALARM_count int `json:"DALARM_count"`
Distance_alarm int `json:"Distance_alarm"`
Interrupt_alarm int `json:"Interrupt_alarm"`
Dis1 int `json:"dis1"`
Dis2 int `json:"dis2"`
}
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)
}
var message message
err := json.Unmarshal(decodedPayload, &message)
if err != nil {
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
}
(*variables)["Battery"] = database.VariableType {
Label: "Battery",
Variable: "Voltage",
Unit: "V",
Value: message.Bat,
}
distance1 := message.Dis1 * 10
(*variables)["Distance1"] = database.VariableType {
Label: "Distance1",
Variable: "Level",
Unit: "mm",
Value: distance1,
}
distance2 := message.Dis2 * 10
(*variables)["Distance2"] = database.VariableType {
Label: "Distance2",
Variable: "Level",
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)
if exists && err3 == nil && ok {
correctedDistance1 := groundLevel - distance1
(*variables)["CorrectedDistance1"] = database.VariableType {
Label: "CorrectedDistance1",
Variable: "Level",
Unit: "mm",
Value: correctedDistance1,
}
correctedDistance2 := groundLevel - distance2
(*variables)["CorrectedDistance2"] = database.VariableType {
Label: "CorrectedDistance2",
Variable: "Level",
Unit: "mm",
Value: correctedDistance2,
}
}
return nil
}