~sungo/sbs-http

763ad0f8638bda35f9e03cafe6ed6d03f7a23f08 — sungo 1 year, 9 months ago 5787dcc master
restructure a bit
1 files changed, 80 insertions(+), 95 deletions(-)

M main.go
M main.go => main.go +80 -95
@@ 130,9 130,12 @@ func talkToDump1090(url string) {
				break
			}

			str = strings.TrimSpace(str)

			if len(str) == 0 {
				continue
			}

			msg, err := sbs.Parse(str)
			if err != nil {
				fmt.Printf(


@@ 148,122 151,104 @@ func talkToDump1090(url string) {
			if msg.MessageType != sbs.Transmission {
				continue
			}

			if msg.HexIdent == "" {
				continue
			}

			now := time.Now()

			QueueMutex.Lock()
			a, ok := CurrentAircraft[msg.HexIdent]
			if ok {
				now := time.Now()

				a.Seen = now.Sub(a.LastSeen).Seconds()
				a.LastSeen = now
			if !ok {
				a = &models.Aircraft{Hex: msg.HexIdent}
				CurrentAircraft[a.Hex] = a
			}

				if msg.HexIdent != "" {
					a.Hex = strings.TrimSpace(msg.HexIdent)
				}
				if msg.Squawk != "" {
					a.Squawk = strings.TrimSpace(msg.Squawk)
			a.Seen = now.Sub(a.LastSeen).Seconds()
			a.LastSeen = now

			if msg.HexIdent != "" {
				a.Hex = strings.TrimSpace(msg.HexIdent)
			}
			if msg.Squawk != "" {
				a.Squawk = strings.TrimSpace(msg.Squawk)
			}
			if msg.CallSign != "" {
				a.Flight = strings.TrimSpace(msg.CallSign)
			}

			if msg.TransmissionType == sbs.ESSurfacePosition {
				// Alt, GS, Trk, Lat, Long, Gnd
				if msg.IsOnGround.IsSet && msg.IsOnGround.Value {
					a.Altitude = "ground"
				} else {
					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
				}
				if msg.CallSign != "" {
					a.Flight = strings.TrimSpace(msg.CallSign)

				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 {
					a.Altitude = "ground"
				} else {
					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
				}

				if msg.TransmissionType == sbs.ESSurfacePosition {
					// Alt, GS, Trk, Lat, Long, Gnd
					if msg.IsOnGround.IsSet && msg.IsOnGround.Value {
						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 {
						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 {
						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 {
						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 {
						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 {
						a.Altitude = "ground"
					}
				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 {
					a.Altitude = "ground"
				} else {
					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
				}

				a.Messages = a.Messages + 1
			} else {
				aircraft := &models.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.SurveillanceID {
				// Alt, Sq, Alrt, Emerg, SPI, Ground

				if msg.IsOnGround.IsSet && msg.IsOnGround.Value {
					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 {
					aircraft.Altitude = "ground"
					a.Altitude = "ground"
				} else {
					aircraft.Altitude = fmt.Sprintf("%d", msg.Altitude)
					a.Altitude = fmt.Sprintf("%d", msg.Altitude)
				}

			} else if msg.TransmissionType == sbs.AllCallReply {
				// Ground
				if msg.IsOnGround.IsSet && msg.IsOnGround.Value {
					a.Altitude = "ground"
				}

				CurrentAircraft[aircraft.Hex] = aircraft
				a = aircraft
			}

			a.Messages = a.Messages + 1

			if Flags.ReceiverLat != 0 && Flags.ReceiverLon != 0 {
				if a.Lat != 0 && a.Lon != 0 {
					us := orb.Point{Flags.ReceiverLon, Flags.ReceiverLat}