added paramcheck too all functions

updated documentation
updated codestyle
This commit is contained in:
Giovanni Harting
2015-10-11 17:04:54 +02:00
parent d5f403d557
commit 2bb52aa3a4

View File

@@ -24,7 +24,7 @@ import signal
from sqlalchemy import create_engine
from jsonrpc import JSONRPCResponseManager, dispatcher
from jsonrpc.exceptions import JSONRPCError
from jsonrpc.exceptions import JSONRPCError, JSONRPCInvalidParams
import spectra
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm.exc import NoResultFound
@@ -40,7 +40,6 @@ log = logging.getLogger(__name__)
daemonSection = 'daemon'
databaseSection = 'db'
loop = None
""" :type : asyncio.BaseEventLoop """
effects = []
@@ -54,7 +53,6 @@ def run():
config.read_file(f)
except FileNotFoundError:
log.info("No config file found!")
pass
# SQL init
global engine
@@ -130,13 +128,12 @@ def init_db():
def start_effect(**kwargs):
"""
Part of the Color API. Used to start a specific effect.
Required JSON parameters: stripe IDs: sids; effect id: eid, effect options: eopt
:param req_json: dict of request json
Required parameters: stripe IDs: sids; effect id: eid, effect options: eopt
"""
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'])):
# TODO: add anything required to start effect with req_json['eid']
# on stripes[] with options in req_json['eopt']
@@ -153,7 +150,7 @@ def start_effect(**kwargs):
}
return rjson
else:
return JSONRPCError(-1003, "Stripeid not found")
@@ -161,8 +158,7 @@ def start_effect(**kwargs):
def stop_effect(**kwargs):
"""
Part of the Color API. Used to stop a specific effect.
Required JSON parameters: effect identifier: eident
:param req_json: dict of request json
Required parameters: effect identifier: eident
"""
# TODO: add stop effect by eident logic
@@ -172,8 +168,7 @@ def stop_effect(**kwargs):
def get_effects(**kwargs):
"""
Part of the Color API. Used to show all available and running effects.
Required JSON parameters: -
:param req_json: dict of request json
Required parameters: -
"""
# TODO: list all effects here and on which stripes they run atm
@@ -185,9 +180,12 @@ def get_effects(**kwargs):
def set_color(**kwargs):
"""
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
:param req_json: dict of request json
Required parameters: stripe ID: sid; HSV values hsv: h,s,v, controller id: cid
"""
if "sid" not in kwargs or "hsv" not in kwargs:
return JSONRPCInvalidParams()
try:
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
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):
"""
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
: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:
ncontroller = Controller(channels=int(kwargs['channels']), i2c_device=int(kwargs['i2c_dev']),
address=kwargs['address'])
@@ -213,67 +214,57 @@ def add_controller(**kwargs):
session.add(ncontroller)
session.commit()
rjson = {
'cid': ncontroller.id,
}
return rjson
return {'cid': ncontroller.id}
@dispatcher.add_method
def get_color(**kwargs):
"""
Part of the Color API. Used to get the current color of an stripe.
Required JSON parameters: stripes
:param req_json: dict of request json
Required parameters: sid
"""
if "sid" not in kwargs:
return JSONRPCInvalidParams()
try:
stripe = Stripe.query.filter(Stripe.id == kwargs['sid']).one()
except NoResultFound:
log.warning("Stripe not found: id=%s", kwargs['sid'])
return JSONRPCError(-1003, "Stripeid not found")
rjson = {
'color': stripe.color.values,
}
return rjson
return {'color': stripe.color.values}
@dispatcher.add_method
def add_stripe(**kwargs):
"""
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
:param req_json: dict of request json
Required parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel, cid
"""
if "stripe" in kwargs:
stripe = kwargs['stripe']
c = Controller.query.filter(Controller.id == int(stripe['cid'])).first()
if "name" not in kwargs or "rgb" not in kwargs or "map" not in kwargs or "cid" not in kwargs:
return JSONRPCInvalidParams()
c = Controller.query.filter(Controller.id == int(kwargs['cid'])).first()
""" :type c: ledd.controller.Controller """
if c is None:
return JSONRPCError(-1002, "Controller not found")
s = Stripe(name=stripe['name'], rgb=bool(stripe['rgb']),
channel_r=stripe['map']['r'], channel_g=stripe['map']['g'], channel_b=stripe['map']['b'])
s = Stripe(name=kwargs['name'], rgb=bool(kwargs['rgb']),
channel_r=kwargs['map']['r'], channel_g=kwargs['map']['g'], channel_b=kwargs['map']['b'])
s.controller = c
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
rjson = {
'sid': s.id,
}
return rjson
return {'sid': s.id}
@dispatcher.add_method
def get_stripes(**kwargs):
"""
Part of the Color API. Used to get all registered stripes known to the daemon.
Required JSON parameters: none
:param req_json: dict of request json
Required parameters: -
"""
rjson = {
@@ -288,43 +279,29 @@ def get_stripes(**kwargs):
def test_channel(**kwargs):
"""
Part of the Color API. Used to test a channel on a specified controller.
Required JSON parameters: controller id: cid, channel, value
:param req_json: dict of request json
Required parameters: controller id: cid, channel, value
"""
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()
""" :type : ledd.controller.Controller """
if result is not None:
result.set_channel(kwargs['channel'], kwargs['value'], 2.8)
else:
return JSONRPCError(-1002, "Controller not found")
@dispatcher.add_method
def discover(**kwargs):
"""
Part of the Color API. Used by mobile applications to find the controller.
Required JSON parameters: none
: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
Required parameters: -
"""
return Stripe.query.filter(Stripe.id == sid).first()
return {'version': VERSION}
class LedDProtocol(asyncio.Protocol):
@@ -352,6 +329,4 @@ class LedDProtocol(asyncio.Protocol):
self.transport.write(JSONRPCResponseManager.handle(line, dispatcher).json.encode())
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"))