forked from brocaar/chirpstack-simulator
/
api.go
113 lines (88 loc) · 2.76 KB
/
api.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
package as
import (
"context"
"crypto/tls"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/brocaar/chirpstack-api/go/v3/as/external/api"
"github.com/brocaar/chirpstack-simulator/internal/config"
)
var clientConn *grpc.ClientConn
var mqttClient mqtt.Client
type jwtCredentials struct {
token string
}
func (j *jwtCredentials) GetRequestMetadata(ctx context.Context, url ...string) (map[string]string, error) {
return map[string]string{
"authorization": j.token,
}, nil
}
func (j *jwtCredentials) RequireTransportSecurity() bool {
return false
}
// Setup configures the AS API client.
func Setup(c config.Config) error {
conf := c.ApplicationServer
// connect gRPC
log.WithFields(log.Fields{
"server": conf.API.Server,
"insecure": conf.API.Insecure,
}).Info("as: connecting api client")
dialOpts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithPerRPCCredentials(&jwtCredentials{token: conf.API.JWTToken}),
}
if conf.API.Insecure {
dialOpts = append(dialOpts, grpc.WithInsecure())
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, conf.API.Server, dialOpts...)
if err != nil {
return errors.Wrap(err, "grpc dial error")
}
clientConn = conn
// connect MQTT
opts := mqtt.NewClientOptions()
opts.AddBroker(conf.Integration.MQTT.Server)
opts.SetUsername(conf.Integration.MQTT.Username)
opts.SetPassword(conf.Integration.MQTT.Password)
opts.SetCleanSession(true)
opts.SetAutoReconnect(true)
log.WithFields(log.Fields{
"server": conf.Integration.MQTT.Server,
}).Info("as: connecting to mqtt broker")
mqttClient = mqtt.NewClient(opts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
return errors.Wrap(token.Error(), "mqtt client connect error")
}
return nil
}
func ServiceProfile() api.ServiceProfileServiceClient {
return api.NewServiceProfileServiceClient(clientConn)
}
func Gateway() api.GatewayServiceClient {
return api.NewGatewayServiceClient(clientConn)
}
func NetworkServer() api.NetworkServerServiceClient {
return api.NewNetworkServerServiceClient(clientConn)
}
func DeviceProfile() api.DeviceProfileServiceClient {
return api.NewDeviceProfileServiceClient(clientConn)
}
func Application() api.ApplicationServiceClient {
return api.NewApplicationServiceClient(clientConn)
}
func Device() api.DeviceServiceClient {
return api.NewDeviceServiceClient(clientConn)
}
// MQTTClient returns the MQTT client for the Application Server MQTT integration.
func MQTTClient() mqtt.Client {
return mqttClient
}