Make the MySQL table name configurable instead of hardcoding TempHumid.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-16 11:44:18 +08:00
co-authored by Cursor
parent 8010cd9045
commit 2d1e319160
7 changed files with 31 additions and 10 deletions
+2 -2
View File
@@ -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'
)
+2 -1
View File
@@ -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
+4 -2
View File
@@ -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
+3
View File
@@ -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
+15
View File
@@ -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'),
+4 -4
View File
@@ -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):