-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateAzaanTimers.py
executable file
·95 lines (79 loc) · 2.86 KB
/
updateAzaanTimers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import datetime
import time
import sys
sys.path.insert(0, '/home/pi/adhan/crontab')
from praytimes import PrayTimes
PT = PrayTimes()
from crontab import CronTab
system_cron = CronTab(user='pi')
now = datetime.datetime.now()
assetSholat = "curl 'http://localhost/api/v1/assets/control/asset&1740a9e1daf24a2cb58f9e08a9546703'"
strPlayFajrAzaanMP3Command = assetSholat+'&& omxplayer -o local /home/pi/adhan/Adhan-fajr.mp3 > /dev/null 2>&1'
strPlayAzaanMP3Command = assetSholat+'&& omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1'
strUpdateCommand = 'python /home/pi/adhan/updateAzaanTimers.py >> /home/pi/adhan/adhan.log 2>&1'
strClearLogsCommand = 'truncate -s 0 /home/pi/adhan/adhan.log 2>&1'
strJobComment = 'rpiAdhanClockJob'
#Set latitude and longitude here
#--------------------
lat = -6.21462
long = 106.84513
#Set calculation method, utcOffset and dst here
#By default system timezone will be used
#--------------------
PT.setMethod('Makkah')
utcOffset = -(time.timezone/3600)
isDst = time.localtime().tm_isdst
#HELPER FUNCTIONS
#---------------------------------
#---------------------------------
#Function to add azaan time to cron
def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):
job = objCronTab.new(command=strCommand,comment=strPrayerName)
timeArr = strPrayerTime.split(':')
hour = timeArr[0]
min = timeArr[1]
job.minute.on(int(min))
job.hour.on(int(hour))
job.set_comment(strJobComment)
print job
return
def addUpdateCronJob (objCronTab, strCommand):
job = objCronTab.new(command=strCommand)
job.minute.on(15)
job.hour.on(3)
job.set_comment(strJobComment)
print job
return
def addClearLogsCronJob (objCronTab, strCommand):
job = objCronTab.new(command=strCommand)
job.day.on(1)
job.minute.on(0)
job.hour.on(0)
job.set_comment(strJobComment)
print job
return
#---------------------------------
#---------------------------------
#HELPER FUNCTIONS END
# Remove existing jobs created by this script
system_cron.remove_all(comment=strJobComment)
# Calculate prayer times
times = PT.getTimes((now.year,now.month,now.day), (lat, long), utcOffset, isDst)
print times['fajr']
print times['dhuhr']
print times['asr']
print times['maghrib']
print times['isha']
# Add times to crontab
addAzaanTime('fajr',times['fajr'],system_cron,strPlayFajrAzaanMP3Command)
addAzaanTime('dhuhr',times['dhuhr'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('asr',times['asr'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('maghrib',times['maghrib'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('isha',times['isha'],system_cron,strPlayAzaanMP3Command)
# Run this script again overnight
addUpdateCronJob(system_cron, strUpdateCommand)
# Clear the logs every month
addClearLogsCronJob(system_cron,strClearLogsCommand)
system_cron.write_to_user(user='pi')
print 'Script execution finished at: ' + str(now)