This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
/
mqtt_mgr.go
270 lines (233 loc) · 7.55 KB
/
mqtt_mgr.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* (C) 2021 Geotab
*
* All files and artifacts in the repository at https://github.com/MEAE-GOT/W3C_VehicleSignalInterfaceImpl
* are licensed under the provisions of the license provided by the LICENSE file in this repository.
*
**/
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/MEAE-GOT/W3C_VehicleSignalInterfaceImpl/utils"
"github.com/akamensky/argparse"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gorilla/websocket"
)
var mqttChannel chan string
type NodeValue struct {
topicId int
topic string
}
type Node struct {
value NodeValue
next *Node
}
type TopicList struct {
head *Node
nodes int
}
var topicList TopicList
func vissV2Receiver(dataConn *websocket.Conn, vissv2Channel chan string) {
defer dataConn.Close()
for {
_, response, err := dataConn.ReadMessage() // receive message from server core
if err != nil {
utils.Error.Println("Datachannel read error:" + err.Error())
break
}
utils.Info.Printf("MQTT mgr: Response from server core:%s\n", string(response))
vissv2Channel <- string(response) // send message to hub
}
}
//TODO add conf file
func getBrokerSocket(isSecure bool) string {
// FVTAddr := os.Getenv("MQTT_BROKER_ADDR")
FVTAddr := "test.mosquitto.org"
if FVTAddr == "" {
FVTAddr = "127.0.0.1"
}
if isSecure == true {
return "ssl://" + FVTAddr + ":8883"
}
return "tcp://" + FVTAddr + ":1883"
}
var publishHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
// mqttChannel <- msg.Topic()
utils.Info.Printf("publishHandler:payload=%s", string(msg.Payload()))
mqttChannel <- string(msg.Payload())
}
func mqttSubscribe(brokerSocket string, topic string) MQTT.Client {
utils.Info.Printf("mqttSubscribe:Topic=%s", topic)
opts := MQTT.NewClientOptions().AddBroker(brokerSocket)
// opts.SetClientID("VIN001")
opts.SetDefaultPublishHandler(publishHandler)
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
if token := c.Subscribe(topic, 0, nil); token.Wait() && token.Error() != nil {
utils.Error.Println(token.Error())
os.Exit(1)
}
return c
}
func getTopic(topicId int) string {
iterator := topicList.head
for i := 0; i < topicList.nodes; i++ {
if iterator.value.topicId == topicId {
return iterator.value.topic
}
iterator = iterator.next
}
return ""
}
func pushTopic(topic string, topicId int) {
var newNode Node
newNode.value.topic = topic
newNode.value.topicId = topicId
newNode.next = nil
if topicList.nodes == 0 {
topicList.head = &newNode
topicList.nodes++
return
}
iterator := topicList.head
for i := 0; i < topicList.nodes; i++ {
if iterator.next == nil {
iterator.next = &newNode
break
}
iterator = iterator.next
}
topicList.nodes++
}
func popTopic(topicId int) { //TODO: to be used at unsubscribe, get, set responses from VISSv2
if topicList.nodes > 0 && topicList.head.value.topicId == topicId {
if topicList.nodes > 1 {
topicList.head = topicList.head.next
} else {
topicList.head = nil
}
topicList.nodes--
}
iterator := topicList.head
var previousNode *Node
i := 0
for i = 0; i < topicList.nodes; i++ {
if iterator.value.topicId == topicId {
break
}
previousNode = iterator
iterator = iterator.next
}
if i == topicList.nodes {
return
}
previousNode.next = iterator.next
topicList.nodes--
}
func publishMessage(brokerSocket string, topic string, payload string) {
utils.Info.Printf("publishMessage:Topic=%s, Payload=%s", topic, payload)
opts := MQTT.NewClientOptions().AddBroker(brokerSocket)
// opts.SetClientID("VIN001")
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
utils.Error.Println(token.Error())
os.Exit(1)
}
token := c.Publish(topic, 0, false, payload)
token.Wait()
c.Disconnect(250)
}
func getVissV2Topic(dataConn *websocket.Conn, regData utils.RegData) string {
vinRequest := "{\"RouterId\":\"" + strconv.Itoa(regData.Mgrid) + `?0", "action":"get", "path":"Vehicle.VehicleIdentification.VIN", "requestId":"570415"}`
err := dataConn.WriteMessage(websocket.TextMessage, []byte(vinRequest))
if err != nil {
utils.Error.Println("Datachannel write error:" + err.Error())
}
_, response, err := dataConn.ReadMessage() // receive message from server core
if err != nil {
utils.Error.Println("Datachannel read error:" + err.Error())
os.Exit(1)
}
vin := extractVin(string(response))
utils.Info.Printf("VIN=%s", vin)
return "/" + vin + "/Vehicle"
}
func extractVin(response string) string {
vinStartIndex := strings.Index(response, "value")
if vinStartIndex == -1 {
utils.Error.Printf("VIN cannot be extracted in %s", response)
os.Exit(1)
}
vinStartIndex += 8 // value”:”
vinEndIndex := utils.NextQuoteMark([]byte(response), vinStartIndex)
return response[vinStartIndex:vinEndIndex]
}
func decomposeMqttPayload(mqttPayload string) (string, string) { // {"topic":"X", "request":"{...}"}
var payloadMap = make(map[string]interface{})
utils.MapRequest(mqttPayload, &payloadMap)
payload, err := json.Marshal(payloadMap["request"])
if err != nil {
utils.Error.Printf("decomposeMqttPayload: cannot marshal request in response=%s", mqttPayload)
os.Exit(1)
}
return payloadMap["topic"].(string), string(payload)
}
func main() {
// Create new parser object
parser := argparse.NewParser("print", "mqtt manager")
// Create string flag
logFile := parser.Flag("", "logfile", &argparse.Options{Required: false, Help: "outputs to logfile in ./logs folder"})
logLevel := parser.Selector("", "loglevel", []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, &argparse.Options{
Required: false,
Help: "changes log output level",
Default: "info"})
// Parse input
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
}
utils.TransportErrorMessage = "MQTT transport mgr-finalizeResponse: JSON encode failed.\n"
utils.InitLog("mqtt-mgr-log.txt", "./logs", *logFile, *logLevel)
regData := utils.RegData{}
utils.RegisterAsTransportMgr(®Data, "MQTT")
mqttChannel = make(chan string)
vissv2Channel := make(chan string)
dataConn := utils.InitDataSession(utils.MuxServer[1], regData)
brokerSocket := getBrokerSocket(false)
serverSubscription := mqttSubscribe(brokerSocket, getVissV2Topic(dataConn, regData))
topicId := 0
topicList.nodes = 0
go vissV2Receiver(dataConn, vissv2Channel) //message reception from server core
utils.Info.Println("**** MQTT manager hub entering server loop... ****")
for {
select {
case mqttPayload := <-mqttChannel:
topic, payload := decomposeMqttPayload(mqttPayload)
utils.Info.Printf("MQTT hub: Message from broker:Topic=%s, Payload=%s\n", topic, payload)
pushTopic(topic, topicId)
// add mgrId + clientId=0 to message, forward to server core
newPrefix := "{\"RouterId\":\"" + strconv.Itoa(regData.Mgrid) + "?" + strconv.Itoa(topicId) + "\", "
request := strings.Replace(payload, "{", newPrefix, 1)
err := dataConn.WriteMessage(websocket.TextMessage, []byte(request)) // send request to server core
if err != nil {
utils.Error.Println("Datachannel write error:" + err.Error())
}
topicId++
case vissv2Message := <-vissv2Channel:
utils.Info.Printf("MQTT hub: Message from VISSv2 server:%s\n", vissv2Message)
// link routerId to topic, remove routerId from message, create mqtt message, send message to mqtt transport
payload, topicHandle := utils.RemoveInternalData(string(vissv2Message))
publishMessage(brokerSocket, getTopic(topicHandle), payload)
default:
time.Sleep(25 * time.Millisecond)
}
}
serverSubscription.Disconnect(250)
}