This commit is contained in:
@ -2,6 +2,7 @@ package ttn
|
||||
|
||||
import "log"
|
||||
import "fmt"
|
||||
import "encoding/json"
|
||||
import "udi/config"
|
||||
import "udi/handlers/handler"
|
||||
|
||||
@ -11,6 +12,54 @@ type TTNHandler struct {
|
||||
id int
|
||||
}
|
||||
|
||||
type uplinkMessage struct {
|
||||
EndDeviceIds struct {
|
||||
DeviceId string `json:"device_id"`
|
||||
ApplicationIds struct {
|
||||
ApplicationId string `json:"application_id"`
|
||||
} `json:"application_ids"`
|
||||
DevEui string `json:"dev_eui"`
|
||||
JoinEui string `json:"join_eui"`
|
||||
DevAddr string `json:"dev_addr"`
|
||||
} `json:"end_device_ids"`
|
||||
ReceivedAt string `json:"received_at"`
|
||||
UplinkMessage struct {
|
||||
FCnt int `json:"f_cnt"`
|
||||
FPort int `json:"f_port"`
|
||||
FrmPayload string `json:"frm_payload"`
|
||||
DecodedPayload map[string]interface{} `json:"decoded_payload"`
|
||||
RxMetadata []struct {
|
||||
GatewayIds struct {
|
||||
GatewayId string `json:"gateway_id"`
|
||||
Eui string `json:"eui"`
|
||||
} `json:"gateway_ids"`
|
||||
Time string `json:"time"`
|
||||
Rssi int `json:"rssi"`
|
||||
ChannelRssi int `json:"channel_rssi"`
|
||||
Snr float32 `json:"snr"`
|
||||
ChannelIndex int `json:"channel_index"`
|
||||
} `json:"rx_metadata"`
|
||||
ConsumedAirtime string `json:"consumed_airtime"`
|
||||
} `json:"uplink_message"`
|
||||
}
|
||||
|
||||
type gatewayAttributes struct {
|
||||
GatewayId string `json:"gateway_id"`
|
||||
Rssi int `json:"rssi"`
|
||||
Snr float32 `json:"snr"`
|
||||
}
|
||||
|
||||
type attributes struct {
|
||||
DeviceId string `json:"device_id"`
|
||||
ApplicationId string `json:"application_id"`
|
||||
FCnt int `json:"f_cnt"`
|
||||
FPort int `json:"f_port"`
|
||||
FrmPayload string `json:"frm_payload"`
|
||||
Gateways []gatewayAttributes `json:"gateways"`
|
||||
ConsumedAirtime string `json:"consumed_airtime"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
func NewTTNHandler(config config.HandlerConfigT) handler.Handler {
|
||||
t := &TTNHandler {
|
||||
@ -26,6 +75,27 @@ func (self *TTNHandler) GetId() string {
|
||||
|
||||
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)
|
||||
}
|
||||
log.Printf("Parsed message: %s", uplinkMessage)
|
||||
|
||||
var attributes attributes
|
||||
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
|
||||
attributes.ApplicationId = uplinkMessage.EndDeviceIds.ApplicationIds.ApplicationId
|
||||
attributes.FCnt = uplinkMessage.UplinkMessage.FCnt
|
||||
attributes.FPort = uplinkMessage.UplinkMessage.FPort
|
||||
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
|
||||
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
|
||||
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
|
||||
log.Printf("RXM: %s", rxm)
|
||||
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
|
||||
attributes.Gateways = append(attributes.Gateways, g)
|
||||
}
|
||||
log.Printf("Attributes: %s", attributes)
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user