Commit ff446b05 authored by David Trattnig's avatar David Trattnig
Browse files

Threaded event handling. #30

parent 85a67440
......@@ -20,6 +20,8 @@
import logging
import datetime
from threading import Thread
from modules.base.utils import SimpleUtil as SU
from modules.base.exceptions import NoActiveEntryException
from modules.base.mail import AuraMailer
......@@ -175,78 +177,110 @@ class EngineEventDispatcher():
Args:
source (String): The `Entry` object *or* the URI of the media source currently playing
"""
self.logger.debug("on_play(..)")
entry = None
def func(self, source):
self.logger.debug("on_play(..)")
entry = None
if isinstance(source, str):
try:
self.logger.info(SU.pink("Source '%s' started playing. Resolving ..." % source))
entry = self.player_state.resolve_entry(source)
except NoActiveEntryException:
self.logger.error("Cannot resolve '%s'" % source)
else:
entry = source
if isinstance(source, str):
try:
self.logger.info(SU.pink("Source '%s' started playing. Resolving ..." % source))
entry = self.player_state.resolve_entry(source)
except NoActiveEntryException:
self.logger.error("Cannot resolve '%s'" % source)
else:
entry = source
# Assign timestamp for play time
entry.entry_start_actual = datetime.datetime.now()
self.call_event("on_play", entry)
# Assign timestamp for play time
entry.entry_start_actual = datetime.datetime.now()
self.call_event("on_play", entry)
thread = Thread(target = func, args = (self, source))
thread.start()
def on_stop(self, entry):
"""
The entry on the assigned channel has been stopped playing.
"""
self.logger.debug("on_stop(..)")
self.call_event("on_stop", entry)
def func(self, entry):
self.logger.debug("on_stop(..)")
self.call_event("on_stop", entry)
thread = Thread(target = func, args = (self, entry))
thread.start()
def on_idle(self):
"""
Callend when no entry is playing
"""
self.logger.debug("on_idle(..)")
self.logger.error(SU.red("Currently there's nothing playing!"))
self.call_event("on_idle", None)
def func(self):
self.logger.debug("on_idle(..)")
self.logger.error(SU.red("Currently there's nothing playing!"))
self.call_event("on_idle", None)
thread = Thread(target = func, args = (self, ))
thread.start()
def on_schedule_change(self, schedule):
"""
Called when the playlist or entries of the current schedule have changed.
"""
self.logger.debug("on_schedule_change(..)")
self.call_event("on_schedule_change", schedule)
def func(self, schedule):
self.logger.debug("on_schedule_change(..)")
self.call_event("on_schedule_change", schedule)
thread = Thread(target = func, args = (self, schedule))
thread.start()
def on_queue(self, entries):
"""
One or more entries have been queued and are currently pre-loaded.
"""
self.logger.debug("on_queue(..)")
self.player_state.add_to_history(entries)
self.call_event("on_queue", entries)
def func(self, entries):
self.logger.debug("on_queue(..)")
self.player_state.add_to_history(entries)
self.call_event("on_queue", entries)
thread = Thread(target = func, args = (self, entries))
thread.start()
def on_sick(self, data):
"""
Called when the engine is in some unhealthy state.
"""
self.logger.debug("on_sick(..)")
self.call_event("on_sick", data)
def func(self, data):
self.logger.debug("on_sick(..)")
self.call_event("on_sick", data)
thread = Thread(target = func, args = (self, data))
thread.start()
def on_resurrect(self, data):
"""
Called when the engine turned healthy again after being sick.
"""
self.logger.debug("on_resurrect(..)")
self.call_event("on_resurrect", data)
def func(self, data):
self.logger.debug("on_resurrect(..)")
self.call_event("on_resurrect", data)
thread = Thread(target = func, args = (self, data))
thread.start()
def on_critical(self, subject, message, data=None):
"""
Callend when some critical event occurs
"""
self.logger.debug("on_critical(..)")
if not data: data = ""
self.mailer.send_admin_mail(subject, message + "\n\n" + str(data))
self.call_event("on_critical", (subject, message, data))
def func(self, subject, message, data):
self.logger.debug("on_critical(..)")
if not data: data = ""
self.mailer.send_admin_mail(subject, message + "\n\n" + str(data))
self.call_event("on_critical", (subject, message, data))
thread = Thread(target = func, args = (self, subject, message, data))
thread.start()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment