change instance id and add err handler

This commit is contained in:
Carsten Schmiemann 2022-08-10 12:09:36 +02:00
parent 4389be36cf
commit b843ea72e6
2 changed files with 65 additions and 44 deletions

View file

@ -35,6 +35,7 @@ class NodeRedMeterEinspeisung:
# Create the mandatory objects
self._dbusservice.add_path('/DeviceInstance', deviceinstance)
self._dbusservice.add_path('/ProductId', 0xB017) # like a EM24
self._dbusservice.add_path('/DeviceType', 72) # like a EM24
self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/FirmwareVersion', 1.0)
self._dbusservice.add_path('/HardwareVersion', 0)
@ -49,7 +50,6 @@ class NodeRedMeterEinspeisung:
def _update(self):
try:
self._dbusservice['/ErrorCode'] = 0
self._dbusservice['/DeviceType'] = 72
nodered_url = "http://localhost:1880/meters"
nodered_r = requests.get(url=nodered_url) # request data from the Fronius PV inverter
nodered_data = nodered_r.json() # convert JSON data
@ -72,9 +72,20 @@ class NodeRedMeterEinspeisung:
self._dbusservice['/Ac/L1/Energy/Reverse'] = nodered_data['einspeisung']['l1_export']
self._dbusservice['/Ac/L2/Energy/Reverse'] = nodered_data['einspeisung']['l2_export']
self._dbusservice['/Ac/L3/Energy/Reverse'] = nodered_data['einspeisung']['l3_export']
logging.debug("House Consumption (/Ac/Power): %s" % (self._dbusservice['/Ac/Power']))
logging.debug("House Forward (/Ac/Energy/Forward): %s" % (self._dbusservice['/Ac/Energy/Forward']))
logging.debug("House Reverse (/Ac/Energy/Revers): %s" % (self._dbusservice['/Ac/Energy/Reverse']))
logging.debug("---");
except:
logging.info("WARNING: Could not read from Node Red, check if Node Red service is running")
index = self._dbusservice[path_UpdateIndex] + 1 # increment index
if index > 255: # maximum value of the index
index = 0 # overflow from 255 to 0
self._dbusservice[path_UpdateIndex] = index
except Exception as e:
logging.critical("WARNING: Could not read from Node Red, check if Node Red service is running")
logging.critical('Error at %s', '_update', exc_info=e)
self._dbusservice['/ErrorCode'] = 1
self._dbusservice['/Ac/Power'] = 0
self._dbusservice['/Ac/Current'] = 0
@ -87,10 +98,7 @@ class NodeRedMeterEinspeisung:
self._dbusservice['/Ac/L1/Power'] = 0
self._dbusservice['/Ac/L2/Power'] = 0
self._dbusservice['/Ac/L3/Power'] = 0
index = self._dbusservice[path_UpdateIndex] + 1 # increment index
if index > 255: # maximum value of the index
index = 0 # overflow from 255 to 0
self._dbusservice[path_UpdateIndex] = index
return True
def _handlechangedvalue(self, path, value):
@ -98,45 +106,58 @@ class NodeRedMeterEinspeisung:
return True # accept the change
def main():
logging.basicConfig(level=logging.DEBUG) # use .INFO for less logging
logging.basicConfig( format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG,
handlers=[
logging.FileHandler("%s/current.log" % (os.path.dirname(os.path.realpath(__file__)))),
logging.StreamHandler()
])
thread.daemon = True # allow the program to quit
try:
from dbus.mainloop.glib import DBusGMainLoop
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
_kwh = lambda p, v: (str(round(v, 2)) + ' KWh')
_A = lambda p, v: (str(round(v, 1)) + ' A')
_W = lambda p, v: (str(round(v, 1)) + ' W')
_V = lambda p, v: (str(round(v, 1)) + ' V')
from dbus.mainloop.glib import DBusGMainLoop
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
pvac_output = NodeRedMeterEinspeisung(
servicename='com.victronenergy.grid',
deviceinstance=50,
paths={
'/ErrorCode': {'initial': 0},
'/DeviceType': {'initial': 72},
'/Ac/Power': {'initial': 0},
'/Ac/Current': {'initial': 0},
'/Ac/L1/Voltage': {'initial': 0},
'/Ac/L2/Voltage': {'initial': 0},
'/Ac/L3/Voltage': {'initial': 0},
'/Ac/L1/Current': {'initial': 0},
'/Ac/L2/Current': {'initial': 0},
'/Ac/L3/Current': {'initial': 0},
'/Ac/L1/Power': {'initial': 0},
'/Ac/L2/Power': {'initial': 0},
'/Ac/L3/Power': {'initial': 0},
'/Ac/Energy/Forward': {'initial': 0},
'/Ac/Energy/Reverse': {'initial': 0},
'/Ac/L1/Energy/Forward': {'initial': 0},
'/Ac/L2/Energy/Forward': {'initial': 0},
'/Ac/L3/Energy/Forward': {'initial': 0},
'/Ac/L1/Energy/Reverse': {'initial': 0},
'/Ac/L2/Energy/Reverse': {'initial': 0},
'/Ac/L3/Energy/Reverse': {'initial': 0},
pvac_output = NodeRedMeterEinspeisung(
servicename='com.victronenergy.grid',
deviceinstance=40,
paths={
'/ErrorCode': {'initial': 0},
'/Ac/Power': {'initial': 0, 'textformat': _W},
'/Ac/Current': {'initial': 0, 'textformat': _A},
'/Ac/L1/Voltage': {'initial': 0, 'textformat': _V},
'/Ac/L2/Voltage': {'initial': 0, 'textformat': _V},
'/Ac/L3/Voltage': {'initial': 0, 'textformat': _V},
'/Ac/L1/Current': {'initial': 0, 'textformat': _A},
'/Ac/L2/Current': {'initial': 0, 'textformat': _A},
'/Ac/L3/Current': {'initial': 0, 'textformat': _A},
'/Ac/L1/Power': {'initial': 0, 'textformat': _W},
'/Ac/L2/Power': {'initial': 0, 'textformat': _W},
'/Ac/L3/Power': {'initial': 0, 'textformat': _W},
'/Ac/Energy/Forward': {'initial': 0, 'textformat': _kwh},
'/Ac/Energy/Reverse': {'initial': 0, 'textformat': _kwh},
'/Ac/L1/Energy/Forward': {'initial': 0, 'textformat': _kwh},
'/Ac/L2/Energy/Forward': {'initial': 0, 'textformat': _kwh},
'/Ac/L3/Energy/Forward': {'initial': 0, 'textformat': _kwh},
'/Ac/L1/Energy/Reverse': {'initial': 0, 'textformat': _kwh},
'/Ac/L2/Energy/Reverse': {'initial': 0, 'textformat': _kwh},
'/Ac/L3/Energy/Reverse': {'initial': 0, 'textformat': _kwh},
path_UpdateIndex: {'initial': 0},
})
path_UpdateIndex: {'initial': 0},
})
logging.info('Connected to dbus, and switching over to gobject.MainLoop() (= event based)')
mainloop = gobject.MainLoop()
mainloop.run()
logging.info('Connected to dbus, and switching over to gobject.MainLoop() (= event based)')
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as e:
logging.critical('Error at %s', 'main', exc_info=e)
if __name__ == "__main__":
main()

