feature: restructured Datapoints
This commit is contained in:
parent
db34941c30
commit
dd9ee96396
2 changed files with 51 additions and 54 deletions
24
DataPoints.py
Normal file
24
DataPoints.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from influxdb_client import Point
|
||||
class Pulse:
|
||||
label = str(__name__).lower()
|
||||
def __init__(self,data):
|
||||
measurement = data['liveMeasurement']
|
||||
timestamp = measurement['timestamp']
|
||||
power = measurement['power']
|
||||
cost = measurement.get('accumulatedCost',None)
|
||||
lastMeterConsumption = measurement['lastMeterConsumption']
|
||||
tags = {self.label: ""}
|
||||
fields = {
|
||||
"power": float(power),
|
||||
"lastMeterConsumption": float(lastMeterConsumption)
|
||||
}
|
||||
if cost is not None:
|
||||
fields.update({'accumulatedCost':cost})
|
||||
self.datapoint = {"fields": fields,
|
||||
"tags": tags,
|
||||
"measurement": "pulse",
|
||||
"time": timestamp
|
||||
}
|
||||
def get_datapoint(self):
|
||||
return Point(measurement_name=str(self.label)).from_dict(self.datapoint)
|
||||
|
81
pulse.py
81
pulse.py
|
@ -1,86 +1,59 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import time
|
||||
import random
|
||||
|
||||
from dateutil.parser import parse
|
||||
|
||||
|
||||
print("tibber_v001")
|
||||
import tibber.const
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import tibber
|
||||
# settings from EnvionmentValue
|
||||
from influxdb_client import InfluxDBClient
|
||||
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||
from DataPoints import Pulse
|
||||
|
||||
TOKEN=os.getenv('TOKEN', '')
|
||||
TIBBERTOKEN=os.getenv('TIBBERTOKEN', '')
|
||||
URL = os.getenv('URL',"" )
|
||||
BUCKET = os.getenv('BUCKET',"tibber" )
|
||||
ORG = os.getenv('ORG',"Default" )
|
||||
|
||||
from influxdb_client import InfluxDBClient, Point
|
||||
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||
|
||||
bucket = "Default"
|
||||
__version__ = "v0.0.2"
|
||||
print(f"tibber_{__version__}")
|
||||
|
||||
client = InfluxDBClient(url=URL, token=TOKEN, org=ORG)
|
||||
|
||||
write_api = client.write_api(write_options=SYNCHRONOUS)
|
||||
query_api = client.query_api()
|
||||
|
||||
def _callback(pkg):
|
||||
def _incoming(pkg):
|
||||
try:
|
||||
data = pkg.get("data")
|
||||
if data is None:
|
||||
return
|
||||
lm=data.get("liveMeasurement")
|
||||
print(lm)
|
||||
write_to_db(data)
|
||||
return lm
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
|
||||
def write_to_db(data,bucket = BUCKET):
|
||||
|
||||
measurement = data['liveMeasurement']
|
||||
timestamp = measurement['timestamp']
|
||||
power = measurement['power']
|
||||
lastMeterConsumption = measurement['lastMeterConsumption']
|
||||
|
||||
tags = {"adress":""}
|
||||
fields = {
|
||||
"power": float(power),
|
||||
"lastMeterConsumption": float(lastMeterConsumption)
|
||||
}
|
||||
datapoint = {"fields": fields,
|
||||
"tags": tags,
|
||||
"measurement": "pulse",
|
||||
time: timestamp
|
||||
|
||||
}
|
||||
p = Point(measurement_name="pulse").from_dict(datapoint)
|
||||
write_api.write(record=p, bucket=bucket)
|
||||
|
||||
|
||||
|
||||
|
||||
exit(1)
|
||||
p = Pulse(data).get_datapoint()
|
||||
write_api.write(record=p, bucket=BUCKET)
|
||||
print(p)
|
||||
return True
|
||||
except:
|
||||
exit(1)
|
||||
|
||||
async def run():
|
||||
async with aiohttp.ClientSession() as session:
|
||||
if session.closed:
|
||||
print("session closed")
|
||||
exit(1)
|
||||
tibber_connection = tibber.Tibber(TIBBERTOKEN, websession=session, user_agent="python")
|
||||
await tibber_connection.update_info()
|
||||
home = tibber_connection.get_homes()[0]
|
||||
try:
|
||||
await home.rt_subscribe(_callback)
|
||||
except:
|
||||
raise Exception("Session gone")
|
||||
await home.rt_subscribe(_incoming)
|
||||
|
||||
except:
|
||||
exit(1)
|
||||
|
||||
while 1:
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(4)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
try:
|
||||
loop.run_until_complete(run())
|
||||
except:
|
||||
exit(1)
|
||||
loop.run_until_complete(run())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue