more sql work

This commit is contained in:
Giovanni Harting
2015-07-14 14:28:13 +02:00
parent a816f31632
commit b127697ab9
3 changed files with 63 additions and 26 deletions

5
.gitignore vendored
View File

@@ -169,3 +169,8 @@ target/
*.exe *.exe
*.out *.out
*.app *.app
# LEDD
LedD/ledd.config
LedD/ledd.sqlite

View File

@@ -20,40 +20,72 @@ import configparser
import json import json
import sqlite3 import sqlite3
from . import controller from . import controller
import os
import sys
class Daemon: class Daemon:
daemonSection = 'daemon' daemonSection = 'daemon'
databaseSection = 'db' databaseSection = 'db'
instance = None
def __init__(self): def __init__(self):
Daemon.instance = self
config = configparser.ConfigParser() config = configparser.ConfigParser()
try:
self.config = configparser.ConfigParser()
try: try:
with open('ledd.config', 'w+') as f: with open('ledd.config', 'w+') as f:
config.read_file(f) self.config.read_file(f)
except FileNotFoundError: except FileNotFoundError:
print("no config file found!") print("no config file found!")
sqldb = sqlite3.connect(config.get(self.databaseSection, 'name', fallback='ledd.sqlite')) self.sqldb = sqlite3.connect(self.config.get(self.databaseSection, 'name', fallback='ledd.sqlite'))
sqldb.row_factory = sqlite3.Row sqldb.row_factory = sqlite3.Row
c = sqldb.cursor()
c.execute("SELECT db_version FROM meta")
db_version = c.fetchone()
if db_version: if not self.check_db():
print("DB connection established; version={}".format(db_version[0])) self.init_db()
else:
with open("LedD/sql/ledd.sql", "r") as sqlfile: self.sqldb.commit()
c.executescript(sqlfile.read())
sqldb.commit()
c.close()
self.controller = controller.Controller.from_db(sqldb) self.controller = controller.Controller.from_db(sqldb)
print(self.controller) print(self.controller)
server = self.SocketServer(config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
config.get(self.daemonSection, 'port', fallback=1425)) server = self.SocketServer(self.config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
# asyncore.loop() self.config.get(self.daemonSection, 'port', fallback=1425))
asyncore.loop()
except (KeyboardInterrupt, SystemExit):
print("\nShutting down...")
self.sqldb.close()
sys.exit(0)
def check_db(self):
c = self.sqldb.cursor()
try:
c.execute("SELECT db_version FROM meta")
db_version = c.fetchone()
c.close()
print(db_version)
if db_version is not None:
print("DB connection established; version={}".format(db_version[0]))
return True
else:
return False
except sqlite3.OperationalError:
c.close()
return False
def init_db(self):
self.sqldb.close()
if os.path.exists("ledd.sqlite"):
os.remove("ledd.sqlite")
self.sqldb = sqlite3.connect(self.config.get(self.databaseSection, 'name', fallback='ledd.sqlite'))
sqldb.row_factory = sqlite3.Row
with open("LedD/sql/ledd.sql", "r") as sqlfile:
c = self.sqldb.cursor()
c.executescript(sqlfile.read())
c.close()
self.check_db()
class ConnectionHandler(asyncore.dispatcher_with_send): class ConnectionHandler(asyncore.dispatcher_with_send):
def handle_read(self): def handle_read(self):
@@ -92,3 +124,4 @@ class Daemon:
sock, addr = pair sock, addr = pair
print('Incoming connection from %s' % repr(addr)) print('Incoming connection from %s' % repr(addr))
handler = Daemon.ConnectionHandler(sock) handler = Daemon.ConnectionHandler(sock)

View File

@@ -19,4 +19,3 @@ CREATE TABLE "controller" (
`channels` INTEGER, `channels` INTEGER,
`pwm_freq` INTEGER `pwm_freq` INTEGER
); );
COMMIT;