This commit is contained in:
@ -35,8 +35,8 @@ type DeviceType struct {
|
|||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Label string `gorm:"not null"`
|
Label string `gorm:"not null;uniqueIndex:idx_label_application_id"`
|
||||||
ApplicationID int `gorm:"not null"`
|
ApplicationID int `gorm:"not null;uniqueIndex:idx_label_application_id"`
|
||||||
Application Application
|
Application Application
|
||||||
DeviceTypeID int `gorm:"not null"`
|
DeviceTypeID int `gorm:"not null"`
|
||||||
DeviceType DeviceType
|
DeviceType DeviceType
|
||||||
|
@ -4,6 +4,7 @@ package database
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
//"time"
|
//"time"
|
||||||
|
"fmt"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@ -27,13 +28,13 @@ func NewDatabaseHandle(dsn string) *DatabaseHandle {
|
|||||||
return &db
|
return &db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dbh *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
||||||
if ! dbh.initialized {
|
if ! self.initialized {
|
||||||
log.Printf("Database connection not initialized, can not store, measurement %s lost", measurement)
|
log.Printf("Database connection not initialized, can not store, measurement %s lost", measurement)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result := dbh.dbh.Create(measurement)
|
result := self.dbh.Create(measurement)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
log.Printf("Unable to insert, measurement %s lost, error: %s", measurement, result.Error)
|
log.Printf("Unable to insert, measurement %s lost, error: %s", measurement, result.Error)
|
||||||
return
|
return
|
||||||
@ -42,4 +43,27 @@ func (dbh *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
|||||||
log.Println("Successfully stored measurement")
|
log.Println("Successfully stored measurement")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *DatabaseHandle) GetDeviceByLabelAndApplication(applicationLabel string, deviceLabel string) (*Device, error) {
|
||||||
|
if ! self.initialized {
|
||||||
|
err := fmt.Errorf("Database connection not initialized")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var device Device
|
||||||
|
result := self.dbh.
|
||||||
|
Preload("Application").
|
||||||
|
Preload("DeviceType").
|
||||||
|
Joins("JOIN applications ON devices.application_id = applications.id").
|
||||||
|
Where("devices.label = ? AND applications.label = ?", deviceLabel, applicationLabel).
|
||||||
|
First(&device)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
err := fmt.Errorf("Query failed: %s", result.Error)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
11
src/udi/handlers/ttn/models/emuProfIILoRa/emuProfIILoRa.go
Normal file
11
src/udi/handlers/ttn/models/emuProfIILoRa/emuProfIILoRa.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package emuProfIILoRa
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Parse(decodedPayload interface{}) ([]database.VariableType, error) {
|
||||||
|
return nil, fmt.Errorf("Nothing works so far")
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,14 @@ import "fmt"
|
|||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
|
import "udi/handlers/ttn/models/emuProfIILoRa"
|
||||||
|
import "udi/database"
|
||||||
|
|
||||||
var idSeq int = 0
|
var idSeq int = 0
|
||||||
|
|
||||||
type TTNHandler struct {
|
type TTNHandler struct {
|
||||||
id int
|
id int
|
||||||
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
type uplinkMessage struct {
|
type uplinkMessage struct {
|
||||||
@ -66,6 +69,7 @@ func NewTTNHandler(config config.HandlerConfigT) handler.Handler {
|
|||||||
id: idSeq,
|
id: idSeq,
|
||||||
}
|
}
|
||||||
idSeq += 1
|
idSeq += 1
|
||||||
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,15 +77,20 @@ func (self *TTNHandler) GetId() string {
|
|||||||
return fmt.Sprintf("TTN%d", self.id)
|
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) {
|
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 uplinkMessage uplinkMessage
|
var uplinkMessage uplinkMessage
|
||||||
err := json.Unmarshal([]byte(message.Payload), &uplinkMessage)
|
err := json.Unmarshal([]byte(message.Payload), &uplinkMessage)
|
||||||
if err != nil {
|
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
|
var attributes attributes
|
||||||
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
|
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 }
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user