-
-
Notifications
You must be signed in to change notification settings - Fork 548
/
ul_metadata.go
143 lines (115 loc) · 3.23 KB
/
ul_metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package roaming
import (
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/brocaar/chirpstack-api/go/v3/common"
"github.com/brocaar/chirpstack-api/go/v3/gw"
"github.com/brocaar/chirpstack-network-server/v3/internal/band"
"github.com/brocaar/chirpstack-network-server/v3/internal/helpers"
"github.com/brocaar/lorawan/backend"
)
func ULMetaDataToTXInfo(ulMetaData backend.ULMetaData) (*gw.UplinkTXInfo, error) {
out := &gw.UplinkTXInfo{}
// freq
if ulMetaData.ULFreq != nil {
out.Frequency = uint32(*ulMetaData.ULFreq * 1000000)
}
// data-rate
if ulMetaData.DataRate != nil {
if err := helpers.SetUplinkTXInfoDataRate(out, *ulMetaData.DataRate, band.Band()); err != nil {
return nil, errors.Wrap(err, "set uplink txinfo data-rate error")
}
}
return out, nil
}
func ULMetaDataToRXInfo(ulMetaData backend.ULMetaData) ([]*gw.UplinkRXInfo, error) {
var out []*gw.UplinkRXInfo
for i := range ulMetaData.GWInfo {
gwInfo := ulMetaData.GWInfo[i]
rxInfo := gw.UplinkRXInfo{
GatewayId: gwInfo.ID[:],
Context: gwInfo.ULToken[:],
CrcStatus: gw.CRCStatus_CRC_OK,
}
if gwInfo.RSSI != nil {
rxInfo.Rssi = int32(*gwInfo.RSSI)
}
if gwInfo.SNR != nil {
rxInfo.LoraSnr = *gwInfo.SNR
}
if gwInfo.Lat != nil && gwInfo.Lon != nil {
rxInfo.Location = &common.Location{
Latitude: *gwInfo.Lat,
Longitude: *gwInfo.Lon,
Source: common.LocationSource_UNKNOWN,
}
}
if gwInfo.FineRecvTime != nil {
ts := time.Time(ulMetaData.RecvTime)
ts = ts.Round(time.Second)
ts = ts.Add(time.Duration(*gwInfo.FineRecvTime) * time.Nanosecond)
tsProto, err := ptypes.TimestampProto(ts)
if err != nil {
return nil, errors.Wrap(err, "timestamp proto error")
}
rxInfo.FineTimestampType = gw.FineTimestampType_PLAIN
rxInfo.FineTimestamp = &gw.UplinkRXInfo_PlainFineTimestamp{
PlainFineTimestamp: &gw.PlainFineTimestamp{
Time: tsProto,
},
}
}
out = append(out, &rxInfo)
}
return out, nil
}
func RecvTimeFromRXInfo(rxInfo []*gw.UplinkRXInfo) backend.ISO8601Time {
for _, r := range rxInfo {
if r.Time != nil {
t, err := ptypes.Timestamp(r.Time)
if err != nil {
continue
}
return backend.ISO8601Time(t.UTC())
}
}
return backend.ISO8601Time(time.Now().UTC())
}
func RXInfoToGWInfo(rxInfo []*gw.UplinkRXInfo) ([]backend.GWInfoElement, error) {
var out []backend.GWInfoElement
for i := range rxInfo {
rssi := int(rxInfo[i].Rssi)
var lat, lon *float64
var fineRecvTime *int
if loc := rxInfo[i].Location; loc != nil {
lat = &loc.Latitude
lon = &loc.Longitude
}
if fineTS := rxInfo[i].GetPlainFineTimestamp(); fineTS != nil {
nanos := int(fineTS.GetTime().GetNanos())
fineRecvTime = &nanos
}
b, err := proto.Marshal(rxInfo[i])
if err != nil {
return nil, errors.Wrap(err, "marshal rxinfo error")
}
id := backend.HEXBytes(rxInfo[i].GatewayId)
if len(id) == 8 {
id = id[4:]
}
e := backend.GWInfoElement{
ID: id,
FineRecvTime: fineRecvTime,
RSSI: &rssi,
SNR: &rxInfo[i].LoraSnr,
Lat: lat,
Lon: lon,
ULToken: backend.HEXBytes(b),
DLAllowed: true,
}
out = append(out, e)
}
return out, nil
}