Dragino parser
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-12-15 11:44:35 +01:00
parent 084f9fbf31
commit 7ee7d4df89
7 changed files with 263 additions and 30 deletions

View File

@ -0,0 +1,61 @@
package draginoLdds75
import (
"fmt"
"strings"
"strconv"
"encoding/json"
"udi/database"
)
/*
"decoded_payload": {
"Bat": 3.299,
"Distance": "352 mm",
"Interrupt_flag": 0,
"Sensor_flag": 1,
"TempC_DS18B20": "0.00"
},
*/
type message struct {
Bat float32 `json:"Bat"`
Distance string `json:"Distance"`
Interrupt_flag int `json:"Interrupt_flag"`
Sensor_flag int `json:"Sensor_flag"`
TempC_DS18B20 string `json:"TempC_DS18B20"`
}
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType) 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)
}
distanceParts := strings.Split(message.Distance, " ")
if len(distanceParts) != 2 && distanceParts[1] != "mm" {
return fmt.Errorf("Invalid format for distance value: %s", message.Distance)
}
distance, err2 := strconv.Atoi(distanceParts[0])
if err2 != nil {
return fmt.Errorf("Distance value is no number: %s -> %s", message.Distance, err2)
}
(*variables)["Battery"] = database.VariableType {
Label: "Battery",
Variable: "Voltage",
Unit: "V",
Value: message.Bat,
}
(*variables)["Distance"] = database.VariableType {
Label: "Distance",
Variable: "Level",
Unit: "mm",
Value: distance,
}
return nil
}