From 2d1e3191609fede8fe2613502b022cbbe7c72027 Mon Sep 17 00:00:00 2001 From: Jonny Ervine Date: Sun, 16 Aug 2026 11:44:18 +0800 Subject: [PATCH] Make the MySQL table name configurable instead of hardcoding TempHumid. Co-authored-by: Cursor --- install.sh | 2 +- python_code/readMysql.py | 4 ++-- python_code/temp-humid-read-loop.py | 3 ++- python_code/temp-humid-read-single.py | 6 ++++-- python_code/temphumid.conf | 3 +++ python_code/thmonitor_common.py | 15 +++++++++++++++ python_code/updateMysql.py | 8 ++++---- 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/install.sh b/install.sh index 597889d..3fd8bbf 100755 --- a/install.sh +++ b/install.sh @@ -330,7 +330,7 @@ Installation complete. Next steps: 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 if [[ "$RUN_MODE" == "systemd" ]]; then diff --git a/python_code/readMysql.py b/python_code/readMysql.py index 7fc6a21..ff11be3 100644 --- a/python_code/readMysql.py +++ b/python_code/readMysql.py @@ -9,11 +9,11 @@ import MySQLdb 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 ...') sql = ( - 'SELECT Temperature, Humidity FROM TempHumid ' + f'SELECT Temperature, Humidity FROM `{table}` ' 'ORDER BY ComputerTime DESC LIMIT 1' ) diff --git a/python_code/temp-humid-read-loop.py b/python_code/temp-humid-read-loop.py index 9018ed5..7cd1bf6 100755 --- a/python_code/temp-humid-read-loop.py +++ b/python_code/temp-humid-read-loop.py @@ -53,7 +53,8 @@ def sensor_read(cfg, dev_type, dhtpin): if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits): updateMysql.main( 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'], ) oldtemp = t diff --git a/python_code/temp-humid-read-single.py b/python_code/temp-humid-read-single.py index 65bbe9a..9f26abb 100644 --- a/python_code/temp-humid-read-single.py +++ b/python_code/temp-humid-read-single.py @@ -13,7 +13,8 @@ import thmonitor_common as common def sensor_read(cfg, dev_type, dhtpin): """Perform a single sensor read using the last DB values for validation.""" 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.debug( @@ -57,7 +58,8 @@ def sensor_read(cfg, dev_type, dhtpin): if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits): updateMysql.main( 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'], ) return 0 diff --git a/python_code/temphumid.conf b/python_code/temphumid.conf index e84badc..6266bd3 100644 --- a/python_code/temphumid.conf +++ b/python_code/temphumid.conf @@ -48,6 +48,9 @@ host=localhost # The name of the database 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) username=USRENAME diff --git a/python_code/thmonitor_common.py b/python_code/thmonitor_common.py index ed10979..1229e36 100644 --- a/python_code/thmonitor_common.py +++ b/python_code/thmonitor_common.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- """Shared configuration, logging, and sensor validation for thMonitor scripts.""" +import re import sys import logging import configparser @@ -11,6 +12,19 @@ DHT22 = 22 AM2302 = 22 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): @@ -37,6 +51,7 @@ def load_config(path=DEFAULT_CONFIG): 'loglevel': config.get('software', 'loglevel'), 'host': config.get('database', 'host'), 'db': config.get('database', 'db'), + 'table': validate_table_name(config.get('database', 'table')), 'username': config.get('database', 'username'), 'password': config.get('database', 'password'), 'sql_retries': config.getint('database', 'sql_retries'), diff --git a/python_code/updateMysql.py b/python_code/updateMysql.py index 8ecf3b9..98fcd17 100644 --- a/python_code/updateMysql.py +++ b/python_code/updateMysql.py @@ -7,18 +7,18 @@ import time 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('Updating MySQL database next ...') sql = ( - 'INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) ' + f'INSERT INTO `{table}` (ComputerTime, Temperature, Humidity, id) ' 'VALUES (unix_timestamp(now()), %s, %s, NULL)' ) args = (format(temp, '5.1f'), format(humid, '5.1f')) logging.info( - 'Temperature: %s*C Humidity: %s%% ... updating to MySQL database', - args[0], args[1], + 'Temperature: %s*C Humidity: %s%% ... updating MySQL table %s', + args[0], args[1], table, ) for attempt in range(sql_retries):