feat(bkgpslog):Add GPX KML output from buffer
[EVA-2020-02.git] / exec / bkgpslog
index d9297a6d2e0869e8bf7614ab4561d63d4aa0fb92..9de6a09717d3b6574a6003195f4b2c9259126847 100755 (executable)
@@ -29,7 +29,7 @@ checkapp() {
     #===Process Args===
     for arg in "$@"; do
        #echo "DEBUG:processing arg:$arg"
-       if command -v $arg 1>/dev/null 2>&1; then # Check if arg is a valid command
+       if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
            appRollCall[$arg]="true";
            #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
            if ! [ "$returnState" = "false" ]; then returnState="true"; fi
@@ -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
 
@@ -145,19 +147,36 @@ showVersion() {
     echoerr "$SCRIPT_VERSION"
 } # Display script version.
 vbm() {
-    if [ $OPTION_VERBOSE -eq "true" ]; then
-       echoerr "$@"
+    # Usage: vbm "DEBUG:verbose message here"
+    # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
+    # Input:
+    #   - OPTION_VERBOSE  variable set by processArguments function. (ex: "true", "false")
+    #   - "$@"            positional arguments fed to this function.
+    # Output: stderr
+    # Script function dependencies: echoerr
+    # External function dependencies: echo
+    # Last modified: 2020-04-11T23:57Z
+    # Last modified by: Steven Baltakatei Sandoval
+    # License: GPLv3+
+    # Ref./Attrib:
+
+    if [ "$OPTION_VERBOSE" = "true" ]; then
+       FUNCTION_TIME=$(date --iso-8601=ns); # Save current time in nano seconds.
+       echoerr "[$FUNCTION_TIME] ""$*"; # Display argument text.
     fi
+
+    # End function
+    return 0; # Function finished.
 } # Verbose message display function.
 processArguments() {
     while [ ! $# -eq 0 ]; do   # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
        echoerr "DEBUG:Starting processArguments while loop."
-       echoerr "DEBUG:Provided arguments are:""$@"
+       echoerr "DEBUG:Provided arguments are:""$*"
        case "$1" in
            --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
@@ -207,26 +226,38 @@ dateTimeShort(){
     echo "$TIME_CURRENT_SHORT";
 } # Get date&time without separators
 main() {
-    processArguments # Process arguments.
+    processArguments "$@" # Process arguments.
     if checkapp gpspipe && checkdir "$DIROUT"; then
        # Determine output file paths
-       FILEOUT_NMEA="$(dateTimeShort)".."$SCRIPT_HOSTNAME"_location.nmea ;
-       #FILEOUT_GPX="" ;
-       #FILEOUT_KML="" ;
+       FILEOUT_BASENAME="$(dateTimeShort)".."$SCRIPT_HOSTNAME"_location
+       FILEOUT_NMEA="$FILEOUT_BASENAME".nmea ;
+       FILEOUT_GPX="$FILEOUT_BASENAME".gpx ;
+       FILEOUT_KML="$FILEOUT_BASENAME".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        
         
        # Determine buffer lifespan
-       bufferTTL="15";
+       #bufferTTL="60";
+       bufferTTL="15"; #DEBUG DEBUG DEBUG DEBUG
 
-       # 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;
+           # Fill buffer
+           buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
+           # Process buffer
+           buffer_GPX="$(echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - )"; # Convert buffer to GPX format
+           buffer_KML="$(echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - )"; # Convert buffer to KML format
+           # Save processed buffer data
+           echo "$buffer" >> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter" # Save NMEA format
+           echo "$buffer_GPX" >> "$DIROUT"/"$FILEOUT_GPX""$debugCounter" # Save GPX format
+           echo "$buffer_GPX" >> "$DIROUT"/"$FILEOUT_KML""$debugCounter" # Save KML format
+           vbm "DEBUG:Completed buffer session $debugCounter" 1>&2;
+           # Reset buffer
+           unset buffer
        done
     fi
 } # Main function.