diff --git a/1090es_relay.go b/1090es_relay.go index 3daef79d..da1a906e 100644 --- a/1090es_relay.go +++ b/1090es_relay.go @@ -64,78 +64,70 @@ func main() { mutex := &sync.Mutex{} blips = make(map[int64]PositionInfo) go ipadUpdate(mutex) - - context := new(daemon.Context) - child, _ := context.Reborn() - if child != nil { - fmt.Printf("going into background.\n") - } else { - defer context.Release() + for { + inConn, err := net.Dial("tcp", dump1090Addr) + if err != nil { + time.Sleep(1 * time.Second) + continue + } + rdr := bufio.NewReader(inConn) for { - inConn, err := net.Dial("tcp", dump1090Addr) - if err != nil { - time.Sleep(1 * time.Second) + buf, err := rdr.ReadString('\n') + if err != nil { // Must have disconnected? + break + } + buf = strings.Trim(buf, "\r\n") + //fmt.Printf("%s\n", buf) + x := strings.Split(buf, ",") + //TODO: Add more sophisticated stuff that combines heading/speed updates with the location. + if len(x) < 22 { continue } - rdr := bufio.NewReader(inConn) - for { - buf, err := rdr.ReadString('\n') - if err != nil { // Must have disconnected? - break - } - buf = strings.Trim(buf, "\r\n") - //fmt.Printf("%s\n", buf) - x := strings.Split(buf, ",") - //TODO: Add more sophisticated stuff that combines heading/speed updates with the location. - if len(x) < 22 { - continue - } - icao := x[4] - icaoDec, err := strconv.ParseInt(icao, 16, 32) - if err != nil { - continue - } - mutex.Lock() - // Retrieve previous information on this ICAO code. - var pi PositionInfo - if _, ok := blips[icaoDec]; ok { - pi = blips[icaoDec] - } - - if x[1] == "3" { - //MSG,3,111,11111,AC2BB7,111111,2015/07/28,03:59:12.363,2015/07/28,03:59:12.353,,5550,,,42.35847,-83.42212,,,,,,0 - alt := x[11] - lat := x[14] - lng := x[15] - - //fmt.Printf("icao=%s, icaoDec=%d, alt=%s, lat=%s, lng=%s\n", icao, icaoDec, alt, lat, lng) - pi.alt = alt - pi.lat = lat - pi.lng = lng - } - if x[1] == "4" { - // MSG,4,111,11111,A3B557,111111,2015/07/28,06:13:36.417,2015/07/28,06:13:36.398,,,414,278,,,-64,,,,,0 - vel := x[12] - hdg := x[13] - vr := x[16] - - //fmt.Printf("icao=%s, icaoDec=%d, vel=%s, hdg=%s, vr=%s\n", icao, icaoDec, vel, hdg, vr) - pi.vel = vel - pi.hdg = hdg - pi.vr = vr - } - if x[1] == "1" { - // MSG,1,,,%02X%02X%02X,,,,,,%s,,,,,,,,0,0,0,0 - tail := x[10] - pi.tail = tail - } - - // Update "last seen" (any type of message). - pi.last_seen = time.Now() - - blips[icaoDec] = pi // Update information on this ICAO code. - mutex.Unlock() + icao := x[4] + icaoDec, err := strconv.ParseInt(icao, 16, 32) + if err != nil { + continue } + mutex.Lock() + // Retrieve previous information on this ICAO code. + var pi PositionInfo + if _, ok := blips[icaoDec]; ok { + pi = blips[icaoDec] + } + + if x[1] == "3" { + //MSG,3,111,11111,AC2BB7,111111,2015/07/28,03:59:12.363,2015/07/28,03:59:12.353,,5550,,,42.35847,-83.42212,,,,,,0 + alt := x[11] + lat := x[14] + lng := x[15] + + //fmt.Printf("icao=%s, icaoDec=%d, alt=%s, lat=%s, lng=%s\n", icao, icaoDec, alt, lat, lng) + pi.alt = alt + pi.lat = lat + pi.lng = lng + } + if x[1] == "4" { + // MSG,4,111,11111,A3B557,111111,2015/07/28,06:13:36.417,2015/07/28,06:13:36.398,,,414,278,,,-64,,,,,0 + vel := x[12] + hdg := x[13] + vr := x[16] + + //fmt.Printf("icao=%s, icaoDec=%d, vel=%s, hdg=%s, vr=%s\n", icao, icaoDec, vel, hdg, vr) + pi.vel = vel + pi.hdg = hdg + pi.vr = vr + } + if x[1] == "1" { + // MSG,1,,,%02X%02X%02X,,,,,,%s,,,,,,,,0,0,0,0 + tail := x[10] + pi.tail = tail + } + + // Update "last seen" (any type of message). + pi.last_seen = time.Now() + + blips[icaoDec] = pi // Update information on this ICAO code. + mutex.Unlock() } } }