소스 검색

pipoint: make the MQTT server and Mavlink port configurable.

Michael Hope 9 달 전
부모
커밋
3c466dba96
1개의 변경된 파일26개의 추가작업 그리고 14개의 파일을 삭제
  1. 26 14
      pipoint/main.go

+ 26 - 14
pipoint/main.go 파일 보기

@@ -16,7 +16,7 @@
16 16
 package main
17 17
 
18 18
 import (
19
-	"log"
19
+	"flag"
20 20
 	"net/http"
21 21
 
22 22
 	"juju.net.nz/x/pipoint"
@@ -28,31 +28,43 @@ import (
28 28
 )
29 29
 
30 30
 func main() {
31
-	p := pipoint.NewPiPoint()
31
+	mqttUrl := flag.String("mqtt.url", "", "URI of the MQTT server, such as tls://iot.juju.net.nz:8883")
32
+	mavAddr := flag.String("mavlink.address", ":14550", "Address to listen on for Mavlink messages")
32 33
 
33
-	mav := mavlink.NewUDPAdaptor(":14550")
34
-	driver := mavlink.NewDriver(mav)
34
+	flag.Parse()
35 35
 
36
-	// TODO(michaelh): make configurable.
37
-	mq := mqtt.NewAdaptor("tls://iot.juju.net.nz:8883", "pipoint")
38
-	mq.SetAutoReconnect(true)
39
-	p.AddMQTT(mq)
36
+	var cons []gobot.Connection
37
+	var drivers []gobot.Device
40 38
 
41
-	http.HandleFunc("/metrics", p.Params.Metrics)
39
+	pi := pipoint.NewPiPoint()
40
+
41
+	if mavAddr != nil && *mavAddr != "" {
42
+		mav := mavlink.NewUDPAdaptor(*mavAddr)
43
+		cons = append(cons, mav)
44
+		driver := mavlink.NewDriver(mav)
45
+		drivers = append(drivers, driver)
46
+		driver.On(driver.Event(mavlink.MessageEvent), pi.Message)
47
+	}
48
+
49
+	if mqttUrl != nil && *mqttUrl != "" {
50
+		mq := mqtt.NewAdaptor(*mqttUrl, "pipoint")
51
+		cons = append(cons, mq)
52
+		mq.SetAutoReconnect(true)
53
+		pi.AddMQTT(mq)
54
+	}
55
+
56
+	http.HandleFunc("/metrics", pi.Params.Metrics)
42 57
 
43 58
 	master := gobot.NewMaster()
44 59
 	a := api.NewAPI(master)
45 60
 	a.Start()
46 61
 
47 62
 	robot := gobot.NewRobot("pipoint",
48
-		[]gobot.Connection{mav, mq},
49
-		[]gobot.Device{driver},
63
+		cons, drivers,
50 64
 		func() {
51
-			go p.Run()
52
-			driver.On(driver.Event(mavlink.MessageEvent), p.Message)
65
+			go pi.Run()
53 66
 		})
54 67
 
55 68
 	master.AddRobot(robot)
56
-	log.Println("Start")
57 69
 	master.Start()
58 70
 }