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

This commit is contained in:
2023-12-08 17:13:56 +01:00
parent 4950b67afd
commit ea9db110a5
4 changed files with 71 additions and 8 deletions

View File

@ -5,11 +5,14 @@ import "fmt"
import "encoding/json"
import "udi/config"
import "udi/handlers/handler"
import "udi/handlers/ttn/models/emuProfIILoRa"
import "udi/database"
var idSeq int = 0
type TTNHandler struct {
id int
dbh *database.DatabaseHandle
}
type uplinkMessage struct {
@ -66,6 +69,7 @@ func NewTTNHandler(config config.HandlerConfigT) handler.Handler {
id: idSeq,
}
idSeq += 1
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
return t
}
@ -73,15 +77,20 @@ func (self *TTNHandler) GetId() string {
return fmt.Sprintf("TTN%d", self.id)
}
func lost(msg string, message handler.MessageT) {
log.Printf("Error: %s, message %s is lost", msg, message)
}
func (self *TTNHandler) Handle(message handler.MessageT) {
log.Printf("Handler TTN %d processing %s -> %s", self.id, message.Topic, message.Payload)
var uplinkMessage uplinkMessage
err := json.Unmarshal([]byte(message.Payload), &uplinkMessage)
if err != nil {
log.Printf("Error when unmarshaling message: %s", err)
lost(fmt.Sprintf("Error when unmarshaling message: %s, ", err), message)
return
}
log.Printf("Parsed message: %s", uplinkMessage)
//log.Printf("Parsed message: %s", uplinkMessage)
var attributes attributes
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
@ -95,7 +104,26 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
attributes.Gateways = append(attributes.Gateways, g)
}
log.Printf("Attributes: %s", attributes)
//log.Printf("Attributes: %s", attributes)
log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
if err2 != nil {
lost(fmt.Sprintf("Error when loading device: %s, ", err2), message)
return
}
log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
switch device.DeviceType.ModelIdentifier {
case "emu-prof-ii-lora":
_, err3 := emuProfIILoRa.Parse(uplinkMessage.UplinkMessage.DecodedPayload)
if err3 != nil {
lost(fmt.Sprintf("Model parser failed: %s", err3), message)
return
}
}
}