moved class stripe to another file

renamed decorator
This commit is contained in:
Giovanni Harting
2015-08-28 02:35:52 +02:00
parent 1aa955ce91
commit 7e421bc676
4 changed files with 77 additions and 64 deletions

View File

@@ -17,8 +17,8 @@
from json import JSONEncoder
import smbus
from colour import Color
from ledd.stripe import Stripe
PCA9685_SUBADR1 = 0x2
PCA9685_SUBADR2 = 0x3
@@ -105,53 +105,6 @@ class Controller:
self.stripes.append(stripe)
class Stripe:
"""
A stripe is the smallest controllable unit.
"""
def __init__(self, controller, name, rgb, channels, sid=-1, from_db=False):
self.controller = controller
self.name = name
self.rgb = bool(rgb)
self.channels = channels
self.id = sid
self._color = Color()
self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB
self.read_color()
if not from_db:
self.save_to_db()
def save_to_db(self):
cur = self.controller.db.cursor()
if self.id == -1:
cur.execute("INSERT INTO stripes DEFAULT VALUES")
self.id = cur.lastrowid
cur.execute(
"UPDATE stripes SET channel_r = ?, channel_g = ?, channel_b = ?,controller_id = ?, name = ? WHERE id = ?",
self.channels + [self.controller.id, self.name, self.id])
cur.close()
self.controller.db.commit()
def read_color(self):
self._color.rgb = [self.controller.get_channel(channel) ** (1 / 2.8) for channel in self.channels]
@classmethod
def from_db(cls, controller, row):
return cls(controller, name=row["name"], rgb=row["rgb"],
channels=(row["channel_r"], row["channel_g"], row["channel_b"]), sid=row["id"], from_db=True)
def set_color(self, c):
self._color = c
for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.rgb):
self.controller.set_channel(channel, value ** gamma_correct)
def get_color(self):
return self._color
color = property(get_color, set_color)
class ControllerEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, Controller):

View File

@@ -25,11 +25,7 @@ import time
import asyncio
from ledd import controller, VERSION
from ledd.decorators import add_action
from multiprocessing import Process
log = logging.getLogger(__name__)
clients = {} # task -> (reader, writer)
from ledd.decorators import ledd_protocol
log = logging.getLogger(__name__)
@@ -41,7 +37,7 @@ class Daemon:
""":type : Daemon """
loop = None
""" :type : asyncio.BaseEventLoop """
action_dict = {}
protocol = {}
def __init__(self):
Daemon.instance = self
@@ -112,7 +108,7 @@ class Daemon:
c.close()
self.check_db()
@add_action(action_dict)
@ledd_protocol(protocol)
def set_color(self, req_json):
"""
Part of the Color API. Used to set color of a stripe.
@@ -122,7 +118,7 @@ class Daemon:
# TODO: add adapter setting stripe with color here
log.debug("recieved action: %s", req_json['action'])
@add_action(action_dict)
@ledd_protocol(protocol)
def add_controller(self, req_json):
"""
Part of the Color API. Used to add a controller.
@@ -154,7 +150,7 @@ class Daemon:
return json.dumps(rjson)
@add_action(action_dict)
@ledd_protocol(protocol)
def get_color(self, req_json):
"""
Part of the Color API. Used to get the currect color of an stripe.
@@ -164,7 +160,7 @@ class Daemon:
log.debug("recieved action: %s", req_json['action'])
# TODO: Add get color logic
@add_action(action_dict)
@ledd_protocol(protocol)
def add_stripes(self, req_json):
"""
Part of the Color API. Used to add stripes.
@@ -174,10 +170,10 @@ class Daemon:
log.debug("recieved action: %s", req_json['action'])
if "stripes" in req_json:
for stripe in req_json['stripes']:
# TODO: add stripe here
log.debug(len(req_json['stripes']))
@add_action(action_dict)
@ledd_protocol(protocol)
def get_controllers(self, req_json):
"""
Part of the Color API. Used to get all registered controllers known to the daemon.
@@ -195,7 +191,7 @@ class Daemon:
return json.dumps(rjson, cls=controller.ControllerEncoder)
@add_action(action_dict)
@ledd_protocol(protocol)
def connection_check(self, req_json):
"""
Part of the Color API. Used to query all channels on a specified controller.
@@ -221,7 +217,7 @@ class Daemon:
return json.dumps(rjson)
@add_action(action_dict)
@ledd_protocol(protocol)
def discover(self, req_json):
"""
Part of the Color API. Used by mobile applications to find the controller.

View File

@@ -15,7 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def add_action(actiondict):
def ledd_protocol(proto):
"""
Decorator used to add functions to action dict
:param actiondict: dict to add to
@@ -23,7 +23,7 @@ def add_action(actiondict):
"""
def wrap(f):
actiondict[f.__name__] = f
proto[f.__name__] = f
def wrapped_f(*args):
f(*args)

64
ledd/stripe.py Normal file
View File

@@ -0,0 +1,64 @@
# 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/>.
from colour import Color
class Stripe:
"""
A stripe is the smallest controllable unit.
"""
def __init__(self, controller, name, rgb, channels, sid=-1, from_db=False):
self.controller = controller
self.name = name
self.rgb = bool(rgb)
self.channels = channels
self.id = sid
self._color = Color()
self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB
self.read_color()
if not from_db:
self.save_to_db()
def save_to_db(self):
cur = self.controller.db.cursor()
if self.id == -1:
cur.execute("INSERT INTO stripes DEFAULT VALUES")
self.id = cur.lastrowid
cur.execute(
"UPDATE stripes SET channel_r = ?, channel_g = ?, channel_b = ?,controller_id = ?, name = ? WHERE id = ?",
self.channels + [self.controller.id, self.name, self.id])
cur.close()
self.controller.db.commit()
def read_color(self):
self._color.rgb = [self.controller.get_channel(channel) ** (1 / 2.8) for channel in self.channels]
@classmethod
def from_db(cls, controller, row):
return cls(controller, name=row["name"], rgb=row["rgb"],
channels=(row["channel_r"], row["channel_g"], row["channel_b"]), sid=row["id"], from_db=True)
def set_color(self, c):
self._color = c
for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.rgb):
self.controller.set_channel(channel, value ** gamma_correct)
def get_color(self):
return self._color
color = property(get_color, set_color)