Skip to content

Commit b6d9738

Browse files
authored
Merge pull request achaudhry#10 from hammady/hooks
Add before/after hooks to run custom actions
2 parents 3331c80 + f00a1f8 commit b6d9738

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
*.pyc
44
adhan.log
55
.settings
6+
before-hooks.d/*.sh
7+
after-hooks.d/*.sh

README.md

+44-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This projects uses a python script which automatically calculates [adhan](https:
1919
Run this command:
2020

2121
```bash
22-
$ python /home/pi/adhan/updateAzaanTimers.py --lat <YOUR_LAT> --lng <YOUR_LNG> --method <METHOD>
22+
$ /home/pi/adhan/updateAzaanTimers.py --lat <YOUR_LAT> --lng <YOUR_LNG> --method <METHOD>
2323
```
2424

2525
Replace the arguments above with your location information and calculation method:
@@ -34,12 +34,12 @@ If everythig worked, your output will look something like this:
3434
14:11
3535
16:30
3636
17:53
37-
51 5 * * * omxplayer -o local /home/pi/adhan/Adhan-fajr.mp3 > /dev/null 2>&1 # rpiAdhanClockJob
38-
52 11 * * * omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1 # rpiAdhanClockJob
39-
11 14 * * * omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1 # rpiAdhanClockJob
40-
30 16 * * * omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1 # rpiAdhanClockJob
41-
53 17 * * * omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1 # rpiAdhanClockJob
42-
0 1 * * * python /home/pi/adhan/updateAzaanTimers.py >> /home/pi/adhan/adhan.log 2>&1 # rpiAdhanClockJob
37+
51 5 * * * /home/pi/adhan/playAzaan.sh /home/pi/adhan/Adhan-fajr.mp3 0 # rpiAdhanClockJob
38+
52 11 * * * /home/pi/adhan/playAzaan.sh /home/pi/adhan/Adhan-Madinah.mp3 0 # rpiAdhanClockJob
39+
11 14 * * * /home/pi/adhan/playAzaan.sh /home/pi/adhan/Adhan-Madinah.mp3 0 # rpiAdhanClockJob
40+
30 16 * * * /home/pi/adhan/playAzaan.sh /home/pi/adhan/Adhan-Madinah.mp3 0 # rpiAdhanClockJob
41+
53 17 * * * /home/pi/adhan/playAzaan.sh /home/pi/adhan/Adhan-Madinah.mp3 0 # rpiAdhanClockJob
42+
0 1 * * * /home/pi/adhan/updateAzaanTimers.py >> /home/pi/adhan/adhan.log 2>&1 # rpiAdhanClockJob
4343
@monthly truncate -s 0 /home/pi/adhan/adhan.log 2>&1 # rpiAdhanClockJob
4444
Script execution finished at: 2017-01-06 21:22:31.512667
4545
```
@@ -56,11 +56,47 @@ There are 2 additional arguments that are optional, you can set them in the firs
5656
further runs: `--fajr-azaan-volume` and `azaan-volume`. You can control the volume of the Azaan
5757
by supplying numbers in millibels. To get more information on how to select the values, run the command with `-h`.
5858

59+
## Configuring custom actions before/after adhan
60+
61+
Sometimes it is needed to run custom commands either before, after or before
62+
and after playing adhan. For example, if you have
63+
[Quran playing continuously](https://github.com/LintangWisesa/RPi_QuranSpeaker),
64+
you would want to pause and resume the playback. Another example, is to set your
65+
status on a social network, or a calendar, to block/unblock the Internet
66+
using [pi.hole rules](https://docs.pi-hole.net/), ... etc.
67+
68+
You can easily do this by adding scripts in the following directories:
69+
- `before-hooks.d`: Scripts to run before adhan playback
70+
- `after-hooks.d`: Scripts to run after adhan playback
71+
72+
### Example:
73+
To pause/resume Quran playback if using the
74+
[RPi_QuranSpeaker](https://github.com/LintangWisesa/RPi_QuranSpeaker) project, place
75+
the following in 2 new files under the above 2 directories:
76+
77+
```bash
78+
# before-hooks.d/01-pause-quran-speaker.sh
79+
#!/usr/bin/env bash
80+
/home/pi/RPi_QuranSpeaker/pauser.py pause
81+
```
82+
83+
```bash
84+
# after-hooks.d/01-resume-quran-speaker.sh
85+
#!/usr/bin/env bash
86+
/home/pi/RPi_QuranSpeaker/pauser.py resume
87+
```
88+
89+
Do not forget to make the scripts executable:
90+
```bash
91+
chmod u+x ./before-hooks.d/01-pause-quran-speaker.sh
92+
chmod u+x ./after-hooks.d/01-resume-quran-speaker.sh
93+
```
94+
5995
## Tips:
6096
1. You can see your currently scheduled jobs by running `crontab -l`
6197
2. The output of the job that runs at 1am every night is being captured in `/home/pi/adhan/adhan.log`. This way you can keep track of all successful runs and any potential issues. This file will be truncated at midnight on the forst day of each month. To view the output type `$ cat /home/pi/adhan/adhan.log`
6298

63-
### Credits
99+
## Credits
64100
I have made modifications / bug fixes but I've used the following as starting point:
65101
* Python code to calculate adhan times: http://praytimes.org/code/
66102
* Basic code to turn the above into an adhan clock: http://randomconsultant.blogspot.co.uk/2013/07/turn-your-raspberry-pi-into-azaanprayer.html

after-hooks.d/00-example.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
# This file must be executable, to make it as such, run the following:
3+
# chmod u+x <this-file>
4+
# It can also be written in any language so long as a valid shebang (#!) is used as above
5+
echo "Placeholder for an after hook"

before-hooks.d/00-example.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
# This file must be executable, to make it as such, run the following:
3+
# chmod u+x <this-file>
4+
# It can also be written in any language so long as a valid shebang (#!) is used as above
5+
echo "Placeholder for a before hook"

playAzaan.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
if [ $# -lt 1 ]; then
3+
echo "USAGE: $0 <azaan-audio-path> [<volume>]"
4+
exit 1
5+
fi
6+
7+
audio_path="$1"
8+
vol=${2:-0}
9+
root_dir=`dirname $0`
10+
11+
# Run before hooks
12+
for hook in $root_dir/before-hooks.d/*; do
13+
echo "Running before hook: $hook"
14+
$hook
15+
done
16+
17+
# Play Azaan audio
18+
omxplayer --vol $vol -o local $audio_path
19+
20+
# Run after hooks
21+
for hook in $root_dir/after-hooks.d/*; do
22+
echo "Running after hook: $hook"
23+
$hook
24+
done

updateAzaanTimers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
import datetime
44
import time
@@ -118,9 +118,9 @@ def addClearLogsCronJob (objCronTab, strCommand):
118118
isDst = time.localtime().tm_isdst
119119

120120
now = datetime.datetime.now()
121-
strPlayFajrAzaanMP3Command = 'omxplayer --vol {} -o local {}/Adhan-fajr.mp3 > /dev/null 2>&1'.format(fajr_azaan_vol, root_dir)
122-
strPlayAzaanMP3Command = 'omxplayer --vol {} -o local {}/Adhan-Madinah.mp3 > /dev/null 2>&1'.format(azaan_vol, root_dir)
123-
strUpdateCommand = 'python {}/updateAzaanTimers.py >> {}/adhan.log 2>&1'.format(root_dir, root_dir)
121+
strPlayFajrAzaanMP3Command = '{}/playAzaan.sh {}/Adhan-fajr.mp3 {}'.format(root_dir, root_dir, fajr_azaan_vol)
122+
strPlayAzaanMP3Command = '{}/playAzaan.sh {}/Adhan-Madinah.mp3 {}'.format(root_dir, root_dir, azaan_vol)
123+
strUpdateCommand = '{}/updateAzaanTimers.py >> {}/adhan.log 2>&1'.format(root_dir, root_dir)
124124
strClearLogsCommand = 'truncate -s 0 {}/adhan.log 2>&1'.format(root_dir)
125125
strJobComment = 'rpiAdhanClockJob'
126126

0 commit comments

Comments
 (0)