Restructured project, added start wrapper.

This commit is contained in:
Marius Schiffer
2015-07-14 14:26:39 +02:00
parent 32fe4c9916
commit a816f31632
5 changed files with 68 additions and 9 deletions

55
LedD/controller.py Normal file
View File

@@ -0,0 +1,55 @@
class Controller:
"""
A controller controls a number of stripes.
"""
@classmethod
def from_row(cls, db, row):
# load from db
return cls(db, pwm_freq=row["pwm_freq"], channels=row["channels"], i2c_device=row["i2c_device"],
address=row["address"], cid=row["id"])
@staticmethod
def from_db(db):
l = []
cur = db.cursor()
for row in cur.execute("select * from controller"):
l.append(Controller.from_row(db, row))
cur.close()
return l
def __init__(self, db, pwm_freq, channels, i2c_device, address, cid=-1):
self.pwm_freq = pwm_freq
self.channels = channels
self.i2c_device = i2c_device
self.address = address
self.id = cid
self.db = db
self.stripes = []
self.load_stripes()
def load_stripes(self):
cur = self.db.cursor()
for stripe in cur.execute("select * from stripes where controller_id = ?", (self.id,)):
self.stripes.append(Stripe.from_db(self, stripe))
def __repr__(self):
return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id)
class Stripe:
"""
A stripe is the smallest controllable unit.
"""
def __init__(self, controller, name, rgb, channels):
self.controller = controller
self.name = name
self.rgb = bool(rgb)
self.channels = channels
@classmethod
def from_db(cls, controller, row):
return cls(controller, name=row["name"], rgb=row["rgb"], channels={"r": row["channel_r"],
"g": row["channel_g"],
"b": row["channel_b"]})

View File

@@ -19,13 +19,15 @@ import socket
import configparser
import json
import sqlite3
from . import controller
class Daemon:
daemonSection = 'daemon'
databaseSection = 'db'
instance = None
def __init__(self):
Daemon.instance = self
config = configparser.ConfigParser()
try:
with open('ledd.config', 'w+') as f:
@@ -34,22 +36,24 @@ class Daemon:
print("no config file found!")
sqldb = sqlite3.connect(config.get(self.databaseSection, 'name', fallback='ledd.sqlite'))
sqldb.row_factory = sqlite3.Row
c = sqldb.cursor()
c.execute("SELECT db_version FROM meta")
db_version = c.fetchone()
if db_version:
print("DB connection established; version={}".format(db_version))
print("DB connection established; version={}".format(db_version[0]))
else:
with open("sql/ledd.sql", "r") as sqlfile:
with open("LedD/sql/ledd.sql", "r") as sqlfile:
c.executescript(sqlfile.read())
sqldb.commit()
c.close()
self.controller = controller.Controller.from_db(sqldb)
print(self.controller)
server = self.SocketServer(config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
config.get(self.daemonSection, 'port', fallback=1425))
asyncore.loop()
# asyncore.loop()
class ConnectionHandler(asyncore.dispatcher_with_send):
def handle_read(self):
@@ -88,7 +92,3 @@ class Daemon:
sock, addr = pair
print('Incoming connection from %s' % repr(addr))
handler = Daemon.ConnectionHandler(sock)
if __name__ == "__main__":
daemon = Daemon()

4
start.py Normal file
View File

@@ -0,0 +1,4 @@
import LedD.daemon
if __name__ == "__main__":
daemon = LedD.daemon.Daemon()