-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
160 lines (135 loc) · 3.94 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
jwt "github.com/dgrijalva/jwt-go"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/tiiuae/communication_link/missionengine"
ros "github.com/tiiuae/communication_link/ros"
)
const (
registryID = "fleet-registry"
projectID = "auto-fleet-mgnt"
region = "europe-west1"
algorithm = "RS256"
defaultServer = "ssl://mqtt.googleapis.com:8883"
)
var (
deafultFlagSet = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
deviceID = deafultFlagSet.String("device_id", "", "The provisioned device id")
mqttBrokerAddress = deafultFlagSet.String("mqtt_broker", "", "MQTT broker protocol, address and port")
privateKeyPath = deafultFlagSet.String("private_key", "/enclave/rsa_private.pem", "The private key for the MQTT authentication")
)
// MQTT parameters
const (
TopicType = "events" // or "state"
QoS = 1 // QoS 2 isn't supported in GCP
Retain = false
Username = "unused" // always this value in GCP
)
func main() {
deafultFlagSet.Parse(os.Args[1:])
// attach sigint & sigterm listeners
terminationSignals := make(chan os.Signal, 1)
signal.Notify(terminationSignals, syscall.SIGINT, syscall.SIGTERM)
// quitFunc will be called when process is terminated
ctx, quitFunc := context.WithCancel(context.Background())
// wait group will make sure all goroutines have time to clean up
var wg sync.WaitGroup
mqttClient := newMQTTClient()
defer mqttClient.Disconnect(1000)
localNode := ros.InitRosNode(*deviceID, "communication_link")
defer localNode.ShutdownRosNode()
fleetNode := ros.InitRosNode("fleet", "communication_link")
defer fleetNode.ShutdownRosNode()
me := missionengine.New(ctx, &wg, localNode, fleetNode, mqttClient, *deviceID)
startTelemetry(ctx, &wg, mqttClient, localNode)
startCommandHandlers(ctx, &wg, mqttClient, localNode, me)
// wait for termination and close quit to signal all
<-terminationSignals
// cancel the main context
log.Printf("Shuttding down..")
quitFunc()
// wait until goroutines have done their cleanup
log.Printf("Waiting for routines to finish..")
wg.Wait()
log.Printf("Signing off - BYE")
}
func newMQTTClient() mqtt.Client {
serverAddress := *mqttBrokerAddress
if serverAddress == "" {
serverAddress = defaultServer
}
log.Printf("address: %v", serverAddress)
// generate MQTT client
clientID := fmt.Sprintf(
"projects/%s/locations/%s/registries/%s/devices/%s",
projectID, region, registryID, *deviceID)
log.Println("Client ID:", clientID)
// load private key
keyData, err := ioutil.ReadFile(*privateKeyPath)
if err != nil {
panic(err)
}
var key interface{}
switch algorithm {
case "RS256":
key, err = jwt.ParseRSAPrivateKeyFromPEM(keyData)
case "ES256":
key, err = jwt.ParseECPrivateKeyFromPEM(keyData)
default:
log.Fatalf("Unknown algorithm: %s", algorithm)
}
if err != nil {
panic(err)
}
// generate JWT as the MQTT password
t := time.Now()
token := jwt.NewWithClaims(jwt.GetSigningMethod(algorithm), &jwt.StandardClaims{
IssuedAt: t.Unix(),
ExpiresAt: t.Add(time.Minute * 120).Unix(),
Audience: projectID,
})
pass, err := token.SignedString(key)
if err != nil {
panic(err)
}
// configure MQTT client
opts := mqtt.NewClientOptions().
AddBroker(serverAddress).
SetClientID(clientID).
SetUsername(Username).
SetTLSConfig(&tls.Config{MinVersion: tls.VersionTLS12}).
SetPassword(pass).
SetProtocolVersion(4) // Use MQTT 3.1.1
client := mqtt.NewClient(opts)
for {
// retury for ever
// connect to GCP Cloud IoT Core
log.Printf("Connecting MQTT...")
tok := client.Connect()
if err := tok.Error(); err != nil {
panic(err)
}
if !tok.WaitTimeout(time.Second * 5) {
log.Println("Connection Timeout")
continue
}
if err := tok.Error(); err != nil {
panic(err)
}
log.Printf("..Connected")
break
}
// need mqtt reconnect each 120 minutes for long use
return client
}