Update temperature script

This commit is contained in:
Carsten Schmiemann 2022-08-19 23:03:53 +02:00
parent 46c8478c12
commit 4671a96e3d

View file

@ -1,95 +1,161 @@
#!/usr/bin/env python #!/usr/bin/env python
try: # import normal packages
import gobject # Python 2.x import platform
except:
from gi.repository import GLib as gobject # Python 3.x
import platform
import logging import logging
import sys import sys
import os import os
import sys
if sys.version_info.major == 2:
import gobject
else:
from gi.repository import GLib as gobject
import sys
import time
import requests # for http GET import requests # for http GET
try:
import thread # for daemon = True / Python 2.x # our own packages from victron
except: sys.path.insert(1, os.path.join(os.path.dirname(__file__), '/opt/victronenergy/dbus-systemcalc-py/ext/velib_python'))
import _thread as thread # for daemon = True / Python 3.x
# our own packages
sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../ext/velib_python'))
from vedbus import VeDbusService from vedbus import VeDbusService
path_UpdateIndex = '/UpdateIndex'
class NodeRedMeterTemperature:
class NodeRedTempOutside: def __init__(self, servicename, deviceinstance, paths, productname='External temp sensor', connection='Node RED HTTP JSON service'):
def __init__(self, servicename, deviceinstance, paths, productname='External temp sensor', connection='NodeRED local'): self._dbusservice = VeDbusService("{}.http_{:02d}".format(servicename, deviceinstance))
self._dbusservice = VeDbusService(servicename)
self._paths = paths self._paths = paths
logging.debug("%s /DeviceInstance = %d" % (servicename, deviceinstance)) logging.debug("%s /DeviceInstance = %d" % (servicename, deviceinstance))
# Create the management objects, as specified in the ccgx dbus-api document # Create the management objects, as specified in the ccgx dbus-api document
self._dbusservice.add_path('/Mgmt/ProcessName', __file__) self._dbusservice.add_path('/Mgmt/ProcessName', __file__)
self._dbusservice.add_path('/Mgmt/ProcessVersion', 'Python ' + platform.python_version())
self._dbusservice.add_path('/Mgmt/Connection', connection) self._dbusservice.add_path('/Mgmt/Connection', connection)
# Create the mandatory objects # Create the mandatory objects
self._dbusservice.add_path('/DeviceInstance', deviceinstance) self._dbusservice.add_path('/DeviceInstance', deviceinstance)
self._dbusservice.add_path('/ProductId', 0xFFFF) # like ruvii sensors, for symbol only self._dbusservice.add_path('/ProductId', 41314)
self._dbusservice.add_path('/FilterLength', 10)
self._dbusservice.add_path('/Offset', 0)
self._dbusservice.add_path('/Scale', 1)
self._dbusservice.add_path('/ProductName', productname) self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/CustomName', "Temperatur Außen")
self._dbusservice.add_path('/TemperatureType', 2) # 0=battery; 1=fridge; 2=generic
self._dbusservice.add_path('/FirmwareVersion', 1.0) self._dbusservice.add_path('/FirmwareVersion', 1.0)
self._dbusservice.add_path('/HardwareVersion', 0) self._dbusservice.add_path('/HardwareVersion', 0)
self._dbusservice.add_path('/Connected', 1) self._dbusservice.add_path('/Connected', 1)
self._dbusservice.add_path('/Serial', 1337)
self._dbusservice.add_path('/UpdateIndex', 0)
# add path values to dbus
for path, settings in self._paths.items(): for path, settings in self._paths.items():
self._dbusservice.add_path( self._dbusservice.add_path(
path, settings['initial'], writeable=True, onchangecallback=self._handlechangedvalue) path, settings['initial'], gettextcallback=settings['textformat'], writeable=True, onchangecallback=self._handlechangedvalue)
# last update
self._lastUpdate = 0
# add _update function 'timer'
gobject.timeout_add(2000, self._update) # pause 2000ms before the next request gobject.timeout_add(2000, self._update) # pause 2000ms before the next request
def _update(self): # add _signOfLife 'timer' to get feedback in log every 5minutes
try: gobject.timeout_add(self._getSignOfLifeInterval()*60*1000, self._signOfLife)
nodered_url = "http://localhost:1880/temps"
nodered_r = requests.get(url=nodered_url) # request data from Node RED HTTP JSON API def _getSignOfLifeInterval(self):
nodered_data = nodered_r.json() # convert JSON data value = 1
nodered_temperature = nodered_data['outside']
if not value:
self._dbusservice['/Temperature'] = float(nodered_temperature) value = 0
self._dbusservice['/TemperatureType'] = 2 # 0=battery; 1=fridge; 2=generic
self._dbusservice['/CustomName'] = "Temperatur Außen" return int(value)
logging.info("Temperature reading: {:.0f}".format(nodered_temperature))
except: def _getNodeRedData(self):
logging.info("WARNING: Could not read from Node Red, check if Node Red service is running") URL = "http://localhost:1880/temps"
index = self._dbusservice[path_UpdateIndex] + 1 # increment index temperature_r = requests.get(url = URL)
if index > 255: # maximum value of the index
index = 0 # overflow from 255 to 0 # check for response
self._dbusservice[path_UpdateIndex] = index if not temperature_r:
raise ConnectionError("No response from NodeRed - %s" % (URL))
meter_data = temperature_r.json()
# check for Json
if not meter_data:
raise ValueError("Converting response to JSON failed")
return meter_data
def _signOfLife(self):
logging.info("--- Start: sign of life ---")
logging.info("Last _update() call: %s" % (self._lastUpdate))
logging.info("Last '/Temperature': %s" % (self._dbusservice['/Temperature']))
logging.info("--- End: sign of life ---")
return True return True
def _update(self):
try:
#get data from NodeRed 3em
meter_data = self._getNodeRedData()
#send data to DBus
self._dbusservice['/Temperature'] = meter_data['outside']
#logging
logging.debug("Temperature Outside (/Temperature): %s" % (self._dbusservice['/Temperature']))
logging.debug("---");
# increment UpdateIndex - to show that new data is available
index = self._dbusservice['/UpdateIndex'] + 1 # increment index
if index > 255: # maximum value of the index
index = 0 # overflow from 255 to 0
self._dbusservice['/UpdateIndex'] = index
#update lastupdate vars
self._lastUpdate = time.time()
except Exception as e:
logging.critical('Error at %s', '_update', exc_info=e)
# return true, otherwise add_timeout will be removed from GObject - see docs http://library.isr.ist.utl.pt/docs/pygtk2reference/gobject-functions.html#function-gobject--timeout-add
return True
def _handlechangedvalue(self, path, value): def _handlechangedvalue(self, path, value):
logging.debug("someone else updated %s to %s" % (path, value)) logging.debug("someone else updated %s to %s" % (path, value))
return True # accept the change return True # accept the change
def main(): def main():
logging.basicConfig(level=logging.DEBUG) # use .INFO for less logging #configure logging
thread.daemon = True # allow the program to quit logging.basicConfig( format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
from dbus.mainloop.glib import DBusGMainLoop level=logging.INFO,
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus handlers=[
DBusGMainLoop(set_as_default=True) logging.FileHandler("%s/current.log" % (os.path.dirname(os.path.realpath(__file__)))),
logging.StreamHandler()
pvac_output = NodeRedTempOutside( ])
servicename='com.victronenergy.temperature',
deviceinstance=28, try:
paths={ logging.info("Start");
'/Temperature': {'initial': 0},
'/TemperatureType': {'initial': 0}, from dbus.mainloop.glib import DBusGMainLoop
'/CustomName': {'initial': 0}, # Have a mainloop, so we can send/receive asynchronous calls to and from dbus
path_UpdateIndex: {'initial': 0}, DBusGMainLoop(set_as_default=True)
})
#formatting
logging.info('Connected to dbus, and switching over to gobject.MainLoop() (= event based)') _celcius = lambda p, v: (str(round(v, 2)) + ' °C')
mainloop = gobject.MainLoop()
mainloop.run() #start our main-service
pvac_output = NodeRedMeterTemperature(
servicename='com.victronenergy.temperature',
deviceinstance=18,
paths={
'/Temperature': {'initial': 0, 'textformat': _celcius},
})
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__": if __name__ == "__main__":
main() main()