Browse Source

perhaps overly simple reconnect logic for the sbs server

sungo 4 months ago
parent
commit
9631708ee3
1 changed files with 144 additions and 132 deletions
  1. 144 132
      main.go

+ 144 - 132
main.go

@@ -35,14 +35,15 @@ func init() {
 }
 
 type cmdFlags struct {
-	URL             string
-	ClientRefresh   int
-	ReceiverLat     float64
-	ReceiverLon     float64
-	AircraftTimeout int
-	ExpiryCheckTime int
-	UnhateCORS      bool
-	ListenOn        string
+	URL              string
+	ClientRefresh    int
+	ReceiverLat      float64
+	ReceiverLon      float64
+	AircraftTimeout  int
+	ExpiryCheckTime  int
+	UnhateCORS       bool
+	ListenOn         string
+	SbsReconnectTime int
 }
 
 /////////////
@@ -104,6 +105,7 @@ func main() {
 	flag.IntVar(&Flags.ExpiryCheckTime, "expiry-check", 1, "How often, in seconds, to check for expired aircraft")
 	flag.BoolVar(&Flags.UnhateCORS, "disable-insecure-cors", false, "By default, the http server allows all origins via CORS. If you dislike this behavior, use this flag")
 	flag.StringVar(&Flags.ListenOn, "listen", ":8000", "Listen ip:port for the HTTP server")
+	flag.IntVar(&Flags.SbsReconnectTime, "sbs-reconnect", 1, "Time to pause between attempts to connect to the SBS server")
 
 	flag.Parse()
 
@@ -137,160 +139,170 @@ func main() {
 // Stream data from dump1090
 /////////////
 func talkToDump1090(url string) {
-	// BUG(sungo) need reconnect logic
-	fmt.Printf("Connecting to %s ...\n", url)
-
-	conn, err := net.Dial("tcp", url)
-	if err != nil {
-		fmt.Println(err)
-		os.Exit(1)
-	}
-	fmt.Println("Connected.")
-
-	buf := bufio.NewReader(conn)
 	for {
-		str, err := buf.ReadString('\n')
-		if err != nil {
-			ddp(err)
-			continue
-		}
+		fmt.Printf("Connecting to %s ...\n", url)
 
-		if len(str) == 0 {
-			continue
-		}
-		msg, err := sbs.Parse(str)
+		conn, err := net.Dial("tcp", url)
 		if err != nil {
-			ddp(err)
+			fmt.Printf(
+				"Failed to connect to %s: %s\nWill attempt reconnection in %d seconds\n",
+				url,
+				err,
+				Flags.SbsReconnectTime,
+			)
+			time.Sleep(time.Duration(Flags.SbsReconnectTime) * time.Second)
 			continue
 		}
 
-		atomic.AddUint64(&TotalMessages, 1)
+		fmt.Println("Connected.")
 
-		if msg.MessageType != sbs.Transmission {
-			continue
-		}
-		if msg.HexIdent == "" {
-			continue
-		}
+		buf := bufio.NewReader(conn)
+		for {
+			str, err := buf.ReadString('\n')
+			if err != nil {
+				ddp(err)
+				break
+			}
 
-		QueueMutex.Lock()
-		a, ok := CurrentAircraft[msg.HexIdent]
-		if ok {
-			now := time.Now()
+			if len(str) == 0 {
+				continue
+			}
+			msg, err := sbs.Parse(str)
+			if err != nil {
+				ddp(err)
+				continue
+			}
 
-			a.Seen = now.Sub(a.lastSeen).Seconds()
-			a.lastSeen = now
+			atomic.AddUint64(&TotalMessages, 1)
 
-			if msg.HexIdent != "" {
-				a.Hex = msg.HexIdent
-			}
-			if msg.Squawk != "" {
-				a.Squawk = msg.Squawk
+			if msg.MessageType != sbs.Transmission {
+				continue
 			}
-			if msg.CallSign != "" {
-				a.Flight = msg.CallSign
+			if msg.HexIdent == "" {
+				continue
 			}
 
-			if msg.TransmissionType == sbs.ESSurfacePosition {
-				// Alt, GS, Trk, Lat, Long, Gnd
-				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
-				} else {
-					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
-				}
+			QueueMutex.Lock()
+			a, ok := CurrentAircraft[msg.HexIdent]
+			if ok {
+				now := time.Now()
 
-				a.Track = msg.Track
-				a.Speed = msg.GroundSpeed
+				a.Seen = now.Sub(a.lastSeen).Seconds()
+				a.lastSeen = now
 
-				a.Lat = msg.Latitude
-				a.Lon = msg.Longitude
-				a.SeenPos = now.Sub(a.lastPosSeen).Seconds()
-				a.lastPosSeen = now
-
-			} else if msg.TransmissionType == sbs.ESAirbornePosition {
-				// Alt, Lat, Long, Alert, Emerg, SPI, Ground
-				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
-				} else {
-					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+				if msg.HexIdent != "" {
+					a.Hex = msg.HexIdent
 				}
-
-				a.Lat = msg.Latitude
-				a.Lon = msg.Longitude
-				a.SeenPos = now.Sub(a.lastPosSeen).Seconds()
-				a.lastPosSeen = now
-
-			} else if msg.TransmissionType == sbs.ESAirborneVelocity {
-				// GS, Trk, VR
-				a.VertRate = msg.VerticalRate
-				a.Track = msg.Track
-				a.Speed = msg.GroundSpeed
-
-			} else if msg.TransmissionType == sbs.SurveillanceAlt {
-				// Alt, Alrt, SPI, Ground
-				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
-				} else {
-					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+				if msg.Squawk != "" {
+					a.Squawk = msg.Squawk
+				}
+				if msg.CallSign != "" {
+					a.Flight = msg.CallSign
 				}
 
-			} else if msg.TransmissionType == sbs.SurveillanceID {
-				// Alt, Sq, Alrt, Emerg, SPI, Ground
+				if msg.TransmissionType == sbs.ESSurfacePosition {
+					// Alt, GS, Trk, Lat, Long, Gnd
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					} else {
+						a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+					}
+
+					a.Track = msg.Track
+					a.Speed = msg.GroundSpeed
+
+					a.Lat = msg.Latitude
+					a.Lon = msg.Longitude
+					a.SeenPos = now.Sub(a.lastPosSeen).Seconds()
+					a.lastPosSeen = now
+
+				} else if msg.TransmissionType == sbs.ESAirbornePosition {
+					// Alt, Lat, Long, Alert, Emerg, SPI, Ground
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					} else {
+						a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+					}
+
+					a.Lat = msg.Latitude
+					a.Lon = msg.Longitude
+					a.SeenPos = now.Sub(a.lastPosSeen).Seconds()
+					a.lastPosSeen = now
+
+				} else if msg.TransmissionType == sbs.ESAirborneVelocity {
+					// GS, Trk, VR
+					a.VertRate = msg.VerticalRate
+					a.Track = msg.Track
+					a.Speed = msg.GroundSpeed
+
+				} else if msg.TransmissionType == sbs.SurveillanceAlt {
+					// Alt, Alrt, SPI, Ground
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					} else {
+						a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+					}
+
+				} else if msg.TransmissionType == sbs.SurveillanceID {
+					// Alt, Sq, Alrt, Emerg, SPI, Ground
+
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					} else {
+						a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+					}
+
+				} else if msg.TransmissionType == sbs.AirToAir {
+					// Alt, Ground
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					} else {
+						a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+					}
+
+				} else if msg.TransmissionType == sbs.AllCallReply {
+					// Ground
+					if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
+						a.Altitude = "ground"
+					}
 
-				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
-				} else {
-					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
 				}
 
-			} else if msg.TransmissionType == sbs.AirToAir {
-				// Alt, Ground
-				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
-				} else {
-					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
+				a.Messages = a.Messages + 1
+			} else {
+				aircraft := &Aircraft{
+					Hex:      msg.HexIdent,
+					Squawk:   msg.Squawk,
+					Flight:   msg.CallSign,
+					Lat:      msg.Latitude,
+					Lon:      msg.Longitude,
+					SeenPos:  0,
+					VertRate: msg.VerticalRate,
+					Track:    msg.Track,
+					Speed:    msg.GroundSpeed,
+					Messages: 0,
+					Seen:     0,
+					lastSeen: time.Now(),
 				}
-
-			} else if msg.TransmissionType == sbs.AllCallReply {
-				// Ground
 				if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-					a.Altitude = "ground"
+					aircraft.Altitude = "ground"
+				} else {
+					aircraft.Altitude = fmt.Sprintf("%d", msg.Altitude)
 				}
 
+				CurrentAircraft[aircraft.Hex] = aircraft
 			}
 
-			a.Messages = a.Messages + 1
-
-			// ddp(a)
-		} else {
-			aircraft := &Aircraft{
-				Hex:      msg.HexIdent,
-				Squawk:   msg.Squawk,
-				Flight:   msg.CallSign,
-				Lat:      msg.Latitude,
-				Lon:      msg.Longitude,
-				SeenPos:  0,
-				VertRate: msg.VerticalRate,
-				Track:    msg.Track,
-				Speed:    msg.GroundSpeed,
-				Messages: 0,
-				Seen:     0,
-				lastSeen: time.Now(),
-			}
-			if msg.IsOnGround.IsSet && msg.IsOnGround.Value == true {
-				aircraft.Altitude = "ground"
-			} else {
-				aircraft.Altitude = fmt.Sprintf("%d", msg.Altitude)
-			}
-
-			CurrentAircraft[aircraft.Hex] = aircraft
-			// ddp(aircraft)
+			QueueMutex.Unlock()
 		}
 
-		QueueMutex.Unlock()
-
+		fmt.Printf(
+			"Disconnected from %s\nWill attempt reconnection in %d seconds\n",
+			url,
+			Flags.SbsReconnectTime,
+		)
+		time.Sleep(time.Duration(Flags.SbsReconnectTime) * time.Second)
 	}
-
 }
 
 /////////////