-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.py
More file actions
212 lines (151 loc) · 4.39 KB
/
Copy pathScheduler.py
File metadata and controls
212 lines (151 loc) · 4.39 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#main logic to recieve configuration information and execute commands on schedule
#Infinitly running loop, checks for updated schedule every minute, then determines
#appropriate actions
#schedule format:
# 1(access flag) {"status":status , "schedule": {
# date:[STATE, STATE, STATE, ...],
# date:[STATE, STATE, ...],
# ... }
# }
#
# status : enabled/disabled
#
# STATE = {"code":code, "start":time, "end":time,
# "progress":prog}
#
# code: A(A on, D off) 0000(channel) 00(volume)
#
# prog = {0 (pending), 1(active), 2(finished)}
#
import json
import pytz
import time
import os
from datetime import datetime
def main():
#create first time routine that turns tv off
mainLoop("testFile.txt")
def mainLoop(file):
while True:
time.sleep(2)
# read file -> make local copy, set accessed flag to 0
f = open(file, "r")
text = f.read()
f.close()
Data = text[2:]
if text[0] == "1":
tempText = "0 " + Data
f = open(file, "w")
f.write(tempText)
f.close()
try:
JData = json.loads(Data)
except ValueError:
#print("couldn't parse Json")
continue
#check if status is enabled
status = JData.get('status', 0)
#if an error is encountered (badly formated file)
#it just loops back to prevent crash
if status == 0:
continue
if status == "disabled":
#print("disabled")
continue
#Find current task
schedule = JData.get('schedule', 0)
if schedule == 0:
continue
tz_BA = pytz.timezone('America/Buenos_Aires')
todayObj = datetime.now(tz_BA)
today = todayObj.strftime("%d/%m/%Y")
#make sure there's a schedule for today
if not (schedule.get(today, False)):
#print("nothing for today")
continue
Tasks = schedule.get(today)
#loop through the tasks until we find current one
found = False;
foundIndex = 0;
for i in range(0, len(Tasks)):
currentTask = Tasks[i]
start = currentTask.get("start")
start_DT = tz_BA.localize(datetime.strptime(today+" "+start, "%d/%m/%Y %H:%M"))
end = currentTask.get("end")
end_DT = tz_BA.localize(datetime.strptime(today+" "+end, "%d/%m/%Y %H:%M"))
if (start_DT < todayObj)and(todayObj < end_DT):
found = True
foundIndex = i
break
if not found:
#print("nothing to do right now")
continue
#check if already active
if currentTask.get("progress") == 1:
#print("current task already in progress")
continue
#get code and send approriate commands to queue
code = str(currentTask.get("code"))
#modify the currentTask to have progress = 1
mod = {"progress":1}
currentTask.update(mod)
#update the Tasks list
Tasks[foundIndex] = currentTask
#update the schedule dict
schedule.update({today: Tasks})
#update Jdata
JData.update({"schedule": schedule})
#make sure file hasn't changed since beginning of loop
f = open(file, "r")
text = f.read()
f.close()
if text[0] == "1":
#print('oops, file changed while processing it')
continue
Data = json.dumps(JData)
text = "0 " + Data
f = open(file, "w")
f.write(text)
f.close()
executeCode(code)
def executeCode(code):
codeArray = code.split()
if codeArray[0] == 'D':
control_powerOFF()
else:
control_on(codeArray[1], codeArray[2])
def control_on(channel, volume):
#check if tv is on, then change channel accordingly and set volume
control_powerON()
control_setChannel(channel)
control_setVolume(volume)
#print ("turning tv on to channel " + channel + " at volume "+volume)
def control_powerOFF():
os.system('irsend SEND_START RC64 KEY_POWEROFF')
time.sleep(1.5)
os.system('irsend SEND_STOP RC64 KEY_POWEROFF')
os.system('irsend SEND_START sonyTV3 KEY_power2')
time.sleep(1.5)
os.system('irsend SEND_STOP sonyTV3 KEY_power2')
def control_powerON():
#send ir to power on
os.system('irsend SEND_START RC64 KEY_POWERON')
time.sleep(1.5)
os.system('irsend SEND_STOP RC64 KEY_POWERON')
os.system('irsend SEND_START sonyTV3 KEY_POWER')
time.sleep(1.5)
os.system('irsend SEND_STOP sonyTV3 KEY_POWER')
def control_setChannel(channel):
for i in range(0, len(channel)):
os.system('irsend SEND_ONCE RC64 KEY_'+channel[i])
time.sleep(0.15)
def control_setVolume(volume):
#first set volume to 0
os.system('irsend SEND_START sonyTV3 KEY_volumedown')
time.sleep(4)
os.system('irsend SEND_STOP sonyTV3 KEY_volumedown')
for j in range (0, volume-1):
#send ir blast for volume up
os.system('irsend SEND_ONCE sonyTV3 KEY_volumeup')
time.sleep(1/5)
main()