Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # gnome-clocks_inhibit_sleep.py
- #!/usr/bin/env python3
- # Extract alarm trigger time including alarms that repeat several days
- import json
- import sys
- import re
- import time
- from datetime import datetime
- DAYS = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
- for p in json.load(sys.stdin):
- if "ring_time" in p:
- days = ",".join([DAYS[d] for d in p["days"]])
- h = p["hour"]
- m = p["minute"]
- #print("# {}".format(p["name"]))
- if days:
- m -= 1 # needed to have the systemd timer before the alarm trigger time (otherwise device could go randomly to sleep during the alarm)
- if m < 0:
- m = 59
- h -= 1
- print("OnCalendar={} {:02d}:{:02d}:55".format(days, h, m))
- else:
- #print("OnCalendar={:02d}:{:02d}:55".format(h, m))
- ring_time = p["ring_time"].replace("T", " ")
- ring_time = re.sub(r"\+.*", "", ring_time) # https://stackoverflow.com/questions/52335970/how-to-fix-syntaxwarning-invalid-escape-sequence-in-python/52335971#52335971
- timestamp = time.mktime(time.strptime(ring_time, "%Y-%m-%d %H:%M:%S")) - 5 # https://stackoverflow.com/questions/33621639/convert-date-to-timestamp-in-python/33621755#33621755
- ring_time = datetime.fromtimestamp(int(timestamp)).strftime("%Y-%m-%d %H:%M:%S") # https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date/3682808#3682808
- print("OnCalendar=" + ring_time) # include date to not execute script when alarms should not repeat and get timestamp to subtract day like done for hours and minutes
- # gnome-clocks_inhibit_sleep.sh
- #!/usr/bin/env bash
- # Gnome Clocks inhibit sleep (tested with Mobian / Phosh on the PinePhone)
- # Without this script, the sleep operation "suspend" is not inhibited during ring time (defaults to 5 minutes) so an alarm can ring for a shorter time than configured and can be missed if the phone was used a bit less than the automatic suspend time (defaults to 5 minutes) before an alarm.
- # Based on Gnome Clocks scripts https://github.com/sbeaugrand/debinst/blob/master/mobian/clks.sh and https://github.com/sbeaugrand/debinst/blob/master/mobian/clks.py
- # Issue: https://salsa.debian.org/Mobian-team/devices/sunxi-support/-/work_items/8
- # Script needs to be run each time we configure a new alarm.
- # A terminal window opens to view the inhibit in progress and can be viewed canceled earlier by closing the window or pressing Ctrl+C.
- # To automatically run the script, copy /usr/share/applications/org.gnome.clocks.desktop to ~/.local/share/applications/ (which persists after updates) to comment the DBusActivatable=true line by adding "#" before it and replace the Exec= line with this (using bash instead of /bin/bash does not work for the D-BUS service file of apps like Birdie but works for desktop and service files in this script):
- # Exec=bash -c "gnome-clocks & kgx -e bash -c 'echo -n Press Enter key after editing alarms: ; read -s; echo; /path/to/gnome-clocks_inhibit_sleep.sh'"
- # Escaping single quotes with '\'' to include the ending space in the echo command is not working in the desktop file on Mobian (why?).
- # Gnome Clocks and script will be started at the same time but script will only run after you press the Enter key in a terminal.
- # The alarm snooze feature is not supported, you need to change the alarm time instead and run the script again.
- # The countdown (timer) is not supported but you can use alarms instead until there is an fix upstream or in the Linux distribution.
- # If the phone was powered off during an alarm, we don't get a missed alarm notification after booting the phone unlike Gnome Calendar so it is recommended to open Gnome Clocks as a workaround to check if we did not miss an alarm (this can be done automatically after login by copying the desktop file or creating a symlink in ~/.config/autostart/ like mentioned on https://forum.pine64.org/showthread.php?tid=17278 instead of executing commands in ~/.profile which broke login in a infinite loading spinner loop that could be fixed with SSH access from a computer).
- # There is a bug if we create an alarm that rings 4 minutes after another alarm, both alarms continued ringing even after the default ring time of 5 minutes.
- # Other issues noticed during sleep: when going to sleep, the blue blinking notification led is either always on or always off; when going out of sleep, the network signal bars of Phosh keep changing very fast which seems to display all signal changes that happened while the device was sleeping.
- # Make sure to have the volume level loud enough to hear alarms or follow instructions from https://gitlab.gnome.org/GNOME/calls/-/work_items/745#note_2745975:
- # "Use media roles in pipewire / feedbackd and you can set the alarm volume indendently, see e.g. https://phosh.mobi/posts/role-based-routing (works for alarms too btw and makes sure the speaker is used for alarm sounds)"
- python_script="gnome-clocks_inhibit_sleep.py" # file is expected to be in the same folder as gnome-clocks_inhibit_sleep.sh
- path=`dirname $0`
- # Use other files and service names to avoid interference with alarm app Wake Mobile or Birdie (for system and user wake up timer)
- if [ "$1" = "off" ]; then
- if systemctl --user -q is-enabled wake-up_gnome-clocks.timer; then
- systemctl --user disable wake-up_gnome-clocks.timer
- fi
- if systemctl --user -q is-active wake-up_gnome-clocks.timer; then
- systemctl --user stop wake-up_gnome-clocks.timer
- fi
- exit 0
- fi
- # User Wake Up Timer
- dir=~/.config/systemd/user
- mkdir -p $dir
- file=$dir/wake-up_gnome-clocks.timer
- cat > $file << EOF
- [Unit]
- Description=User Wake Up Timer
- [Install]
- WantedBy=timers.target
- [Timer]
- Persistent=true
- AccuracySec=1us
- EOF
- # convert GVariant format to JSON for easier time extraction
- result=`gsettings get org.gnome.clocks alarms | sed -e 's/@[a-z{}]* //g' -e 's/[<>]//g' -e "s/'/\"/g" | $path/$python_script`
- # avoid error in case all alarms are disabled
- if [ "$result" ]; then
- echo "$result" | tee -a $file # preserve new lines using quotes in echo (using tee to display data)
- else
- echo "OnCalendar=1970-01-01 00:00" | tee -a $file
- fi
- # loop only needed if not handled in Python script in case we don't support alarm that repeat several days
- #time=`gsettings get org.gnome.clocks alarms | sed -e 's/@[a-z{}]* //g' -e 's/[<>]//g' -e "s/'/\"/g" | jq -r '.[].ring_time | select(. != null)' | sed -e 's/T/ /g' -e 's/+.*//g'`
- # https://linuxsimply.com/bash-scripting-tutorial/array/print-array/
- #for item in ${time[@]}
- #do
- # echo "OnCalendar=$item" >> $file
- #done
- # User Wake Up Action
- file=$dir/wake-up_gnome-clocks.service
- # using RuntimeMaxSec to avoid issue of automatic sleep before a second alarm ended (315 seconds if we want the default automatic suspend time and alarm time of 5 minutes including 5 seconds to end process gnome-session-inhibit to cancel suspend inhibit in a terminal window, sleep of 310 seconds which includes 5 seconds inhibit before alarm and 5 seconds after alarm to hear entire sound)
- cat > $file << EOF
- [Unit]
- Description=User Wake Up Action
- [Service]
- ExecStart=kgx -e bash -c "gnome-session-inhibit --inhibit suspend --inhibit-only & pid=\$!; sleep 310; kill \$pid; systemctl sleep"
- RuntimeMaxSec=315
- EOF
- systemctl --user daemon-reload
- if ! systemctl --user -q is-enabled wake-up_gnome-clocks.timer; then
- systemctl --user enable wake-up_gnome-clocks.timer
- fi
- systemctl --user restart wake-up_gnome-clocks.timer
Advertisement
Add Comment
Please, Sign In to add comment