Compare commits

..

4 Commits

Author SHA1 Message Date
879825a260 format paylaod
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-12-27 14:21:21 +01:00
b6132afb11 disable logging in ttn
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-12-27 12:14:05 +01:00
5e94782575 add RawPayloadPrinter
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-12-27 12:06:50 +01:00
57c63adeb2 handover FrmPayload to model parsers too 2023-12-27 12:00:02 +01:00
6 changed files with 46 additions and 14 deletions

View File

@ -26,7 +26,7 @@ type message struct {
TempC_DS18B20 string `json:"TempC_DS18B20"` TempC_DS18B20 string `json:"TempC_DS18B20"`
} }
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error { func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error {
if fPort != 2 { if fPort != 2 {
return fmt.Errorf("Unexpected fPort %d", fPort) return fmt.Errorf("Unexpected fPort %d", fPort)
} }

View File

@ -26,7 +26,7 @@ type message struct {
Dis2 int `json:"dis2"` Dis2 int `json:"dis2"`
} }
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error { func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error {
if fPort != 2 { if fPort != 2 {
return fmt.Errorf("Unexpected fPort %d", fPort) return fmt.Errorf("Unexpected fPort %d", fPort)
} }

View File

@ -24,7 +24,7 @@ type message struct {
Water_SOIL string `json:"water_SOIL"` Water_SOIL string `json:"water_SOIL"`
} }
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error { func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, device *database.Device) error {
if fPort != 2 { if fPort != 2 {
return fmt.Errorf("Unexpected fPort %d", fPort) return fmt.Errorf("Unexpected fPort %d", fPort)
} }

View File

@ -177,7 +177,7 @@ type emuMessage1 struct {
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, _ *database.Device) error { func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *database.Device) error {
//log.Printf("Parse input: %d, %s", fPort, decodedPayload) //log.Printf("Parse input: %d, %s", fPort, decodedPayload)
switch fPort { switch fPort {
case 1: case 1:

View File

@ -0,0 +1,25 @@
package rawPayloadPrinter
import (
"log"
"fmt"
"encoding/base64"
"encoding/hex"
"udi/database"
)
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, device *database.Device) error {
if fPort != 2 {
return fmt.Errorf("Unexpected fPort %d", fPort)
}
bytes, err := base64.StdEncoding.DecodeString(frmPayload)
if err != nil {
return fmt.Errorf("Unable to base64-decode payload: %v", err)
}
hexString := hex.EncodeToString(bytes)
log.Printf("Payload: %s", hexString)
return nil
}

View File

@ -2,7 +2,7 @@ package ttn
import ( import (
"fmt" "fmt"
"log" //"log"
"time" "time"
"encoding/json" "encoding/json"
"udi/config" "udi/config"
@ -11,6 +11,7 @@ import (
"udi/handlers/ttn/models/draginoLdds75" "udi/handlers/ttn/models/draginoLdds75"
"udi/handlers/ttn/models/draginoLmds200" "udi/handlers/ttn/models/draginoLmds200"
"udi/handlers/ttn/models/draginoLse01" "udi/handlers/ttn/models/draginoLse01"
"udi/handlers/ttn/models/rawPayloadPrinter"
"udi/database" "udi/database"
) )
@ -85,7 +86,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
} }
func (self *TTNHandler) Handle(message handler.MessageT) { func (self *TTNHandler) Handle(message handler.MessageT) {
log.Printf("Handler TTN %d processing %s -> %s", self.Id, message.Topic, message.Payload) //log.Printf("Handler TTN %d processing %s -> %s", self.Id, message.Topic, message.Payload)
var measurement database.Measurement var measurement database.Measurement
measurement.Time = time.Now() measurement.Time = time.Now()
@ -96,7 +97,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
self.Lost("Error when unmarshaling message", err, message) self.Lost("Error when unmarshaling message", err, message)
return return
} }
log.Printf("Parsed message: %s", uplinkMessage) //log.Printf("Parsed message: %s", uplinkMessage)
var attributes attributes var attributes attributes
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
@ -106,11 +107,11 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata { for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
log.Printf("RXM: %s", rxm) //log.Printf("RXM: %s", rxm)
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr } g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
attributes.Gateways = append(attributes.Gateways, g) attributes.Gateways = append(attributes.Gateways, g)
} }
log.Printf("Attributes: %s", attributes) //log.Printf("Attributes: %s", attributes)
measurement.Attributes = map[string]interface{} { measurement.Attributes = map[string]interface{} {
"DeviceId": attributes.DeviceId, "DeviceId": attributes.DeviceId,
"ApplicationId": attributes.ApplicationId, "ApplicationId": attributes.ApplicationId,
@ -121,7 +122,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
"ConsumedAirtime": attributes.ConsumedAirtime, "ConsumedAirtime": attributes.ConsumedAirtime,
} }
log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId) //log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId) device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
if err2 != nil { if err2 != nil {
self.Lost("Error when loading device", err2, message) self.Lost("Error when loading device", err2, message)
@ -131,9 +132,9 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
measurement.Device = attributes.DeviceId measurement.Device = attributes.DeviceId
measurement.Attributes["DeviceType"] = device.DeviceType.ModelIdentifier measurement.Attributes["DeviceType"] = device.DeviceType.ModelIdentifier
log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier) //log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
var parser func(int, []byte, *map[string]database.VariableType, *database.Device) error var parser func(int, []byte, string, *map[string]database.VariableType, *database.Device) error
switch device.DeviceType.ModelIdentifier { switch device.DeviceType.ModelIdentifier {
case "emu-prof-ii-lora-cfg1": case "emu-prof-ii-lora-cfg1":
parser = emuProfIILoRaCfg1.Parse parser = emuProfIILoRaCfg1.Parse
@ -143,18 +144,24 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
parser = draginoLmds200.Parse parser = draginoLmds200.Parse
case "dragino-lse01": case "dragino-lse01":
parser = draginoLse01.Parse parser = draginoLse01.Parse
case "raw-payload-printer":
parser = rawPayloadPrinter.Parse
default: default:
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message) self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
return return
} }
measurement.Values = make(map[string]database.VariableType) measurement.Values = make(map[string]database.VariableType)
err3 := parser(uplinkMessage.UplinkMessage.FPort, uplinkMessage.UplinkMessage.DecodedPayload.Payload, &(measurement.Values), device) err3 := parser(uplinkMessage.UplinkMessage.FPort,
uplinkMessage.UplinkMessage.DecodedPayload.Payload,
uplinkMessage.UplinkMessage.FrmPayload,
&(measurement.Values),
device)
if err3 != nil { if err3 != nil {
self.Lost("Model parser failed", err3, message) self.Lost("Model parser failed", err3, message)
return return
} }
log.Printf("Prepared measurement item: %s", measurement) //log.Printf("Prepared measurement item: %s", measurement)
self.dbh.StoreMeasurement(&measurement) self.dbh.StoreMeasurement(&measurement)
self.S() self.S()
} }