Skip to content

Commit

Permalink
Minor change for easy log truncate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pduck27 committed Nov 29, 2020
1 parent 4f2f0c8 commit 6238b6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Up to now I could test it with the following hardware components:
But as long as I see it will work with all other devices of the same family in the same way. Please check the mappingfile for the *knowndevices* which should work.

# Latest release notes
- Simple cut off log file after one day
- The heating control integration has place for improvement like the handling with units of measure.
- The code itself is a little bit blown now. It needs some re-design.
- Periodical status updates via MQTT are integrated.
- Heating control as a new type is integrated.
- Heating control as a new type is integrated.

19 changes: 18 additions & 1 deletion hp2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ def on_set_message(client, userdata, message):
device_mapping = {}
log_file_name = "log/hp2mqtt.log"
dev_file_name = "data/device_info.json"
client = mqttClient.Client()
client = mqttClient.Client()
logfile_next_check_date = datetime.datetime.utcnow() + datetime.timedelta(days=1)
logfile_countdown_default = 14400;
logfile_check_countdown = logfile_countdown_default

mqtt_connected = False
mqtt_broker_address= "localhost"
Expand Down Expand Up @@ -407,10 +410,24 @@ def on_set_message(client, userdata, message):
try:
while True:
time.sleep(1)

# main routine
mqtt_update_countdown -= 1
if mqtt_update_countdown <= 0:
try_deviceUpdate()
mqtt_update_countdown = mqtt_update_sec

# logfile check
logfile_check_countdown -= 1
if logfile_check_countdown <= 0:
log_message("Check logfile age. Now = %s --> next truncate %s" % (datetime.datetime.utcnow(), logfile_next_check_date))
logfile_check_countdown = logfile_countdown_default
if (datetime.datetime.utcnow() > logfile_next_check_date):
logfile_next_check_date = datetime.datetime.utcnow() + datetime.timedelta(days=1)
log_file.close
log_file = open(log_file_name, "w")
log_message("Truncate logfile now: %s" % (datetime.datetime.utcnow()))


except KeyboardInterrupt:
log_message("Exiting")
Expand Down

0 comments on commit 6238b6c

Please sign in to comment.