Commit 515dc9ce authored by Gigadoc 2's avatar Gigadoc 2

stuff

parent 41e0dd17
#!/usr/bin/env python3
from .core import Eins
from .eins import Eins
#!/usr/bin/env python3
from .core import Eins
......@@ -77,19 +77,18 @@ class Eins:
"""
pass
def handle_cmd(self, cmd_name=None,
def simple_cmd(self, cmd_name=None,
args="none", descr=None, attribs={}):
"""Decorator to register an Eins command callback."""
def decorator(f):
self.register_command(f, cmd_name=cmd_name,
args=args, descr=descr, attribs=attribs)
self.register_simple_cmd(f, cmd_name=cmd_name,
args=args, descr=descr, attribs=attribs)
return f
return decorator
def register_command(self, callback, cmd_name=None,
args="none", descr=None, attribs={}):
def register_simple_cmd(self, callback, cmd_name=None,
args="none", descr=None, attribs={}):
print("Registering command {}".format(callback.__name__))
command = Command(callback = callback,
command = Command(callback = self._wrap_simple_cmd(callback),
cmd_name = cmd_name or callback.__name__,
args = args,
descr = descr,
......@@ -113,8 +112,7 @@ class Eins:
"""
topic = "edi/cmd/{}".format(command.cmd_name)
print("Register " + topic)
self._mqttc.message_callback_add(topic,
self._wrap_cmd_callback(command.callback))
self._mqttc.message_callback_add(topic, command.callback)
print("Registered!")
self._mqttc.subscribe(topic, self._sub_qos)
......@@ -148,7 +146,7 @@ class Eins:
print("Unsubscribed!") #XXX
#TODO: Check with the help of mid if we wanted that unsubscription
def _wrap_cmd_callback(self, callback):
def _wrap_simple_cmd(self, callback):
@wraps(callback)
def wrapper(client, userdata, message):
try:
......@@ -156,6 +154,8 @@ class Eins:
given_args = list(data.keys())
given_args.append("mqtt_topic")
#XXX: split args string, check amount of required args, check
# for too much args, go
if function_args_complete(callback, given_args):
reduce_args(callback)(mqtt_topic=message.topic, **data)
else:
......
......@@ -4,45 +4,45 @@ from eins import Eins
eins = Eins()
@eins.handle_cmd()
@eins.simple_cmd()
def test_sofort1():
print("Test sofort1 called!")
@eins.handle_cmd()
@eins.simple_cmd()
def test_sofort2():
print("Test sofort2 called!")
def on_connect():
@eins.handle_cmd
@eins.simple_cmd
def test_running():
print("Test running called!")
eins.on_connect = on_connect
@eins.handle_cmd("print")
@eins.simple_cmd("print")
def cmd_print(a):
print(a)
@eins.handle_cmd()
@eins.simple_cmd()
def multiarg(a, b="asdf"):
print("Multiarg called!")
@eins.handle_cmd()
@eins.simple_cmd()
def topic_arg(mqtt_topic):
print(mqtt_topic)
@eins.handle_cmd()
@eins.simple_cmd()
def kwargs(**kwargs):
print(kwargs)
@eins.handle_cmd()
@eins.simple_cmd()
def ètoile():
print("fancy")
@eins.handle_cmd()
@eins.simple_cmd()
def brøther():
print("LÂMP")
@eins.handle_cmd()
@eins.simple_cmd()
def reflector(**kwargs):
del kwargs['mqtt_topic']
eins.emit_cmd("reflector-target", **kwargs)
......
Markdown is supported
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