allow schedule entries with datetime
This commit is contained in:
parent
f1e5e9a765
commit
877f0e4c28
@ -10,21 +10,39 @@ import logging
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ScheduleEntry(object):
|
class ScheduleEntry(ABC):
|
||||||
def __init__(self, startTime, endTime, profile):
|
def __init__(self, startTime, endTime, profile):
|
||||||
self.startTime = startTime
|
self.startTime = startTime
|
||||||
self.endTime = endTime
|
self.endTime = endTime
|
||||||
self.profile = profile
|
self.profile = profile
|
||||||
|
|
||||||
def isCurrent(self, time):
|
def getProfile(self):
|
||||||
|
return self.profile
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "{0} - {1}: {2}".format(self.startTime, self.endTime, self.profile)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def isCurrent(self, dt):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def getScheduledEnd(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def getNextActivation(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TimeScheduleEntry(ScheduleEntry):
|
||||||
|
def isCurrent(self, dt):
|
||||||
|
time = dt.time()
|
||||||
if self.startTime < self.endTime:
|
if self.startTime < self.endTime:
|
||||||
return self.startTime <= time < self.endTime
|
return self.startTime <= time < self.endTime
|
||||||
else:
|
else:
|
||||||
return self.startTime <= time or time < self.endTime
|
return self.startTime <= time or time < self.endTime
|
||||||
|
|
||||||
def getProfile(self):
|
|
||||||
return self.profile
|
|
||||||
|
|
||||||
def getScheduledEnd(self):
|
def getScheduledEnd(self):
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
end = now.combine(date=now.date(), time=self.endTime)
|
end = now.combine(date=now.date(), time=self.endTime)
|
||||||
@ -40,6 +58,16 @@ class ScheduleEntry(object):
|
|||||||
return start
|
return start
|
||||||
|
|
||||||
|
|
||||||
|
class DatetimeScheduleEntry(ScheduleEntry):
|
||||||
|
def isCurrent(self, dt):
|
||||||
|
return self.startTime <= dt < self.endTime
|
||||||
|
|
||||||
|
def getScheduledEnd(self):
|
||||||
|
return self.endTime
|
||||||
|
|
||||||
|
def getNextActivation(self):
|
||||||
|
return self.startTime
|
||||||
|
|
||||||
class Schedule(ABC):
|
class Schedule(ABC):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse(props):
|
def parse(props):
|
||||||
@ -51,8 +79,8 @@ class Schedule(ABC):
|
|||||||
t = sc["type"] if "type" in sc else "static"
|
t = sc["type"] if "type" in sc else "static"
|
||||||
if t == "static":
|
if t == "static":
|
||||||
return StaticSchedule(sc["schedule"])
|
return StaticSchedule(sc["schedule"])
|
||||||
elif t == "sunlight":
|
elif t == "daylight":
|
||||||
return SunlightSchedule(sc["schedule"])
|
return DaylightSchedule(sc["schedule"])
|
||||||
else:
|
else:
|
||||||
logger.warning("Invalid scheduler type: %s", t)
|
logger.warning("Invalid scheduler type: %s", t)
|
||||||
|
|
||||||
@ -71,7 +99,7 @@ class TimerangeSchedule(Schedule, metaclass=ABCMeta):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def getCurrentEntry(self):
|
def getCurrentEntry(self):
|
||||||
current = [p for p in self.getEntries() if p.isCurrent(datetime.utcnow().time())]
|
current = [p for p in self.getEntries() if p.isCurrent(datetime.utcnow())]
|
||||||
if current:
|
if current:
|
||||||
return current[0]
|
return current[0]
|
||||||
return None
|
return None
|
||||||
@ -93,13 +121,13 @@ class StaticSchedule(TimerangeSchedule):
|
|||||||
|
|
||||||
startTime = datetime.strptime(time[0:4], "%H%M").replace(tzinfo=timezone.utc).time()
|
startTime = datetime.strptime(time[0:4], "%H%M").replace(tzinfo=timezone.utc).time()
|
||||||
endTime = datetime.strptime(time[5:9], "%H%M").replace(tzinfo=timezone.utc).time()
|
endTime = datetime.strptime(time[5:9], "%H%M").replace(tzinfo=timezone.utc).time()
|
||||||
self.entries.append(ScheduleEntry(startTime, endTime, profile))
|
self.entries.append(TimeScheduleEntry(startTime, endTime, profile))
|
||||||
|
|
||||||
def getEntries(self):
|
def getEntries(self):
|
||||||
return self.entries
|
return self.entries
|
||||||
|
|
||||||
|
|
||||||
class SunlightSchedule(TimerangeSchedule):
|
class DaylightSchedule(TimerangeSchedule):
|
||||||
def __init__(self, scheduleDict):
|
def __init__(self, scheduleDict):
|
||||||
self.schedule = scheduleDict
|
self.schedule = scheduleDict
|
||||||
|
|
||||||
@ -143,14 +171,14 @@ class SunlightSchedule(TimerangeSchedule):
|
|||||||
sunrise, sunset = self.getSunTimes(date)
|
sunrise, sunset = self.getSunTimes(date)
|
||||||
if sunset < now:
|
if sunset < now:
|
||||||
sunrise, sunset = self.getSunTimes(date + timedelta(days=1))
|
sunrise, sunset = self.getSunTimes(date + timedelta(days=1))
|
||||||
return ScheduleEntry(sunrise.time(), sunset.time(), profile)
|
return DatetimeScheduleEntry(sunrise, sunset, profile)
|
||||||
elif t == "night":
|
elif t == "night":
|
||||||
sunrise, _ = self.getSunTimes(date)
|
sunrise, _ = self.getSunTimes(date)
|
||||||
_, sunset = self.getSunTimes(date - timedelta(days=1))
|
_, sunset = self.getSunTimes(date - timedelta(days=1))
|
||||||
if sunrise < now:
|
if sunrise < now:
|
||||||
sunrise, _ = self.getSunTimes(date + timedelta(days=1))
|
sunrise, _ = self.getSunTimes(date + timedelta(days=1))
|
||||||
_, sunset = self.getSunTimes(date)
|
_, sunset = self.getSunTimes(date)
|
||||||
return ScheduleEntry(sunset.time(), sunrise.time(), profile)
|
return DatetimeScheduleEntry(sunset, sunrise, profile)
|
||||||
|
|
||||||
def getEntries(self):
|
def getEntries(self):
|
||||||
return [self.getEntry(t, profile) for t, profile in self.schedule.items()]
|
return [self.getEntry(t, profile) for t, profile in self.schedule.items()]
|
||||||
@ -215,11 +243,10 @@ class ServiceScheduler(object):
|
|||||||
self.scheduleSelection(nextEntry.getNextActivation())
|
self.scheduleSelection(nextEntry.getNextActivation())
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug("scheduling end for current profile: %s", entry.getScheduledEnd())
|
logger.debug("selected profile %s until %s", entry.getProfile(), entry.getScheduledEnd())
|
||||||
self.scheduleSelection(entry.getScheduledEnd())
|
self.scheduleSelection(entry.getScheduledEnd())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.debug("scheduler is activating profile %s", entry.getProfile())
|
|
||||||
self.source.activateProfile(entry.getProfile())
|
self.source.activateProfile(entry.getProfile())
|
||||||
self.source.start()
|
self.source.start()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
Loading…
Reference in New Issue
Block a user