Skip to content

Commit

Permalink
switch to logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseklm committed Jul 10, 2024
1 parent 0e97958 commit fa1494e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import signal
import sys
from time import sleep, time
Expand All @@ -18,7 +19,7 @@ def convert_to_type(value: int, datatype: str) -> int:
elif datatype == 'int32':
value = value.to_bytes(length=4, byteorder='big', signed=False)
return int.from_bytes(value, byteorder='big', signed=True)
print('unknown datatype', datatype, value)
logging.warning(f'unknown datatype {datatype} {value}.')


class SungrowModbus2Mqtt:
Expand Down Expand Up @@ -149,5 +150,7 @@ def publish(self):


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info('starting SungrowModbus2Mqtt.')
app = SungrowModbus2Mqtt()
app.loop()
9 changes: 5 additions & 4 deletions modbus_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from time import sleep

from pymodbus.exceptions import ConnectionException
Expand All @@ -10,14 +11,14 @@ class ModbusHandler:
def __init__(self):
self.modbus_client = SungrowModbusTcpClient(host=config['ip'], port=config['port'], timeout=10, retries=1)
if self.modbus_client.connect():
print('modbus connected.')
logging.info('modbus connected.')

def reconnect(self):
while True:
try:
connected = self.modbus_client.connect()
except (ConnectionResetError, ConnectionException) as e:
print(e)
logging.error(f'connect failed: {e}.')
connected = False
if connected:
break
Expand All @@ -33,11 +34,11 @@ def read(self, table, address, count):
else:
raise Exception('Invalid table')
except ConnectionResetError as e:
print(e)
logging.error(f'read failed: {e}.')
self.reconnect()
continue
return result.registers

def close(self):
self.modbus_client.close()
print('modbus closed.')
logging.info('modbus closed.')
5 changes: 3 additions & 2 deletions mqtt_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import queue
import threading
from time import sleep
Expand All @@ -23,7 +24,7 @@ def __init__(self):

def mqtt_on_connect(self, mqttc, userdata, flags, reason_code, properties):
self.mqttc.publish(config['mqtt_topic'] + 'available', 'online', retain=True)
print('mqtt connected.')
logging.info('mqtt connected.')

def publish(self, topic, payload, retain=False):
self.publishing_queue.put({
Expand All @@ -39,5 +40,5 @@ def publishing_handler(self):
sleep(1)
result = self.mqttc.publish(message['topic'], message['payload'], retain=message['retain'])
if result.rc != mqtt.MQTT_ERR_SUCCESS:
print(message, result)
logging.error(f'publish failed: {message} {result}.')
self.publishing_queue.task_done()

0 comments on commit fa1494e

Please sign in to comment.