diff --git a/cbpi/controller/log_file_controller.py b/cbpi/controller/log_file_controller.py index 8546e7d..1decdc5 100644 --- a/cbpi/controller/log_file_controller.py +++ b/cbpi/controller/log_file_controller.py @@ -54,25 +54,45 @@ class LogController: self.datalogger[name].info("%s,%s" % (formatted_time, value)) 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.influxdbport = self.cbpi.config.get("INFLUXDBPORT", None) self.influxdbname = self.cbpi.config.get("INFLUXDBNAME", None) self.influxdbuser = self.cbpi.config.get("INFLUXDBUSER", 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: + chars = {'ö':'oe','ä':'ae','ü':'ue','Ö':'Oe','Ä':'Ae','Ü':'Ue'} sensor=self.cbpi.sensor.find_by_id(name) - sensorname=sensor.name.replace(" ", "_") - out="measurement,source=" + sensorname + "___" + name + " value="+str(value) - 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) + if sensor is not None: + itemname=sensor.name.replace(" ", "_") + for char in chars: + itemname = itemname.replace(char,chars[char]) + out="measurement,source=" + itemname + ",itemID=" + str(id) + " value="+str(value) 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)) diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index 4895dc2..bc24110 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -44,6 +44,7 @@ class ConfigUpdate(CBPiExtension): influxdbname = self.cbpi.config.get("INFLUXDBNAME", None) influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None) influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None) + influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", None) mqttupdate = self.cbpi.config.get("MQTTUpdate", None) @@ -203,7 +204,7 @@ class ConfigUpdate(CBPiExtension): except: logger.warning('Unable to update config') - ## Check if CSV logfiles is on config + ## Check if influxdb is on config if influxdb is None: logger.info("INIT Influxdb") try: @@ -217,7 +218,7 @@ class ConfigUpdate(CBPiExtension): if influxdbaddr is None: logger.info("INIT Influxdbaddr") 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: logger.warning('Unable to update config') @@ -233,23 +234,33 @@ class ConfigUpdate(CBPiExtension): if influxdbname is None: logger.info("INIT Influxdbname") 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: logger.warning('Unable to update config') - ## Check if influxdber is in config + ## Check if influxduser is in config if influxdbuser is None: logger.info("INIT Influxdbuser") 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: logger.warning('Unable to update config') - ## Check if influxdber is in config + ## Check if influxdpwd is in config if influxdbpwd is None: logger.info("INIT Influxdbpwd") 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: logger.warning('Unable to update config')