added paramcheck too all functions
updated documentation updated codestyle
This commit is contained in:
107
ledd/daemon.py
107
ledd/daemon.py
@@ -24,7 +24,7 @@ import signal
|
|||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from jsonrpc import JSONRPCResponseManager, dispatcher
|
from jsonrpc import JSONRPCResponseManager, dispatcher
|
||||||
from jsonrpc.exceptions import JSONRPCError
|
from jsonrpc.exceptions import JSONRPCError, JSONRPCInvalidParams
|
||||||
import spectra
|
import spectra
|
||||||
from sqlalchemy.exc import OperationalError
|
from sqlalchemy.exc import OperationalError
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
@@ -40,7 +40,6 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
daemonSection = 'daemon'
|
daemonSection = 'daemon'
|
||||||
databaseSection = 'db'
|
databaseSection = 'db'
|
||||||
loop = None
|
|
||||||
""" :type : asyncio.BaseEventLoop """
|
""" :type : asyncio.BaseEventLoop """
|
||||||
effects = []
|
effects = []
|
||||||
|
|
||||||
@@ -54,7 +53,6 @@ def run():
|
|||||||
config.read_file(f)
|
config.read_file(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
log.info("No config file found!")
|
log.info("No config file found!")
|
||||||
pass
|
|
||||||
|
|
||||||
# SQL init
|
# SQL init
|
||||||
global engine
|
global engine
|
||||||
@@ -130,13 +128,12 @@ def init_db():
|
|||||||
def start_effect(**kwargs):
|
def start_effect(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to start a specific effect.
|
Part of the Color API. Used to start a specific effect.
|
||||||
Required JSON parameters: stripe IDs: sids; effect id: eid, effect options: eopt
|
Required parameters: stripe IDs: sids; effect id: eid, effect options: eopt
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
stripes = []
|
if "sids" not in kwargs or "eid" not in kwargs or "eopt" not in kwargs:
|
||||||
|
return JSONRPCInvalidParams()
|
||||||
|
|
||||||
if "sids" in kwargs:
|
|
||||||
for stripe in Stripe.query.filter(Stripe.id.in_(kwargs['sids'])):
|
for stripe in Stripe.query.filter(Stripe.id.in_(kwargs['sids'])):
|
||||||
# TODO: add anything required to start effect with req_json['eid']
|
# TODO: add anything required to start effect with req_json['eid']
|
||||||
# on stripes[] with options in req_json['eopt']
|
# on stripes[] with options in req_json['eopt']
|
||||||
@@ -153,7 +150,7 @@ def start_effect(**kwargs):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return rjson
|
return rjson
|
||||||
else:
|
|
||||||
return JSONRPCError(-1003, "Stripeid not found")
|
return JSONRPCError(-1003, "Stripeid not found")
|
||||||
|
|
||||||
|
|
||||||
@@ -161,8 +158,7 @@ def start_effect(**kwargs):
|
|||||||
def stop_effect(**kwargs):
|
def stop_effect(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to stop a specific effect.
|
Part of the Color API. Used to stop a specific effect.
|
||||||
Required JSON parameters: effect identifier: eident
|
Required parameters: effect identifier: eident
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: add stop effect by eident logic
|
# TODO: add stop effect by eident logic
|
||||||
@@ -172,8 +168,7 @@ def stop_effect(**kwargs):
|
|||||||
def get_effects(**kwargs):
|
def get_effects(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to show all available and running effects.
|
Part of the Color API. Used to show all available and running effects.
|
||||||
Required JSON parameters: -
|
Required parameters: -
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: list all effects here and on which stripes they run atm
|
# TODO: list all effects here and on which stripes they run atm
|
||||||
@@ -185,9 +180,12 @@ def get_effects(**kwargs):
|
|||||||
def set_color(**kwargs):
|
def set_color(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to set color of a stripe.
|
Part of the Color API. Used to set color of a stripe.
|
||||||
Required JSON parameters: stripe ID: sid; HSV values hsv: h,s,v, controller id: cid
|
Required parameters: stripe ID: sid; HSV values hsv: h,s,v, controller id: cid
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if "sid" not in kwargs or "hsv" not in kwargs:
|
||||||
|
return JSONRPCInvalidParams()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
|
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
|
||||||
stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
|
stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
|
||||||
@@ -199,10 +197,13 @@ def set_color(**kwargs):
|
|||||||
def add_controller(**kwargs):
|
def add_controller(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to add a controller.
|
Part of the Color API. Used to add a controller.
|
||||||
Required JSON parameters: channels; i2c_dev: number of i2c device (e.g. /dev/i2c-1 would be i2c_dev = 1);
|
Required parameters: channels; i2c_dev: number of i2c device (e.g. /dev/i2c-1 would be i2c_dev = 1);
|
||||||
address: hexdecimal address of controller on i2c bus, e.g. 0x40
|
address: hexdecimal address of controller on i2c bus, e.g. 0x40
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if "i2c_dev" not in kwargs or "channels" not in kwargs or "address" not in kwargs:
|
||||||
|
return JSONRPCInvalidParams()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ncontroller = Controller(channels=int(kwargs['channels']), i2c_device=int(kwargs['i2c_dev']),
|
ncontroller = Controller(channels=int(kwargs['channels']), i2c_device=int(kwargs['i2c_dev']),
|
||||||
address=kwargs['address'])
|
address=kwargs['address'])
|
||||||
@@ -213,67 +214,57 @@ def add_controller(**kwargs):
|
|||||||
session.add(ncontroller)
|
session.add(ncontroller)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
rjson = {
|
return {'cid': ncontroller.id}
|
||||||
'cid': ncontroller.id,
|
|
||||||
}
|
|
||||||
|
|
||||||
return rjson
|
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
def get_color(**kwargs):
|
def get_color(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to get the current color of an stripe.
|
Part of the Color API. Used to get the current color of an stripe.
|
||||||
Required JSON parameters: stripes
|
Required parameters: sid
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if "sid" not in kwargs:
|
||||||
|
return JSONRPCInvalidParams()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
|
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
||||||
return JSONRPCError(-1003, "Stripeid not found")
|
return JSONRPCError(-1003, "Stripeid not found")
|
||||||
|
|
||||||
rjson = {
|
return {'color': stripe.color.values}
|
||||||
'color': stripe.color.values,
|
|
||||||
}
|
|
||||||
|
|
||||||
return rjson
|
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
def add_stripe(**kwargs):
|
def add_stripe(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to add stripes.
|
Part of the Color API. Used to add stripes.
|
||||||
Required JSON parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel, cid
|
Required parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel, cid
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if "stripe" in kwargs:
|
if "name" not in kwargs or "rgb" not in kwargs or "map" not in kwargs or "cid" not in kwargs:
|
||||||
stripe = kwargs['stripe']
|
return JSONRPCInvalidParams()
|
||||||
c = Controller.query.filter(Controller.id == int(stripe['cid'])).first()
|
|
||||||
|
c = Controller.query.filter(Controller.id == int(kwargs['cid'])).first()
|
||||||
""" :type c: ledd.controller.Controller """
|
""" :type c: ledd.controller.Controller """
|
||||||
|
|
||||||
if c is None:
|
if c is None:
|
||||||
return JSONRPCError(-1002, "Controller not found")
|
return JSONRPCError(-1002, "Controller not found")
|
||||||
|
|
||||||
s = Stripe(name=stripe['name'], rgb=bool(stripe['rgb']),
|
s = Stripe(name=kwargs['name'], rgb=bool(kwargs['rgb']),
|
||||||
channel_r=stripe['map']['r'], channel_g=stripe['map']['g'], channel_b=stripe['map']['b'])
|
channel_r=kwargs['map']['r'], channel_g=kwargs['map']['g'], channel_b=kwargs['map']['b'])
|
||||||
s.controller = c
|
s.controller = c
|
||||||
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
|
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
|
||||||
|
|
||||||
rjson = {
|
return {'sid': s.id}
|
||||||
'sid': s.id,
|
|
||||||
}
|
|
||||||
|
|
||||||
return rjson
|
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
def get_stripes(**kwargs):
|
def get_stripes(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to get all registered stripes known to the daemon.
|
Part of the Color API. Used to get all registered stripes known to the daemon.
|
||||||
Required JSON parameters: none
|
Required parameters: -
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
rjson = {
|
rjson = {
|
||||||
@@ -288,43 +279,29 @@ def get_stripes(**kwargs):
|
|||||||
def test_channel(**kwargs):
|
def test_channel(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used to test a channel on a specified controller.
|
Part of the Color API. Used to test a channel on a specified controller.
|
||||||
Required JSON parameters: controller id: cid, channel, value
|
Required parameters: controller id: cid, channel, value
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if "cid" not in kwargs or "channel" not in kwargs or "value" not in kwargs:
|
||||||
|
return JSONRPCInvalidParams()
|
||||||
|
|
||||||
result = Controller.query.filter(Controller.id == kwargs['cid']).first()
|
result = Controller.query.filter(Controller.id == kwargs['cid']).first()
|
||||||
""" :type : ledd.controller.Controller """
|
""" :type : ledd.controller.Controller """
|
||||||
|
|
||||||
if result is not None:
|
if result is not None:
|
||||||
result.set_channel(kwargs['channel'], kwargs['value'], 2.8)
|
result.set_channel(kwargs['channel'], kwargs['value'], 2.8)
|
||||||
|
else:
|
||||||
|
return JSONRPCError(-1002, "Controller not found")
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
def discover(**kwargs):
|
def discover(**kwargs):
|
||||||
"""
|
"""
|
||||||
Part of the Color API. Used by mobile applications to find the controller.
|
Part of the Color API. Used by mobile applications to find the controller.
|
||||||
Required JSON parameters: none
|
Required parameters: -
|
||||||
:param req_json: dict of request json
|
|
||||||
"""
|
|
||||||
log.debug("recieved action: %s", kwargs['action'])
|
|
||||||
|
|
||||||
rjson = {
|
|
||||||
'version': VERSION
|
|
||||||
}
|
|
||||||
|
|
||||||
return rjson
|
|
||||||
|
|
||||||
|
|
||||||
def find_stripe(sid):
|
|
||||||
"""
|
|
||||||
Deprecated. Use a query instead. Or this should be moved to a classmethod in Stripe
|
|
||||||
Finds a given stripeid in the currently known controllers
|
|
||||||
:param sid stripe id
|
|
||||||
:return: stripe if found or none
|
|
||||||
:rtype: ledd.Stripe | None
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return Stripe.query.filter(Stripe.id == sid).first()
|
return {'version': VERSION}
|
||||||
|
|
||||||
|
|
||||||
class LedDProtocol(asyncio.Protocol):
|
class LedDProtocol(asyncio.Protocol):
|
||||||
@@ -352,6 +329,4 @@ class LedDProtocol(asyncio.Protocol):
|
|||||||
self.transport.write(JSONRPCResponseManager.handle(line, dispatcher).json.encode())
|
self.transport.write(JSONRPCResponseManager.handle(line, dispatcher).json.encode())
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
# The socket has been closed, stop the event loop
|
|
||||||
# Daemon.loop.stop()
|
|
||||||
log.info("Lost connection to %s", self.transport.get_extra_info("peername"))
|
log.info("Lost connection to %s", self.transport.get_extra_info("peername"))
|
||||||
|
Reference in New Issue
Block a user