Added simple i2c emulator. Cleaned up database insertion.

This commit is contained in:
Marius Schiffer
2015-07-15 12:30:31 +02:00
parent 7e2706b81c
commit 288f673cee
3 changed files with 33 additions and 3 deletions

View File

@@ -44,8 +44,10 @@ class Controller:
def save_to_db(self): def save_to_db(self):
cur = self.db.cursor() cur = self.db.cursor()
if self.id == -1: if self.id == -1:
cur.execute("INSERT INTO controller DEFAULT VALUES") cur.execute("INSERT INTO controller (pwm_freq, channels, i2c_device, address) VALUES ()",
(self.pwm_freq, self.channels, self.i2c_device, self.address))
self.id = cur.lastrowid self.id = cur.lastrowid
else:
cur.execute("UPDATE controller SET pwm_freq=?, channels=?, i2c_device=?, address=? WHERE id = ?", 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)) (self.pwm_freq, self.channels, self.i2c_device, self.address, self.id))
cur.close() cur.close()

View File

@@ -21,7 +21,7 @@ import json
import sqlite3 import sqlite3
import os import os
import sys import sys
import traceback
from . import controller from . import controller
@@ -60,6 +60,11 @@ class Daemon:
sys.exit(0) sys.exit(0)
def check_db(self): def check_db(self):
"""
Checks database version
:return: database validity
:rtype: bool
"""
c = self.sqldb.cursor() c = self.sqldb.cursor()
try: try:
c.execute("SELECT value FROM meta WHERE option = 'db_version'") c.execute("SELECT value FROM meta WHERE option = 'db_version'")
@@ -125,6 +130,7 @@ class Daemon:
print("No valid JSON found: {}".format(e)) print("No valid JSON found: {}".format(e))
except ValueError: except ValueError:
print("No valid JSON detected!") print("No valid JSON detected!")
traceback.print_exc(file=sys.stdout)
class SocketServer(asyncore.dispatcher): class SocketServer(asyncore.dispatcher):
def __init__(self, host, port): def __init__(self, host, port):

View File

@@ -1,3 +1,25 @@
from pkgutil import iter_modules
if "smbus" not in (name for loader, name, ispkg in iter_modules()):
print("smbus not found, installing replacement")
class SMBus:
def __init__(self, i2c_address):
self.i2c_address = i2c_address
self.channels = {}
def write_word_data(self, cmd, val):
if (cmd - 6) % 4 == 0:
self.channels[(cmd - 6) / 4] = val
def read_word_data(self, cmd):
return self.channels[(cmd - 8) / 4]
import sys
sys.modules['smbus'] = SMBus
import LedD.daemon import LedD.daemon
if __name__ == "__main__": if __name__ == "__main__":