diff --git a/LedD/controller.py b/LedD/controller.py index 775b82b..fbda7a0 100644 --- a/LedD/controller.py +++ b/LedD/controller.py @@ -48,6 +48,8 @@ class Controller: self.id = cur.lastrowid cur.execute("UPDATE controller SET pwm_freq=?, channels=?, i2c_device=?, address=? WHERE id = ?", (self.pwm_freq, self.channels, self.i2c_device, self.address, self.id)) + cur.close() + self.db.commit() def __init__(self, db, channels, i2c_device, address, pwm_freq=-1, cid=-1, from_db=False): self.pwm_freq = pwm_freq @@ -81,6 +83,7 @@ class Controller: def add_stripe(self, stripe): self.stripes.append(stripe) + class Stripe: """ A stripe is the smallest controllable unit. @@ -107,6 +110,7 @@ class Stripe: "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] diff --git a/LedD/daemon.py b/LedD/daemon.py index 9e1ed29..b089af5 100644 --- a/LedD/daemon.py +++ b/LedD/daemon.py @@ -32,7 +32,6 @@ class Daemon: def __init__(self): Daemon.instance = self - config = configparser.ConfigParser() try: self.config = configparser.ConfigParser() try: @@ -91,6 +90,7 @@ class Daemon: class ConnectionHandler(asyncore.dispatcher_with_send): def handle_read(self): data = self.recv(5120) + self.debug = True if data: print(data) try: @@ -102,8 +102,15 @@ class Daemon: # TODO: add adapter setting stripe with color here print("recieved action: {}".format(json_decoded['action'])) elif json_decoded['action'] == "add_controller": - # TODO: add controller adding logic here print("recieved action: {}".format(json_decoded['action'])) + ncontroller = None + try: + ncontroller = controller.Controller(Daemon.instance.sqldb, json_decoded['channels'], + json_decoded['i2c_dev'], json_decoded['address']) + except OSError as e: + print("Error opening i2c device!") + self.send("{}\n".format(ncontroller.id).encode()) + Daemon.instance.controllers.append(ncontroller) elif json_decoded['action'] == "get_color": # TODO: add stripe color get logic print("recieved action: {}".format(json_decoded['action'])) @@ -112,16 +119,12 @@ class Daemon: for stripe in json_decoded['stripes']: # TODO: add stripe here print(len(json_decoded['stripes'])) - elif json_decoded['action'] == "add_controller": - ncontroller = controller.Controller(self.daemon.sqldb, 0, json_decoded['channels'], - json_decoded['i2c_dev'], json_decoded['address']) - self.send(ncontroller.id) - Daemon.instance.controllers.append(ncontroller) - else: print("no action found, ignoring") - except (TypeError, ValueError): - print("No valid JSON found!") + except TypeError as e: + print("No valid JSON found: {}".format(e)) + except ValueError: + print("No valid JSON detected!") class SocketServer(asyncore.dispatcher): def __init__(self, host, port): diff --git a/LedD/sql/ledd.sql b/LedD/sql/ledd.sql index d448ede..0206a16 100644 --- a/LedD/sql/ledd.sql +++ b/LedD/sql/ledd.sql @@ -15,7 +15,7 @@ INSERT INTO `meta` VALUES ('db_version','1'); CREATE TABLE "controller" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `address` TEXT, - `i2c_device` TEXT, + `i2c_device` INTEGER, `channels` INTEGER, `pwm_freq` INTEGER ); \ No newline at end of file