influxdb.py 763 B

12345678910111213141516171819
  1. from influxdb_client import InfluxDBClient, Point
  2. from influxdb_client.client.write_api import SYNCHRONOUS
  3. class Database():
  4. def __init__(self):
  5. self.client = InfluxDBClient(url="http://localhost:8086", token="Jqv8THpAkRo3zzSQjz-mOHJDNJk3FzL8pDkbgPvgjMUwHigBEGbFJdbcoFJlauURkTaWvHUqJ_yiO9xYN66NXA==", org="laempe")
  6. self.bucket = "energy-monitor"
  7. self.write_api = self.client.write_api(write_options=SYNCHRONOUS)
  8. self.query_api = self.client.query_api()
  9. def write(self, values):
  10. points = []
  11. for meas in values:
  12. p = Point("24v").time(meas.timestamp).tag("source", meas.source).tag("channel", meas.channel).field("current", meas.current)
  13. points.append(p)
  14. self.write_api.write(bucket=self.bucket, record=points)