added first sql implemention
This commit is contained in:
@@ -17,33 +17,64 @@
|
|||||||
import asyncore
|
import asyncore
|
||||||
import socket
|
import socket
|
||||||
import configparser
|
import configparser
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
class Daemon:
|
class Daemon:
|
||||||
|
|
||||||
daemonSection = 'daemon'
|
daemonSection = 'daemon'
|
||||||
|
databaseSection = 'db'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
with open('ledd.config', 'w+') as f:
|
with open('ledd.config', 'w+') as f:
|
||||||
config.read_file(f)
|
config.read_file(f)
|
||||||
except FileExistsError:
|
except FileNotFoundError:
|
||||||
print("no config file found!")
|
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'),
|
server = self.SocketServer(config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
|
||||||
config.get(self.daemonSection, 'port', fallback=1425))
|
config.get(self.daemonSection, 'port', fallback=1425))
|
||||||
asyncore.loop()
|
asyncore.loop()
|
||||||
|
|
||||||
class ConnectionHandler(asyncore.dispatcher_with_send):
|
class ConnectionHandler(asyncore.dispatcher_with_send):
|
||||||
|
|
||||||
def handle_read(self):
|
def handle_read(self):
|
||||||
data = self.recv(8192)
|
data = self.recv(5120)
|
||||||
if data:
|
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):
|
class SocketServer(asyncore.dispatcher):
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port):
|
||||||
asyncore.dispatcher.__init__(self)
|
asyncore.dispatcher.__init__(self)
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
@@ -58,5 +89,6 @@ class Daemon:
|
|||||||
print('Incoming connection from %s' % repr(addr))
|
print('Incoming connection from %s' % repr(addr))
|
||||||
handler = Daemon.ConnectionHandler(sock)
|
handler = Daemon.ConnectionHandler(sock)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
daemon = Daemon()
|
daemon = Daemon()
|
||||||
|
22
LedD/sql/ledd.sql
Normal file
22
LedD/sql/ledd.sql
Normal file
@@ -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;
|
Reference in New Issue
Block a user