Merge pull request #38 from pascal1404/master

Add Influxdb-cloud connection and log for actor, fermenter and kettles
This commit is contained in:
Alexander Vollkopf 2022-02-06 14:46:57 +01:00 committed by GitHub
commit 195705d79e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 18 deletions

View file

@ -54,25 +54,45 @@ class LogController:
self.datalogger[name].info("%s,%s" % (formatted_time, value)) self.datalogger[name].info("%s,%s" % (formatted_time, value))
if self.influxdb == "Yes": if self.influxdb == "Yes":
self.influxdb = self.cbpi.config.get("INFLUXDB", "No") self.influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", "No")
self.influxdbaddr = self.cbpi.config.get("INFLUXDBADDR", None) self.influxdbaddr = self.cbpi.config.get("INFLUXDBADDR", None)
self.influxdbport = self.cbpi.config.get("INFLUXDBPORT", None) self.influxdbport = self.cbpi.config.get("INFLUXDBPORT", None)
self.influxdbname = self.cbpi.config.get("INFLUXDBNAME", None) self.influxdbname = self.cbpi.config.get("INFLUXDBNAME", None)
self.influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None) self.influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None)
self.influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None) self.influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None)
self.base64string = base64.b64encode(('%s:%s' % (self.influxdbuser,self.influxdbpwd)).encode())
self.influxdburl='http://' + self.influxdbaddr + ':' + str(self.influxdbport) + '/write?db=' + self.influxdbname id = name
try: try:
chars = {'ö':'oe','ä':'ae','ü':'ue','Ö':'Oe','Ä':'Ae','Ü':'Ue'}
sensor=self.cbpi.sensor.find_by_id(name) sensor=self.cbpi.sensor.find_by_id(name)
sensorname=sensor.name.replace(" ", "_") if sensor is not None:
out="measurement,source=" + sensorname + "___" + name + " value="+str(value) itemname=sensor.name.replace(" ", "_")
header = {'User-Agent': name, 'Content-Type': 'application/x-www-form-urlencoded','Authorization': 'Basic %s' % self.base64string.decode('utf-8')} for char in chars:
http = urllib3.PoolManager() itemname = itemname.replace(char,chars[char])
req = http.request('POST',self.influxdburl, body=out, headers = header) out="measurement,source=" + itemname + ",itemID=" + str(id) + " value="+str(value)
except Exception as e: except Exception as e:
logging.error("InfluxDB write Error: {}".format(e)) logging.error("InfluxDB ID Error: {}".format(e))
if self.influxdbcloud == "Yes":
self.influxdburl="https://" + self.influxdbaddr + "/api/v2/write?org=" + self.influxdbuser + "&bucket=" + self.influxdbname + "&precision=s"
try:
header = {'User-Agent': name, 'Authorization': "Token {}".format(self.influxdbpwd)}
http = urllib3.PoolManager()
req = http.request('POST',self.influxdburl, body=out, headers = header)
except Exception as e:
logging.error("InfluxDB cloud write Error: {}".format(e))
else:
self.influxdb = self.cbpi.config.get("INFLUXDB", "No")
self.base64string = base64.b64encode(('%s:%s' % (self.influxdbuser,self.influxdbpwd)).encode())
self.influxdburl='http://' + self.influxdbaddr + ':' + str(self.influxdbport) + '/write?db=' + self.influxdbname
try:
header = {'User-Agent': name, 'Content-Type': 'application/x-www-form-urlencoded','Authorization': 'Basic %s' % self.base64string.decode('utf-8')}
http = urllib3.PoolManager()
req = http.request('POST',self.influxdburl, body=out, headers = header)
except Exception as e:
logging.error("InfluxDB write Error: {}".format(e))

View file

@ -44,6 +44,7 @@ class ConfigUpdate(CBPiExtension):
influxdbname = self.cbpi.config.get("INFLUXDBNAME", None) influxdbname = self.cbpi.config.get("INFLUXDBNAME", None)
influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None) influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None)
influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None) influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None)
influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", None)
mqttupdate = self.cbpi.config.get("MQTTUpdate", None) mqttupdate = self.cbpi.config.get("MQTTUpdate", None)
@ -203,7 +204,7 @@ class ConfigUpdate(CBPiExtension):
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')
## Check if CSV logfiles is on config ## Check if influxdb is on config
if influxdb is None: if influxdb is None:
logger.info("INIT Influxdb") logger.info("INIT Influxdb")
try: try:
@ -217,7 +218,7 @@ class ConfigUpdate(CBPiExtension):
if influxdbaddr is None: if influxdbaddr is None:
logger.info("INIT Influxdbaddr") logger.info("INIT Influxdbaddr")
try: try:
await self.cbpi.config.add("INFLUXDBADDR", "localhost", ConfigType.STRING, "IP Address of your influxdb server") await self.cbpi.config.add("INFLUXDBADDR", "localhost", ConfigType.STRING, "IP Address of your influxdb server (If INFLUXDBCLOUD set to Yes use URL Address of your influxdb cloud server)")
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')
@ -233,23 +234,33 @@ class ConfigUpdate(CBPiExtension):
if influxdbname is None: if influxdbname is None:
logger.info("INIT Influxdbname") logger.info("INIT Influxdbname")
try: try:
await self.cbpi.config.add("INFLUXDBNAME", "cbpi4", ConfigType.STRING, "Name of your influxdb database name") await self.cbpi.config.add("INFLUXDBNAME", "cbpi4", ConfigType.STRING, "Name of your influxdb database name (If INFLUXDBCLOUD set to Yes use bucket of your influxdb cloud database)")
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')
## Check if influxdber is in config ## Check if influxduser is in config
if influxdbuser is None: if influxdbuser is None:
logger.info("INIT Influxdbuser") logger.info("INIT Influxdbuser")
try: try:
await self.cbpi.config.add("INFLUXDBUSER", " ", ConfigType.STRING, "User name for your influxdb database (only if required)") await self.cbpi.config.add("INFLUXDBUSER", " ", ConfigType.STRING, "User name for your influxdb database (only if required)(If INFLUXDBCLOUD set to Yes use organisation of your influxdb cloud database)")
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')
## Check if influxdber is in config ## Check if influxdpwd is in config
if influxdbpwd is None: if influxdbpwd is None:
logger.info("INIT Influxdbpwd") logger.info("INIT Influxdbpwd")
try: try:
await self.cbpi.config.add("INFLUXDBPWD", " ", ConfigType.STRING, "Password for your influxdb database (only if required)") await self.cbpi.config.add("INFLUXDBPWD", " ", ConfigType.STRING, "Password for your influxdb database (only if required)(If INFLUXDBCLOUD set to Yes use token of your influxdb cloud database)")
except:
logger.warning('Unable to update config')
## Check if influxdb cloud is on config
if influxdbcloud is None:
logger.info("INIT influxdbcloud")
try:
await self.cbpi.config.add("INFLUXDBCLOUD", "No", ConfigType.SELECT, "Write sensor data to influxdb cloud (INFLUXDB must set to Yes)",
[{"label": "Yes", "value": "Yes"},
{"label": "No", "value": "No"}])
except: except:
logger.warning('Unable to update config') logger.warning('Unable to update config')