pub / sguard

Suspend Guard for the PhoneShell "Phosh"
git clone https://https://src.jayvii.de/pub/sguard.git
Home | Log | Files | Exports | Refs | README | RSS

commit 828f592fd74c6c8d3e73ced9c68d02102e02583a
parent 12c287373745eaf59fe8f0bd8fd66c1cad55bbf8
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Tue, 22 Sep 2020 10:54:07 +0200

rename to b(ash)alarm; daemon for balarm

Diffstat:
Dalarm.sh | 57---------------------------------------------------------
Abalarm/balarm.sh | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abalarm/daemon.sh | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 189 insertions(+), 57 deletions(-)

diff --git a/alarm.sh b/alarm.sh @@ -1,57 +0,0 @@ -#!/usr/bin/env bash - -# defaults ------------------------------------------------------------- -zzzaction=0 -fullvolume=1 -export DISPLAY=:0 - -# checks --------------------------------------------------------------- -# has alarmtone been chosen yet? -if [ ! -f "$HOME/.alarmtone" ]; then - # assuming some tone... - ln -s /usr/share/sounds/freedesktop/stereo/phone-incoming-call.oga \ - "$HOME/.alarmtone" -fi - -# turn up volume -if [ $fullvolume == 1 ]; then - pactl set-sink-volume 0 0.99 # not really sure why, but it works... - pactl set-sink-volume 1 0.99 # can be either apparently... -fi - -# Function: ring alarm ------------------------------------------------- -function alarm_ring { - # play ringtone - mpv --loop-file=inf ~/.alarmtone & - # noting down PID of alarm process - alarmpid=$! -} - -# Function: ask user what to do ---------------------------------------- -function zzzorawake { - yad --title="Alarm!" \ - --text="$(date +%H:%M)" \ - --button="Snooze..." \ - --button="Stop!" - zzzaction=$? -} - -# initial run ---------------------------------------------------------- -# ringing alarm... -alarm_ring -# asking user what to do -zzzorawake - -# the famous snooze loop ----------------------------------------------- -# if snooze was chosen, wait for another 5 minutes before ringing -while [ $zzzaction == 0 ]; do - kill $alarmpid - sleep 300 # 5 minutes - alarm_ring # ring alarm - zzzorawake # ask user what to do -done - -# if you finally get up, we can stop the misery ------------------------ -kill $alarmpid - -# EOF alarm.sh diff --git a/balarm/balarm.sh b/balarm/balarm.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# Copyright (C) 2020 Jan "JayVii" +# Author 2020 Jan "JayVii" <jayvii [AT] posteo [DOT] de> +# SPDX-License-Identifier: gpl-3.0 +# About this header: <https://reuse.software> + +############################################################################### +# balarm.sh - the alarm-script written in bash. # +# # +# this script is typically executed by daemon.sh, but may also work with cron # +# # +# this script requires "yad" and "mpv" to be installed. # +############################################################################### + +# Configuration --------------------------------------------------------------- +BASE_PATH="~/.config/balarm" +ALARM_FILE="$BASE_PATH/sound" +SNOOZE_TIME=300 + +# defaults -------------------------------------------------------------------- +ZZZACTION=0 +FULLVOLUME=1 + +# checks ---------------------------------------------------------------------- +# has alarmtone been chosen yet? +if [ ! -f "$ALARM_FILE" ]; then + # assuming some tone... + ln -s /usr/share/sounds/freedesktop/stereo/phone-incoming-call.oga \ + "$ALARM_FILE" +fi + +# turn up volume +if [ $FULLVOLUME == 1 ]; then + pactl set-sink-volume 0 0.99 # not really sure why, but it works... + pactl set-sink-volume 1 0.99 # can be either apparently... +fi + +# Function: ring alarm -------------------------------------------------------- +function alarm_ring { + # play ringtone + mpv --loop-file=inf "$1" & + # noting down PID of alarm process + ALARMPID=$! +} + +# Function: ask user what to do ----------------------------------------------- +function zzzorawake { + yad --title="Alarm!" \ + --text="$(date '+%a, %Y-%m-%d %H:%M:%S')" \ + --button="Snooze..." \ + --button="Stop!" + ZZZACTION=$? +} + +# initial run ----------------------------------------------------------------- +# ringing alarm... +alarm_ring "$ALARM_FILE" +# asking user what to do +zzzorawake + +# the famous snooze loop ------------------------------------------------------ +# if snooze was chosen, wait for another 5 minutes before ringing +while [ $ZZZACTION == 0 ]; do + kill -9 $ALARMPID + sleep $SNOOZE_TIME # snooze... + alarm_ring "$ALARM_FILE" # ring alarm + zzzorawake # ask user what to do +done + +# if you finally get up, we can stop the misery ------------------------------- +kill $ALARMPID + +# EOF balarm.sh diff --git a/balarm/daemon.sh b/balarm/daemon.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# Copyright (C) 2020 Jan "JayVii" +# Author 2020 Jan "JayVii" <jayvii [AT] posteo [DOT] de> +# SPDX-License-Identifier: gpl-3.0 +# About this header: <https://reuse.software> + +############################################################################### +# daemon.sh - time-watcher script for balarm.sh, the alarm-script written in # +# bash. # +# # +# ATTENTION: requires balarm.sh to be in $PATH # +############################################################################### + +# Configuration --------------------------------------------------------------- +BASE_PATH="~/.config/balarm" +PID_FILE="~/.cache/balarm.pid" +RANG_FILE="~/.cache/balarm.log" +TIME_FILE="$BASE_PATH/timings.txt" + +# SetUp ----------------------------------------------------------------------- +if [ ! -d "$BASE_PATH" ]; then mkdir -p "$BASE_PATH"; fi +if [ ! -f "$TIME_FILE" ]; then touch "$TIME_FILE"; fi +if [ ! -f "$RANG_FILE" ]; then touch "$RANG_FILE"; fi +if [ ! -f "$PID_FILE" ]; then touch "$PID_FILE"; fi + +# Functions ------------------------------------------------------------------- + +# compares two time strings in "%H %M %a" format. +# sets $TIME_MATCH to 1 if time is equal or diff by 1 +function compare_time { + # Extract hours, minutes & day from current time + _HOURS=$(echo "$1" | awk '{ print $1 }') + _MINUTES=$(echo "$1" | awk '{ print $2 }') + _DAY=$(echo "$1" | awk '{ print $3 }') + # construct +1 and -1 minute strings + if [ $_MINUTES == 0 ]; then + M1="$(($_HOURS - 1)) 59 $_DAY" + P1="$_HOURS $(($_MINUTES + 1)) $_DAY" + elif [ $_MINUTES == 59 ]; then + M1="$_HOURS $(($_MINUTES - 1)) $_DAY" + P1="$(($_HOURS + 1)) 00 $_DAY" + else + M1="$_HOURS $(($_MINUTES - 1)) $_DAY" + P1="$_HOURS $(($_MINUTES + 1)) $_DAY" + fi + # compare strings + if [[ "$2" == "$1" ]] || [[ "$2" == "$M1" ]] || [[ "$2" == "$P1"]]; then + TIME_MATCH=1 + else + TIME_MATCH=0 + fi +} + +# checks whether current alarm has been executed already. +rang_yet { + # check whether current alarm has been executed today already... + _RANG=$(grep "$CURRDATE $ALARM" "$RANG_FILE") + if [[ "$_RANG" ]]; then + HAS_RANG=1 # yes, it has! + else + HAS_RANG=0 # no, it has not! + fi +} + +# Refresh PID file ------------------------------------------------------------ + +# reported PID for daemon +TMP_PID=$(cat "$PID_FILE") + +# does reported PID correspond to THIS process? +DAE_PID=$(ps all | grep " $TMP_PID " | grep -v "grep" | grep "daemon.sh") + +# if so, restart daemon. Else just refresh PID +if [ "$DAE_PID" ]; then + echo "Refreshing..." + kill -9 "$TMP_PID" +fi + +# either way, refresh reported PID for daemon +echo $$ > "$PID_FILE" + +# Run time-watch daemon ------------------------------------------------------- + +while :; do + + # How many active alarms do we have? + ALARM_N=$(wc -l "$TIME_FILE" | awk '{ print $1 }') + + # Check current time and date + CURRTIME=$(date "+%H %M %a") + CURRDATE=$(date "+%Y %m %d") + + # loop through all set up alarms + for i in {1..$ALARM_N}; do + # current alarm's time + ALARMTIME=$(sed -ne '${i}p' "$TIME_FILE") + # compare alarm time to current time + compare_time "$CURRTIME" "$ALARMTIME" + # if they match, check whether it has been executed already + if [ $TIME_MATCH == 1 ]; then + rang_yet + # if it has not been executed yet, execute it + if [ $HAS_RANG == 0 ]; then + balarm.sh & + echo "$CURRDATE $ALARMTIME" > "$RANG_FILE" + fi + fi + done + + # timeout (1min - execution time) + CURRSEC=$(date +%S) # current time (seconds) after execution loop + sleep $((60 - $CURRSEC)) # should precisely land on HH:MM+1:00. + +done + +# EOF daemon.sh