View file

@ -34,7 +34,7 @@ class NodeRedTempOutside:
# Create the mandatory objects
self._dbusservice.add_path('/DeviceInstance', deviceinstance)
self._dbusservice.add_path('/ProductId', 0xC029) # like ruvii sensors, for symbol only
self._dbusservice.add_path('/ProductId', 0xFFFF) # like ruvii sensors, for symbol only
self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/FirmwareVersion', 1.0)
self._dbusservice.add_path('/HardwareVersion', 0)
@ -53,7 +53,7 @@ class NodeRedTempOutside:
nodered_data = nodered_r.json() # convert JSON data
nodered_temperature = nodered_data['outside']
self._dbusservice['/Temperature'] = nodered_temperature
self._dbusservice['/Temperature'] = float(nodered_temperature)
self._dbusservice['/TemperatureType'] = 2 # 0=battery; 1=fridge; 2=generic
self._dbusservice['/CustomName'] = "Temperatur Außen"
logging.info("Temperature reading: {:.0f}".format(nodered_temperature))
@ -79,7 +79,7 @@ def main():
pvac_output = NodeRedTempOutside(
servicename='com.victronenergy.temperature',
deviceinstance=11,
deviceinstance=28,
paths={
'/Temperature': {'initial': 0},
'/TemperatureType': {'initial': 0},