feat(bkgpslog):Directly convert and write buffer to GPX, KML
[EVA-2020-02.git] / exec / bkgpslog
index 9a3744a01cb97ab64d19138356ed74953d4d714a..ef7e13f393add94ca90a13aee4cd288a41f5538a 100755 (executable)
@@ -97,8 +97,10 @@ checkdir() {
            dirRollCall["$arg"]="true";
            #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
            if ! [ "$returnState" = "false" ]; then returnState="true"; fi
-       else
+       elif [ "$arg" = "" ]; then
            dirRollCall["$arg"]="false"; returnState="false";
+       else
+           returnState="false";
        fi
     done
 
@@ -126,7 +128,7 @@ echoerr() {
 } # Define stderr message function.
 showUsage() {
     echoerr "USAGE:"
-    echoerr "    bkgpslog.sh [ options ]"
+    echoerr "    bkgpslog [ options ]"
     echoerr
     echoerr "OPTIONS:"
     echoerr "    -h, --help"
@@ -174,7 +176,7 @@ processArguments() {
            --h | --help) showUsage; exit 1;; # Display usage.
            --version) showVersion; exit 1;; # Show version
            --v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode.
-           --o | --output) if [ -d "$2" ]; then DIROUT="$2"; fi ;; # Define output directory.
+           --o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory.
            *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
        esac
        shift
@@ -226,24 +228,33 @@ dateTimeShort(){
 main() {
     processArguments "$@" # Process arguments.
     if checkapp gpspipe && checkdir "$DIROUT"; then
-       # Determine output file paths
-       FILEOUT_NMEA="$(dateTimeShort)".."$SCRIPT_HOSTNAME"_location.nmea ;
-       #FILEOUT_GPX="" ;
-       #FILEOUT_KML="" ;
+
 
        # Determine script lifespan (note: exit if <= 0 since 'timeout' runs forever if provided "0s".
        #if ! scriptTTL="$(timeUntilMidnight)"; then yell "ERROR: timeUntilMidnight exit code $?"; exit 1; fi 
-       scriptTTL="60"; #DEBUG DEBUG DEBUG DEBUG        
+       scriptTTL="3600"; # DEBUG DEBUG DEBUG DEBUG
         
        # Determine buffer lifespan
-       bufferTTL="15";
+       bufferTTL="60";
 
-       # Record gps data until life ends
+       # Record gps data until script lifespan ends
        declare debugCounter; debugCounter="0"
        while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
            ((debugCounter++))
-           timeout "$bufferTTL""s" gpspipe -r 1>> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter"
-           echo "do stuff" 1>&2;
+           # Determine output file paths (time is start of buffer period)
+           FILEOUT_BASENAME="$(dateTimeShort)--P$(bufferTTL)S..$SCRIPT_HOSTNAME_location" ;
+           FILEOUT_NMEA="$FILEOUT_BASENAME".nmea ;
+           FILEOUT_GPX="$FILEOUT_BASENAME".gpx ;
+           FILEOUT_KML="$FILEOUT_BASENAME".kml ;
+           # Fill buffer
+           buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
+           # Process and save buffers
+           echo "$buffer"                                     > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format
+           echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - > "$DIROUT"/"$FILEOUT_GPX"  & # Save GPX format
+           echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - > "$DIROUT"/"$FILEOUT_KML"  & # Save KML format
+           vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2;
+           # Reset buffer and filenames
+           unset buffer buffer_GPX buffer_KML FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML;
        done
     fi
 } # Main function.