diff --git a/python_code/temp-humid-read-loop.py b/python_code/temp-humid-read-loop.py old mode 100644 new mode 100755 index d46d026..470f44f --- a/python_code/temp-humid-read-loop.py +++ b/python_code/temp-humid-read-loop.py @@ -56,7 +56,7 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid) if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid): logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid)) - updateMysql.main(t, h, host, db, username, password, logging) + updateMysql.main(t, h, host, db, username, password, logging, sql_retries, sql_timeout) oldtemp=t oldhumid=h break @@ -75,7 +75,7 @@ DHT22 = 22 AM2302 = 22 config = ConfigParser.ConfigParser() -config.read('temphumid.conf') +config.read('/etc/thMonitor.conf') hwtype=config.get('hardware', 'DHT') pin=config.get('hardware', 'PIN') retries=int(config.get('software', 'retries')) @@ -93,6 +93,8 @@ host=(config.get('database', 'host')) db=(config.get('database', 'db')) username=(config.get('database', 'username')) password=(config.get('database', 'password')) +sql_retries=int((config.get('database', 'sql_retries'))) +sql_timeout=(config.get('database', 'sql_timeout')) if loglevel == "debug": diff --git a/python_code/temp-humid-read-single.py b/python_code/temp-humid-read-single.py index dce9576..ff884db 100644 --- a/python_code/temp-humid-read-single.py +++ b/python_code/temp-humid-read-single.py @@ -34,7 +34,7 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid) if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid): logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid)) - updateMysql.main(t, h, host, db, username, password, logging) + updateMysql.main(t, h, host, db, username, password, logging, sql_retries, sql_timeout) oldtemp=t oldhumid=h break @@ -67,6 +67,8 @@ host=(config.get('database', 'host')) db=(config.get('database', 'db')) username=(config.get('database', 'username')) password=(config.get('database', 'password')) +sql_retries=int((config.get('database', 'sql_retries'))) +sql_timeout=(config.get('database', 'sql_timeout')) if loglevel == "debug": diff --git a/python_code/temphumid.conf b/python_code/temphumid.conf index 82cfb63..e84badc 100644 --- a/python_code/temphumid.conf +++ b/python_code/temphumid.conf @@ -53,3 +53,9 @@ username=USRENAME # The password of the username above password=PASSWORD + +# This is the maximum number of retries made to update the MySQL database with the data. If these all fail, the failure is recorded, but the daemon should continue to run +sql_retries=3 + +# This is the timeout between retries when updating the MySQL database. It is useful to set this parameter and sql_retries parameter, as the thMonitor daemon frequently starts up before the MySQL daemon has finished starting. +sql_timeout=5 diff --git a/python_code/updateMysql.py b/python_code/updateMysql.py index 3cc9db4..f7d2be8 100644 --- a/python_code/updateMysql.py +++ b/python_code/updateMysql.py @@ -5,7 +5,7 @@ import sys import MySQLdb -def main(temp, humid, host, db, user, passwd, logging): +def main(temp, humid, host, db, user, passwd, logging, sql_retries, sql_timeout): logging.debug('Temp = {0} *C, Hum = {1} %'.format(temp, humid)) logging.debug('Updating MySQL database next ...') connection = MySQLdb.connect(host,user,passwd,db) @@ -15,7 +15,20 @@ def main(temp, humid, host, db, user, passwd, logging): logging.info('Temperature: {0}*C Humidity: {1}% ... updating to MySQL database'.format(sqltemp, sqlhumid)) sql = """INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) VALUES (unix_timestamp(now()), %s, %s, NULL)""" args = (sqltemp, sqlhumid) - cursor.execute(sql, args) - connection.commit() - cursor.close() + + success = False + attempts = 0 + while attempts < sql_retries and not success: + try: + cursor.execute(sql, args) + connection.commit() + cursor.close() + success = True + logging.debug('The MySQL database was successfully updated.') + except MySQLdb.Error, e: + logging.warn('The MySQL database could not be updated and returned the following error %d: %s', (e.args[0], e.args[1])) + attempts += 1 + if attempts == sql_retries: + logging.error('All configured attempts to update the MySQL database have failed. We are going to skip this attempt.') + time.sleep(sql_interval) return 0