381 lines
11 KiB
Python
381 lines
11 KiB
Python
# LEDD Project
|
|
# Copyright (C) 2015 LEDD Team
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import asyncio
|
|
import configparser
|
|
import errno
|
|
import logging
|
|
import os
|
|
import signal
|
|
import sqlite3
|
|
import sys
|
|
|
|
import spectra
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from ledd import VERSION
|
|
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__)
|
|
|
|
daemonSection = 'daemon'
|
|
databaseSection = 'db'
|
|
""" :type : asyncio.BaseEventLoop """
|
|
effects = []
|
|
leds = []
|
|
clients = []
|
|
conn = None
|
|
loop = None
|
|
server = None
|
|
|
|
|
|
def run():
|
|
try:
|
|
# read config
|
|
config = configparser.ConfigParser()
|
|
try:
|
|
with open('ledd.config', 'w+') as f:
|
|
config.read_file(f)
|
|
except FileNotFoundError:
|
|
log.info("No config file found!")
|
|
|
|
# SQL init
|
|
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())
|
|
|
|
# sigterm handler
|
|
def sigterm_handler():
|
|
raise SystemExit
|
|
|
|
signal.signal(signal.SIGTERM, sigterm_handler)
|
|
|
|
# init plugins
|
|
# TODO: check all plugins for existing hooks
|
|
|
|
# init clients
|
|
|
|
|
|
# main loop
|
|
global loop, server
|
|
loop = asyncio.get_event_loop()
|
|
coro = loop.create_server(LedDProtocol,
|
|
config.get(daemonSection, 'host', fallback='0.0.0.0'),
|
|
config.get(daemonSection, 'port', fallback=1425))
|
|
server = loop.run_until_complete(coro)
|
|
log.info("Start phase finished; starting main loop")
|
|
loop.run_forever()
|
|
except (KeyboardInterrupt, SystemExit):
|
|
log.info("Exiting")
|
|
|
|
for c in clients:
|
|
c.close()
|
|
|
|
try:
|
|
os.remove("ledd.pid")
|
|
except FileNotFoundError:
|
|
pass
|
|
conn.commit()
|
|
conn.close()
|
|
if server is not None:
|
|
server.close()
|
|
if loop is not None:
|
|
loop.run_until_complete(server.wait_closed())
|
|
loop.close()
|
|
sys.exit(0)
|
|
|
|
|
|
def start_effect(**kwargs):
|
|
"""
|
|
Part of the Color API. Used to start a specific effect.
|
|
Required parameters: stripe IDs: sids; effect id: eid, effect options: eopt
|
|
:param kwargs:
|
|
"""
|
|
|
|
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()
|
|
effects.append(effect)
|
|
effect.stripes.append(stripe)
|
|
effect.start()
|
|
|
|
# asyncio.ensure_future(asyncio.get_event_loop().run_in_executor(executor, effect.execute))
|
|
|
|
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 effect
|
|
}
|
|
|
|
return rjson
|
|
|
|
# return JSONRPCError(-1003, "Stripeid not found")
|
|
|
|
|
|
def stop_effect(**kwargs):
|
|
"""
|
|
Part of the Color API. Used to stop a specific effect.
|
|
Required parameters: effect identifier: eident
|
|
"""
|
|
|
|
# TODO: add stop effect by eident logic
|
|
|
|
|
|
def get_effects(**kwargs):
|
|
"""
|
|
Part of the Color API. Used to show all available and running effect.
|
|
Required parameters: -
|
|
"""
|
|
|
|
# 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".
|
|
|
|
|
|
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()
|
|
|
|
stripe = get_stripe(kwargs['sid'])
|
|
|
|
if stripe:
|
|
try:
|
|
stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
|
|
except OSError as e:
|
|
if int(e) == errno.ECOMM:
|
|
log.warning("Communication error on I2C Bus")
|
|
return e
|
|
else:
|
|
raise
|
|
else:
|
|
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
|
# return JSONRPCError(-1003, "Stripeid not found")
|
|
|
|
|
|
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()
|
|
|
|
try:
|
|
c = get_controller(kwargs['cid'])
|
|
""" :type c: ledd.controller.Controller """
|
|
|
|
c.set_all_channel(kwargs['v'])
|
|
except NoResultFound:
|
|
log.warning("Controller not found: id=%s", kwargs['cid'])
|
|
# return JSONRPCError(-1002, "Controller not found")
|
|
|
|
return ""
|
|
|
|
|
|
def add_client(**kwargs):
|
|
"""
|
|
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()
|
|
|
|
try:
|
|
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)
|
|
|
|
# session = Session()
|
|
# session.add(ncontroller)
|
|
# session.commit()
|
|
|
|
# clients.append(ncontroller)
|
|
|
|
# return {'cid': ncontroller.id}
|
|
|
|
|
|
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()
|
|
|
|
stripe = get_stripe(kwargs['sid'])
|
|
|
|
if not stripe:
|
|
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
|
# 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")
|
|
|
|
|
|
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()
|
|
|
|
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")
|
|
|
|
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 = Session()
|
|
# session.add(s)
|
|
# session.commit()
|
|
leds.append(s)
|
|
|
|
return {'sid': s.id}
|
|
|
|
|
|
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
|
|
}
|
|
|
|
return rjson
|
|
|
|
|
|
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()
|
|
|
|
contr = get_controller(kwargs['cid'])
|
|
""" :type : ledd.controller.Controller """
|
|
|
|
if contr is not None:
|
|
try:
|
|
contr.set_channel(kwargs['channel'], kwargs['value'], 2.8)
|
|
except OSError as e:
|
|
pass
|
|
# return JSONRPCError(-1009, "Internal Error", e)
|
|
else:
|
|
pass
|
|
# return JSONRPCError(-1002, "Controller not found")
|
|
|
|
|
|
def discover(**kwargs):
|
|
"""
|
|
Part of the Color API. Used by mobile applications to find the controller.
|
|
Required parameters: -
|
|
"""
|
|
|
|
return {'version': VERSION}
|
|
|
|
|
|
def get_stripe(sid):
|
|
pass
|
|
# for s in stripes:
|
|
# if s.id == sid:
|
|
# return s
|
|
|
|
|
|
def get_controller(cid):
|
|
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):
|
|
transport = None
|
|
|
|
def connection_made(self, transport):
|
|
log.debug("New connection from %s", transport.get_extra_info("peername"))
|
|
self.transport = transport
|
|
|
|
def data_received(self, data):
|
|
try:
|
|
wrapper_msg = WrapperMsg()
|
|
wrapper_msg = wrapper_msg.ParseFromString(data)
|
|
except UnicodeDecodeError:
|
|
log.warning("Recieved undecodable data, ignoring")
|
|
else:
|
|
self.select_task(wrapper_msg)
|
|
|
|
def select_task(self, data):
|
|
if data:
|
|
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"))
|