first implementation of protobuf

restructure in client server model
loads of work still missing
This commit is contained in:
2017-01-31 15:00:44 +01:00
parent 7d6d289178
commit d79dde527f
29 changed files with 1611 additions and 400 deletions

View File

@@ -20,21 +20,17 @@ import errno
import logging
import os
import signal
import sqlite3
import sys
import spectra
from jsonrpc import JSONRPCResponseManager, dispatcher
from jsonrpc.exceptions import JSONRPCError, JSONRPCInvalidParams
from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm.exc import NoResultFound
from ledd import VERSION
from ledd.controller import Controller
from ledd.effectstack import EffectStack
from ledd.models import Meta
from ledd.stripe import Stripe
from . import Base, session
from ledd.db_helper import check_db
from ledd.effect.effectstack import EffectStack
from ledd.led.rgb_stripe import RGBStripe
from ledd.protobuf.ledd_pb2 import WrapperMsg
log = logging.getLogger(__name__)
@@ -42,8 +38,11 @@ daemonSection = 'daemon'
databaseSection = 'db'
""" :type : asyncio.BaseEventLoop """
effects = []
stripes = []
controller = []
leds = []
clients = []
conn = None
loop = None
server = None
def run():
@@ -57,23 +56,13 @@ def run():
log.info("No config file found!")
# SQL init
global engine
engine = create_engine("sqlite:///" + config.get(databaseSection, 'name', fallback='ledd.sqlite'),
echo=log.getEffectiveLevel() == logging.DEBUG)
session.configure(bind=engine)
Base.metadata.bind = engine
if not check_db():
init_db()
global conn
conn = sqlite3.connect(config.get(databaseSection, 'name', fallback='ledd.sqlite'))
conn.row_factory = sqlite3.Row
check_db(conn)
logging.getLogger("asyncio").setLevel(log.getEffectiveLevel())
# Load to cache
global controller, stripes
controller = Controller.query.all()
for c in controller:
stripes.extend(c.stripes)
# sigterm handler
def sigterm_handler():
raise SystemExit
@@ -83,6 +72,9 @@ def run():
# init plugins
# TODO: check all plugins for existing hooks
# init clients
# main loop
global loop, server
loop = asyncio.get_event_loop()
@@ -95,15 +87,15 @@ def run():
except (KeyboardInterrupt, SystemExit):
log.info("Exiting")
for c in controller:
for c in clients:
c.close()
try:
os.remove("ledd.pid")
except FileNotFoundError:
pass
session.commit()
session.close()
conn.commit()
conn.close()
if server is not None:
server.close()
if loop is not None:
@@ -112,32 +104,6 @@ def run():
sys.exit(0)
def check_db():
"""
Checks database version
:return: database validity
:rtype: bool
"""
try:
db_version = Meta.get_version()
if db_version is not None:
log.info("DB connection established; db_version=%s", db_version)
return True
except OperationalError:
return False
return False
def init_db():
Base.metadata.drop_all()
Base.metadata.create_all()
session.add(Meta(option="db_version", value="2"))
session.commit()
check_db()
@dispatcher.add_method
def start_effect(**kwargs):
"""
Part of the Color API. Used to start a specific effect.
@@ -145,10 +111,7 @@ def start_effect(**kwargs):
:param kwargs:
"""
if "sids" not in kwargs or "eid" not in kwargs or "eopt" not in kwargs:
return JSONRPCInvalidParams()
for stripe in Stripe.query.filter(Stripe.id.in_(kwargs['sids'])):
for stripe in RGBStripe.query.filter(RGBStripe.id.in_(kwargs['sids'])):
# TODO: add anything required to start effect with req_json['eid']
# on stripes[] with options in req_json['eopt']
effect = EffectStack()
@@ -160,15 +123,14 @@ def start_effect(**kwargs):
rjson = {
'eident': None, # unique effect identifier that identifies excatly this effect started on this set of
# stripes, used to stop them later and to give informations about running effects
# stripes, used to stop them later and to give informations about running effect
}
return rjson
return JSONRPCError(-1003, "Stripeid not found")
# return JSONRPCError(-1003, "Stripeid not found")
@dispatcher.add_method
def stop_effect(**kwargs):
"""
Part of the Color API. Used to stop a specific effect.
@@ -178,27 +140,25 @@ def stop_effect(**kwargs):
# TODO: add stop effect by eident logic
@dispatcher.add_method
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 effect.
Required parameters: -
"""
# TODO: list all effects here and on which stripes they run atm
# TODO: all effects get runtime only ids, "eid"'s. They are shown here for the client to start effects.
# TODO: list all effect here and on which stripes they run atm
# TODO: all effect get runtime only ids, "eid"'s. They are shown here for the client to start effect.
# TODO: All options that an effect may have need to be transmitted here too with "eopt".
@dispatcher.add_method
def set_color(**kwargs):
"""
Part of the Color API. Used to set color of a stripe.
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()
# if "sid" not in kwargs or "hsv" not in kwargs:
# return JSONRPCInvalidParams()
stripe = get_stripe(kwargs['sid'])
@@ -213,20 +173,17 @@ def set_color(**kwargs):
raise
else:
log.warning("Stripe not found: id=%s", kwargs['sid'])
return JSONRPCError(-1003, "Stripeid not found")
return ""
# return JSONRPCError(-1003, "Stripeid not found")
@dispatcher.add_method
def set_color_all(**kwargs):
"""
Part of the Color API. Used to set brightness of all stripes a controller owns.
Required parameters: controller id: cid, value: v
"""
if "cid" not in kwargs or "v" not in kwargs:
return JSONRPCInvalidParams()
# if "cid" not in kwargs or "v" not in kwargs:
# return JSONRPCInvalidParams()
try:
c = get_controller(kwargs['cid'])
@@ -235,113 +192,109 @@ def set_color_all(**kwargs):
c.set_all_channel(kwargs['v'])
except NoResultFound:
log.warning("Controller not found: id=%s", kwargs['cid'])
return JSONRPCError(-1002, "Controller not found")
# return JSONRPCError(-1002, "Controller not found")
return ""
@dispatcher.add_method
def add_controller(**kwargs):
def add_client(**kwargs):
"""
Part of the Color API. Used to add a controller.
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
Part of the ledd protocol. Used to add a client.
"""
if "i2c_dev" not in kwargs or "channels" not in kwargs or "address" not in kwargs:
return JSONRPCInvalidParams()
# 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'], _pwm_freq=1526)
pass
# ncontroller = Controller(channels=int(kwargs['channels']), i2c_device=int(kwargs['i2c_dev']),
# address=kwargs['address'], _pwm_freq=1526)
except OSError as e:
log.error("Error opening i2c device: %s (%s)", kwargs['i2c_dev'], e)
return JSONRPCError(-1004, "Error while opening i2c device", e)
# return JSONRPCError(-1004, "Error while opening i2c device", e)
session.add(ncontroller)
session.commit()
# session = Session()
# session.add(ncontroller)
# session.commit()
controller.append(ncontroller)
# clients.append(ncontroller)
return {'cid': ncontroller.id}
# 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 parameters: sid
"""
if "sid" not in kwargs:
return JSONRPCInvalidParams()
# if "sid" not in kwargs:
# return JSONRPCInvalidParams()
stripe = get_stripe(kwargs['sid'])
if not stripe:
log.warning("Stripe not found: id=%s", kwargs['sid'])
return JSONRPCError(-1003, "Stripeid not found")
# return JSONRPCError(-1003, "Stripeid not found")
if stripe.color:
return {'color': stripe.color.values}
else:
log.warning("Stripe has no color: id=%s", kwargs['sid'])
return JSONRPCError(-1009, "Internal Error")
# return JSONRPCError(-1009, "Internal Error")
@dispatcher.add_method
def add_stripe(**kwargs):
def add_led(**kwargs):
"""
Part of the Color API. Used to add stripes.
Required parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel, cid
"""
if "name" not in kwargs or "rgb" not in kwargs or "map" not in kwargs or "cid" not in kwargs:
return JSONRPCInvalidParams()
# if "name" not in kwargs or "rgb" not in kwargs or "map" not in kwargs or "cid" not in kwargs:
# return JSONRPCInvalidParams()
c = get_controller(kwargs['cid'])
""" :type c: ledd.controller.Controller """
if c is None:
log.warning("Controller not found: id=%s", kwargs['cid'])
return JSONRPCError(-1002, "Controller not found")
# return JSONRPCError(-1002, "Controller not found")
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 = RGBStripe(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", s.id, c.id, len(c.stripes))
session.add(s)
session.commit()
stripes.append(s)
# session = Session()
# session.add(s)
# session.commit()
leds.append(s)
return {'sid': s.id}
@dispatcher.add_method
def get_stripes(**kwargs):
def get_leds(**kwargs):
"""
Part of the Color API. Used to get all registered stripes known to the daemon.
Required parameters: -
"""
rjson = {
'ccount': len(controller),
'controller': controller
# 'ccount': len(controller),
# 'controller': controller
}
return rjson
@dispatcher.add_method
def test_channel(**kwargs):
"""
Part of the Color API. Used to test a channel on a specified controller.
Required parameters: controller id: cid, channel, value
"""
if "cid" not in kwargs or "channel" not in kwargs or "value" not in kwargs:
return JSONRPCInvalidParams()
# if "cid" not in kwargs or "channel" not in kwargs or "value" not in kwargs:
# return JSONRPCInvalidParams()
contr = get_controller(kwargs['cid'])
""" :type : ledd.controller.Controller """
@@ -350,14 +303,13 @@ def test_channel(**kwargs):
try:
contr.set_channel(kwargs['channel'], kwargs['value'], 2.8)
except OSError as e:
return JSONRPCError(-1009, "Internal Error", e)
pass
# return JSONRPCError(-1009, "Internal Error", e)
else:
return JSONRPCError(-1002, "Controller not found")
return ""
pass
# 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.
@@ -368,15 +320,33 @@ def discover(**kwargs):
def get_stripe(sid):
for s in stripes:
if s.id == sid:
return s
pass
# for s in stripes:
# if s.id == sid:
# return s
def get_controller(cid):
for c in controller:
if c.id == cid:
return c
pass
# for c in controller:
# if c.id == cid:
# return c
function_mapping = {WrapperMsg.Type.LED_GET_ALL: get_leds,
WrapperMsg.Type.LED_ADD: add_led,
WrapperMsg.Type.CLIENT_ADD: add_client,
# WrapperMsg.Type.LED_PERC_SET: ,
# WrapperMsg.Type.LED_PERC_GET: ,
# WrapperMsg.Type.CLIENT_LED_SET: ,
# WrapperMsg.Type.CLIENT_GET: ,
# WrapperMsg.Type.CLIENT_GET_LED_OPTIONS: ,
# WrapperMsg.Type.CLIENT_SET_LOCAL_DIRECT: ,
# WrapperMsg.Type.LEDGROUP_GET: ,
# WrapperMsg.Type.LEDGROUP_GET_ALL: ,
# WrapperMsg.Type.LEDGROUP_SET_COLOR: ,
# WrapperMsg.Type.LEDGROUP_ADD: ,
}
class LedDProtocol(asyncio.Protocol):
@@ -388,21 +358,23 @@ class LedDProtocol(asyncio.Protocol):
def data_received(self, data):
try:
d_decoded = data.decode()
wrapper_msg = WrapperMsg()
wrapper_msg = wrapper_msg.ParseFromString(data)
except UnicodeDecodeError:
log.warning("Recieved undecodable data, ignoring")
else:
self.select_task(d_decoded)
self.select_task(wrapper_msg)
def select_task(self, data):
if data:
data_split = data.splitlines()
for line in data_split:
if line:
try:
self.transport.write(JSONRPCResponseManager.handle(line, dispatcher).json.encode())
except TypeError as te:
log.warning("Can't send response: %s", te)
response = function_mapping[data.type](data)
if response:
try:
# noinspection PyUnresolvedReferences
self.transport.write(response.SerializeToString())
except TypeError as te:
log.warning("Can't send response: %s", te)
def connection_lost(self, exc):
log.info("Lost connection to %s", self.transport.get_extra_info("peername"))