Skip to content

Commit

Permalink
Merge pull request #585 from thomasjacquin/allsky.sh-cleanup
Browse files Browse the repository at this point in the history
allsky.sh general cleanup
  • Loading branch information
ckuethe authored Oct 7, 2021
2 parents bfe6e55 + ae60e30 commit 96d7bc1
Showing 1 changed file with 90 additions and 79 deletions.
169 changes: 90 additions & 79 deletions allsky.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/bin/bash

if [ -z "$ALLSKY_HOME" ]
# Make it easy to find the beginning of this run in the log file.
echo " ***** Starting AllSky *****"

if [ -z "${ALLSKY_HOME}" ]
then
export ALLSKY_HOME="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
export ALLSKY_HOME="$(realpath $(dirname "${BASH_ARGV0}")/..)"
fi

# reset auto camera selection, so $ALLSKY_HOME/config.sh do not pick up old camera selection
cd $ALLSKY_HOME
source $ALLSKY_HOME/variables.sh
cd "${ALLSKY_HOME}"

echo "" > "$ALLSKY_CONFIG/autocam.sh"
source $ALLSKY_CONFIG/config.sh
# Reset auto camera selection, so config.sh does not pick up old camera selection.
> "${ALLSKY_CONFIG}/autocam.sh"

# Make it easy to find the beginning of this run in the log file.
echo " ***** Starting AllSky *****"
echo "Making sure allsky.sh is not already running..."
ps -ef | grep allsky.sh | grep -v $$ | xargs "sudo kill -9" 2>/dev/null
source "${ALLSKY_HOME}/variables.sh"
source "${ALLSKY_CONFIG}/config.sh"
mkdir -p "${ALLSKY_TMP}"

mv -f log.txt OLD_log.txt 2> /dev/null # save the prior log file for debugging
> log.txt
# Make sure allsky.sh is not already running.
ps -ef | grep allsky.sh | grep -v $$ | xargs "sudo kill -9" 2>/dev/null

# old/regular manual camera selection mode => exit if no requested camera was found
if [[ $(vcgencmd get_camera) == "supported=1 detected=1" ]]; then
Expand All @@ -28,8 +28,8 @@ else
fi
if [[ $CAMERA == "RPiHQ" && $RPiHQIsPresent -eq 0 ]]; then
echo "RPiHQ Camera not found. Exiting." >&2
sudo systemctl stop allsky
exit 0
sudo systemctl stop allsky
exit 1
fi

if [[ $CAMERA != "RPiHQ" ]]; then
Expand All @@ -38,7 +38,7 @@ if [[ $CAMERA != "RPiHQ" ]]; then
ZWOdev=$(lsusb | awk '/ 03c3:/ { bus=$2; dev=$4; gsub(/[^0-9]/,"",dev); print "/dev/bus/usb/"bus"/"dev;}')
ZWOIsPresent=$(lsusb -D ${ZWOdev} 2>/dev/null | grep -c 'iProduct .*ASI[0-9]')
if [[ $CAMERA == "ZWO" && $ZWOIsPresent -eq 0 ]]; then
echo "ZWO Camera not found..." >&2
echo "ZWO Camera not found..." >&2
if [[ $ZWOdev == "" ]]; then
echo " and no USB entry found for it." >&2
else
Expand All @@ -48,64 +48,63 @@ if [[ $CAMERA != "RPiHQ" ]]; then
if tty --silent ; then
echo " Resetting USB ports; restart allsky.sh when done." >&2
else
echo " Resetting USB ports and restarting." >&2
echo " Resetting USB ports and restarting." >&2
# The service will automatically restart this script.
fi
sudo "$UHUBCTL_PATH" -a cycle -l "$UHUBCTL_PORT"
exit 0
exit 1
else
echo " Exiting." >&2
echo " If you have the 'uhubctl' command installed, add it to config.sh." >&2
echo " In the meantime, try running it to reset the USB bus." >&2
sudo systemctl stop allsky
exit 0
echo " Exiting." >&2
echo " If you have the 'uhubctl' command installed, add it to config.sh." >&2
echo " In the meantime, try running it to reset the USB bus." >&2
sudo systemctl stop allsky
exit 1
fi
fi
fi

