Skip to content

Commit

Permalink
Performance improvements and a couple of extra plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed May 28, 2011
1 parent 68ae5e7 commit b2a8ab2
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
MANIFEST
build
dist
#*#
*~
5 changes: 4 additions & 1 deletion mhub/controllers.py
Expand Up @@ -159,8 +159,11 @@ def start(self):

self.on_init()

cfg_general = self.cfg.get("general", dict())
mq_poll_interval = float(cfg_general.get("poll_interval", 0.1))

mq_task = task.LoopingCall(self.poll_message)
mq_task.start(0.01)
mq_task.start(mq_poll_interval)

for plugin_name, plugin_inst in self.plugins.iteritems():
if not hasattr(plugin_inst, "tasks"): continue
Expand Down
24 changes: 17 additions & 7 deletions mhub/plugins/byebyestandby.py
Expand Up @@ -6,7 +6,7 @@

class Plugin(object):

""" Logic Processor Plugin """
""" ByeByeStandby online controller plugin """

def __init__(self, cfg, producer, logger):

Expand All @@ -19,7 +19,7 @@ def __init__(self, cfg, producer, logger):
self.producer = producer
self.logger = logger
self.tasks = list()


def on_init(self):

Expand All @@ -34,21 +34,31 @@ def on_message(self, data, message):

action, params = data.get("action"), data.get("params")

if action == "%s.action" % (self.name):

if action == "%s.switch" % (self.name):
device, state = params.get("device", None), params.get("state", None)

device = device.upper()
state_desc = "ON" if state else "OFF"

if device is not None and state is not None:
self.logger.info("ByeByeStandby Trigger: %s %s" % (device, state_desc))
self.switch(device, state)

if action == "%s.scene" % (self.name):
scenes = self.cfg.get("scenes", dict())
name = params.get("name")
if name in scenes:
self.logger.debug("Scene '%s' running" % (name))
scene = scenes.get(name)
for action in scene:
device, state = action.get("device"), action.get("state")
self.switch(device, state)


def switch(self, device, state):

""" BBS device switcher helper """

state = 1 if state else 0
h, u = device[0], device[1:]
cmd = "D:%i%s%02d:E" % (int(state), h, int(u))
cmd = "D:%i%s%02d:E" % (int(state), h.upper(), int(u))
self.socket.sendto(cmd, (self.cfg.get("host"), self.cfg.get("port")))
time.sleep(1)
10 changes: 8 additions & 2 deletions mhub/plugins/email.py
Expand Up @@ -9,7 +9,7 @@

class Plugin(object):

""" Email Sending Plugin """
""" Email sending plugin """

def __init__(self, cfg, producer, logger):

Expand All @@ -24,9 +24,13 @@ def __init__(self, cfg, producer, logger):
self.tasks = list()


def on_init(self):

""" On initialisation handler """

def on_message(self, data, message):

""" On AMQP message handler """
""" AMQP on-message handler """

action, params = data.get("action"), data.get("params")

Expand All @@ -43,6 +47,8 @@ def on_message(self, data, message):

def send_email(self, recipients, subject, body, attachments=None):

""" Send email helper """

self.logger.debug("To: %s" % (", ".join(recipients)))
self.logger.debug("Subject: %s" % (subject))

Expand Down
62 changes: 62 additions & 0 deletions mhub/plugins/events.py
@@ -0,0 +1,62 @@
import datetime
import fnmatch

class Plugin(object):

""" Events plugin """

def __init__(self, cfg, producer, logger):

""" Constructor """

self.name = "events"
self.description = "Event management plugin"
self.author = "MHub"
self.cfg = cfg
self.producer = producer
self.logger = logger
self.tasks = list()


def on_message(self, data, message):

""" On AMQP message handler """

action, params = data.get("action"), data.get("params")

self.process_events(action, params)


def on_init(self):

""" On Init """

self.events = self.cfg.get("events", list())


def process_events(self, action, params):

