Make the MySQL table name configurable instead of hardcoding TempHumid.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+1
-1
@@ -330,7 +330,7 @@ Installation complete.
|
|||||||
|
|
||||||
Next steps:
|
Next steps:
|
||||||
1. Edit ${CONFIG_FILE} (GPIO pin, MySQL credentials, limits).
|
1. Edit ${CONFIG_FILE} (GPIO pin, MySQL credentials, limits).
|
||||||
2. Ensure the TempHumid table exists in your MySQL database.
|
2. Ensure the configured table (default: bedRoomTempHumid) exists in your MySQL database.
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
if [[ "$RUN_MODE" == "systemd" ]]; then
|
if [[ "$RUN_MODE" == "systemd" ]]; then
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ import MySQLdb
|
|||||||
NO_PREVIOUS = ('NULL', 'NULL')
|
NO_PREVIOUS = ('NULL', 'NULL')
|
||||||
|
|
||||||
|
|
||||||
def main(host, db, user, passwd, logging, sql_retries, sql_timeout):
|
def main(host, db, table, user, passwd, logging, sql_retries, sql_timeout):
|
||||||
logging.debug('Reading MySQL database next ...')
|
logging.debug('Reading MySQL database next ...')
|
||||||
|
|
||||||
sql = (
|
sql = (
|
||||||
'SELECT Temperature, Humidity FROM TempHumid '
|
f'SELECT Temperature, Humidity FROM `{table}` '
|
||||||
'ORDER BY ComputerTime DESC LIMIT 1'
|
'ORDER BY ComputerTime DESC LIMIT 1'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ def sensor_read(cfg, dev_type, dhtpin):
|
|||||||
if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits):
|
if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits):
|
||||||
updateMysql.main(
|
updateMysql.main(
|
||||||
t, h,
|
t, h,
|
||||||
cfg['host'], cfg['db'], cfg['username'], cfg['password'],
|
cfg['host'], cfg['db'], cfg['table'],
|
||||||
|
cfg['username'], cfg['password'],
|
||||||
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
||||||
)
|
)
|
||||||
oldtemp = t
|
oldtemp = t
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import thmonitor_common as common
|
|||||||
def sensor_read(cfg, dev_type, dhtpin):
|
def sensor_read(cfg, dev_type, dhtpin):
|
||||||
"""Perform a single sensor read using the last DB values for validation."""
|
"""Perform a single sensor read using the last DB values for validation."""
|
||||||
oldtemp, oldhumid = readMysql.main(
|
oldtemp, oldhumid = readMysql.main(
|
||||||
cfg['host'], cfg['db'], cfg['username'], cfg['password'],
|
cfg['host'], cfg['db'], cfg['table'],
|
||||||
|
cfg['username'], cfg['password'],
|
||||||
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
||||||
)
|
)
|
||||||
logging.debug(
|
logging.debug(
|
||||||
@@ -57,7 +58,8 @@ def sensor_read(cfg, dev_type, dhtpin):
|
|||||||
if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits):
|
if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits):
|
||||||
updateMysql.main(
|
updateMysql.main(
|
||||||
t, h,
|
t, h,
|
||||||
cfg['host'], cfg['db'], cfg['username'], cfg['password'],
|
cfg['host'], cfg['db'], cfg['table'],
|
||||||
|
cfg['username'], cfg['password'],
|
||||||
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
||||||
)
|
)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ host=localhost
|
|||||||
# The name of the database
|
# The name of the database
|
||||||
db=Monitoring
|
db=Monitoring
|
||||||
|
|
||||||
|
# The table used for temperature/humidity readings (INSERT and SELECT)
|
||||||
|
table=bedRoomTempHumid
|
||||||
|
|
||||||
# The username to us to connect to MySQL (should have INSERT rights on the database)
|
# The username to us to connect to MySQL (should have INSERT rights on the database)
|
||||||
username=USRENAME
|
username=USRENAME
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Shared configuration, logging, and sensor validation for thMonitor scripts."""
|
"""Shared configuration, logging, and sensor validation for thMonitor scripts."""
|
||||||
|
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import configparser
|
import configparser
|
||||||
@@ -11,6 +12,19 @@ DHT22 = 22
|
|||||||
AM2302 = 22
|
AM2302 = 22
|
||||||
|
|
||||||
DEFAULT_CONFIG = '/etc/thMonitor.conf'
|
DEFAULT_CONFIG = '/etc/thMonitor.conf'
|
||||||
|
_TABLE_NAME_RE = re.compile(r'^[A-Za-z_][A-Za-z0-9_]*$')
|
||||||
|
|
||||||
|
|
||||||
|
def validate_table_name(name):
|
||||||
|
"""Return table name if it is a safe SQL identifier, else exit."""
|
||||||
|
if not _TABLE_NAME_RE.match(name):
|
||||||
|
print(
|
||||||
|
f'ERROR: invalid database table name {name!r} '
|
||||||
|
'(use letters, digits, and underscores only)',
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
def load_config(path=DEFAULT_CONFIG):
|
def load_config(path=DEFAULT_CONFIG):
|
||||||
@@ -37,6 +51,7 @@ def load_config(path=DEFAULT_CONFIG):
|
|||||||
'loglevel': config.get('software', 'loglevel'),
|
'loglevel': config.get('software', 'loglevel'),
|
||||||
'host': config.get('database', 'host'),
|
'host': config.get('database', 'host'),
|
||||||
'db': config.get('database', 'db'),
|
'db': config.get('database', 'db'),
|
||||||
|
'table': validate_table_name(config.get('database', 'table')),
|
||||||
'username': config.get('database', 'username'),
|
'username': config.get('database', 'username'),
|
||||||
'password': config.get('database', 'password'),
|
'password': config.get('database', 'password'),
|
||||||
'sql_retries': config.getint('database', 'sql_retries'),
|
'sql_retries': config.getint('database', 'sql_retries'),
|
||||||
|
|||||||
@@ -7,18 +7,18 @@ import time
|
|||||||
import MySQLdb
|
import MySQLdb
|
||||||
|
|
||||||
|
|
||||||
def main(temp, humid, host, db, user, passwd, logging, sql_retries, sql_timeout):
|
def main(temp, humid, host, db, table, user, passwd, logging, sql_retries, sql_timeout):
|
||||||
logging.debug('Temp = %s *C, Hum = %s %%', temp, humid)
|
logging.debug('Temp = %s *C, Hum = %s %%', temp, humid)
|
||||||
logging.debug('Updating MySQL database next ...')
|
logging.debug('Updating MySQL database next ...')
|
||||||
|
|
||||||
sql = (
|
sql = (
|
||||||
'INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) '
|
f'INSERT INTO `{table}` (ComputerTime, Temperature, Humidity, id) '
|
||||||
'VALUES (unix_timestamp(now()), %s, %s, NULL)'
|
'VALUES (unix_timestamp(now()), %s, %s, NULL)'
|
||||||
)
|
)
|
||||||
args = (format(temp, '5.1f'), format(humid, '5.1f'))
|
args = (format(temp, '5.1f'), format(humid, '5.1f'))
|
||||||
logging.info(
|
logging.info(
|
||||||
'Temperature: %s*C Humidity: %s%% ... updating to MySQL database',
|
'Temperature: %s*C Humidity: %s%% ... updating MySQL table %s',
|
||||||
args[0], args[1],
|
args[0], args[1], table,
|
||||||
)
|
)
|
||||||
|
|
||||||
for attempt in range(sql_retries):
|
for attempt in range(sql_retries):
|
||||||
|
|||||||
Reference in New Issue
Block a user