workable functionality and test for running the station

master
Ken Barbour 2022-07-02 22:12:31 -04:00
parent 73510ef188
commit 61d6d278a7
2 changed files with 260 additions and 0 deletions

180
run_station.sh 100755
View File

@ -0,0 +1,180 @@
#!/bin/bash
## Handles the reception, decoding, and website generation of a single pass
##
## usage: __PROG__ [options]
##
set -o pipefail
prog="$0"
me=${HELP:-$(basename "${prog}")}
rootdir=$(dirname $(realpath ${BASH_SOURCE}))
source "${rootdir}/lib/utils.sh"
# defaults
freq="137M"
duration=10
now=$(date +%s)
# bandwidth = 2 * (17kHz deviation + 2.4kHz tone) * doppler shift (~1.2kHz)
# wavfile is determined by options
function process_args() {
while (( "$#" ))
do
case $1 in
## --freq <frequency> (default: 137M)
'--freq')
freq=${2}
log "frequency set to ${freq}"
shift
;;
## --duration <seconds> (default: 900)
'--duration')
duration=${2}
log "duration set to ${duration}"
shift
;;
## --gain <integer> (default: auto)
'--gain')
gain_flag="-g ${2}"
log "gain set to ${2}"
shift
;;
## --satellite <name> Satellite name (ex: noaa-15)
'--satellite')
satellite=${2}
shift
;;
## --noaa-15 Handle a pass of NOAA 15
'--noaa-15')
satellite='noaa-15'
;;
## --noaa-18 Handle a pass of NOAA 18
'--noaa-18')
satellite='noaa-18'
;;
## --noaa-19 Handle a pass of NOAA 19
'--noaa-19')
satellite='noaa-19'
;;
## --dir <path> Directory to place data (default: ./<month>/<day>)
'--dir')
output_path=${2}
shift
;;
## --basename <string> Base name to prefix files with (default: <satellite>-<timestamp>)
'--basename')
file_basename=${2}
shift
;;
## --help, -h
'--help' | '-h')
usage
exit
;;
*)
logerr "Unknown argument %s" ${1}
exit 1
;;
esac
shift
done
}
#
# @param duration of pass in seconds
# @param name of satellite 'ex: noaa-15'
# @param path to output wavfile
# @output flags to pass to receive pass command
function receive_pass_flags() {
local duration=${1}
local satellite=${2}
local outfile=${3}
printf ' --duration %s --%s --output %s' "${duration}" "${satellite}" "${outfile}"
}
# @param wavfile path
# @param name of satellite 'ex: noaa-15'
# @param timestamp (unix)
# @param output_prefix
# @output flags to pass to build images command
function build_images_flags() {
local audio_file=${1}
local timestamp=${2}
local satellite=${3}
printf " --timestamp %s --%s %s" "${timestamp}" "${satellite}" "${audio_file}"
}
function generate_website_flags() {
printf ""
}
# Determine a reasonable default directory to place files generated
# @param integer timestamp
# @output string
function get_default_dir() {
local ts=${1:-$(date +%s)}
echo "./$(date -d @${ts} +%Y\/%m)"
}
# Determine a resonable default basename for files generated
# @param string satellite name
# @param integer timestamp
# @output string basename
function get_default_basename() {
local satellite=${1:-pass}
local timestamp=${2:-${now}}
echo "$(printf "%s" ${satellite} | sed s/-/_/)-${timestamp}"
}
# if sourced, return here. the rest of this script has side effects
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
return
fi
# -- Default Values --
output_path=${output_path:=$(get_default_dir ${now})}
satellite=${satellite:='noaa-15'}
file_basename=${file_basename:=$(get_default_basename "${satellite}" "${now}")}
# -- Do work below here --
process_args $@
mkdir -p ${output_path}
# Receive the pass (receive_pass.sh)
audio_file=${output_path}/${file_basename}.wav
wxrx record $(receive_pass_flags "$duration" "${satellite}" "${audio_file}")
if [[ $? != 0 ]]; then
logerr "Errors occurred receiving signal"
exit 10
fi
# TODO: exit if ${audio_file} does not exist
if [[ ! -r "${audio_file}" ]]; then
logerr "Missing audio file %s. This is likely a bug in wxrx record" "${audio_file}"
exit 11
fi
# Decode the images
wxrx decode $(build_images_flags "${audio_file}" "${now}" "${satellite}")
if [[ $? != 0 ]]; then
logerr "Errors occurred decoding recorded signal"
exit 20
fi
# Generate website
wxrx web $(generate_website_flags)

View File

@ -0,0 +1,80 @@
#!/usr/bin/bash
prog="$0"
me=`basename "$prog"`
unit=$(realpath $(dirname "$0")/../run_station.sh)
fixture_dir=$(realpath $(dirname "$0")/fixtures)
##
# Tests is a rtl_sdr is connected
# @return 0 if rtl_sdr is found
device_is_connected() {
lsusb | grep RTL2838 > /dev/null
}
# test_helptext() {
# timeout --foreground 1 ${unit} --help >"${stdoutF}" 2>"${stderr}"
#
# assertTrue "unexpected error status" ${rtrn}
# assertNotNull "missing helptext" "`cat ${stdoutF}`"
# assertNotNull "missing usage" "`grep 'usage:' ${stdoutF}`"
# assertNull "unexpected STDERR" "`cat ${stderrF}`"
# }
test_get_default_dir() {
source ${unit}
get_default_dir >${stdoutF} 2>${stderrF}
assertTrue "unexpected error status with no args" $?
assertNull "unexpected error output with no args" "`cat ${stderrF}`"
assertNotNull "unexpected empty output with no args" "`cat ${stdoutF}`"
get_default_dir 1656770769 >${stdoutF} 2>${stderrF}
assertTrue "unexpected error status with args" $?
assertNull "unexpected error output with args" "`cat ${stderrF}`"
assertSame "./2022/07" "`cat ${stdoutF}`"
}
test_get_default_basename() {
source ${unit}
get_default_basename >${stdoutF} 2>${stderrF}
assertTrue "unexpected error status with no args" $?
cat ${stderrF}
assertNull "unexpected error output with no args" "`cat ${stderrF}`"
assertNotNull "unexpected empty output with no args" "`cat ${stdoutF}`"
get_default_basename 'noaa-15' 12341234 >${stdoutF} 2>${stderrF}
assertTrue "unexpected error status with args" $?
assertNull "unexpected error output with args" "`cat ${stderrF}`"
assertSame "noaa_15-12341234" "`cat ${stdoutF}`"
}
test_run_station() {
device_is_connected || startSkipping
outputPath="${SHUNIT_TMPDIR}/record-test/cwd/test.wav"
export WXRX_WEB_PUBDIR="${SHUNIT_TMPDIR}/record-test/public"
mkdir -p "$(dirname ${outputPath})"
mkdir -p "${WXRX_WEB_PUBDIR}"
cd $(dirname $outputPath)
${unit} --duration 1 #>${stdoutF} 2>${stderrF}
rtrn=$?
cat ${stdoutF}
if [ -nz $rtrna ]; then
cat ${stderrF}
fi
assertTrue "expected a success exit status" $rtrn
tree ..
}
setUp() {
stdoutF="${SHUNIT_TMPDIR}/stdout"
stderrF="${SHUNIT_TMPDIR}/stderr"
>"${stdoutF}"
>"${stderrF}"
}
. shunit2