Code: Alles auswählen
#!/usr/bin/python
import threading
import time
from datetime import datetime as DateTime
from datetime import date as Date
MONTAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
DIENSTAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('14:00:00', '17:48:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
MITTWOCH = [
('Prog 1', ('10:00:00', '11:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
DONNERSTAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
FREITAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
SAMSTAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
SONNTAG = [
('Prog 1', ('00:00:00', '00:00:00')),
('Prog 2', ('00:00:00', '00:00:00')),
('Prog 3', ('00:00:00', '00:00:00')),
('Prog 4', ('00:00:00', '00:00:00')),
('Prog 5', ('00:00:00', '00:00:00')),
('Prog 6', ('00:00:00', '00:00:00')),
('Prog 7', ('00:00:00', '00:00:00')),
('Prog 8', ('00:00:00', '00:00:00')),
('Prog 9', ('00:00:00', '00:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
WOCHE = [
('Prog 1', ('07:00:00', '09:00:00')),
('Prog 2', ('10:00:00', '10:15:00')),
('Prog 3', ('11:00:00', '11:15:00')),
('Prog 4', ('12:30:00', '14:00:00')),
('Prog 5', ('15:00:00', '15:15:00')),
('Prog 6', ('16:00:00', '16:15:00')),
('Prog 7', ('17:00:00', '17:15:00')),
('Prog 8', ('18:00:00', '20:00:00')),
('Prog 9', ('21:30:00', '23:00:00')),
('Prog 10', ('00:00:00', '00:00:00'))
]
#
#--------------------------------------------------------------------------------
#
def parse_time(time):
return DateTime.strptime(time, "%H:%M:%S").time()
def get_dow_n():
return Date.today().weekday()
def get_dow_a():
return ("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")[get_dow_n()]
#
#--------------------------------------------------------------------------------
#
class Zeitschaltuhr(object):
def __init__(self, ProgType):
self.dow = get_dow_n()
self.programm = []
self.progs = []
self.progs_states = {}
if ProgType == "Woche":
self.programm = WOCHE
self._initialize()
self._update()
self._zsu_thread = threading.Thread(target=self._run_uhr_woche)
if ProgType == "Tag":
self._get_tages_programm()
self._initialize()
self._update()
self._zsu_thread = threading.Thread(target=self._run_uhr_tag)
self._zsu_thread.daemon = True # Don't let this thread block exiting.
self._zsu_thread.start()
def _initialize(self):
self.progs = [
(prog, parse_time(start), parse_time(end))
for prog, (start, end) in self.programm
]
def _get_tages_programm(self):
if self.dow == 0:
self.programm = MONTAG
elif self.dow == 1:
self.programm = DIENSTAG
elif self.dow == 2:
self.programm = MITTWOCH
elif self.dow == 3:
self.programm = DONNERSTAG
elif self.dow == 4:
self.programm = FREITAG
elif self.dow == 5:
self.programm = SAMSTAG
elif self.dow == 6:
self.programm = SONNTAG
else:
print("falscher Wochentag")
def _update(self):
states = {}
now = DateTime.now().time()
for prog, start, end in self.progs:
#print(prog)
states[prog] = start <= now < end
#print(states[prog])
self.progs_states = states
#print(self.progs_states)
def get_zsu_dow_n(self):
return self.dow
def get_zsu_dow_a(self):
return ("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")[self.dow]
def _run_uhr_tag(self):
while True:
while self.dow == get_dow_n():
self._update()
time.sleep(1)
self.dow = get_dow_n()
self._get_tages_programm()
self._initialize()
self._update()
def _run_uhr_woche(self):
while True:
self._update()
time.sleep(1)
#
#--------------------------------------------------------------------------------
#
Die Wochen- bzw Tages-zeitschaltuhr läuft jetzt. Wird Sie mit Woche intitialisiert läuft jeden Tag das Wochenprogramm. Wir sie mit Tag initialisiert läuft jeden Wochentag das entsprechende Tageszeitprogramm.