kopia lustrzana https://github.com/cyoung/stratux
/traffic websocket - stream traffic updates (1090ES and UAT) as received.
rodzic
2bf466a1c8
commit
94876edd40
|
@ -440,7 +440,7 @@ type WeatherMessage struct {
|
|||
|
||||
var weatherMessages []WeatherMessage
|
||||
|
||||
//TODO: Will likely hook into stream on management interface.
|
||||
// Send update to attached client.
|
||||
func registerADSBTextMessageReceived(msg string) {
|
||||
x := strings.Split(msg, " ")
|
||||
if len(x) < 5 {
|
||||
|
|
|
@ -18,7 +18,7 @@ type SettingMessage struct {
|
|||
|
||||
// Weather updates channel.
|
||||
var weatherUpdate chan WeatherMessage
|
||||
|
||||
var trafficUpdate chan TrafficInfo
|
||||
|
||||
/*
|
||||
The /weather websocket starts off by sending the current buffer of weather messages, then sends updates as they are received.
|
||||
|
@ -38,6 +38,22 @@ func handleWeatherWS(conn *websocket.Conn) {
|
|||
}
|
||||
}
|
||||
|
||||
// Works just as weather updates do.
|
||||
|
||||
func handleTrafficWS(conn *websocket.Conn) {
|
||||
trafficMutex.Lock()
|
||||
for _, traf := range traffic {
|
||||
trafficJSON, _ := json.Marshal(&traf)
|
||||
conn.Write(trafficJSON)
|
||||
}
|
||||
trafficMutex.Unlock()
|
||||
for {
|
||||
lastUpdate := <-trafficUpdate
|
||||
trafficJSON, _ := json.Marshal(&lastUpdate)
|
||||
conn.Write(trafficJSON)
|
||||
}
|
||||
}
|
||||
|
||||
func handleStatusWS(conn *websocket.Conn) {
|
||||
// log.Printf("Web client connected.\n")
|
||||
|
||||
|
@ -67,24 +83,6 @@ func handleStatusWS(conn *websocket.Conn) {
|
|||
}
|
||||
}
|
||||
|
||||
// AJAX call - /getTraffic. Responds with currently tracked traffic targets.
|
||||
func handleTrafficRequest(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
/* From JSON package docs:
|
||||
"JSON objects only support strings as keys; to encode a Go map type it must be of the form map[string]T (where T is any Go type supported by the json package)."
|
||||
*/
|
||||
t := make(map[string]TrafficInfo)
|
||||
trafficMutex.Lock()
|
||||
for icao, traf := range traffic {
|
||||
icaoStr := strconv.FormatInt(int64(icao), 16) // Convert to hex.
|
||||
t[icaoStr] = traf
|
||||
}
|
||||
trafficJSON, _ := json.Marshal(&t)
|
||||
trafficMutex.Unlock()
|
||||
fmt.Fprintf(w, "%s\n", trafficJSON)
|
||||
}
|
||||
|
||||
// AJAX call - /getSituation. Responds with current situation (lat/lon/gdspeed/track/pitch/roll/heading/etc.)
|
||||
func handleSituationRequest(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
@ -177,6 +175,7 @@ func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func managementInterface() {
|
||||
weatherUpdate = make(chan WeatherMessage, 1024)
|
||||
trafficUpdate = make(chan TrafficInfo, 1024)
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir("/var/www")))
|
||||
http.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log"))))
|
||||
|
@ -192,8 +191,14 @@ func managementInterface() {
|
|||
Handler: websocket.Handler(handleWeatherWS)}
|
||||
s.ServeHTTP(w, req)
|
||||
})
|
||||
http.HandleFunc("/traffic",
|
||||
func(w http.ResponseWriter, req *http.Request) {
|
||||
s := websocket.Server{
|
||||
Handler: websocket.Handler(handleTrafficWS)}
|
||||
s.ServeHTTP(w, req)
|
||||
})
|
||||
|
||||
|
||||
http.HandleFunc("/getTraffic", handleTrafficRequest)
|
||||
http.HandleFunc("/getSituation", handleSituationRequest)
|
||||
http.HandleFunc("/getTowers", handleTowersRequest)
|
||||
http.HandleFunc("/getSettings", handleSettingsGetRequest)
|
||||
|
|
|
@ -94,6 +94,11 @@ func sendTrafficUpdates() {
|
|||
}
|
||||
}
|
||||
|
||||
// Send update to attached client.
|
||||
func registerTrafficUpdate(ti TrafficInfo) {
|
||||
trafficUpdate <- ti
|
||||
}
|
||||
|
||||
func makeTrafficReport(ti TrafficInfo) {
|
||||
msg := make([]byte, 28)
|
||||
// See p.16.
|
||||
|
@ -352,6 +357,7 @@ func parseDownlinkReport(s string) {
|
|||
}
|
||||
|
||||
traffic[ti.Icao_addr] = ti
|
||||
registerTrafficUpdate(ti)
|
||||
seenTraffic[ti.Icao_addr] = true // Mark as seen.
|
||||
}
|
||||
|
||||
|
@ -505,6 +511,7 @@ func esListen() {
|
|||
}
|
||||
|
||||
traffic[icaoDec] = ti // Update information on this ICAO code.
|
||||
registerTrafficUpdate(ti)
|
||||
seenTraffic[icaoDec] = true // Mark as seen.
|
||||
trafficMutex.Unlock()
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue