Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
* Added: Set logging level in `config.default.ini`
* Changed: Logging levels of different messages for clearer output
* Changed: Optimized log output for faster troubleshooting
  • Loading branch information
mr-manuel committed Mar 6, 2023
1 parent c20a0e1 commit 950eab3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 52 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

## v0.0.2
* Added: Set logging level in `config.default.ini`
* Changed: Logging levels of different messages for clearer output
* Changed: Optimized log output for faster troubleshooting

## v0.0.1
Initial release
17 changes: 17 additions & 0 deletions dbus-mqtt-grid/config.sample.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
; CONFIG FILE
; GitHub reporitory: https://github.com/mr-manuel/venus-os_dbus-mqtt-grid
; remove semicolon ; to enable desired setting

[DEFAULT]
; Set logging level
; ERROR = shows errors only
; WARNING = shows ERROR and warnings
; INFO = shows WARNING and running functions
; DEBUG = shows INFO and data/values
; default: WARNING
logging = WARNING

; used when no voltage is received
voltage = 230

Expand All @@ -13,12 +25,16 @@ broker_address = IP_ADDR_OR_FQDN
broker_port = 1883

; Enables TLS
; 0 = Disabled
; 1 = Enabled
;tls_enabled = 1

; Absolute path to the Certificate Authority certificate file that is to be treated as trusted by this client
;tls_path_to_ca = /data/keys/mosquitto.crt

; Disables verification of the server hostname in the server certificate
; 0 = Disabled
; 1 = Enabled
;tls_insecure = 1

; Username used for connection
Expand All @@ -28,4 +44,5 @@ broker_port = 1883
;password = mypassword

; Topic where the meters data as JSON string is published
; minimum required JSON payload: {"grid": { "power": 0.0 } }
topic_meters = enphase/envoy-s/meters
137 changes: 85 additions & 52 deletions dbus-mqtt-grid/dbus-mqtt-grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,39 @@
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'ext', 'velib_python'))
from vedbus import VeDbusService

# use WARNING for default, INFO for displaying actual steps and values, DEBUG for debugging
logging.basicConfig(level=logging.WARNING)

# get values from config.ini file
try:
config = configparser.ConfigParser()
config.read("%s/config.ini" % (os.path.dirname(os.path.realpath(__file__))))
if (config['MQTT']['broker_address'] == "IP_ADDR_OR_FQDN"):
logging.error("config.ini file using invalid default values.")
raise
print("ERROR:config.ini file is using invalid default values like IP_ADDR_OR_FQDN. The driver restarts in 60 seconds.")
time.sleep(60)
sys.exit()
except:
logging.error("config.ini file not found. Copy or rename the config.sample.ini to config.ini")
print("ERROR:config.ini file not found. Copy or rename the config.sample.ini to config.ini. The driver restarts in 60 seconds.")
time.sleep(60)
sys.exit()


# Get logging level from config.ini
# ERROR = shows errors only
# WARNING = shows ERROR and warnings
# INFO = shows WARNING and running functions
# DEBUG = shows INFO and data/values
if 'DEFAULT' in config and 'logging' in config['DEFAULT']:
if config['DEFAULT']['logging'] == 'DEBUG':
logging.basicConfig(level=logging.DEBUG)
elif config['DEFAULT']['logging'] == 'INFO':
logging.basicConfig(level=logging.INFO)
elif config['DEFAULT']['logging'] == 'ERROR':
logging.basicConfig(level=logging.ERROR)
else:
logging.basicConfig(level=logging.WARNING)
else:
logging.basicConfig(level=logging.WARNING)


# set variables
connected = 0

Expand Down Expand Up @@ -62,12 +81,12 @@ def on_disconnect(client, userdata, rc):
global connected
logging.warning("MQTT client: Got disconnected")
if rc != 0:
logging.debug('MQTT client: Unexpected MQTT disconnection. Will auto-reconnect')
logging.warning('MQTT client: Unexpected MQTT disconnection. Will auto-reconnect')
else:
logging.debug('MQTT client: rc value:' + str(rc))
logging.warning('MQTT client: rc value:' + str(rc))

try:
logging.info("MQTT client: Trying to reconnect")
logging.warning("MQTT client: Trying to reconnect")
client.connect(config['MQTT']['broker_address'])
connected = 1
except Exception as e:
Expand All @@ -91,47 +110,61 @@ def on_message(client, userdata, msg):
grid_L1_power, grid_L1_current, grid_L1_voltage, grid_L1_forward, grid_L1_reverse, \
grid_L2_power, grid_L2_current, grid_L2_voltage, grid_L2_forward, grid_L2_reverse, \
grid_L3_power, grid_L3_current, grid_L3_voltage, grid_L3_forward, grid_L3_reverse

