]> zdv.bktei.com Git - EVA-2020-02.git/commitdiff
Merge branch 'feature/time-server/EVA-2020-02' into develop
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 14 Mar 2023 19:56:39 +0000 (19:56 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 14 Mar 2023 19:56:39 +0000 (19:56 +0000)
.gitmodules [new file with mode: 0644]
TODO.org
doc/process_flow_diagram.odg [new file with mode: 0644]
exec/nfg_run_daily_jobs.sh [new file with mode: 0755]
exec/photograph/update_temp_photograph.sh [new file with mode: 0755]
exec/pimoroni/all-in-one-enviro-mini-bk.py [new symlink]
exec/unitproc/sleepRand.py [new file with mode: 0755]
lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk [new submodule]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..b3520d3
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk"]
+       path = lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk
+       url = https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git
index 31380366b5fea6d4a7cfefc3197d42a8ccb12595..db92f337684d312523261a9426410767baf5cc49 100644 (file)
--- a/TODO.org
+++ b/TODO.org
@@ -52,6 +52,9 @@ Date Created: 2021-01-25T00:25Z
 
 Setting up time tracking via ~chrony~.
 
+** TODO Use ~age --version~ in creating ~VERSION~ output files
+~age~ as of ~beta7~ outputs its version using the ~--version~ option.
+
 ** TODO Create scripts to store current sensor values in ~/dev/shm/~ dir
 Date Created: 2021-01-25T00:36Z
 
diff --git a/doc/process_flow_diagram.odg b/doc/process_flow_diagram.odg
new file mode 100644 (file)
index 0000000..5759d61
Binary files /dev/null and b/doc/process_flow_diagram.odg differ
diff --git a/exec/nfg_run_daily_jobs.sh b/exec/nfg_run_daily_jobs.sh
new file mode 100755 (executable)
index 0000000..5ba3923
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/bash
+# Desc: A list of executables to be started every day at midnight.
+# Usage: nfg_run_daily_jobs.sh arg1
+# Input: arg1: path to directory containing ninfacyzga-1 repository
+# Note: Run this script daily at midnight using `cron`.
+# Depends: GNU Coreutils
+
+script_path="$(dirname "$(realpath -s "$0")")"; # Get script dir Ref/Attrib: https://stackoverflowcom/questions/4774054/
+nfg_base_dir="$script_path/../";
+nfg_exec_dir="$nfg_base_dir"/exec;
+length_day_s=$((24 * 60 * 60)); # seconds
+
+yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
+die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
+try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+
+# Note: Use '&' to run scripts and immediately detach them.
+
+# Temperature
+
+# Pressure
+
+# Location
+
+# Photograph
+photo_script_path="$nfg_exec_dir"/photograph/update_temp_photograph.sh;
+camera_name="DC1";
+timeout "$length_day_s"s /bin/bash "$photo_script_path" "$camera_name" &
diff --git a/exec/photograph/update_temp_photograph.sh b/exec/photograph/update_temp_photograph.sh
new file mode 100755 (executable)
index 0000000..5b380c3
--- /dev/null
@@ -0,0 +1,292 @@
+#!/bin/bash
+# Desc: Ninfacyzga-1: Capture and store photograph from a camera to temporary directory for 24 hours
+# Usage: update_temp_photo.sh arg1
+# Input: arg1: camera tag name (e.g. "DC1")
+# Depends: age, gnu coreutils, libgps, sleepRand.py
+
+#==BEGIN Define Parameters==
+temp_dir="/dev/shm"; # default
+iso_date="$(date +%Y%m%dT%H%M%S%z)";
+#iso_date_ns="$(date +%Y%m%dT%H%M%S.%N%z)";
+device_hostname="$(hostname)";
+
+script_path="$(dirname "$(realpath -s "$0")")"; # Get script dir Ref/Attrib: https://stackoverflowcom/questions/4774054/
+nfg_base_dir="$script_path/../../"; # root directory of ninfacyzga-1 git repo
+nfg_unitproc_path="$nfg_base_dir"/exec/unitproc;
+sleep_rand_path="$nfg_base_dir"/exec/unitproc/sleepRand.py;
+PATH="$nfg_unitproc_path:$PATH"; # include unitprocess nfg executables
+
+declare -Ag appRollCall # Associative array for storing app status
+declare -Ag fileRollCall # Associative array for storing file status
+declare -Ag dirRollCall # Associative array for storing dir status
+#==END Define Parameters==
+
+#==BEGIN Define Functions==
+yell() { echo "$0: $*" >&2; }      #o Yell, Die, Try Three-Fingered Claw technique
+die() { yell "$*"; exit 111; }     #o Ref/Attrib: https://stackoverflow.com/a/25515370
+try() { "$@" || die "cannot $*"; } #o
+checkapp() {
+    # Desc: If arg is a command, save result in assoc array 'appRollCall'
+    # Usage: checkapp arg1 arg2 arg3 ...
+    # Version: 0.1.1
+    # Input: global assoc. array 'appRollCall'
+    # Output: adds/updates key(value) to global assoc array 'appRollCall'
+    # Depends: bash 5.0.3
+    local returnState    
+
+    #===Process Args===
+    for arg in "$@"; do
+       if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
+           appRollCall[$arg]="true";
+           if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
+       else
+           appRollCall[$arg]="false"; returnState="false";
+       fi;
+    done;
+
+    #===Determine function return code===
+    if [ "$returnState" = "true" ]; then
+       return 0;
+    else
+       return 1;
+    fi;
+} # Check that app exists
+checkfile() {
+    # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
+    # Usage: checkfile arg1 arg2 arg3 ...
+    # Version: 0.1.1
+    # Input: global assoc. array 'fileRollCall'
+    # Output: adds/updates key(value) to global assoc array 'fileRollCall';
+    # Output: returns 0 if app found, 1 otherwise
+    # Depends: bash 5.0.3
+    local returnState
+
+    #===Process Args===
+    for arg in "$@"; do
+       if [ -f "$arg" ]; then
+           fileRollCall["$arg"]="true";
+           if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
+       else
+           fileRollCall["$arg"]="false"; returnState="false";
+       fi;
+    done;
+    
+    #===Determine function return code===
+    if [ "$returnState" = "true" ]; then
+       return 0;
+    else
+       return 1;
+    fi;
+} # Check that file exists
+checkdir() {
+    # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
+    # Usage: checkdir arg1 arg2 arg3 ...
+    # Version 0.1.1
+    # Input: global assoc. array 'dirRollCall'
+    # Output: adds/updates key(value) to global assoc array 'dirRollCall';
+    # Output: returns 0 if app found, 1 otherwise
+    # Depends: Bash 5.0.3
+    local returnState
+
+    #===Process Args===
+    for arg in "$@"; do
+       if [ -d "$arg" ]; then
+           dirRollCall["$arg"]="true";
+           if ! [ "$returnState" = "false" ]; then returnState="true"; fi
+       else
+           dirRollCall["$arg"]="false"; returnState="false";
+       fi
+    done
+    
+    #===Determine function return code===
+    if [ "$returnState" = "true" ]; then
+       return 0;
+    else
+       return 1;
+    fi
+} # Check that dir exists
+displayMissing() {
+    # Desc: Displays missing apps, files, and dirs
+    # Usage: displayMissing
+    # Version 0.1.1
+    # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
+    # Output: stderr: messages indicating missing apps, file, or dirs
+    # Depends: bash 5, checkAppFileDir()
+    local missingApps value appMissing missingFiles fileMissing
+    local missingDirs dirMissing
+    
+    #==BEGIN Display errors==
+    #===BEGIN Display Missing Apps===
+    missingApps="Missing apps  :";
+    #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
+    for key in "${!appRollCall[@]}"; do
+       value="${appRollCall[$key]}";
+       if [ "$value" = "false" ]; then
+           #echo "DEBUG:Missing apps: $key => $value";
+           missingApps="$missingApps""$key ";
+           appMissing="true";
+       fi;
+    done;
+    if [ "$appMissing" = "true" ]; then  # Only indicate if an app is missing.
+       echo "$missingApps" 1>&2;
+    fi;
+    unset value;
+    #===END Display Missing Apps===
+
+    #===BEGIN Display Missing Files===
+    missingFiles="Missing files:";
+    #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
+    for key in "${!fileRollCall[@]}"; do
+       value="${fileRollCall[$key]}";
+       if [ "$value" = "false" ]; then
+           #echo "DEBUG:Missing files: $key => $value";
+           missingFiles="$missingFiles""$key ";
+           fileMissing="true";
+       fi;
+    done;
+    if [ "$fileMissing" = "true" ]; then  # Only indicate if an app is missing.
+       echo "$missingFiles" 1>&2;
+    fi;
+    unset value;
+    #===END Display Missing Files===
+
+    #===BEGIN Display Missing Directories===
+    missingDirs="Missing dirs:";
+    #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
+    for key in "${!dirRollCall[@]}"; do
+       value="${dirRollCall[$key]}";
+       if [ "$value" = "false" ]; then
+           #echo "DEBUG:Missing dirs: $key => $value";
+           missingDirs="$missingDirs""$key ";
+           dirMissing="true";
+       fi;
+    done;
+    if [ "$dirMissing" = "true" ]; then  # Only indicate if an dir is missing.
+       echo "$missingDirs" 1>&2;
+    fi;
+    unset value;
+    #===END Display Missing Directories===
+
+    #==END Display errors==
+} # Display missing apps, files, dirs
+timeUntilNextDay(){
+    # Desc: Report seconds until next day.
+    # Version: 1.0.3
+    # Output: stdout: integer seconds until next day
+    # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
+    # Usage: timeUntilNextDay
+    # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi
+    # Depends: date 8, echo 8, yell, try
+    
+    local returnState timeCurrent timeNextDay secondsUntilNextDay
+    timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
+    timeNextDay="$(date -d "$timeCurrent next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
+    secondsUntilNextDay="$(( $(date +%s -d "$timeNextDay") - $(date +%s -d "$timeCurrent") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
+    if [[ "$secondsUntilNextDay" -gt 0 ]]; then
+       returnState="true";
+    elif [[ "$secondsUntilNextDay" -eq 0 ]]; then
+       returnState="warning_zero";
+       yell "WARNING:Reported time until next day exactly zero.";
+    elif [[ "$secondsUntilNextDay" -lt 0 ]]; then
+       returnState="warning_negative";
+       yell "WARNING:Reported time until next day is negative.";
+    fi
+
+    try echo "$secondsUntilNextDay"; # Report
+    
+    # Determine function return code
+    if [[ "$returnState" = "true" ]]; then
+       return 0;
+    elif [[ "$returnState" = "warning_zero" ]]; then
+       return 1;
+    elif [[ "$returnState" = "warning_negative" ]]; then
+       return 2;
+    fi
+} # Report seconds until next day
+set_script_ttl() {
+    #Desc: Sets script_ttl seconds
+    #Usage: set_script_ttl
+    #Input: none
+    #Output: var: script_ttl (integer seconds)
+    #Depends: timeUntilNextDay()
+
+    # Set script lifespan to end at start of next day
+    if ! script_ttl="$(timeUntilNextDay)"; then # sets scriptTTL, then checks exit code
+       if [[ "$script_ttl" -eq 0 ]]; then
+           ((script_ttl++)); # Add 1 because 0 would cause 'timeout' to never timeout.
+       fi;
+    fi;
+} # Set script_ttl in seconds until next day
+update_temp_file() {
+    # Input: var: $temp_photo_path file path to save photo
+    # Depends: checkapp(), raspistill
+    encoding="jpg";
+    try raspistill -gps -t 0 -q 95 -ex auto -e "$encoding" -o "$temp_photo_path";
+};
+showUsage() {
+    # Desc: Display script usage information
+    # Usage: showUsage
+    # Version 0.0.1
+    # Input: none
+    # Output: stdout
+    # Depends: GNU-coreutils 8.30 (cat)
+    cat <<'EOF'
+    USAGE:
+        update_temp_photograph.sh [camera_name]
+
+    EXAMPLE:
+      update_temp_photograph.sh DC1
+EOF
+} # Display information on how to use this script.
+
+main() {
+    :
+    #===BEGIN process arguments===
+    if [[ $# -ge 1 ]]; then
+       camera_name="$1";
+    else
+       die "ERROR:Not enough arguments.";
+       showUsage;
+    fi;
+    
+    temp_photo_dir="$temp_dir"/ninfacyzga/photograph; # generic-use dir
+    temp_photo_filename="$camera_name".jpg; # use jpg encoding
+    temp_photo_path="$temp_photo_dir"/"$temp_photo_filename";
+    #===END process arguments===
+    #===BEGIN Check for required files, dirs, and apps===
+    # Create output dir for photo dir
+    flag_exit_early="false";
+    if ! checkdir "$temp_photo_dir"; then
+       try mkdir -p "$temp_photo_dir";
+    fi;
+    
+    # Check for missing files
+    if ! checkfile "$sleep_rand_path"; then
+       flag_exit_early="true";
+    fi;
+    
+    # Check for missing apps
+    if ! checkapp raspistill; then
+       flag_exit_early="true";
+    fi;
+    
+    # Display missing
+    displayMissing;
+    if [[ $flag_exit_early == "true" ]]; then
+       die "ERROR:Exiting early.";
+    fi;
+    #===END Check for required files, dirs, and apps===
+
+    #===BEGIN main loop===
+    set_script_ttl;
+    while [[ $SECONDS -lt "$script_ttl" ]]; do
+       # Capture new photograph
+       try update_temp_file;
+       try python3 "$sleep_rand_path" --upper 3600.0 60.0;
+    done;
+    #===END main loop===
+};
+
+#==END Define Functions==
+
+main "$@";
diff --git a/exec/pimoroni/all-in-one-enviro-mini-bk.py b/exec/pimoroni/all-in-one-enviro-mini-bk.py
new file mode 120000 (symlink)
index 0000000..2711ecb
--- /dev/null
@@ -0,0 +1 @@
+../../lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk/examples/all-in-one-enviro-mini-bk.py
\ No newline at end of file
diff --git a/exec/unitproc/sleepRand.py b/exec/unitproc/sleepRand.py
new file mode 100755 (executable)
index 0000000..016e9f3
--- /dev/null
@@ -0,0 +1,127 @@
+#!/usr/bin/env python3
+# Desc: Pauses a random amount of time. Random distribution is inverse gaussian.
+# Version: 0.0.6
+# Depends: python 3.7.3
+# Usage: ./sleepRand.py [-v] [-p P] SECONDS
+# Input: SECONDS: float seconds (mean of inverse gaussian distribution)
+#        P:       precision (lambda of inverse gaussian distribution)
+# Example: python3 sleepRand.py -vv -p 8.0 60.0
+
+import argparse;
+import math, time, random, sys;
+import logging;
+
+# Set up argument parser (see https://docs.python.org/3.7/library/argparse.html )
+parser = argparse.ArgumentParser(
+    description='Delay activity for a random number of seconds. Delays sampled from an inverse gaussian distribution.',
+    epilog="Author: Steven Baltakatei Sandoval. License: GPLv3+");
+parser.add_argument('-v','--verbose',
+                    action='count',
+                    dest='verbosity',
+                    default=0,
+                    help='Verbose output. (repeat for increased verbosity)');
+parser.add_argument('mean',
+                    action='store',
+                    metavar='SECONDS',
+                    nargs=1,
+                    default=1,
+                    type=float,
+                    help='Mean seconds of delay. Is the mean of the inverse gaussian distribution.');
+parser.add_argument('--precision','-p',
+                    action='store',
+                    metavar='P',
+                    nargs=1,
+                    default=[4.0],
+                    type=float,
+                    help='How concentrated delays are around the mean (default: 4.0). Must be a positive integer or floating point value. Is the lambda factor in the inverse gaussian distribution. High values (e.g. > 10.0) cause random delays to rarely stray far from MEAN. Small values (e.g. < 0.10) result in many small delays plus occasional long delays.');
+parser.add_argument('--upper','-u',
+                    action='store',
+                    metavar='U',
+                    nargs=1,
+                    default=[None],
+                    type=float,
+                    help='Upper bound for possible delays (default: no bound). Without bound, extremely high delays are unlikely but possible.');
+args = parser.parse_args();
+
+# Define functions
+def setup_logging(verbosity):
+    '''Sets up logging'''
+    # Depends: module: argparse
+    # Ref/Attrib: Haas, Florian; Configure logging with argparse; https://xahteiwi.eu/resources/hints-and-kinks/python-cli-logging-options/
+    base_loglevel = 30;
+    verbosity = min(verbosity, 2);
+    loglevel = base_loglevel - (verbosity * 10);
+    logging.basicConfig(level=loglevel,
+                        format='%(message)s');
+
+def randInvGau(mu, lam):
+    """Returns random variate of inverse gaussian distribution"""
+    # input: mu:  mean of inverse gaussian distribution
+    #        lam: shape parameter
+    # output: float sampled from inv. gaus. with range 0 to infinity, mean mu
+    # example: sample = float(randInvGau(1.0,4.0));
+    # Ref/Attrib: Michael, John R. "Generating Random Variates Using Transformations with Multiple Roots" https://doi.org/10.2307/2683801
+    nu = random.gauss(0,1);
+    y = nu ** 2;
+    xTerm1 = mu;
+    xTerm2 = mu ** 2 * y / (2 * lam);
+    xTerm3 = (- mu / (2 * lam)) * math.sqrt(4 * mu * lam * y + mu ** 2 * y ** 2);
+    x = xTerm1 + xTerm2 + xTerm3;
+    z = random.uniform(0.0,1.0);
+    if z <= (mu / (mu + x)):
+        return x;
+    else:
+        return (mu ** 2 / x);
+
+# Process input
+## Start up logger
+setup_logging(args.verbosity);
+logging.debug('DEBUG:Debug logging output enabled.');
+logging.debug('DEBUG:args.verbosity:' + str(args.verbosity));
+logging.debug('DEBUG:args:' + str(args));
+
+## Receive input arguments
+try:
+    ### Get desired mean
+    desMean = args.mean[0];    
+    logging.debug('DEBUG:Desired mean:' + str(desMean));
+    
+    ### Get lambda precision factor
+    lambdaFactor = args.precision[0];
+    logging.debug('DEBUG:Lambda precision factor:' + str(lambdaFactor));
+    
+    ### Get upper bound
+    if isinstance(args.upper[0], float):
+        logging.debug('DEBUG:args.upper[0] is float:' + str(args.upper[0]));
+        upperBound = args.upper[0];
+    elif args.upper[0] is None:
+        logging.debug('DEBUG:args.upper[0] is None:' + str(args.upper[0]));
+        upperBound = None;
+    else:
+        raise TypeError('Upper bound not set correctly.');
+    logging.debug('DEBUG:Upper bound:' + str(upperBound));
+    
+    ### Reject negative floats.
+    if desMean < 0:
+        logging.error('ERROR:Desired mean is negative:' + str(desMean));
+        raise ValueError('Negative number error.');
+    if lambdaFactor < 0:
+        logging.error('ERROR:Lambda precision factor is negative:' + str(lambdaFactor));
+        raise ValueError('Negative number error.');
+except ValueError:
+    sys.exit(1);
+
+# Calculate delay
+rawDelay = randInvGau(desMean, desMean * lambdaFactor);
+logging.debug('DEBUG:rawDelay(seconds):' + str(rawDelay));
+if isinstance(upperBound,float):
+    delay = min(upperBound, rawDelay);
+elif upperBound is None:
+    delay = rawDelay;
+logging.debug('DEBUG:delay(seconds)   :' + str(delay));
+
+# Sleep
+time.sleep(float(delay));
+
+# Author: Steven Baltakatei Sandoal
+# License: GPLv3+
diff --git a/lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk b/lib/EVA-2020-02-2..pimoroni_enviroplus-python_bk
new file mode 160000 (submodule)
index 0000000..3905a56
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 3905a56d4fce040f05b3e37a8771581f45d6c828