forked from 538838/raspberry-infoscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tv_daemon.py
381 lines (349 loc) · 16 KB
/
tv_daemon.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# -*- coding: utf-8 -*-
# by Sebastian Brand (538838.com)
# Version 1.0
#
# started (forked) by /etc/rc.local
import os #replace with subprocess?
import tv_folder
import time
import parsedatetime
import re as regexp
from datetime import datetime,timedelta
from time import mktime
from logging.handlers import RotatingFileHandler
import logging
import shutil
import RPi.GPIO as gpio
import threading
class tv_daemon:
# Initiates all necessary stuff
def __init__(self):
os.chdir("/root/tvscript_v1.0")
self._configfile = "tv.conf"
self._mount = "/mnt/tv/"
self._temporary = "/var/tmp/tv/"
self._logfile = "/var/log/tv.log"
self._pinIn = 13
self._pinOut = 15
self._cecSleep = 20
self._mountSleep = 30
self._clearSleep = 10
self._settings = {"on": True, "source": True, "off": True, "type": "samba", "source": "", "user": "", "pass": "", "time": 30, "default": True, "noDefaultTvCtrl": True, "maxTime": 60}
self._logMaxBytes = 10000000
self._logbackupCount = 5
self._folders = []
self._tvFolders = []
self._active = None
self._default = None
self._semaphore = threading.Semaphore()
def init(self):
# Add logger
self._logger = logging.getLogger()
self._logger.setLevel(logging.INFO)
handler = RotatingFileHandler(self._logfile, maxBytes=self._logMaxBytes,backupCount=self._logbackupCount)
handler.setFormatter(logging.Formatter("(%(levelname)s) %(asctime)s: %(message)s"))
self._logger.addHandler(handler)
self._logger.info("== Starting ==")
# Create parsedatetime object, and nexttime variable
self._pdtcal = parsedatetime.Calendar()
self._nextTime = self._parse("2038-01-01")[0]
self._lastUpdate = self._parse("1970-01-01")[0]
err = None
err = self._readConf()
if err != None:
self._logger.error(err)
# Mount
if self._settings["type"] == "samba":
c = "mount -t cifs -o username=" + self._settings["user"] + ",password=" + self._settings["pass"] + ",noserverino" + " " + self._settings["source"] + " " + self._mount
os.system(c)
while not os.path.ismount(self._mount):
self._logger.error("Could not mount, retries")
c = "mount -t cifs -o username=" + self._settings["user"] + ",password=" + self._settings["pass"] + ",noserverino" + " " + self._settings["source"] + " " + self._mount
os.system(c)
time.sleep(self._mountSleep)
else:
self._logger.error("Unknown mount_type")
# Empty old temporary folder
if os.path.exists(self._temporary):
shutil.rmtree(self._temporary)
os.mkdir(self._temporary)
# Start TV and set source
if self._settings["on"] == True:
os.system("echo \"on 0\" | /usr/bin/cec-client -s -d 1")
if self._settings["source"] == True:
time.sleep(self._cecSleep)
if self._settings["source"] == True:
os.system("echo \"as 0\" | /usr/bin/cec-client -s -d 1")
# Clear screen (must be before gpio??)
os.system("printf '\033[2J\033[1;1H' > /dev/tty1")
os.system("setterm -cursor off > /dev/tty1")
time.sleep(30) # Might be needed to make trigger funtion correctly?
# Set shutdown pin trigger and add delay to reset
gpio.setmode(gpio.BOARD)
gpio.setwarnings(False)
#Delay
gpio.setup(self._pinOut,gpio.OUT)
gpio.output(self._pinOut, 0)
#Shutdown pin
gpio.setup(self._pinIn, gpio.IN,pull_up_down=gpio.PUD_UP)
gpio.add_event_detect(self._pinIn, gpio.FALLING, callback=self.stop, bouncetime=200)
self._logger.info("Successfully started")
def stop(self,pin):
if gpio.input(pin) == 0:
self._logger.info("Stopping script")
self._semaphore.acquire()
if self._active != None:
self._active.stop()
err = self._active.cleanup()
self._logger.error(err)
os.system("umount " + self._mount)
if self._settings["off"] == True:
os.system("echo \"standby 0\" | /usr/bin/cec-client -s -d 1")
self._logger.info("Shutting down pi")
os.system("shutdown -h 0")
def run(self):
while True:
self._semaphore.acquire()
err = self._update()
self._semaphore.release()
if err != None:
self._logger.error(err)
time.sleep(self._settings["time"])
def _update(self):
updated = False
activeRemoved = False
updatedActive = False
folders = [ f for f in os.listdir(self._mount) if os.path.isdir(os.path.join(self._mount,f)) and f[0] != '#' ]
#if folders == []:
# return "No folders found"
# Check for new folders on disk
if set(folders) != set(self._folders):
updated = True
removed = [f for f in self._folders if f not in folders]
new = [f for f in folders if f not in self._folders]
self._folders = folders
# Remove tv_folders
for t in self._tvFolders:
if t.name in removed:
# If active removed
if t == self._active:
self._active.stop()
self._active.cleanup()
self._active = None
activeRemoved = True
self._tvFolders.remove(t)
# Append new tv_folders
for n in new:
self._tvFolders.append(tv_folder.tv_folder(n,os.path.join(self._mount,n)))
# Check for changes within folders
for t in self._tvFolders:
u,e = t.update()
if e != None:
return e
if u == True:
updated = True
# Active changed?
if t == self._active:
updatedActive = True
# If updated, or next timer: check which to run
now = self._parse("now")[0]
dt = now - self._lastUpdate
dtmin = dt.days * 24 * 60 + dt.seconds/60
if updated == True or now >= self._nextTime or dtmin >= self._settings["maxTime"]:
self._lastUpdate = now
lastStart = self._parse("1970-01-01")[0]
active = None
nextTime = self._parse("2038-01-01")[0]
default = None
#Check all tv_folders
for t in self._tvFolders:
if t.settings["start"] == "": # Default
if default != None: # Multiple defaults
return "Missing start time in too many folders"
default = t
else: # Standard
tStart,tStop = self._parse(t.settings["start"],t.settings["stop"])
if tStop == None:
return "Could not parse time: " + t.name
# Latest started event (and active...)
if tStart <= now and tStart > lastStart and tStop >= now:
lastStart = tStart
active = t
if tStop < nextTime: # Happends next
nextTime = tStop
# Next event?
if tStart > now and tStart < nextTime:
nextTime = tStart
self._nextTime = nextTime
# If no active and default enabled: set default av active
if active == None and self._settings["default"] == True:
active = default
# Change folder if change in active
if self._active != active or activeRemoved == True:
# If new active exists: prepare new
if active != None:
tmpFolder = os.path.join(self._temporary,active.name)
err = active.prepare(tmpFolder)
if err != None:
return err
# If old active exists: stop old
if self._active != None:
self._active.stop()
# If new active exists: start new
if active != None:
active.start()
# If old active exists: cleanup
if self._active != None:
err = self._active.cleanup()
if err != None:
return err
# Check if old active == None and tv_on, tv_source is set. Then talk with tv.
if self._active == None and self._settings["noDefaultTvCtrl"] == True and activeRemoved == False:
# Start TV and set source
#Uglyfix start hdmi
os.system("/opt/vc/bin/tvservice -p")
time.sleep(self._cecSleep)
if self._settings["on"] == True:
os.system("echo \"on 0\" | /usr/bin/cec-client -s -d 1")
if self._settings["source"] == True:
time.sleep(self._cecSleep)
if self._settings["source"] == True:
os.system("echo \"as 0\" | /usr/bin/cec-client -s -d 1")
# Check if new active == None and tv_off is set. Then talk with tv.
if (active == None or activeRemoved == True) and self._settings["noDefaultTvCtrl"] == True:
if self._settings["off"] == True:
os.system("echo \"standby 0\" | /usr/bin/cec-client -s -d 1")
else:
# Clear screen
#time.sleep(self._clearSleep)
os.system("printf '\033[2J\033[1;1H' > /dev/tty1")
#Uglyfix stop hdmi
os.system("/opt/vc/bin/tvservice -o")
# Update active
self._active = active
# Active same as before, and changed
elif self._active != None and updatedActive == True:
# New and old folder names
tmpFolder = os.path.join(self._temporary,active.name + "_2")
oldTmpFolder = os.path.join(self._temporary,active.name)
if os.path.isdir(tmpFolder) == True: # Assume no trash folders
tmp = tmpFolder
tmpFolder = oldTmpFolder
oldTmpFolder = tmp
# Prepare new
err = self._active.prepare(tmpFolder)
if err != None:
return err
# Restart
self._active.stop()
self._active.start()
# Remove old teporary folder
err = self._active.cleanup(oldTmpFolder)
# Write to log
if not self._active == None:
self._logger.info("Active folder: " + self._active.name)
else:
self._logger.info("Active folder: None")
self._logger.info("Next event: " + str(nextTime))
return None
def _parse(self,startTime,stopTime=None):
if stopTime == None:
time_struct, status = self._pdtcal.parse(startTime)
if status == 0: # Could not parse
return None,None
time = datetime.fromtimestamp(mktime(time_struct))
return time,None
else:
now = self._parse("now")[0]
start = self._parse("this " + startTime)[0]
stop = self._parse("this " + stopTime)[0]
if start == None or stop == None: # Could not parse
return None,None
if stop > now:
if start > stop:
start = self._parse("last " + startTime)[0]
if start == None: # Could not parse
return None,None
#Handles special case with time only and pre midnight to pos midnight time
if start > stop:
hhmm = regexp.match("2[0-3]:[0-5][0-9]|[0-1][0-9]:[0-5][0-9]",startTime)
if not hhmm == None:
start = start - timedelta(days=1)
else:
stop = self._parse("next " + stopTime)[0]
if stop == None: # Could not parse
return None,None
#Handles special case with time only and
if stop < now: # Still
hhmm = regexp.match("2[0-3]:[0-5][0-9]|[0-1][0-9]:[0-5][0-9]",startTime)
if not hhmm == None:
stop = stop + timedelta(days=1)
if start < now:
start = self._parse("next " + startTime)[0]
if start == None: # Could not parse
return None,None
#Handles special case with time only
if start < now: # still
hhmm = regexp.match("2[0-3]:[0-5][0-9]|[0-1][0-9]:[0-5][0-9]",startTime)
if not hhmm == None:
start = start + timedelta(days=1)
return start,stop
def _readConf(self):
# Check if path and configfile exists
if os.path.exists(self._configfile) == False:
return "Cannot find the file " + self._configfile
inFile = open(self._configfile,'r')
lineIndex = 0;
for line in inFile:
lineIndex += 1
line = line.strip()
if line != "" and line[0] != '#':
split = line.split('=')
if len(split) != 2:
return "Missing parameter: " + self._configfile + ":" + str(lineIndex)
option = split[0].strip().lower()
value = split[1].strip().lower()
## Settings
if option == "onoff_tv_on":
if value == "0":
self._settings["on"] = False
elif option == "onoff_tv_source":
if value == "0":
self._settings["source"] = False
elif option == "onoff_tv_off":
if value == "0":
self._settings["off"] = False
elif option == "mount_type":
if value != "samba":
return None,"Unknown mount type: " + self._configfile + ":" + str(lineIndex)
self._settings["type"] = value
elif option == "mount_source":
self._settings["source"] = value
elif option == "mount_username":
self._settings["user"] = split[1].strip()
elif option == "mount_password":
self._settings["pass"] = split[1].strip()
elif option == "show_update_time":
try:
t = int(value)
except ValueError:
return "Invalid parameter: " + self._configfile + ":" + str(lineIndex)
self._settings["time"] = t
elif option == "show_max_update_time":
try:
t = int(value)
except ValueError:
return "Invalid parameter: " + self._configfile + ":" + str(lineIndex)
self._settings["maxTime"] = t
elif option == "show_default":
if value == "0":
self._settings["default"] = False
elif option == "onoff_at_noDefault":
if value == "1":
self._settings["noDefaultTvCtrl"] = True
inFile.close()
return None
if __name__ == '__main__':
daemon = tv_daemon()
daemon.init()
daemon.run()