-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.66 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
package main
import (
"log"
"net/http"
"os"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type OT_struct struct {
Type string `json:"_type"`
BSSID string `json:"BSSID"`
SSID string `json:"SSID"`
T string `json:"t"`
Batt int `json:"batt"`
Lat float32 `json:"lat"`
Lon float32 `json:"lon"`
Acc int `json:"acc"`
Alt int `json:"alt"`
Vac int `json:"vac"`
Vel int `json:"vel"`
Tid string `json:"tid"`
Tst int32 `json:"tst"`
Topic string `json:"topic"`
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
log.Print("INFO: Connected to MQTT broker")
}
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
log.Printf("ERROR: MQTT Connection lost: %v", err)
}
func main() {
log.Print("INFO: ot2mqtt starting...")
broker_url := os.Getenv("BROKER_URL")
broker_username := os.Getenv("BROKER_USERNAME")
broker_password := os.Getenv("BROKER_PASSWORD")
listen_address := os.Getenv("LISTEN_ADDRESS")
opts := mqtt.NewClientOptions().AddBroker(broker_url)
// opts.SetClientID("ot-pub")
opts.SetUsername(broker_username)
opts.SetPassword(broker_password)
opts.SetAutoReconnect(true)
opts.SetConnectRetry(true)
opts.SetConnectRetryInterval(time.Second * 5)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
mux := http.NewServeMux()
mux.HandleFunc("/pub", httpHandler(client))
log.Printf("INFO: http listener on %s", listen_address)
err := http.ListenAndServe(listen_address, mux)
log.Fatal(err)
}