moved json encoder to a monkey patch
added bugs introduced in latest sqlalchemy and json-rpc switch (d5f403d557
)
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
from json import JSONEncoder
|
||||||
|
|
||||||
from sqlalchemy.orm import sessionmaker, scoped_session
|
from sqlalchemy.orm import sessionmaker, scoped_session
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
@@ -24,3 +25,11 @@ session = scoped_session(sessionmaker())
|
|||||||
""" :type : sqlalchemy.orm.scoping.scoped_session """
|
""" :type : sqlalchemy.orm.scoping.scoped_session """
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
Base.query = session.query_property()
|
Base.query = session.query_property()
|
||||||
|
|
||||||
|
|
||||||
|
def _default(self, obj):
|
||||||
|
return getattr(obj.__class__, "to_json", _default.default)(obj)
|
||||||
|
|
||||||
|
|
||||||
|
_default.default = JSONEncoder().default
|
||||||
|
JSONEncoder.default = _default
|
||||||
|
@@ -14,15 +14,13 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from json import JSONEncoder
|
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer, String
|
from sqlalchemy import Column, Integer, String
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship, reconstructor
|
||||||
|
|
||||||
import smbus
|
import smbus
|
||||||
from ledd.stripe import Stripe
|
|
||||||
from . import Base
|
from . import Base
|
||||||
|
|
||||||
PCA9685_SUBADR1 = 0x2
|
PCA9685_SUBADR1 = 0x2
|
||||||
@@ -64,6 +62,12 @@ class Controller(Base):
|
|||||||
self.bus = smbus.SMBus(self.i2c_device)
|
self.bus = smbus.SMBus(self.i2c_device)
|
||||||
self._address = int(self.address, 16)
|
self._address = int(self.address, 16)
|
||||||
|
|
||||||
|
@reconstructor
|
||||||
|
def init_on_load(self):
|
||||||
|
self._mode = None
|
||||||
|
self.bus = smbus.SMBus(self.i2c_device)
|
||||||
|
self._address = int(self.address, 16)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id)
|
return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id)
|
||||||
|
|
||||||
@@ -115,24 +119,14 @@ class Controller(Base):
|
|||||||
self.reset()
|
self.reset()
|
||||||
self._pwm_freq = value
|
self._pwm_freq = value
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
class ControllerEncoder(JSONEncoder):
|
|
||||||
def default(self, o):
|
|
||||||
if isinstance(o, Controller):
|
|
||||||
return {
|
return {
|
||||||
'id': o.id,
|
'id': self.id,
|
||||||
'pwm_freq': o.pwm_freq,
|
'pwm_freq': self.pwm_freq,
|
||||||
'channel': o.channels,
|
'channel': self.channels,
|
||||||
'address': o.address,
|
'address': self.address,
|
||||||
'stripes': o.stripes,
|
'stripes': self.stripes,
|
||||||
'cstripes': len(o.stripes),
|
'cstripes': len(self.stripes),
|
||||||
'i2c_device': o.i2c_device,
|
'i2c_device': self.i2c_device,
|
||||||
'mode': o.mode
|
'mode': self.mode
|
||||||
}
|
|
||||||
elif isinstance(o, Stripe):
|
|
||||||
return {
|
|
||||||
'id': o.id,
|
|
||||||
'name': o.name,
|
|
||||||
'rgb': o.rgb,
|
|
||||||
'channel': o.channels
|
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import configparser
|
import configparser
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -27,13 +26,14 @@ from jsonrpc import JSONRPCResponseManager, dispatcher
|
|||||||
from jsonrpc.exceptions import JSONRPCError, JSONRPCInvalidParams
|
from jsonrpc.exceptions import JSONRPCError, JSONRPCInvalidParams
|
||||||
import spectra
|
import spectra
|
||||||
from sqlalchemy.exc import OperationalError
|
from sqlalchemy.exc import OperationalError
|
||||||
|
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from ledd import VERSION
|
from ledd import VERSION
|
||||||
from ledd.effectstack import EffectStack
|
from ledd.effectstack import EffectStack
|
||||||
from ledd.models import Meta
|
from ledd.models import Meta
|
||||||
from ledd.stripe import Stripe
|
from ledd.stripe import Stripe
|
||||||
from ledd.controller import Controller, ControllerEncoder
|
from ledd.controller import Controller
|
||||||
from . import Base, session
|
from . import Base, session
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@@ -191,6 +191,9 @@ def set_color(**kwargs):
|
|||||||
stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
|
stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
log.warning("Stripe not found: id=%s", kwargs['sid'])
|
||||||
|
return JSONRPCError(-1003, "Stripeid not found")
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
@@ -255,7 +258,9 @@ def add_stripe(**kwargs):
|
|||||||
s = Stripe(name=kwargs['name'], rgb=bool(kwargs['rgb']),
|
s = Stripe(name=kwargs['name'], rgb=bool(kwargs['rgb']),
|
||||||
channel_r=kwargs['map']['r'], channel_g=kwargs['map']['g'], channel_b=kwargs['map']['b'])
|
channel_r=kwargs['map']['r'], channel_g=kwargs['map']['g'], channel_b=kwargs['map']['b'])
|
||||||
s.controller = c
|
s.controller = c
|
||||||
log.debug("Added stripe %s to controller %s; new len %s", c.id, s.id, len(c.stripes))
|
log.debug("Added stripe %s to controller %s; new len %s", s.id, c.id, len(c.stripes))
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
|
||||||
return {'sid': s.id}
|
return {'sid': s.id}
|
||||||
|
|
||||||
@@ -268,8 +273,8 @@ def get_stripes(**kwargs):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
rjson = {
|
rjson = {
|
||||||
'ccount': len(Controller.query),
|
'ccount': len(Controller.query.all()),
|
||||||
'controller': json.dumps(Controller.query, cls=ControllerEncoder),
|
'controller': Controller.query.all()
|
||||||
}
|
}
|
||||||
|
|
||||||
return rjson
|
return rjson
|
||||||
@@ -293,6 +298,8 @@ def test_channel(**kwargs):
|
|||||||
else:
|
else:
|
||||||
return JSONRPCError(-1002, "Controller not found")
|
return JSONRPCError(-1002, "Controller not found")
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@dispatcher.add_method
|
@dispatcher.add_method
|
||||||
def discover(**kwargs):
|
def discover(**kwargs):
|
||||||
|
@@ -17,15 +17,16 @@
|
|||||||
from spectra import Color
|
from spectra import Color
|
||||||
from sqlalchemy import Integer, ForeignKey, String, Float, Boolean
|
from sqlalchemy import Integer, ForeignKey, String, Float, Boolean
|
||||||
from sqlalchemy import Column
|
from sqlalchemy import Column
|
||||||
|
from sqlalchemy.orm import reconstructor
|
||||||
|
|
||||||
from . import Base
|
from . import Base
|
||||||
|
|
||||||
|
|
||||||
class Stripe(Base):
|
class Stripe(Base):
|
||||||
__tablename__ = "stripe"
|
|
||||||
"""
|
"""
|
||||||
A stripe is the smallest controllable unit.
|
A stripe is the smallest controllable unit.
|
||||||
"""
|
"""
|
||||||
|
__tablename__ = "stripe"
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
controller_id = Column(Integer, ForeignKey('controller.id'))
|
controller_id = Column(Integer, ForeignKey('controller.id'))
|
||||||
name = Column(String)
|
name = Column(String)
|
||||||
@@ -39,7 +40,7 @@ class Stripe(Base):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def channels(self):
|
def channels(self):
|
||||||
return self.channel_r, self.channel_b, self.channel_g
|
return self.channel_r, self.channel_g, self.channel_b
|
||||||
|
|
||||||
# TODO save channels to db
|
# TODO save channels to db
|
||||||
|
|
||||||
@@ -49,8 +50,18 @@ class Stripe(Base):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
self._color = None
|
||||||
|
self.gamma_correct = (self.channel_r_gamma, self.channel_g_gamma, self.channel_b_gamma)
|
||||||
|
self.read_color()
|
||||||
|
|
||||||
|
@reconstructor
|
||||||
|
def init_on_load(self):
|
||||||
|
self._color = None
|
||||||
|
self.gamma_correct = (self.channel_r_gamma, self.channel_g_gamma, self.channel_b_gamma)
|
||||||
|
self.read_color()
|
||||||
|
|
||||||
def read_color(self):
|
def read_color(self):
|
||||||
|
if self.controller:
|
||||||
rc = tuple([float(self.controller.get_channel(channel)) for channel in self.channels])
|
rc = tuple([float(self.controller.get_channel(channel)) for channel in self.channels])
|
||||||
c = Color("rgb", rc[0], rc[1], rc[2])
|
c = Color("rgb", rc[0], rc[1], rc[2])
|
||||||
self._color = c.to("hsv")
|
self._color = c.to("hsv")
|
||||||
@@ -66,4 +77,12 @@ class Stripe(Base):
|
|||||||
def get_color(self):
|
def get_color(self):
|
||||||
return self._color
|
return self._color
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'name': self.name,
|
||||||
|
'rgb': self.rgb,
|
||||||
|
'channel': self.channels
|
||||||
|
}
|
||||||
|
|
||||||
color = property(get_color, set_color)
|
color = property(get_color, set_color)
|
||||||
|
Reference in New Issue
Block a user