LoRaWAN-Signal-Mapping/build

338 lines
13 KiB
Text
Raw Permalink Normal View History

2019-05-03 12:59:37 +00:00
#!/usr/bin/env bash
# Make sure the current directory is the location of this script to simplify matters
cd "$(dirname $(readlink -f $0))";
################
### Settings ###
################
# The name of this project
project_name="Msc Summer Project";
# The path to the lantern build engine git submodule
lantern_path="./lantern-build-engine";
###
# Custom Settings
###
# Put any custom settings here.
###############################################################################
# Check out the lantern git submodule if needed
if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi
source "${lantern_path}/lantern.sh";
if [[ "$#" -lt 1 ]]; then
echo -e "${FBLE}${project_name}${RS} build script";
echo -e " by Starbeamrainbowlabs";
2019-08-20 14:26:42 +00:00
echo -e "${LC}Powered by the \e[38;5;11mlantern \e[38;5;117mbuild \e[38;5;215mengine${RS}${LC}, v${version}${RS}";
2019-05-03 12:59:37 +00:00
echo -e "";
echo -e "${CSECTION}Usage${RS}";
echo -e " ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ...";
echo -e "";
echo -e "${CSECTION}Available actions${RS}";
2019-07-25 12:17:23 +00:00
echo -e " ${CACTION}setup${RS} - Perform initial setup";
echo -e " ${CACTION}ttn-listener${RS} - Execute the Node.js TTN receiver.";
echo -e " ${CACTION}process-data${RS} - Fold the data in 'DATA.TSV' into the database.";
echo -e " ${CACTION}train-ai${RS} - Train the AI(s).";
echo -e " ${CACTION}server${RS} - Start a temporary web server for the web interface.";
echo -e " ${CACTION}server-stop${RS} - Stop a temporary web server.";
echo -e "";
echo -e "${CSECTION}${LC}Extra development actions${RS}${LC}";
echo -e " ${CACTION}client${RS}${LC} - Build the client-side code.";
echo -e " ${CACTION}client-watch${RS}${LC} - Auto-rebuild the client-side code on modification.";
echo -e " ${CACTION}render-initial${RS}${LC} - Render the initial report";
echo -e " ${CACTION}render-final${RS}${LC} - Render the final report";
2019-09-02 12:23:22 +00:00
echo -e " ${CACTION}render-manuals${RS}${LC} - Render the user manuals";
echo -e " ${CACTION}geojson-debug${RS}${LC} - Generate some GeoJSON from the raw readings for debugging purposes (paste into geojson.io)";
2019-05-03 12:59:37 +00:00
echo -e "";
exit 1;
fi
###############################################################################
# ██████ ██████ ███ ███ ███ ███ ███████ ███ ██ ████████
# ██ ██ ██ ████ ████ ████ ████ ██ ████ ██ ██
# ██ ██ ██ ██ ████ ██ ██ ████ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██████ ██ ██ ██ ██ ███████ ██ ████ ██
# Toggles commenting and uncommenting lines in a file that contain a specific
# substring. Checks for word boundaries either side of the substring.
# From https://stackoverflow.com/a/24901636/1460422
# $1 - Filename
# $2 - Search string
comment_toggle() {
filename="${1}";
search_string="${2}";
awk -v commentId='//' -v word="${search_string}" '
$0 ~ "(^|[[:punct:][:space:]])" word "($|[[:punct:][:space:]])" {
if (match($0, "^[[:space:]]*" commentId))
$0 = substr($0, RSTART + RLENGTH)
else
$0 = commentId $0
}
{ print }' "${filename}" > tmpfile.$$ && mv tmpfile.$$ "${filename}"
}
2019-05-03 12:59:37 +00:00
task_setup() {
stage_begin "Setting up";
2019-05-03 12:59:37 +00:00
task_begin "Checking Environment";
2019-05-03 12:59:37 +00:00
check_command git true;
check_command awk true;
check_command inotifywait true optional;
if [[ "$?" -ne "0" ]]; then echo "${HC}inotifywait${RS} is required to auto-rebuild on when the client-side code changes."; fi
check_command pdflatex true optional;
if [[ "$?" -ne "0" ]]; then echo "${HC}pdflatex${RS} is required to render the reports."; fi
2019-07-16 15:14:32 +00:00
check_command bibtex true optional;
if [[ "$?" -ne "0" ]]; then echo "${HC}bibtex${RS} is required to render the reports."; fi
2019-09-02 12:23:22 +00:00
check_command weasyprint true optional;
if [[ "$?" -ne "0" ]]; then echo "${HC}weasyprint${RS} is required to render the user manuals."; fi
task_end $?;
2019-05-03 12:59:37 +00:00
task_begin "Initialising submodules";
2019-05-03 12:59:37 +00:00
git submodule update --init;
task_end $?;
task_begin "Preconfiguring libraries";
config_file_directory="./iot/libraries/arduino-lmic/src/lmic/";
config_file_name="config.h";
cd "${config_file_directory}";
git reset --hard;
# Disable OTAA
comment_toggle "${config_file_name}" "#define DISABLE_JOIN";
# Disable class b stuff
comment_toggle "${config_file_name}" "#define DISABLE_PING";
comment_toggle "${config_file_name}" "#define DISABLE_BEACONS";
# Disable other misc. stuff we're not likely to use
2019-07-16 15:14:55 +00:00
comment_toggle "${config_file_name}" "#define DISABLE_MCMD_DCAP_REQ"; # Duty cycle cap - won't work anyway 'cause we're shutting down in between
comment_toggle "${config_file_name}" "#define DISABLE_MCMD_DN2P_SET"; # Receiving stuff
# echo "#define DISABLE_JOIN" >>"${config_file_name}";
# echo "#define DISABLE_PING" >>"${config_file_name}";
# echo "#define DISABLE_BEACONS" >>"${config_file_name}";
cd -;
task_end $?;
2019-05-03 12:59:37 +00:00
task_begin "Installing server dependencies";
npm install;
exit_code="${?}";
task_end ${exit_code};
stage_end 0;
2019-05-03 12:59:37 +00:00
}
# ██ ██ ███████ ████████ ███████ ███ ██ ███████ ██████
# ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
# ██ ██ ███████ ██ █████ ██ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ███████ ██ ███████ ██ ███████ ██ ████ ███████ ██ ██
task_ttn-listener() {
execute ./server.sh ttn-app-server;
}
task_train-ai() {
execute ./server.sh train-ai;
}
task_process-data() {
execute ./server.sh process-data DATA.TSV;
}
# ██████ ██ ██ ███████ ███ ██ ████████
# ██ ██ ██ ██ ████ ██ ██
# ██ ██ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ███████ ██ ████ ██
task_client() {
task_begin "Packaging Javascript";
execute node_modules/rollup/bin/rollup --sourcemap --config rollup.main.config.js;
execute node_modules/rollup/bin/rollup --sourcemap --config rollup.worker.config.js;
task_end $? "Error: rollup packing failed!";
task_begin "Copying html";
execute cp client_src/index.html "app/";
task_end $?;
}
task_client-watch() {
set_title "Client Watcher";
# execute node_modules/rollup/bin/rollup --watch --sourcemap --config rollup.config.js &
echo -e "Watching for changes.";
while :; do # : = infinite loop
# Wait for an update
# inotifywait's non-0 exit code forces an exit for some reason :-/
2019-07-30 17:46:07 +00:00
inotifywait -qr --event modify --format '%:e %f' client_src common rollup.*.config.js;
task_begin "Copying html";
execute cp client_src/index.html "app/";
task_end $?;
stage_begin "Rebuilding client code";
set +e; tasks_run client; set -e;
stage_end $?;
done
}
# ██████ ███████ ██ ██ ███████ ███████ ██████ ██ ██ ███████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ █████ ██ ██ █████ ███████ █████ ██████ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ████ ███████ ███████ ██ ██ ████ ███████ ██ ██
task_server() {
if [ ! -f "app/index.html" ]; then
echo "No client-side code detected, running build script";
tasks_run client;
fi
task_begin "Starting development server";
2019-08-01 15:30:13 +00:00
php -S "[::]:40382" -t "app" &
exit_code_a=$?;
pid_a=$!;
php -S "127.0.0.1:40382" -t "app" &
exit_code_b=$?;
pid_b=$!;
[[ "${exit_code_a}" -eq "0" ]] && echo "${pid_a}" >/tmp/summer-project-dev-server-v6.pid;
[[ "${exit_code_b}" -eq "0" ]] && echo "${pid_b}" >/tmp/summer-project-dev-server-v4.pid;
task_end $?; # Should be 0 unless php died for some reason
sleep 1;
}
task_server-stop() {
task_begin "Stopping development server";
2019-08-01 15:30:13 +00:00
if [ ! -f "/tmp/summer-project-dev-server-v6.pid" ]; then
echo -e "${HC}${FRED}Error: The development server doesn't appear to be running, so it can't be stopped. Have you tried running ./build dev-server?${RESET}";
return 1;
fi
2019-08-01 15:30:13 +00:00
kill "$(cat /tmp/summer-project-dev-server-v4.pid)";
kill "$(cat /tmp/summer-project-dev-server-v6.pid)";
rm /tmp/summer-project-dev-server-v[46].pid;
task_end $?;
}
2019-07-31 15:06:58 +00:00
task_geojson-debug() {
2019-07-31 14:11:40 +00:00
sqlite3 lorawan.sqlite 'SELECT readings.latitude, readings.longitude, rssis.rssi FROM readings LEFT JOIN rssis ON readings.id = rssis.reading_id ORDER BY readings.latitude,readings.longitude;' | perl -pe 'chomp if eof' | jq --raw-input --slurp 'split("\n") | map(split("|") | map(if . == "" then null else tonumber end)) | map({type: "Feature", geometry: { type: "Point", "coordinates": [ .[1], .[0] ] }, properties: { "rssi": .[2], "marker-color": (if .[2] == null then "#dd0707" else "#04a104" end), "marker-symbol": "circle" }}) | { type: "FeatureCollection", features: . }'
}
###############################################################################
# ██████ ███████ ██████ ██████ ██████ ████████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ █████ ██████ ██ ██ ██████ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ███████ ██ ██████ ██ ██ ██ ███████
2019-09-02 12:23:22 +00:00
task_render-manuals() {
task_begin "Rendering README";
_render-markdown-pdf "README.md" "README.pdf" "LoRaWAN Signal Mapping - User Manual";
task_end "$?";
task_begin "Rendering HARDWARE";
_render-markdown-pdf "HARDWARE.md" "HARDWARE.pdf" "LoRaWAN Signal Mapping - Hardware Guide";
task_end "$?";
}
task_render-initial() {
2019-05-03 12:59:37 +00:00
_render-latex-pdf "Reports/Initial-Report/Initial-Report.tex";
}
task_render-final() {
2019-09-02 12:23:22 +00:00
tasks_run render-manuals;
_render-latex-pdf "Reports/Final-Report/Final-Report.tex";
}
2019-05-03 12:59:37 +00:00
2019-09-02 12:23:22 +00:00
# $1 - Source file
# $2 - Destination file
# $3 - Title
_render-markdown-pdf() {
source="${1}";
dest="${2}";
title="${3}";
tmpfile="$(mktemp msc-project-md-pdf.XXXXXXX)";
subtask_begin "Markdown -> HTML";
pandoc -s "${source}" -o "${tmpfile}" --metadata "title=${title}";
task_end "$?";
subtask_begin "HTML -> PDF";
weasyprint "${tmpfile}" "${dest}" --stylesheet print.css;
task_end "$?";
subtask_begin "Cleanup";
rm "${tmpfile}";
task_end "$?";
}
2019-05-03 12:59:37 +00:00
# $1 - Location of top-level LaTeX file
_render-latex-pdf() {
if [[ ! -f "$1" ]]; then
task_end 1 "Error: Couldn't find '$1'";
fi
task_begin "Entering directory";
latex_filename="$(basename "$1")";
latex_directory="$(dirname "$1")";
execute cd "${latex_directory}";
execute echo "${PWD}";
task_end $? "Failed to enter directory (does it exist?)";
task_begin "Cleaning up";
find -iname "*.aux" -delete; # Ref: https://tex.stackexchange.com/q/381057
find -iname "*.bbl" -delete;
find -iname "*.blg" -delete;
find -iname "*.out" -delete;
task_end $? "Error: Failed to clean up after last build";
# task_begin "Rendering images";
# # FUTURE: Do this in paralell?
# for svg_filename in $(find "images/" -type f -iname "*.svg"); do
# execute inkscape -e ${svg_filename%%.svg}.png ${svg_filename};
# exit_code=$?;
# [[ "${exit_code}" -eq 0 ]] || break;
# done
# task_end "${exit_code}";
task_begin "Building Report";
set -e;
execute pdflatex --output-directory=. "${latex_filename}";
execute bibtex "${latex_filename%.*}";
execute pdflatex --output-directory=. "${latex_filename}";
execute pdflatex --output-directory=. "${latex_filename}";
execute bibtex "${latex_filename%.*}";
execute pdflatex --output-directory=. "${latex_filename}";
execute pdflatex --output-directory=. "${latex_filename}";
set +e;
task_end $? "Error: Failed to build report";
task_begin "Moving report";
execute mv *.pdf ..;
task_end $? "Failed to move report";
cd -;
}
###############################################################################
tasks_run $@;