generated from me/template-mit
Fix false positives when time was not getting successfully retrieved.
This commit is contained in:
1 parent
7d9bc589c5
commit
949e1baacd
1 file changed
+37
-29
+37
-29
@@ -1,18 +1,14 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# sunset-alarm -- Alert before or after sunset
|
# sunset-alarm -- Alert before or after sunset.
|
||||||
# Displays a persistent alert when current time is within
|
# Displays a persistent alert when current time is within
|
||||||
# (sunset minus specified minutes) of midnight.
|
# (sunset minus specified minutes) of midnight.
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Defaults
|
# Defaults
|
||||||
###############################################################################
|
###############################################################################
|
||||||
LATLONG=""
|
declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE=""
|
||||||
SUNSET_TIME=""
|
typeset -i SUNSET_EPOCH=0
|
||||||
MINUTES=""
|
|
||||||
SUNSET_EPOCH=0
|
|
||||||
LAST_DATE=""
|
|
||||||
PROMPT="sunset-alarm"
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Usage
|
# Usage
|
||||||
@@ -56,7 +52,7 @@ while [ $# -gt 0 ]; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Validate inputs
|
# Validate inputs.
|
||||||
if [ -z "$MINUTES" ]; then
|
if [ -z "$MINUTES" ]; then
|
||||||
echo "ERROR: -m MINUTES is required." >&2
|
echo "ERROR: -m MINUTES is required." >&2
|
||||||
usage
|
usage
|
||||||
@@ -106,24 +102,26 @@ fetch_sunset_time() {
|
|||||||
#REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&forecast_days=1"
|
#REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&forecast_days=1"
|
||||||
REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&start_date=${TODAY}&end_date=${TODAY}"
|
REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&start_date=${TODAY}&end_date=${TODAY}"
|
||||||
RESPONSE=$(curl -s "$REQUEST")
|
RESPONSE=$(curl -s "$REQUEST")
|
||||||
|
status="$?"
|
||||||
|
echo "Curl command completed with a status of $status."
|
||||||
|
|
||||||
if [ -z "$RESPONSE" ]; then
|
if [[ -z "$RESPONSE" ]]; then
|
||||||
echo "ERROR: Did not receive response. $REQUEST" >&2
|
echo "ERROR: Did not receive response. $REQUEST" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Extract sunset timestamp like "2025-06-15T19:32:00"
|
# Extract sunset timestamp like "2025-06-15T19:32:00".
|
||||||
SUNSET_RAW=$(echo "$RESPONSE" | jq -r '.daily.sunset[0]')
|
SUNSET_RAW=$(echo "$RESPONSE" | jq -r '.daily.sunset[0]')
|
||||||
|
|
||||||
if [ -z "$SUNSET_RAW" ]; then
|
if [[ -z "$SUNSET_RAW" ]]; then
|
||||||
echo "ERROR: Could not parse time. $RESPONSE" >&2
|
echo "ERROR: Could not parse time. $RESPONSE" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Convert to epoch
|
# Convert to epoch.
|
||||||
SUNSET_EPOCH=$(date -d "$(echo "$SUNSET_RAW" | cut -dT -f1) $(echo "$SUNSET_RAW" | cut -dT -f2)" +%s 2>/dev/null)
|
SUNSET_EPOCH=$(date -d "$(echo "$SUNSET_RAW" | cut -dT -f1) $(echo "$SUNSET_RAW" | cut -dT -f2)" +%s 2>/dev/null)
|
||||||
|
|
||||||
if [ -z "$SUNSET_EPOCH" ]; then
|
if [[ -z "$SUNSET_EPOCH" ]]; then
|
||||||
echo "ERROR: Could not calculate epoch. $SUNSET_RAW" >&2
|
echo "ERROR: Could not calculate epoch. $SUNSET_RAW" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -166,53 +164,63 @@ while true; do
|
|||||||
TODAY=$(date +%Y-%m-%d)
|
TODAY=$(date +%Y-%m-%d)
|
||||||
CURRENT_TIME=$(date +%s)
|
CURRENT_TIME=$(date +%s)
|
||||||
|
|
||||||
# Check if we need to fetch new sunset time for a new day
|
# Check if we need to fetch new sunset time for a new day.
|
||||||
if [ "$LAST_DATE" != "$TODAY" ]; then
|
if [[ "$LAST_DATE" != "$TODAY" \
|
||||||
|
|| "$SUNSET_EPOCH" == 0 || -z "$SUNSET_TIME" ]]
|
||||||
|
then
|
||||||
LAST_DATE="$TODAY"
|
LAST_DATE="$TODAY"
|
||||||
|
|
||||||
if [ -n "$LATLONG" ]; then
|
if [[ -n "$LATLONG" ]]; then
|
||||||
# Need internet to get sunset time
|
# Need internet to get sunset time.
|
||||||
if ! check_internet; then
|
if ! check_internet; then
|
||||||
echo "No internet connection. Retrying in 5 minutes." >&2
|
echo "ERROR: No internet connection. Retrying in 5 minutes." >&2
|
||||||
sleep 300
|
sleep 300
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fetch_sunset_time || {
|
fetch_sunset_time || {
|
||||||
echo "Failed fetching sunset time. Retrying in 5 minutes." >&2
|
echo "ERROR: Failed fetching sunset time. Retrying in 5 minutes." >&2
|
||||||
sleep 300
|
sleep 300
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if [[ -z "$SUNSET_TIME" ]]; then
|
||||||
|
echo "ERROR: Sunset time is still null. Retrying in 5 minutes." >&2
|
||||||
|
sleep 300
|
||||||
|
continue
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
# User provided sunset time directly, convert to epoch
|
# User provided sunset time directly, convert to epoch.
|
||||||
SUNSET_EPOCH=$(date -d "${TODAY} ${SUNSET_TIME}" +%s 2>/dev/null)
|
SUNSET_EPOCH=$(date -d "${TODAY} ${SUNSET_TIME}" +%s 2>/dev/null)
|
||||||
|
|
||||||
if [ -z "$SUNSET_EPOCH" ]; then
|
if [[ -z "$SUNSET_EPOCH" ]]; then
|
||||||
echo "ERROR: could not parse time '${SUNSET_TIME}'." >&2
|
echo "ERROR: Could not parse time '${SUNSET_TIME}'." >&2
|
||||||
sleep 60
|
sleep 60
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Calculate alert window start (sunset minus specified minutes)
|
# Calculate alert window start (sunset minus specified minutes).
|
||||||
ALERT_WINDOW_START=$(( SUNSET_EPOCH - (MINUTES * 60) ))
|
ALERT_WINDOW_START=$(( SUNSET_EPOCH - (MINUTES * 60) ))
|
||||||
|
|
||||||
# Calculate midnight of the current day
|
# Calculate midnight of the current day
|
||||||
MIDNIGHT=$(date -d "${TODAY} +1 day" +%s)
|
MIDNIGHT=$(date -d "${TODAY} +1 day" +%s)
|
||||||
|
|
||||||
# Alert while current time is within the window (from window start until midnight)
|
# Alert while current time is within the window (from window start until midnight).
|
||||||
if [ "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" ] && \
|
if [[ -n "$SUNSET_TIME" && "$SUNSET_EPOCH" -ge 0 \
|
||||||
[ "$CURRENT_TIME" -lt "$MIDNIGHT" ]; then
|
&& "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \
|
||||||
echo "Alert time has been reached, show message."
|
&& "$CURRENT_TIME" -lt "$MIDNIGHT" ]]
|
||||||
|
then
|
||||||
|
echo "Alert time has been reached for $SUNSET_TIME, show message."
|
||||||
show_alert
|
show_alert
|
||||||
echo "Alert has been dismissed, sleeping for 5 minutes."
|
echo "Alert has been dismissed, sleeping for 5 minutes."
|
||||||
sleep 290
|
sleep 290
|
||||||
echo "Waking back up from notification dismissal."
|
echo "Waking back up from notification dismissal."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Reset when we've passed midnight so we fetch new time tomorrow
|
# Reset when we've passed midnight so we fetch new time tomorrow.
|
||||||
if [ "$CURRENT_TIME" -ge "$MIDNIGHT" ]; then
|
if [[ "$CURRENT_TIME" -ge "$MIDNIGHT" ]]; then
|
||||||
LAST_DATE=""
|
LAST_DATE=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user