Skip to content

Commit

Permalink
Adding Database logging controller
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed Aug 2, 2019
1 parent 154d6f3 commit d920750
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 14 deletions.
2 changes: 1 addition & 1 deletion _C001_DomoHTTP.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,chang
try:
usebattery = float(usebattery)
except:
userbattery = -1
usebattery = -1
if int(sensortype)==rpieGlobals.SENSOR_TYPE_SWITCH:
url = "/json.htm?type=command&param=switchlight&idx="
url += str(idx)
Expand Down
2 changes: 1 addition & 1 deletion _C002_DomoMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,chang
try:
usebattery = float(usebattery)
except:
userbattery = -1
usebattery = -1
if int(idx) > 0:
if usebattery != -1 and usebattery != 255:
bval = usebattery
Expand Down
197 changes: 197 additions & 0 deletions _C016_DBStore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#!/usr/bin/env python3
#############################################################################
##################### DBStore Controller for RPIEasy ########################
#############################################################################
#
# Sqlite and Mysql data storage backend.
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import controller
import rpieGlobals
import misc
import webserver
import datetime
import Settings
import linux_os as OS
import time

_lastRSSIval = 0
_lastRSSItime = 0

backends = []
try:
import lib.lib_db_sqlite as DBSQLITE
backends.append("SQLite")
except:
pass
try:
import lib.lib_db_mysql as DBMYSQL
backends.append("MySQL")
except:
pass

def getCachedRSSI(urssi=-1):
global _lastRSSIval, _lastRSSItime
if ((time.time()-_lastRSSItime)>=3) and (urssi==-1):
_lastRSSIval = OS.get_rssi()
_lastRSSItime = time.time()
try:
if urssi != -1:
rssi = urssi
else:
rssi = int(_lastRSSIval)
except:
pass
return str(rssi)

class Controller(controller.ControllerProto):
CONTROLLER_ID = 16
CONTROLLER_NAME = "DB Data Storage"

def __init__(self,controllerindex):
controller.ControllerProto.__init__(self,controllerindex)
self.usesID = False
self.usesAccount = False
self.usesPassword = False
self.timer30s = True
self.db = None
self.provider = -1
self.connected = False
self.dbname = ""

def controller_init(self,enablecontroller=None):
if enablecontroller != None:
self.enabled = enablecontroller
if self.enabled:
if self.connect():
self.initialized = True
else:
self.disconnect()
self.initialized = False
return True

def connect(self):
self.connected = False
if self.provider==0:
try:
if self.dbname != "":
self.db = DBSQLITE.DB_SQLite3()
self.db.connect(self.dbname)
if self.db.isexist_sensortable() == False:
self.db.create_sensortable()
self.connected = True
except:
self.connected = False
elif self.provider==1:
try:
if self.dbname != "":
self.db = DBMYSQL.DB_MySQL()
if self.controllerport==0:
self.controllerport=3306
self.db.connect(hostname=self.controllerip,dbport=self.controllerport,dbname=self.dbname,username=self.controlleruser,passw=self.controllerpassword)
if self.db.isexist_sensortable() == False:
self.db.create_sensortable()
self.connected = True
except Exception as e:
print(e)
self.connected = False
return self.connected

def disconnect(self):
try:
if self.db is not None:
self.db.disconnect()
except:
pass

def webform_load(self):
global backends
if self.provider==0:
webserver.addFormNote("Server address and port is not used.")
self.controllerport=1
self.controllerip="localhost"
self.usesAccount = False
self.usesPassword = False
elif self.provider==1:
self.usesAccount = True
self.usesPassword = True
if self.controllerport in [0,1,80]:
self.controllerport=3306
try:
options = backends
optionvalues = [0,1]
webserver.addFormSelector("DB Type","c016_type",len(options),options,optionvalues,None,int(self.provider),reloadonchange=True)
except:
pass
webserver.addFormTextBox("Database name","c016_dbname",str(self.dbname),255)
return True

def webform_save(self,params):
try:
self.provider = int(webserver.arg("c016_type",params))
except:
self.provider = 0
try:
self.dbname = str(webserver.arg("c016_dbname",params))
except:
self.dbname = ""
return True

def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1):
if self.enabled:
sqlstr = "insert into easysensor (time,unit,nodename,taskname,sensortype"
sqlstr2 = ") values ('"+ str(datetime.datetime.now()) +"',"+str(Settings.Settings["Unit"])+",'"+str(Settings.Settings["Name"])+"','"+str(Settings.Tasks[tasknum].gettaskname())+"',"+str(sensortype)
vcount = 4
if tasknum!=-1:
try:
sqlstr += ",tasknum"
sqlstr2 += ","+str(tasknum+1)
vcount = Settings.Tasks[tasknum].valuecount
except:
pass
if vcount>0:
if sensortype==rpieGlobals.SENSOR_TYPE_TEXT:
sqlstr += ",valuetext"
else:
sqlstr += ",value1"
sqlstr2 += ","+str(value[0])
if vcount>1:
sqlstr += ",value2"
sqlstr2 += ","+str(value[1])
if vcount>2:
sqlstr += ",value3"
sqlstr2 += ","+str(value[2])
if vcount>3:
sqlstr += ",value4"
sqlstr2 += ","+str(value[3])

sqlstr += ",rssi"
sqlstr2 += ","+str(getCachedRSSI(userssi))
try:
usebattery = float(usebattery)
except:
usebattery = -1
if usebattery != -1 and usebattery != 255: # battery input 0..100%, 255 means not supported
sqlstr += ",battery"
sqlstr2 += ","+str(usebattery)
else:
bval = misc.get_battery_value()
if bval != 255:
sqlstr += ",battery"
sqlstr2 += ","+str(bval)

sqlstr = sqlstr + sqlstr2+")"
# print(sqlstr)
try:
self.db.sqlexec(sqlstr)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"DBStore: "+str(e))

def timer_thirty_second(self):
if self.initialized and self.enabled:
try:
self.db.save(True) # commit only if needed
except:
pass
return self.timer30s
11 changes: 6 additions & 5 deletions _P512_BLEMijia.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ def disconnect(self):
# print("disconn")
self.connected = False
self.waitnotifications = False
try:
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass
if self.enabled:
try:
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass

def __del__(self):
self.disconnect()
Expand Down
11 changes: 6 additions & 5 deletions _P513_BLEMijiaClock.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ def disconnect(self):
# print("disconn")
self.connected = False
self.waitnotifications = False
try:
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass
if self.enabled:
try:
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass

def __del__(self):
self.disconnect()
Expand Down
13 changes: 12 additions & 1 deletion plugindeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@
"pip": ["pytz","pysolar"],
"testcmd": "from pysolar import solar",
"installed":-1},
{"name":"mysql",
"apt": ["python3-pip","python3-setuptools"],
"pip": ["PyMySQL"],
"testcmd": "import pymysql",
"installed":-1},
{"name":"sqlite",
"testcmd": "import sqlite3",
"installed":-1},

]

Expand All @@ -152,7 +160,10 @@
{"controllerid":"13", # ESPEasy P2P
"modules":["linux-kernel"]},
{"controllerid":"14", # Generic MQTT
"modules":["paho-mqtt"]}
"modules":["paho-mqtt"]},
{"controllerid":"16", # DBStore
"modules":["sqlite","mysql"]}

]

plugindependencies = [
Expand Down
2 changes: 1 addition & 1 deletion rpieGlobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
PROGNAME = "RPIEasy"
BUILD = 19210
BUILD = 19214
PROGVER = "0."+str(BUILD/1000)

gpMenu = []
Expand Down

0 comments on commit d920750

Please sign in to comment.