diff --git a/init.d-stratux b/init.d-stratux index 99ba803c..e2889e65 100755 --- a/init.d-stratux +++ b/init.d-stratux @@ -30,6 +30,16 @@ case "$1" in start) log_daemon_msg "Starting $DESC" "$NAME" echo powersave >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor + # Check if we need to run an update. + UPDATE_SCRIPT=`ls -1t /root/update*.sh | head -1` + if [ -n "$UPDATE_SCRIPT" ] ; then + # Execute the script, remove it, then reboot. + echo + echo "Running update script ${UPDATE_SCRIPT}..." + sh ${UPDATE_SCRIPT} + rm -f $UPDATE_SCRIPT + reboot + fi start-stop-daemon --start --background --oknodo --quiet --exec "$DAEMON_SBIN" \ --pidfile "$PIDFILE" --make-pidfile -- $DAEMON_OPTS >/dev/null log_end_msg "$?" diff --git a/main/gen_gdl90.go b/main/gen_gdl90.go index 9a1c19a8..69738149 100644 --- a/main/gen_gdl90.go +++ b/main/gen_gdl90.go @@ -1236,16 +1236,41 @@ func openReplayFile(fn string) ReadCloser { var stratuxClock *monotonic var sigs = make(chan os.Signal, 1) // Signal catch channel (shutdown). +// Close replay log file handles. +func closeReplayLogs() { + if uatReplayWriter != nil { + uatReplayWriter.Close() + } + if esReplayWriter != nil { + esReplayWriter.Close() + } + if gpsReplayWriter != nil { + gpsReplayWriter.Close() + } + if ahrsReplayWriter != nil { + ahrsReplayWriter.Close() + } + if dump1090ReplayWriter != nil { + dump1090ReplayWriter.Close() + } + +} + // Graceful shutdown. -func signalWatcher() { - sig := <-sigs - log.Printf("signal caught: %s - shutting down.\n", sig.String()) +func gracefulShutdown() { // Shut down SDRs. sdrKill() //TODO: Any other graceful shutdown functions. + closeReplayLogs() os.Exit(1) } +func signalWatcher() { + sig := <-sigs + log.Printf("signal caught: %s - shutting down.\n", sig.String()) + gracefulShutdown() +} + func main() { // Catch signals for graceful shutdown. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) diff --git a/main/managementinterface.go b/main/managementinterface.go index da4f16f5..bfe7cf95 100644 --- a/main/managementinterface.go +++ b/main/managementinterface.go @@ -17,6 +17,7 @@ import ( "io" "log" "net/http" + "os" "strings" "syscall" "time" @@ -248,11 +249,15 @@ func handleShutdownRequest(w http.ResponseWriter, r *http.Request) { syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF) } -func handleRebootRequest(w http.ResponseWriter, r *http.Request) { +func doReboot() { syscall.Sync() syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART) } +func handleRebootRequest(w http.ResponseWriter, r *http.Request) { + doReboot() +} + // AJAX call - /getClients. Responds with all connected clients. func handleClientsGetRequest(w http.ResponseWriter, r *http.Request) { setNoCache(w) @@ -262,6 +267,33 @@ func handleClientsGetRequest(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s\n", clientsJSON) } +func delayReboot() { + time.Sleep(1 * time.Second) + doReboot() +} + +// Upload an update file. +func handleUpdatePostRequest(w http.ResponseWriter, r *http.Request) { + r.ParseMultipartForm(1024 * 1024 * 32) // ~32MB update. + file, handler, err := r.FormFile("update_file") + if err != nil { + log.Printf("Update failed from %s (%s).\n", r.RemoteAddr, err.Error()) + return + } + defer file.Close() + updateFile := fmt.Sprintf("/root/%s", handler.Filename) + f, err := os.OpenFile(updateFile, os.O_WRONLY|os.O_CREATE, 0666) + if err != nil { + log.Printf("Update failed from %s (%s).\n", r.RemoteAddr, err.Error()) + return + } + defer f.Close() + io.Copy(f, file) + log.Printf("%s uploaded %s for update.\n", r.RemoteAddr, updateFile) + // Successful update upload. Now reboot. + go delayReboot() +} + func setNoCache(w http.ResponseWriter) { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Header().Set("Pragma", "no-cache") @@ -269,7 +301,7 @@ func setNoCache(w http.ResponseWriter) { } func defaultServer(w http.ResponseWriter, r *http.Request) { - setNoCache(w) + // setNoCache(w) http.FileServer(http.Dir("/var/www")).ServeHTTP(w, r) } @@ -313,6 +345,7 @@ func managementInterface() { http.HandleFunc("/shutdown", handleShutdownRequest) http.HandleFunc("/reboot", handleRebootRequest) http.HandleFunc("/getClients", handleClientsGetRequest) + http.HandleFunc("/updateUpload", handleUpdatePostRequest) err := http.ListenAndServe(managementAddr, nil) diff --git a/main/network.go b/main/network.go index 1ac0a3d3..e83c13d4 100644 --- a/main/network.go +++ b/main/network.go @@ -82,6 +82,17 @@ func getDHCPLeases() (map[string]string, error) { hostname := strings.TrimRight(strings.TrimLeft(strings.Join(spaced[3:], " "), "\""), "\";") ret[block_ip] = hostname open_block = false + } else if open_block && len(spaced) >= 4 && spaced[2] == "ends" { + end_time := spaced[4] + " " + strings.TrimRight(spaced[5], ";") + // Mon Jan 2 15:04:05 -0700 MST 2006. + // 2016/02/02 00:39:59. + t, err := time.Parse("2006/01/02 15:04:05", end_time) // "In the absence of a time zone indicator, Parse returns a time in UTC." + if err == nil && t.Before(time.Now()) { + log.Printf("lease expired for %s (%s) - skipping.\n", block_ip, end_time) + open_block = false + delete(ret, block_ip) + block_ip = "" + } } else if open_block && strings.HasPrefix(spaced[0], "}") { // No hostname. open_block = false ret[block_ip] = "" @@ -215,7 +226,7 @@ func refreshConnectedClients() { } func messageQueueSender() { - secondTimer := time.NewTicker(5 * time.Second) + secondTimer := time.NewTicker(15 * time.Second) queueTimer := time.NewTicker(100 * time.Millisecond) var lastQueueTimeChange time.Time // Reevaluate send frequency every 5 seconds. diff --git a/main/ry835ai.go b/main/ry835ai.go index 377af6cf..5ad8c7f9 100644 --- a/main/ry835ai.go +++ b/main/ry835ai.go @@ -968,7 +968,7 @@ func gpsSerialReader() { for scanner.Scan() && globalStatus.GPS_connected && globalSettings.GPS_Enabled { i++ if i%100 == 0 { - fmt.Printf("gpsSerialReader() scanner loop iteration i=%d\n", i) // debug monitor + log.Printf("gpsSerialReader() scanner loop iteration i=%d\n", i) // debug monitor } s := scanner.Text() @@ -981,7 +981,7 @@ func gpsSerialReader() { log.Printf("reading standard input: %s\n", err.Error()) } - fmt.Printf("Exiting gpsSerialReader() after i=%d loops\n", i) // debug monitor + log.Printf("Exiting gpsSerialReader() after i=%d loops\n", i) // debug monitor globalStatus.GPS_connected = false readyToInitGPS = true // TO-DO: replace with channel control to terminate goroutine when complete return diff --git a/main/sdr.go b/main/sdr.go index e894ad96..f5156dec 100644 --- a/main/sdr.go +++ b/main/sdr.go @@ -340,7 +340,7 @@ func sdrWatcher() { // cleanup if necessary if count < 1 || (!globalSettings.UAT_Enabled && !globalSettings.ES_Enabled) { - log.Println("count == 0, doing cleanup if necessary...") + // log.Println("count == 0, doing cleanup if necessary...") if UATDev != nil { UATDev.shutdown() UATDev = nil diff --git a/selfupdate/makeupdate.sh b/selfupdate/makeupdate.sh index 447ea8e8..4e4fc5bd 100755 --- a/selfupdate/makeupdate.sh +++ b/selfupdate/makeupdate.sh @@ -20,12 +20,21 @@ cp libdump978.so work/bin/ cp linux-mpu9150/libimu.so work/bin/ cp init.d-stratux work/bin/ cp dump1090/dump1090 work/bin/ +cp -r web work/bin/ #TODO: librtlsdr. cd work/ cat ../selfupdate/update_header.sh >update.sh + +echo "stratuxVersion=${stratuxVersion}" >>update.sh +echo "stratuxBuild=${stratuxBuild}" >>update.sh + + +find bin/ -type d | sed -e 's/^bin\///' | grep -v '^$' | while read dn; do + echo "mkdir -p $dn" >>update.sh +done find bin/ -type f | while read fn; do echo -n "packaging $fn... " - UPFN=`echo $fn | cut -d/ -f2` + UPFN=`echo $fn | sed -e 's/^bin\///'` echo "cat >${UPFN}.b64 <<__EOF__" >>update.sh gzip -c $fn | base64 >>update.sh echo "__EOF__" >>update.sh @@ -37,7 +46,7 @@ cat ../selfupdate/update_footer.sh >>update.sh chmod +x update.sh -OUTF="update-${stratuxVersion}.sh" +OUTF="update-${stratuxVersion}-${stratuxBuild:0:10}.sh" mv update.sh $OUTF diff --git a/selfupdate/update_footer.sh b/selfupdate/update_footer.sh index c67855a1..f224f0d0 100755 --- a/selfupdate/update_footer.sh +++ b/selfupdate/update_footer.sh @@ -1,12 +1,15 @@ cp -f gen_gdl90 /usr/bin/gen_gdl90 cp -f libdump978.so /usr/lib/libdump978.so -cp -f linux-mpu9150/libimu.so /usr/lib/libimu.so +cp -f libimu.so /usr/lib/libimu.so -#Startup script. +# Startup script. cp -f init.d-stratux /etc/init.d/stratux chmod 755 /etc/init.d/stratux -ln -s /etc/init.d/stratux /etc/rc2.d/S01stratux -ln -s /etc/init.d/stratux /etc/rc6.d/K01stratux +ln -fs /etc/init.d/stratux /etc/rc2.d/S01stratux +ln -fs /etc/init.d/stratux /etc/rc6.d/K01stratux cp -f dump1090 /usr/bin/ + +# Web files install. +cd web/ && make stratuxBuild=${stratuxBuild} \ No newline at end of file diff --git a/test/uatsummary.go b/test/uatsummary.go index 884124ba..11ae4aa5 100644 --- a/test/uatsummary.go +++ b/test/uatsummary.go @@ -32,6 +32,10 @@ func main() { x := strings.Split(buf, ",") + if len(x) < 2 { + continue + } + uatMsg, err := uatparse.New(x[1]) if err != nil { // fmt.Printf("err %s\n", err.Error()) diff --git a/web/Makefile b/web/Makefile index 2fe57de4..d42cd843 100644 --- a/web/Makefile +++ b/web/Makefile @@ -1,3 +1,7 @@ +ifeq ($(statuxBuild),) +stratuxBuild=`git log -n 1 --pretty=%H` +endif + all: mkdir -p /var/www mkdir -p /var/www/css @@ -30,4 +34,4 @@ all: cp index.html /var/www cp stratux.appcache /var/www # Mark the manifest with the git hash. - echo "# Stratux build: " `git log -n 1 --pretty=%H` >>/var/www/stratux.appcache \ No newline at end of file + echo "# Stratux build: " ${stratuxBuild} >>/var/www/stratux.appcache \ No newline at end of file diff --git a/web/plates/js/settings.js b/web/plates/js/settings.js index 71b23990..bb2166dd 100755 --- a/web/plates/js/settings.js +++ b/web/plates/js/settings.js @@ -110,12 +110,29 @@ function SettingsCtrl($rootScope, $scope, $state, $http) { } }; - $scope.postShutdown = function () { - $http.post('/shutdown'); - }; + $scope.postShutdown = function () { + $http.post('/shutdown'); + }; - $scope.postReboot = function () { - $http.post('/reboot'); - }; + $scope.postReboot = function () { + $http.post('/reboot'); + }; + $scope.uploadFile = function(files) { + var fd = new FormData(); + //Take the first selected file + fd.append("update_file", files[0]); + + $http.post("/updateUpload", fd, { + withCredentials: true, + headers: {'Content-Type': undefined }, + transformRequest: angular.identity + }).success(function (data) { + alert("success. wait 60 seconds and refresh home page to verify new version."); + window.location.replace("/"); + }).error(function (data) { + alert("error"); + }); + + }; }; diff --git a/web/plates/settings.html b/web/plates/settings.html index 37d094af..d5b227de 100755 --- a/web/plates/settings.html +++ b/web/plates/settings.html @@ -80,14 +80,14 @@ -