def __init__ (self):
self.__uo = uo
self.__db = "owl"
- self.__StoreConn = None
- self.__StoreCursor = None
self.__displayTime = time.time()
self.__ensureStoreExists()
conn, cursor = self.__connect()
try:
cursor.execute("CREATE TABLE cm160data(ts timestamp, current float, UNIQUE (ts))")
- # Attempt to create table in case this is first time to run
- # the server; but thereafter we expect an exception because the
- # create will fail...
+ # Attempt to create table in case this is the first time we are launched.
+ # But thereafter we expect an exception because the create will fail if the table already exist
except MySQLdb.OperationalError as e:
-# print "Error %d: %s" % (e.args[0], e.args[1])
+ # Debug : # print "Error %d: %s" % (e.args[0], e.args[1])
if not(e.args[0] == 1050):
raise
conn.commit()
self.info("MySQL database OK: %s" % (self.__db) )
def store(self, cm160Data):
+ StoreConn = None
+ StoreCursor = None
"""Store the CM160 data in the MySQL database.
There may be duplicate timestamps in the CM160data.
If the data was added to the database return True, if
not added to the database return False"""
- #Create a connection if we don't already have one
- if self.__StoreConn == None:
- self.__StoreConn, self.__StoreCursor = self.__connect()
+ #Create a connection
+ StoreConn, StoreCursor = self.__connect()
stored=False
try:
- self.__StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),))
+ StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),))
+ StoreConn.commit()
+ StoreConn.close()
+
stored=True
except MySQLdb.Error as e:
# INSERT will fail this way if this is a duplicate
# record (as it commonly can be when receiving 30-day
# record from CM160 device!)
- #self.info("Duplicate record discarded: %s" % (cm160Data) )
+ self.info("Duplicate record discarded: %s" % (cm160Data) )
pass
else:
raise
- #Display data every few seconds to let user know download is
- #in progress
+ # Display data every few seconds to let user know download is in progress
if self.__displayTime < time.time():
self.info("stored record: %s" % cm160Data)
self.__displayTime=time.time()+2
- #We used to commit every record this was very slow when
- #downloading 30 days of data on slow (E.G raspberypi)
- #platforms. Now we only comit data as it is displayed.
- self.__StoreConn.commit()
return stored
sys.exit(0)
uo.setLogFile("cm160Server.log")
- uo.info("CM160Server v %2.2f" % (VERSION) )
+ uo.info("CM160Server v%2.2f" % (VERSION) )
uo.setDebug(debug)
# Démarrage du datastore :
-
DataStore = MySQLDataStore()
# Démarrage du collect data