""" Event processor helper """

for name, event in self.events.iteritems():

triggers = event.get("triggers", list())
actions = event.get("actions", list())

match = False

for trigger in triggers:

if fnmatch.fnmatch(action, trigger):
match = True
break

if match:

for action in actions:

self.producer.publish({
"action": action.get("action"),
"params": action.get("params", dict())
})

4 changes: 2 additions & 2 deletions mhub/plugins/lirc.py
Expand Up @@ -21,7 +21,7 @@ def on_init(self):

""" Main plugin initialisation """

self.tasks = [(0.10, self.process_events)]
self.tasks = [(0.1, self.process_events)]
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.setblocking(False)
try:
Expand Down Expand Up @@ -49,4 +49,4 @@ def process_events(self):
"command": parts[2],
"remote": parts[3]
}
})
})
4 changes: 2 additions & 2 deletions mhub/plugins/logic_processor.py
Expand Up @@ -15,7 +15,7 @@ def __init__(self, cfg, producer, logger):
self.cfg = cfg
self.producer = producer
self.logger = logger
self.tasks = [(0.01, self.on_tick)]
self.tasks = [(0.1, self.on_tick)]

self.scripts = dict()
self.env = dict()
Expand Down Expand Up @@ -119,4 +119,4 @@ def setup_scripts(self):
self.scripts[trigger] = list()
self.scripts[trigger].append(contents)
else:
self.logger.debug("Script not found")
self.logger.debug("Script not found")
56 changes: 56 additions & 0 deletions mhub/plugins/notify.py
@@ -0,0 +1,56 @@
import datetime
import fnmatch

class Plugin(object):

""" Notify Plugin """

def __init__(self, cfg, producer, logger):

""" Constructor """

self.name = "notify"
self.description = "Notifications (libnotify)"
self.author = "MHub"
self.cfg = cfg
self.producer = producer
self.logger = logger
self.tasks = list()


def on_message(self, data, message):

""" On AMQP message handler """

action, params = data.get("action"), data.get("params")

if self.matches_patterns(action):

title = params.get("title", "")
message = params.get("message", "")

try:
import pynotify
if pynotify.init("MHub"):
n = pynotify.Notification(title, message)
n.show()
except:
pass


def on_init(self):

""" On Init """

self.patterns = self.cfg.get("patterns", list("*"))


def matches_patterns(self, action):

match = False

for pattern in self.patterns:
match = fnmatch.fnmatch(action, pattern)
if match: break

return match
2 changes: 2 additions & 0 deletions mhub/plugins/rss.py
Expand Up @@ -28,6 +28,8 @@ def on_init(self):

def get_feeds(self):

""" RSS feed parser helper """

feeds = self.cfg.get("feeds")

for url in feeds:
Expand Down
28 changes: 26 additions & 2 deletions mhub/utils.py
Expand Up @@ -96,7 +96,8 @@ def configurator(filename=None):

cfg = {
"general": {
"plugins_path": os.path.join(base_dir, "plugins")
"plugins_path": os.path.join(base_dir, "plugins"),
"poll_interval": 0.1
},
"amqp": {
"host": "localhost",
Expand All @@ -108,7 +109,12 @@ def configurator(filename=None):
"byebyestandby": {
"enabled": False,
"host": "192.168.1.100",
"port": 53008
"port": 53008,
"scenes": {
"test": [
{"device": "a1", "state": False}
]
}
},
"echo": {
"enabled": True
Expand Down Expand Up @@ -159,6 +165,24 @@ def configurator(filename=None):
}
}
},
"notify": {
"enabled": False,
"patterns": [
"twitter.*",
"*.action"
]
},
"events": {
"enabled": False,
"events": {
"test": {
"triggers": ["some.trigger"],
"actions": [
{"action": "some.action", "params": {}}
]
}
}
},
"websocket": {
"enabled": False
}
Expand Down

0 comments on commit b2a8ab2

Please sign in to comment.