2022-12-19 12:24:47 +01:00
|
|
|
import influxdb_client
|
|
|
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import os
|
|
|
|
|
2022-12-20 15:39:41 +01:00
|
|
|
from os.path import dirname, join
|
2022-12-19 12:24:47 +01:00
|
|
|
dotenv_path = join(dirname(__file__), '.env')
|
|
|
|
load_dotenv(dotenv_path)
|
|
|
|
|
|
|
|
bucket=os.environ.get('INFLUX_BUCKET')
|
|
|
|
org=os.environ.get('INFLUX_ORG')
|
|
|
|
token=os.environ.get('INFLUX_TOKEN')
|
|
|
|
url=os.environ.get('INFLUX_URL')
|
|
|
|
|
|
|
|
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
|
|
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
|
|
|
|
|
|
|
|
|
|
|
def publish_measurement(value):
|
2023-07-03 23:49:45 +02:00
|
|
|
# Only push to Influx if not logging
|
|
|
|
if (os.environ.get('LOG_ONLY', "0") != "1"):
|
|
|
|
try:
|
|
|
|
p = influxdb_client.Point(f'KNX-{value["type"]}').tag("name", f'{value["name"]}').tag("destination", f'{value["destination"]}').tag("description", f'{value["description"]}').field("value", value["value"])
|
|
|
|
write_api.write(bucket=bucket, org=org, record=p)
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Could not send value {value} to {url}")
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
print(f"debug: {value}")
|