pull/71/head
Christopher Young 2015-09-30 10:22:27 -04:00
rodzic 2d770d5b2e
commit 797f71a4da
1 zmienionych plików z 59 dodań i 51 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ func statusSender(conn *websocket.Conn) {
<-timer.C
update, _ := json.Marshal(InfoMessage{status: &globalStatus, settings: &globalSettings})
// TODO: once we switch to hte new WebUI, we will not send settings via websocket
// TODO: once we switch to hte new WebUI, we will not send settings via websocket
// update, _ := json.Marshal(&globalStatus)
_, err := conn.Write(update)
@ -42,7 +42,7 @@ func handleManagementConnection(conn *websocket.Conn) {
// log.Printf("Web client connected.\n")
go statusSender(conn)
// TODO: once we have the new WebUI established, the websocket will nolonger receive settings changes
// TODO: once we have the new WebUI established, the websocket will nolonger receive settings changes
for {
var msg SettingMessage
err := websocket.JSON.Receive(conn, &msg)
@ -74,8 +74,8 @@ func handleManagementConnection(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")
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)."
*/
@ -92,73 +92,81 @@ func handleTrafficRequest(w http.ResponseWriter, r *http.Request) {
// 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", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
situationJSON, _ := json.Marshal(&mySituation)
fmt.Fprintf(w, "%s\n", situationJSON)
}
// AJAX call - /getTowers. Responds with all ADS-B ground towers that have sent messages that we were able to parse, along with its stats.
func handleTowersRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
towersJSON, _ := json.Marshal(&ADSBTowers)
fmt.Fprintf(w, "%s\n", towersJSON)
}
// AJAX call - /getSettings. Responds with all stratux.conf data.
func handleSettingsGetRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
settingsJSON, _ := json.Marshal(&globalSettings)
fmt.Fprintf(w, "%s\n", settingsJSON)
}
// AJAX call - /setSettings. receives via POST command, any/all stratux.conf data.
func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {
// define header in support of cross-domain AJAX
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Method", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Set("Content-Type", "application/json")
// for an OPTION method request, we return header without processing.
// this insures we are recognized as supporting cross-domain AJAX REST calls
if (r.Method == "POST") {
// raw, _ := httputil.DumpRequest(r, true)
// log.Printf("handleSettingsSetRequest:raw: %s\n", raw)
// define header in support of cross-domain AJAX
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Method", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(r.Body);
for {
var msg map[string]interface{} // support arbitrary JSON
// for an OPTION method request, we return header without processing.
// this insures we are recognized as supporting cross-domain AJAX REST calls
if r.Method == "POST" {
// raw, _ := httputil.DumpRequest(r, true)
// log.Printf("handleSettingsSetRequest:raw: %s\n", raw)
err := decoder.Decode(&msg)
if err == io.EOF {
break
} else if err != nil {
log.Printf("handleSettingsSetRequest:error: %s\n", err.Error())
} else {
for key, val := range msg {
// log.Printf("handleSettingsSetRequest:json: testing for key:%s of type %s\n", key, reflect.TypeOf(val))
switch key {
case "UAT_Enabled": globalSettings.UAT_Enabled = val.(bool)
case "ES_Enabled": globalSettings.ES_Enabled = val.(bool)
case "GPS_Enabled": globalSettings.GPS_Enabled = val.(bool)
case "AHRS_Enabled": globalSettings.AHRS_Enabled = val.(bool)
case "DEBUG": globalSettings.DEBUG = val.(bool)
case "ReplayLog": globalSettings.ReplayLog = val.(bool)
case "PPM": globalSettings.PPM = int(val.(float64))
default: log.Printf("handleSettingsSetRequest:json: unrecognized key:%s\n", key)
}
}
saveSettings()
}
}
// while it may be redundent, we return the latest settings
settingsJSON, _ := json.Marshal(&globalSettings)
fmt.Fprintf(w, "%s\n", settingsJSON)
}
decoder := json.NewDecoder(r.Body)
for {
var msg map[string]interface{} // support arbitrary JSON
err := decoder.Decode(&msg)
if err == io.EOF {
break
} else if err != nil {
log.Printf("handleSettingsSetRequest:error: %s\n", err.Error())
} else {
for key, val := range msg {
// log.Printf("handleSettingsSetRequest:json: testing for key:%s of type %s\n", key, reflect.TypeOf(val))
switch key {
case "UAT_Enabled":
globalSettings.UAT_Enabled = val.(bool)
case "ES_Enabled":
globalSettings.ES_Enabled = val.(bool)
case "GPS_Enabled":
globalSettings.GPS_Enabled = val.(bool)
case "AHRS_Enabled":
globalSettings.AHRS_Enabled = val.(bool)
case "DEBUG":
globalSettings.DEBUG = val.(bool)
case "ReplayLog":
globalSettings.ReplayLog = val.(bool)
case "PPM":
globalSettings.PPM = int(val.(float64))
default:
log.Printf("handleSettingsSetRequest:json: unrecognized key:%s\n", key)
}
}
saveSettings()
}
}
// while it may be redundent, we return the latest settings
settingsJSON, _ := json.Marshal(&globalSettings)
fmt.Fprintf(w, "%s\n", settingsJSON)
}
}
func managementInterface() {