fixed some bugs

rearranged all to single request only
ready for first release \o/
This commit is contained in:
Giovanni Harting
2015-09-13 16:55:07 +02:00
parent 1623bba1fe
commit ad009bd0d5
3 changed files with 59 additions and 78 deletions

View File

@@ -84,11 +84,11 @@ class Controller:
self.id = cid self.id = cid
self.db = db self.db = db
self.stripes = [] self.stripes = []
self.load_stripes()
self._pwm_freq = None self._pwm_freq = None
self.pwm_freq = pwm_freq self.pwm_freq = pwm_freq
if not from_db: if not from_db:
self.save_to_db() self.save_to_db()
self.load_stripes()
def load_stripes(self): def load_stripes(self):
cur = self.db.cursor() cur = self.db.cursor()

View File

@@ -21,7 +21,6 @@ import sqlite3
import os import os
import sys import sys
import traceback import traceback
import time
import asyncio import asyncio
import signal import signal
@@ -208,15 +207,12 @@ class Daemon:
""" """
log.debug("recieved action: %s", req_json['action']) log.debug("recieved action: %s", req_json['action'])
if "stripes" in req_json: found_s = self.find_stripe(req_json['sid'])
for stripe in req_json['stripes']:
found_s = self.find_stripe(stripe['sid'])
if found_s is None: if found_s is None:
log.warning("Stripe not found: id=%s", stripe['sid']) log.warning("Stripe not found: id=%s", req_json['sid'])
continue else:
found_s.set_color(spectra.hsv(req_json['hsv']['h'], req_json['hsv']['s'], req_json['hsv']['v']))
found_s.set_color(spectra.hsv(stripe['hsv']['h'], stripe['hsv']['s'], stripe['hsv']['v']))
def find_stripe(self, sid): def find_stripe(self, sid):
""" """
@@ -245,11 +241,11 @@ class Daemon:
ncontroller = controller.Controller(Daemon.instance.sqldb, req_json['channels'], ncontroller = controller.Controller(Daemon.instance.sqldb, req_json['channels'],
req_json['i2c_dev'], req_json['address']) req_json['i2c_dev'], req_json['address'])
except OSError as e: except OSError as e:
log.error("Error opening i2c device: %s", req_json['i2c_dev']) log.error("Error opening i2c device: %s (%s)", req_json['i2c_dev'], os.strerror(int(str(e))))
rjson = { rjson = {
'success': False, 'success': False,
'message': "Error while opening i2c device", 'message': "Error while opening i2c device",
'message_detail': os.strerror(e.errno), 'message_detail': os.strerror(int(str(e))),
'ref': req_json['ref'] 'ref': req_json['ref']
} }
return json.dumps(rjson) return json.dumps(rjson)
@@ -267,75 +263,60 @@ class Daemon:
@ledd_protocol(protocol) @ledd_protocol(protocol)
def get_color(self, req_json): def get_color(self, req_json):
""" """
Part of the Color API. Used to get the currect color of an stripe. Part of the Color API. Used to get the current color of an stripe.
Required JSON parameters: stripes Required JSON parameters: stripes
:param req_json: dict of request json :param req_json: dict of request json
""" """
log.debug("recieved action: %s", req_json['action']) log.debug("recieved action: %s", req_json['action'])
res_stripes = [] found_s = self.find_stripe(req_json['sid'])
if "stripes" in req_json: if found_s is None:
for stripe in req_json['stripes']: log.warning("Stripe not found: id=%s", req_json['sid'])
found_s = self.find_stripe(stripe['sid']) return {
'success': False,
if found_s is None: 'message': "Stripe not found",
log.warning("Stripe not found: id=%s", stripe['sid'])
continue
res_stripes.append({
'success': True,
'sid': found_s.id,
'color': found_s.get_color.values
})
rjson = {
'success': True,
'stripes': res_stripes,
'ref': req_json['ref'] 'ref': req_json['ref']
} }
return json.dumps(rjson) rjson = {
'success': True,
'color': found_s.color.values,
'ref': req_json['ref']
}
return json.dumps(rjson)
@ledd_protocol(protocol) @ledd_protocol(protocol)
def add_stripes(self, req_json): def add_stripe(self, req_json):
""" """
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 Required JSON parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel, cid
:param req_json: dict of request json :param req_json: dict of request json
""" """
log.debug("recieved action: %s", req_json['action']) log.debug("recieved action: %s", req_json['action'])
res_stripes = [] if "stripe" in req_json:
stripe = req_json['stripe']
c = next((x for x in self.controllers if x.id == stripe['cid']), None)
""" :type c: ledd.controller.Controller """
if "stripes" in req_json: if c is None:
for stripe in req_json['stripes']: return {
c = next((x for x in self.controllers if x.id == stripe['cid']), None) 'success': False,
""" :type c: ledd.controller.Controller """ 'message': "Controller not found",
if c is None:
res_stripes.append({
'success': False,
'message': "Controller not found",
'ref': stripe['ref']
})
continue
s = Stripe(c, stripe['name'], stripe['rgb'],
(stripe['map']['r'], stripe['map']['g'], stripe['map']['b']))
c.stripes.append(s)
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
res_stripes.append({
'success': True,
'sid': s.id,
'ref': stripe['ref'] 'ref': stripe['ref']
}) }
s = Stripe(c, stripe['name'], stripe['rgb'],
(stripe['map']['r'], stripe['map']['g'], stripe['map']['b']))
c.stripes.append(s)
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
rjson = { rjson = {
'success': True, 'success': True,
'stripes': res_stripes, 'sid': s.id,
'ref': req_json['ref'] 'ref': req_json['ref']
} }
@@ -360,23 +341,19 @@ class Daemon:
return json.dumps(rjson, cls=controller.ControllerEncoder) return json.dumps(rjson, cls=controller.ControllerEncoder)
@ledd_protocol(protocol) @ledd_protocol(protocol)
def connection_check(self, req_json): def test_channel(self, req_json):
""" """
Part of the Color API. Used to query all channels on a specified controller. Part of the Color API. Used to test a channel on a specified controller.
Required JSON parameters: controller id: cid Required JSON parameters: controller id: cid, channel, value
:param req_json: dict of request json :param req_json: dict of request json
""" """
log.debug("recieved action: %s", req_json['action']) log.debug("recieved action: %s", req_json['action'])
result = next(filter(lambda x: x.id == req_json['cid'], self.controllers), None) result = next(filter(lambda x: x.id == req_json['cid'], self.controllers), None)
""" :type : Controller """ """ :type : ledd.controller.Controller """
if result is not None: if result is not None:
for i in range(result.channels): result.set_channel(req_json['channel'], req_json['value'])
log.debug("set channel %d=%s", i, "1")
result.set_channel(i, 1)
time.sleep(10)
result.set_channel(i, 0)
rjson = { rjson = {
'success': True, 'success': True,
@@ -430,16 +407,20 @@ class LedDProtocol(asyncio.Protocol):
def select_task(self, data): def select_task(self, data):
if data: if data:
try: try:
json_decoded = json.loads(data) data_split = data.splitlines()
log.debug(data_split)
for line in data_split:
if line:
json_decoded = json.loads(line)
if "action" in json_decoded and "ref" in json_decoded: if "action" in json_decoded and "ref" in json_decoded:
return_data = Daemon.instance.protocol.get(json_decoded['action'], Daemon.no_action_found)( return_data = Daemon.instance.protocol.get(json_decoded['action'], Daemon.no_action_found)(
Daemon.instance, json_decoded) Daemon.instance, json_decoded)
if return_data is not None: if return_data is not None:
self.transport.write("{}\n".format(return_data).encode()) self.transport.write("{}\n".format(return_data).encode())
else: else:
log.debug("no action or ref value found in JSON, ignoring") log.debug("no action or ref value found in JSON, ignoring")
except TypeError: except TypeError:
log.debug("No valid JSON found: %s", traceback.format_exc()) log.debug("No valid JSON found: %s", traceback.format_exc())
except ValueError: except ValueError:

View File

@@ -18,8 +18,8 @@
def ledd_protocol(proto): def ledd_protocol(proto):
""" """
Decorator used to add functions to action dict Decorator used to add functions to action dict
:param actiondict: dict to add to :param proto: dict to add to
:type actiondict: dict :type proto: dict
""" """
def wrap(f): def wrap(f):