# get JSON from topic
if msg.topic == config['MQTT']['topic_meters']:
if msg.payload != '{"value": null}' and msg.payload != b'{"value": null}':
if msg.payload != '' and msg.payload != b'':
jsonpayload = json.loads(msg.payload)
grid_power = float(jsonpayload['grid']['power'])
grid_current = float(jsonpayload['grid']['current']) if 'current' in jsonpayload['grid'] else grid_power/float(config['DEFAULT']['voltage'])
grid_voltage = float(jsonpayload['grid']['voltage']) if 'voltage' in jsonpayload['grid'] else float(config['DEFAULT']['voltage'])
grid_forward = float(jsonpayload['grid']['energy_forward']) if 'energy_forward' in jsonpayload['grid'] else 0
grid_reverse = float(jsonpayload['grid']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid'] else 0

# check if L1 and L1 -> power exists
if 'L1' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L1']:
grid_L1_power = float(jsonpayload['grid']['L1']['power'])
grid_L1_current = float(jsonpayload['grid']['L1']['current']) if 'current' in jsonpayload['grid']['L1'] else grid_L1_power/float(config['DEFAULT']['voltage'])
grid_L1_voltage = float(jsonpayload['grid']['L1']['voltage']) if 'voltage' in jsonpayload['grid']['L1'] else float(config['DEFAULT']['voltage'])
grid_L1_forward = float(jsonpayload['grid']['L1']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L1'] else 0
grid_L1_reverse = float(jsonpayload['grid']['L1']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L1'] else 0

# check if L2 and L2 -> power exists
if 'L2' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L2']:
grid_L2_power = float(jsonpayload['grid']['L2']['power'])
grid_L2_current = float(jsonpayload['grid']['L2']['current']) if 'current' in jsonpayload['grid']['L2'] else grid_L2_power/float(config['DEFAULT']['voltage'])
grid_L2_voltage = float(jsonpayload['grid']['L2']['voltage']) if 'voltage' in jsonpayload['grid']['L2'] else float(config['DEFAULT']['voltage'])
grid_L2_forward = float(jsonpayload['grid']['L2']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L2'] else 0
grid_L2_reverse = float(jsonpayload['grid']['L2']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L2'] else 0

# check if L3 and L3 -> power exists
if 'L3' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L3']:
grid_L3_power = float(jsonpayload['grid']['L3']['power'])
grid_L3_current = float(jsonpayload['grid']['L3']['current']) if 'current' in jsonpayload['grid']['L3'] else grid_L3_power/float(config['DEFAULT']['voltage'])
grid_L3_voltage = float(jsonpayload['grid']['L3']['voltage']) if 'voltage' in jsonpayload['grid']['L3'] else float(config['DEFAULT']['voltage'])
grid_L3_forward = float(jsonpayload['grid']['L3']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L3'] else 0
grid_L3_reverse = float(jsonpayload['grid']['L3']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L3'] else 0

if 'grid' in jsonpayload:
if type(jsonpayload['grid']) == dict and 'power' in jsonpayload['grid']:
grid_power = float(jsonpayload['grid']['power'])
grid_current = float(jsonpayload['grid']['current']) if 'current' in jsonpayload['grid'] else grid_power/float(config['DEFAULT']['voltage'])
grid_voltage = float(jsonpayload['grid']['voltage']) if 'voltage' in jsonpayload['grid'] else float(config['DEFAULT']['voltage'])
grid_forward = float(jsonpayload['grid']['energy_forward']) if 'energy_forward' in jsonpayload['grid'] else 0
grid_reverse = float(jsonpayload['grid']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid'] else 0

# check if L1 and L1 -> power exists
if 'L1' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L1']:
grid_L1_power = float(jsonpayload['grid']['L1']['power'])
grid_L1_current = float(jsonpayload['grid']['L1']['current']) if 'current' in jsonpayload['grid']['L1'] else grid_L1_power/float(config['DEFAULT']['voltage'])
grid_L1_voltage = float(jsonpayload['grid']['L1']['voltage']) if 'voltage' in jsonpayload['grid']['L1'] else float(config['DEFAULT']['voltage'])
grid_L1_forward = float(jsonpayload['grid']['L1']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L1'] else 0
grid_L1_reverse = float(jsonpayload['grid']['L1']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L1'] else 0

# check if L2 and L2 -> power exists
if 'L2' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L2']:
grid_L2_power = float(jsonpayload['grid']['L2']['power'])
grid_L2_current = float(jsonpayload['grid']['L2']['current']) if 'current' in jsonpayload['grid']['L2'] else grid_L2_power/float(config['DEFAULT']['voltage'])
grid_L2_voltage = float(jsonpayload['grid']['L2']['voltage']) if 'voltage' in jsonpayload['grid']['L2'] else float(config['DEFAULT']['voltage'])
grid_L2_forward = float(jsonpayload['grid']['L2']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L2'] else 0
grid_L2_reverse = float(jsonpayload['grid']['L2']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L2'] else 0

# check if L3 and L3 -> power exists
if 'L3' in jsonpayload['grid'] and 'power' in jsonpayload['grid']['L3']:
grid_L3_power = float(jsonpayload['grid']['L3']['power'])
grid_L3_current = float(jsonpayload['grid']['L3']['current']) if 'current' in jsonpayload['grid']['L3'] else grid_L3_power/float(config['DEFAULT']['voltage'])
grid_L3_voltage = float(jsonpayload['grid']['L3']['voltage']) if 'voltage' in jsonpayload['grid']['L3'] else float(config['DEFAULT']['voltage'])
grid_L3_forward = float(jsonpayload['grid']['L3']['energy_forward']) if 'energy_forward' in jsonpayload['grid']['L3'] else 0
grid_L3_reverse = float(jsonpayload['grid']['L3']['energy_reverse']) if 'energy_reverse' in jsonpayload['grid']['L3'] else 0
else:
logging.error("Received JSON MQTT message does not include a power object in the grid object. Expected at least: {\"grid\": {\"power\": 0.0}\"}")
logging.debug("MQTT payload: " + str(msg.payload)[1:])
else:
logging.error("Received JSON MQTT message does not include a grid object. Expected at least: {\"grid\": {\"power\": 0.0}\"}")
logging.debug("MQTT payload: " + str(msg.payload)[1:])

else:
logging.info("Answer from MQTT was NULL and therefore it was ignored")
logging.warning("Received JSON MQTT message was empty and therefore it was ignored")
logging.debug("MQTT payload: " + str(msg.payload)[1:])

except ValueError as e:
logging.error("Received message is not a valid JSON. %s" % e)
logging.debug("MQTT payload: " + str(msg.payload)[1:])

except Exception as e:
logging.error("The programm MQTTtoMeter is crashed. (on message function)")
print(e)
print("In the MQTTtoMeter programm something went wrong during the reading of the messages")
logging.error("Exception occurred: %s" % e)
logging.debug("MQTT payload: " + str(msg.payload)[1:])



Expand Down Expand Up @@ -160,8 +193,8 @@ def __init__(
self._dbusservice.add_path('/ProductId', 0xFFFF)
self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/CustomName', productname)
self._dbusservice.add_path('/FirmwareVersion', '0.0.1')
self._dbusservice.add_path('/HardwareVersion', '0.0.1')
self._dbusservice.add_path('/FirmwareVersion', '0.0.2')
#self._dbusservice.add_path('/HardwareVersion', '')
self._dbusservice.add_path('/Connected', 1)

self._dbusservice.add_path('/Latency', None)
Expand Down Expand Up @@ -208,13 +241,13 @@ def _update(self):
self._dbusservice['/Ac/L3/Energy/Forward'] = round(grid_L3_forward, 2)
self._dbusservice['/Ac/L3/Energy/Reverse'] = round(grid_L3_reverse, 2)

logging.info("Grid: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_power, grid_voltage, grid_current))
logging.debug("Grid: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_power, grid_voltage, grid_current))
if grid_L1_power:
logging.info("|- L1: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L1_power, grid_L1_voltage, grid_L1_current))
logging.debug("|- L1: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L1_power, grid_L1_voltage, grid_L1_current))
if grid_L2_power:
logging.info("|- L2: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L2_power, grid_L2_voltage, grid_L2_current))
logging.debug("|- L2: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L2_power, grid_L2_voltage, grid_L2_current))
if grid_L3_power:
logging.info("|- L3: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L3_power, grid_L3_voltage, grid_L3_current))
logging.debug("|- L3: {:.1f} W - {:.1f} V - {:.1f} A".format(grid_L3_power, grid_L3_voltage, grid_L3_current))


# increment UpdateIndex - to show that new data is available
Expand Down Expand Up @@ -246,21 +279,21 @@ def main():

# check tls and use settings, if provided
if 'tls_enabled' in config['MQTT'] and config['MQTT']['tls_enabled'] == '1':
logging.debug("MQTT client: TLS is enabled")
logging.info("MQTT client: TLS is enabled")

if 'tls_path_to_ca' in config['MQTT'] and config['MQTT']['tls_path_to_ca'] != '':
logging.debug("MQTT client: TLS: custom ca \"%s\" used" % config['MQTT']['tls_path_to_ca'])
logging.info("MQTT client: TLS: custom ca \"%s\" used" % config['MQTT']['tls_path_to_ca'])
client.tls_set(config['MQTT']['tls_path_to_ca'], tls_version=2)
else:
client.tls_set(tls_version=2)

if 'tls_insecure' in config['MQTT'] and config['MQTT']['tls_insecure'] != '':
logging.debug("MQTT client: TLS certificate server hostname verification disabled")
logging.info("MQTT client: TLS certificate server hostname verification disabled")
client.tls_insecure_set(True)

# check if username and password are set
if 'username' in config['MQTT'] and 'password' in config['MQTT'] and config['MQTT']['username'] != '' and config['MQTT']['password'] != '':
logging.debug("MQTT client: Using username \"%s\" and password to connect" % config['MQTT']['username'])
logging.info("MQTT client: Using username \"%s\" and password to connect" % config['MQTT']['username'])
client.username_pw_set(username=config['MQTT']['username'], password=config['MQTT']['password'])

# connect to broker
Expand Down

0 comments on commit 950eab3

Please sign in to comment.