Add al files from local folder
This commit is contained in:
114
Influxclient.py
Normal file
114
Influxclient.py
Normal file
@@ -0,0 +1,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Oct 17 13:45:38 2022
|
||||
|
||||
@author: Daniel Häfliger
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
import math
|
||||
import requests, json
|
||||
from datetime import datetime, date, time
|
||||
|
||||
# Convert a date and time to to complex numbers
|
||||
# [year_real, year_imag, day_real, day_imag]
|
||||
def Convert_Date(atime):
|
||||
|
||||
y_r = math.sin(atime.timetuple().tm_yday/365*2*math.pi)
|
||||
y_i = math.cos(atime.timetuple().tm_yday/365*2*math.pi)
|
||||
d_r = math.sin((atime.hour*3600+atime.minute*60+atime.second)/((24*3600))*2*math.pi)
|
||||
d_i = math.cos((atime.hour*3600+atime.minute*60+atime.second)/((24*3600))*2*math.pi)
|
||||
return [y_r, y_i, d_r, d_i]
|
||||
|
||||
|
||||
# Return a list of datas of a specified measurement of an influxDB
|
||||
def Get_Value(client, bucket, atime, measurement, datapoint):
|
||||
|
||||
query_api = client.query_api()
|
||||
query = 'from(bucket:"'+bucket+'")\
|
||||
|> range(start: -'+atime+')\
|
||||
|> filter(fn:(r) => r._field == "'+datapoint+'" )\
|
||||
|> filter(fn:(r) => r._measurement == "'+measurement+'" )'
|
||||
result = query_api.query(org=client.org, query=query)
|
||||
thelist = []
|
||||
for table in result:
|
||||
for record in table.records:
|
||||
thelist.append((record.get_value()))
|
||||
return thelist
|
||||
|
||||
|
||||
# Return the time of datas of a specified measurement of an influxDB
|
||||
def Get_Time(client, bucket, atime, measurement, datapoint):
|
||||
|
||||
query_api = client.query_api()
|
||||
query = 'from(bucket:"'+bucket+'")\
|
||||
|> range(start: -'+atime+')\
|
||||
|> filter(fn:(r) => r._field == "'+datapoint+'" )\
|
||||
|> filter(fn:(r) => r._measurement == "'+measurement+'" )'
|
||||
result = query_api.query(org=client.org, query=query)
|
||||
thelist = []
|
||||
for table in result:
|
||||
for record in table.records:
|
||||
thelist.append((record.get_time()))
|
||||
|
||||
|
||||
a=[]
|
||||
b=[]
|
||||
c=[]
|
||||
d=[]
|
||||
|
||||
for i in thelist:
|
||||
x = Convert_Date(i)
|
||||
a.append(x[0])
|
||||
b.append(x[1])
|
||||
c.append(x[2])
|
||||
d.append(x[3])
|
||||
|
||||
|
||||
|
||||
return [a,b,c,d,thelist]
|
||||
|
||||
|
||||
|
||||
|
||||
# Need duration in hours, interval in minutes
|
||||
# Retruns array with complex time, timestamp and prediction
|
||||
# Works only for max 3 days due api returns only values until then
|
||||
def GetPrediction(duration, intervall, datapoint):
|
||||
|
||||
xfile = pd.read_xml('config.xml')
|
||||
|
||||
apikey = (xfile.loc[xfile['category'] == 'influx'].weatherapitoken[0])
|
||||
place = (xfile.loc[xfile['category'] == 'influx'].place[0])
|
||||
dats = requests.get("https://api.weatherapi.com/v1/forecast.json?key="+apikey+"&q="+place+"&days=3&aqi=yes&alerts=no")
|
||||
dats=json.loads(dats.content)
|
||||
|
||||
a=[]
|
||||
b=[]
|
||||
c=[]
|
||||
d=[]
|
||||
e=[]
|
||||
f=[]
|
||||
now = datetime.now()
|
||||
daycounter = 0
|
||||
lasthour=now.hour
|
||||
for i in range(0, int(duration*60/intervall)):
|
||||
|
||||
now = datetime.timestamp(now)+(intervall*60)
|
||||
now = datetime.fromtimestamp(now)
|
||||
|
||||
x = Convert_Date(now)
|
||||
a.append(x[0])
|
||||
b.append(x[1])
|
||||
c.append(x[2])
|
||||
d.append(x[3])
|
||||
e.append(now)
|
||||
bc = dats["forecast"]["forecastday"][daycounter]["hour"][now.hour][datapoint]
|
||||
|
||||
f.append(bc)
|
||||
if lasthour>now.hour:
|
||||
daycounter+=1
|
||||
|
||||
lasthour=now.hour
|
||||
|
||||
return[a,b,c,d,e,f]
|
||||
Reference in New Issue
Block a user