-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcdc_telegraf_pl.py
executable file
·85 lines (60 loc) · 2.71 KB
/
bcdc_telegraf_pl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env /usr/bin/python3.9
#Get solar energy forecast from www.bcdcenergia.fi and outputs it in format readable by Telegraf
# Called few times a day (plus in startup) from Telegraf, results are resent to Powerguru and InfluDb
import requests
import json
import settings as s
from datetime import datetime
# Telegraf plugin
from typing import Dict
from telegraf_pyplug.main import print_influxdb_format, datetime_tzinfo_to_nano_unix_timestamp
import sys
import pytz
powerguru_settings = s.read_settings(s.powerguru_file_name)
timeZoneLocal = powerguru_settings["timeZoneLocal"]
#bcdcenergiaLocation = powerguru_settings["bcdcenergiaLocation"]
bcdcLocationsHandled = powerguru_settings["bcdcLocationsHandled"]
tz_local = pytz.timezone(timeZoneLocal)
# report
def forecast_to_telegraf(location):
query_data_raw = 'action=getChartData&loc=' + location
r = requests.post('http://www.bcdcenergia.fi/wp-admin/admin-ajax.php',data=query_data_raw,headers={'Content-Type': 'application/x-www-form-urlencoded'})
fcst_data = json.loads(r.text)
day_value = 0.0
for pv_h in fcst_data["pvenergy"]:
day_value += pv_h["value"]
i=0
try:
daytsprev = -1
daytotal = 0.0
METRIC_NAME: str = "solarfcst"
tag_name = "forecastpv"
for pv_h in fcst_data["pvenergy"]:
daytscur = int(pv_h["time"]/(3600000*24))
daytotal += pv_h["value"]+0.0
i += 1
if daytsprev != -1 and (i==len(fcst_data["pvenergy"])): #jos näin, niin vois olla vikana
dtday = datetime.fromtimestamp(daytscur*3600*24+(12*3600))
loc_dtday = tz_local.localize(dtday)
METRIC_FIELDS: Dict[str, int] = {"pvrefday":daytotal}
print_influxdb_format(
measurement=METRIC_NAME,
fields=METRIC_FIELDS,
tags = { "location": location},
nano_timestamp=datetime_tzinfo_to_nano_unix_timestamp(loc_dtday)
)
daytotal = 0.0
daytsprev = daytscur
dt = datetime.fromtimestamp(int(pv_h["time"]/1000))
loc_dt = tz_local.localize(dt)
print_influxdb_format(
measurement=METRIC_NAME,
tags = { "location": location, "name" : tag_name},
fields={"pvrefvalue":pv_h["value"]+0.0,"pv_forecast":pv_h["value"]*30.0/2.5},
nano_timestamp=datetime_tzinfo_to_nano_unix_timestamp(loc_dt)
)
#TODO: error handling järkeväksi
except:
print ("Cannot write to influx", sys.exc_info())
for location in bcdcLocationsHandled:
forecast_to_telegraf(location)