From 32fe4c991627200e51d8064665b58f84521a1bd2 Mon Sep 17 00:00:00 2001 From: Giovanni Harting Date: Tue, 14 Jul 2015 13:28:58 +0200 Subject: [PATCH] added first sql implemention --- LedD/daemon.py | 44 ++++++++++++++++++++++++++++++++++++++------ LedD/sql/ledd.sql | 22 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 LedD/sql/ledd.sql diff --git a/LedD/daemon.py b/LedD/daemon.py index 122e3a7..a5b0bfe 100644 --- a/LedD/daemon.py +++ b/LedD/daemon.py @@ -17,33 +17,64 @@ import asyncore import socket import configparser +import json +import sqlite3 class Daemon: - daemonSection = 'daemon' + databaseSection = 'db' def __init__(self): config = configparser.ConfigParser() try: with open('ledd.config', 'w+') as f: config.read_file(f) - except FileExistsError: + except FileNotFoundError: print("no config file found!") + sqldb = sqlite3.connect(config.get(self.databaseSection, 'name', fallback='ledd.sqlite')) + + 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)) + else: + with open("sql/ledd.sql", "r") as sqlfile: + c.executescript(sqlfile.read()) + sqldb.commit() + c.close() + server = self.SocketServer(config.get(self.daemonSection, 'host', fallback='0.0.0.0'), config.get(self.daemonSection, 'port', fallback=1425)) asyncore.loop() class ConnectionHandler(asyncore.dispatcher_with_send): - def handle_read(self): - data = self.recv(8192) + data = self.recv(5120) if data: - print(data) + try: + json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')) + json_decoded = json.loads(data) + + if "action" in json_decoded: + if json_decoded['action'] == "set_color": + # 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'])) + elif json_decoded['action'] == "get_color": + # TODO: add stripe color get logic + print("recieved action: {}".format(json_decoded['action'])) + except TypeError: + print("No JSON found!") + else: + print("no action found, ignoring") class SocketServer(asyncore.dispatcher): - def __init__(self, host, port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) @@ -58,5 +89,6 @@ class Daemon: print('Incoming connection from %s' % repr(addr)) handler = Daemon.ConnectionHandler(sock) + if __name__ == "__main__": daemon = Daemon() diff --git a/LedD/sql/ledd.sql b/LedD/sql/ledd.sql new file mode 100644 index 0000000..707c4fc --- /dev/null +++ b/LedD/sql/ledd.sql @@ -0,0 +1,22 @@ +CREATE TABLE `stripes` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, + `name` TEXT, + `rgb` INTEGER, + `controller_id` INTEGER, + `channel_r` INTEGER, + `channel_g` INTEGER, + `channel_b` INTEGER +); +CREATE TABLE "meta" ( + `option` TEXT, + `value` TEXT +); +INSERT INTO `meta` VALUES ('db_version','1'); +CREATE TABLE "controller" ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, + `address` TEXT, + `i2c_device` TEXT, + `channels` INTEGER, + `pwm_freq` INTEGER +); +COMMIT;