# CAMERA AUTOSELECT
# exit if no camera found at all
# Exit if no camera found.
WAS_AUTO=0

if [[ $CAMERA == "auto" ]]; then
WAS_AUTO=1
echo "Trying to automatically choose between ZWO and RPI camera"
if [[ $ZWOIsPresent -eq 0 && $RPiHQIsPresent -eq 0 ]]; then
echo "None of RPI or ZWO Cameras were found. Exiting." >&2
sudo systemctl stop allsky
exit 0
fi
# prioritize ZWO camera if exists, and use RPI camera otherwise
if [[ $ZWOIsPresent -eq 0 ]]; then
CAMERA="RPiHQ"
else
CAMERA="ZWO"
fi

# redefine the settings variable
CAMERA_SETTINGS="$CAMERA_SETTINGS_DIR/settings_$CAMERA.json"
fi
WAS_AUTO=1
echo "Trying to automatically determine camera type"
if [[ $ZWOIsPresent -eq 0 && $RPiHQIsPresent -eq 0 ]]; then
echo "None of RPI or ZWO Cameras were found. Exiting." >&2
sudo systemctl stop allsky
exit 1
fi
# Prioritize ZWO camera if exists, otherwise use RPI camera.
if [[ $ZWOIsPresent -eq 0 ]]; then
CAMERA="RPiHQ"
else
CAMERA="ZWO"
fi

echo "CAMERA: ${CAMERA}"
echo "CAMERA_SETTINGS: ${CAMERA_SETTINGS}"
# save auto camera selection for the current session, will be read in "$ALLSKY_HOME/config.sh" file
echo "export CAMERA=$CAMERA" > "$ALLSKY_CONFIG/autocam.sh"
# redefine the settings variable
CAMERA_SETTINGS="${CAMERA_SETTINGS_DIR}/settings_${CAMERA}.json"
fi

if [ $WAS_AUTO -eq 1 ]; then # Get the proper debug level since earlier config.sh run couldn't.
if [ $WAS_AUTO -eq 1 ]; then # Get the proper debug level since earlier config.sh run couldn't.
ALLSKY_DEBUG_LEVEL=$(jq -r '.debuglevel' "${CAMERA_SETTINGS}")
fi

if [ $WAS_AUTO -eq 1 ]; then # Get the proper debug level since earlier config.sh run couldn't.
ALLSKY_DEBUG_LEVEL=$(jq -r '.debuglevel' "${CAMERA_SETTINGS}")
echo "CAMERA: ${CAMERA}"
if [ "${ALLSKY_DEBUG_LEVEL}" -gt 0 ]; then
echo "CAMERA_SETTINGS: ${CAMERA_SETTINGS}"
fi

# this must be called after camera autoselect
source $ALLSKY_SCRIPTS/filename.sh
# Save auto camera selection for the current session, will be read in config.sh file.
echo "export CAMERA=${CAMERA}" > "${ALLSKY_CONFIG}/autocam.sh"

# Optionally display a notification image. This has to come after the creation of "autocam.sh" above.
# This must be called after CAMERA AUTOSELECT above to refresh the file name.
source "${ALLSKY_SCRIPTS}/filename.sh"

# Optionally display a notification image. This must come after the creation of "autocam.sh" above.
USE_NOTIFICATION_IMAGES=$(jq -r '.notificationimages' "$CAMERA_SETTINGS")
if [ "$USE_NOTIFICATION_IMAGES" = "1" ] ; then
$ALLSKY_SCRIPTS/copy_notification_image.sh "StartingUp" 2>&1
"${ALLSKY_SCRIPTS}/copy_notification_image.sh" "StartingUp" 2>&1
fi

