Update settings when change is received from a Web client.

pull/25/head
Brant K. Kyser 2015-08-30 12:46:43 -05:00
rodzic aa6163ea81
commit 2d9b243e00
2 zmienionych plików z 22 dodań i 9 usunięć

Wyświetl plik

@ -4,6 +4,7 @@ import (
"bufio"
"encoding/hex"
"encoding/json"
"io/ioutil"
"log"
"os"
"runtime"
@ -424,14 +425,12 @@ func readSettings() {
}
func saveSettings() {
fd, err := os.OpenFile(configLocation, os.O_CREATE|os.O_WRONLY, os.FileMode(0644))
defer fd.Close()
jsonSettings, _ := json.Marshal(&globalSettings)
err := ioutil.WriteFile(configLocation, jsonSettings, 0644)
if err != nil {
log.Printf("can't save settings %s: %s\n", configLocation, err.Error())
return
}
jsonSettings, _ := json.Marshal(&globalSettings)
fd.Write(jsonSettings)
log.Printf("wrote settings.\n")
}

Wyświetl plik

@ -11,7 +11,7 @@ import (
type SettingMessage struct {
Setting string `json:"setting"`
Value string `json:"state"`
Value bool `json:"state"`
}
func statusSender(conn *websocket.Conn) {
@ -19,8 +19,11 @@ func statusSender(conn *websocket.Conn) {
for {
<-timer.C
resp, _ := json.Marshal(&globalStatus)
_, err := conn.Write(resp)
statResp, _ := json.Marshal(&globalStatus)
conn.Write(statResp)
settingResp, _ := json.Marshal(&globalSettings)
_, err := conn.Write(settingResp)
if err != nil {
log.Printf("Web client disconnected.\n")
@ -40,9 +43,20 @@ func handleManagementConnection(conn *websocket.Conn) {
} else if err != nil {
log.Printf("handleManagementConnection: %s\n", err.Error())
} else {
// TODO: Update specified setting
if msg.Setting == "UAT_Enabled" {
globalSettings.UAT_Enabled = msg.Value
}
if msg.Setting == "ES_Enabled" {
globalSettings.ES_Enabled = msg.Value
}
if msg.Setting == "GPS_Enabled" {
globalSettings.GPS_Enabled = msg.Value
}
if msg.Setting == "AHRS_Enabled" {
globalSettings.AHRS_Enabled = msg.Value
}
// TODO: Send new setting to all the other clients
saveSettings()
}
}
}