Skip to content

Commit

Permalink
use python logging in MQTT client and log it when connection is estab…
Browse files Browse the repository at this point in the history
…lished
  • Loading branch information
yankee42 committed Sep 26, 2022
1 parent 5abe9f6 commit cc5a7a8
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions runs/mqttsub.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import configparser
import fileinput
import logging
import re
import subprocess
import sys
import threading
import time
from datetime import datetime
from json import loads as json_loads
from json.decoder import JSONDecodeError
from pathlib import Path
Expand All @@ -24,6 +24,9 @@
lock=threading.Lock()
RAMDISK_PATH = Path(__file__).resolve().parents[1] / "ramdisk"

logging.basicConfig(filename=str(RAMDISK_PATH / "mqtt.log"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
log = logging.getLogger("MQTT")

for i in range(1,(numberOfSupportedDevices+1)):
try:
confvar = config.get('smarthomedevices', 'device_configured_' + str(i))
Expand Down Expand Up @@ -86,8 +89,7 @@ def getserial():

# connect to broker and subscribe to set topics
def on_connect(client, userdata, flags, rc):
#subscribe to all set topics
#client.subscribe("openWB/#", 2)
log.info("Connected")
client.subscribe("openWB/set/#", 2)
client.subscribe("openWB/config/set/#", 2)

Expand All @@ -99,11 +101,7 @@ def on_message(client, userdata, msg):
lock.acquire()
try:
setTopicCleared = False
theTime = datetime.now()
timestamp = theTime.strftime(format = "%Y-%m-%d %H:%M:%S")
file = open('/var/www/html/openWB/ramdisk/mqtt.log', 'a')
file.write( "%s Topic: %s Message: %s\n" % (timestamp, msg.topic, str(msg.payload.decode("utf-8"))) )
file.close()
log.debug("Topic: %s, Message: %s", msg.topic, msg.payload.decode("utf-8"))

if (( "openWB/set/lp" in msg.topic) and ("ChargePointEnabled" in msg.topic)):
devicenumb=re.sub(r'\D', '', msg.topic)
Expand Down

0 comments on commit cc5a7a8

Please sign in to comment.