-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
120 lines (104 loc) · 3.04 KB
/
gateway.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package gateway
import (
"sync"
"time"
pb "github.com/TheThingsNetwork/api/gateway"
"github.com/TheThingsNetwork/api/logfields"
"github.com/TheThingsNetwork/api/monitor/monitorclient"
pb_lorawan "github.com/TheThingsNetwork/api/protocol/lorawan"
pb_router "github.com/TheThingsNetwork/api/router"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
)
// NewGateway creates a new in-memory Gateway structure
func NewGateway(ctx ttnlog.Interface, id string) *Gateway {
ctx = ctx.WithField("GatewayID", id)
gtw := &Gateway{
ID: id,
Status: NewStatusStore(),
Utilization: NewUtilization(),
Schedule: NewSchedule(ctx),
Ctx: ctx,
}
gtw.Schedule.(*schedule).gateway = gtw // FIXME: Issue #420
return gtw
}
// Gateway contains the state of a gateway
type Gateway struct {
ID string
Status StatusStore
Utilization Utilization
Schedule Schedule
LastSeen time.Time
mu sync.RWMutex // Protect token and authenticated
token string
authenticated bool
MonitorStream monitorclient.Stream
Ctx ttnlog.Interface
}
func (g *Gateway) SetAuth(token string, authenticated bool) {
g.mu.Lock()
defer g.mu.Unlock()
g.authenticated = authenticated
if token == g.token {
return
}
g.token = token
g.MonitorStream.Reset()
}
func (g *Gateway) Token() string {
g.mu.RLock()
token := g.token
g.mu.RUnlock()
return token
}
func (g *Gateway) updateLastSeen() {
g.LastSeen = time.Now()
}
func (g *Gateway) HandleStatus(status *pb.Status) (err error) {
g.mu.RLock()
defer g.mu.RUnlock()
status.GatewayTrusted = g.authenticated
if err = g.Status.Update(status); err != nil {
return err
}
g.updateLastSeen()
return nil
}
func (g *Gateway) HandleUplink(uplink *pb_router.UplinkMessage) (err error) {
if err = g.Utilization.AddRx(uplink); err != nil {
return err
}
g.Schedule.Sync(uplink.GatewayMetadata.Timestamp)
g.updateLastSeen()
status, err := g.Status.Get()
if err == nil {
// Inject Gateway location
if uplink.GatewayMetadata.Location == nil {
uplink.GatewayMetadata.Location = status.GetLocation()
}
// Inject Gateway frequency plan
if frequencyPlan, ok := pb_lorawan.FrequencyPlan_value[status.FrequencyPlan]; ok {
md := uplink.GetProtocolMetadata()
if lorawan := md.GetLoRaWAN(); lorawan != nil {
lorawan.FrequencyPlan = pb_lorawan.FrequencyPlan(frequencyPlan)
}
}
}
// Inject authenticated as GatewayTrusted
g.mu.RLock()
defer g.mu.RUnlock()
uplink.GatewayMetadata.GatewayTrusted = g.authenticated
uplink.GatewayMetadata.GatewayID = g.ID
return nil
}
func (g *Gateway) HandleDownlink(identifier string, downlink *pb_router.DownlinkMessage) (err error) {
ctx := g.Ctx.WithField("Identifier", identifier).WithFields(logfields.ForMessage(downlink))
if err = g.Schedule.Schedule(identifier, downlink); err != nil {
ctx.WithError(err).Warn("Could not schedule downlink")
return err
}
ctx.Debug("Scheduled downlink")
return nil
}