-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT.go
executable file
·312 lines (275 loc) · 9.63 KB
/
MQTT.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"encoding/json"
"fmt"
"strings"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
//define a function for the default message handler
// var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
// fmt.Printf("TOPIC: %s\n", msg.Topic())
// fmt.Printf("MSG: %s\n", msg.Payload())
// }
func publishDataToMQTT(buf []byte, dev1 device, sDevicetype string, sEndpointdest string, jsondata string) {
var databuffer []byte
var err error
fmt.Println("dev1.RawData", dev1.RawData)
if !dev1.RawData {
switch strings.ToLower(dev1.Devicetype) {
case "oy1100":
case "oy1110":
var decodeddata decoded1100data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1200":
var decodeddata decoded1200data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1210":
var decodeddata decoded1210data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1320":
var decodeddata decoded1320data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1320v1":
var decodeddata decoded1320v1data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1400":
var decodeddata decoded1400data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1600":
var decodeddata decoded1600data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1600v1":
var decodeddata decoded1600v1data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1700":
var decodeddata decoded1700data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "oy1700v1":
var decodeddata decoded1700v1data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "peoplecounter":
var decodeddata decodedpeoplecounterdata
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "tetraedretillquistdiz":
var decodeddata decodedTETRAEDRETillquistDIZdata
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "tetraedre_abb_b24":
var decodeddata decodedTETRAEDRE_ABB_B24data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "wateriwmlr3":
var decodeddata decodedwateriwmlr3data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "landisgyr":
var decodeddata decodedlandisgyrdata
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "tds_100f_flow":
var decodeddata decodedtds_100f_flowdata
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "honeywell_ew773":
var decodeddata decodedHoneywell_EW773data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "tetraedregwfmtk3":
var decodeddata decodedTetraedreGWFMTK3data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
case "tetraedrelogt550":
var decodeddata decodedTetraedreLoGT550data
if err := json.Unmarshal([]byte(buf), &decodeddata); err != nil {
//fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
return
}
databuffer, err = json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
default:
return
}
}
//create a ClientOptions struct setting the broker address, clientid, turn
//off trace output and set the default message handler
res1 := strings.Split(sEndpointdest, "*")
fmt.Println("res1[0]", res1[0])
fmt.Println("res1[1]", res1[1])
fmt.Println("res1[2]", res1[2])
fmt.Println("res1[3]", res1[3])
fmt.Println("res1[4]", res1[4])
//fmt.Println("appEui Adil: ", appEui)
opts := MQTT.NewClientOptions().AddBroker(res1[0])
opts.SetClientID("LoraDataPublisher")
opts.SetDefaultPublishHandler(f)
opts.SetUsername(res1[1])
opts.SetPassword(res1[2])
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
defer c.Disconnect(300)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
fmt.Println("Publishing")
//Publish messages to /go-mqtt/sample at qos 1 and wait for the receipt
//from the server after sending each message
//postdata := "{ \"d\" :" + jsonStr + "}"
//fmt.Println("Data: ", postdata)
if !dev1.RawData {
postdata := string(databuffer)
appEui := postdata[71:94]
topic := "/tplora/" + res1[1] + "/" + res1[3] + "/" + res1[4] + "/" + appEui + "/" + dev1.Deviceeui + "/tpingress"
token := c.Publish(topic, 0, false, postdata)
//fmt.Println("Token --> ", token)
token.Wait()
//time.Sleep(3 * time.Second)
c.Disconnect(250)
} else {
appEui := jsondata[71:94]
topic := "/tplora/" + res1[1] + "/" + res1[3] + "/" + res1[4] + "/" + appEui + "/" + dev1.Deviceeui + "/tpingress"
token := c.Publish(topic, 0, false, jsondata)
//fmt.Println("Token --> ", token)
token.Wait()
//time.Sleep(3 * time.Second)
c.Disconnect(250)
}
}