echo "Starting allsky camera..."
Expand All @@ -114,6 +113,16 @@ echo "Starting allsky camera..."
# Want to allow spaces in arguments so need to put quotes around them,
# but in order for it to work need to make ARGUMENTS an array.
ARGUMENTS=()

# Determine if we're called from the service (tty will fail).
# tty should come first so the capture program knows if it should use colors.
if tty --silent ; then
TTY=1
else
TTY=0
fi
ARGUMENTS+=(-tty $TTY)

KEYS=( $(jq -r 'keys[]' $CAMERA_SETTINGS) )
for KEY in ${KEYS[@]}
do
Expand All @@ -126,46 +135,48 @@ done
if [[ $1 == "preview" ]] ; then
ARGUMENTS+=(-preview 1)
fi
ARGUMENTS+=(-daytime $DAYTIME)

# Determine if we're called from the service (tty will fail).
tty --silent
if [ $? -eq 0 ] ; then
TTY=1
else
TTY=0
# "capture" expects 0 or 1; newer versions of config.sh use "true" and "false".
# DAYTIME is the old name for DAYTIME_CAPTURE.
# TODO: These checks will go away in the future.
if [ "${DAYTIME_CAPTURE}" = "true" -o "${DAYTIME}" = "1" ] ; then
DAYTIME_CAPTURE=1
elif [ "${DAYTIME_CAPTURE}" = "false" -o "${DAYTIME}" = "0" ] ; then
DAYTIME_CAPTURE=0
fi
ARGUMENTS+=(-tty $TTY)
ARGUMENTS+=(-daytime $DAYTIME_CAPTURE)

[ "$ADD_PARAMS" != "" ] && ARGUMENTS+=($ADD_PARAMS) # Any additional parameters

Z=""
for A in ${ARGUMENTS[@]}
do
Z="$Z $A"
done
echo "$Z" >> log.txt
echo "${A}"
done > $ALLSKY_TMP/capture_args.txt

RETCODE=0
if [[ $CAMERA == "ZWO" ]]; then
$ALLSKY_HOME/capture "${ARGUMENTS[@]}"
RETCODE=$?
echo "capture exited with retcode=$RETCODE"
CAPTURE="capture"

elif [[ $CAMERA == "RPiHQ" ]]; then
$ALLSKY_HOME/capture_RPiHQ "${ARGUMENTS[@]}"
RETCODE=$?
echo "capture_RPiHQ exited with retcode=$RETCODE"
else
exit 1
CAPTURE="capture_RPiHQ"
fi
"${ALLSKY_HOME}/${CAPTURE}" "${ARGUMENTS[@]}"
RETCODE=$?
[ $RETCODE -ne 0 ] && echo "'${CAPTURE}' exited with RETCODE=${RETCODE}"

if [ "$USE_NOTIFICATION_IMAGES" = "1" -a "$RETCODE" -ne 0 ] ; then
# "capture" will do this if it exited with 0.
if [ "${USE_NOTIFICATION_IMAGES}" = "1" -a "${RETCODE}" -ne 0 ] ; then
# ${CAPTURE} will do this if it exited with 0.
# RETCODE -gt 100 means the we should not restart until the user fixes the error.
if [ "$RETCODE" -gt 100 ]; then
$ALLSKY_SCRIPTS/copy_notification_image.sh "Error" 2>&1
echo "*** Waiting for you to fix this. Restart when done fixing. ***"
sudo service allsky stop
"${ALLSKY_SCRIPTS}/copy_notification_image.sh" "Error" 2>&1
if tty --silent ; then
echo "*** After fixing, restart allsky.sh. ***"
else
echo "*** After fixing, restart the allsky service. ***"
fi
sudo systemctl stop allsky
else
$ALLSKY_SCRIPTS/copy_notification_image.sh "NotRunning" 2>&1
"${ALLSKY_SCRIPTS}/copy_notification_image.sh" "NotRunning" 2>&1
# If started by the service, it will restart us once we exit.
fi
fi

Expand Down

0 comments on commit 96d7bc1

Please sign in to comment.