Merge pull request #57 from phylax2020/master

log_file_controller.py and __init__.py in ConfigUpdate reloaded
This commit is contained in:
Alexander Vollkopf 2022-07-19 21:53:33 +02:00 committed by GitHub
commit 205389f246
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View file

@ -31,8 +31,8 @@ class LogController:
self.influxdb = self.cbpi.config.get("INFLUXDB", "No") self.influxdb = self.cbpi.config.get("INFLUXDB", "No")
if self.logfiles == "Yes": if self.logfiles == "Yes":
if name not in self.datalogger: if name not in self.datalogger:
max_bytes = self.cbpi.config.get("SENSOR_LOG_MAX_BYTES", 1048576) max_bytes = int(self.cbpi.config.get("SENSOR_LOG_MAX_BYTES", 100000))
backup_count = self.cbpi.config.get("SENSOR_LOG_BACKUP_COUNT", 3) backup_count = int(self.cbpi.config.get("SENSOR_LOG_BACKUP_COUNT", 3))
data_logger = logging.getLogger('cbpi.sensor.%s' % name) data_logger = logging.getLogger('cbpi.sensor.%s' % name)
data_logger.propagate = False data_logger.propagate = False
@ -120,10 +120,10 @@ class LogController:
df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', name], header=None) for f in all_filenames]) df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', name], header=None) for f in all_filenames])
logging.info("Read all files for {}".format(names)) logging.info("Read all files for {}".format(names))
# resample if rate provided # resample if rate provided
# if sample_rate is not None: if sample_rate is not None:
# df = df[name].resample(sample_rate).max() df = df[name].resample(sample_rate).max()
# logging.info("Sampled now for {}".format(names)) logging.info("Sampled now for {}".format(names))
df = df[name].dropna() df = df.dropna()
# take every nth row so that total number of rows does not exceed max_rows * 2 # take every nth row so that total number of rows does not exceed max_rows * 2
max_rows = 500 max_rows = 500
total_rows = df.shape[0] total_rows = df.shape[0]
@ -138,6 +138,7 @@ class LogController:
result = pd.merge(result, df, how='outer', left_index=True, right_index=True) result = pd.merge(result, df, how='outer', left_index=True, right_index=True)
data = {"time": df.index.tolist()} data = {"time": df.index.tolist()}
if len(names) > 1: if len(names) > 1:
for name in names: for name in names:
data[name] = result[name].interpolate(limit_direction='both', limit=10).tolist() data[name] = result[name].interpolate(limit_direction='both', limit=10).tolist()
@ -158,6 +159,8 @@ class LogController:
# concat all logs # concat all logs
all_filenames = glob.glob('./logs/sensor_%s.log*' % id) all_filenames = glob.glob('./logs/sensor_%s.log*' % id)
df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', 'Values'], header=None) for f in all_filenames]) df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', 'Values'], header=None) for f in all_filenames])
df = df.resample('60s').max()
df = df.dropna()
result[id] = {"time": df.index.astype(str).tolist(), "value":df.Values.tolist()} result[id] = {"time": df.index.astype(str).tolist(), "value":df.Values.tolist()}
return result return result

View file

@ -47,7 +47,8 @@ class ConfigUpdate(CBPiExtension):
influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", None) influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", None)
mqttupdate = self.cbpi.config.get("MQTTUpdate", None) mqttupdate = self.cbpi.config.get("MQTTUpdate", None)
PRESSURE_UNIT = self.cbpi.config.get("PRESSURE_UNIT", None) PRESSURE_UNIT = self.cbpi.config.get("PRESSURE_UNIT", None)
SENSOR_LOG_BACKUP_COUNT = self.cbpi.config.get("SENSOR_LOG_BACKUP_COUNT", None)
SENSOR_LOG_MAX_BYTES = self.cbpi.config.get("SENSOR_LOG_MAX_BYTES", None)
if boil_temp is None: if boil_temp is None:
logger.info("INIT Boil Temp Setting") logger.info("INIT Boil Temp Setting")
@ -285,6 +286,23 @@ class ConfigUpdate(CBPiExtension):
{"label": "PSI", "value": "PSI"}]) {"label": "PSI", "value": "PSI"}])
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')
# check if SENSOR_LOG_BACKUP_COUNT exists in config
if SENSOR_LOG_BACKUP_COUNT is None:
logger.info("INIT SENSOR_LOG_BACKUP_COUNT")
try:
await self.cbpi.config.add("SENSOR_LOG_BACKUP_COUNT", 3, ConfigType.NUMBER, "Max. number of backup logs")
except:
logger.warning('Unable to update database')
# check if SENSOR_LOG_MAX_BYTES exists in config
if SENSOR_LOG_MAX_BYTES is None:
logger.info("Init maximum size of sensor logfiles")
try:
await self.cbpi.config.add("SENSOR_LOG_MAX_BYTES", 100000, ConfigType.NUMBER, "Max. number of bytes in sensor logfiles")
except:
logger.warning('Unable to update database')
def setup(cbpi): def setup(cbpi):
cbpi.plugin.register("ConfigUpdate", ConfigUpdate) cbpi.plugin.register("ConfigUpdate", ConfigUpdate)