forked from erijo/sunnyportal-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunnyportal2pvoutput
executable file
·213 lines (185 loc) · 7.58 KB
/
sunnyportal2pvoutput
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
# Copyright (c) 2016 Erik Johansson <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
from datetime import date, datetime, timedelta
from getpass import getpass
import argparse
import configparser
import http.client as http
import logging
import sys
import sunnyportal.client, sunnyportal.responses
import time
import urllib.parse
class PvOutput(object):
def __init__(self, apikey, systemid, dry_run):
self.dry_run = dry_run
self.headers = {
"X-Pvoutput-Apikey": apikey,
"X-Pvoutput-SystemId": systemid,
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
}
def send_request(self, url, params):
logging.debug("POST to %s: %s", url, params)
if self.dry_run:
return (http.OK, "OK", "")
conn = http.HTTPConnection('pvoutput.org')
encoded = urllib.parse.urlencode(params)
conn.request("POST", url, encoded, self.headers)
response = conn.getresponse()
status = response.status
reason = response.reason
body = response.read().decode('utf-8')
logging.debug("HTTP response: %d (%s): %s", status, reason, body)
conn.close()
return (status, reason, body)
def add_status(self, datetime, generated_energy, power=None):
params = {"d": datetime.strftime("%Y%m%d"),
"t": datetime.strftime("%H:%M"),
"v1": generated_energy}
if power is not None:
params["v2"] = power
(status, reason, body) = self.send_request(
"/service/r2/addstatus.jsp", params)
if status != http.OK:
logging.error("Failed to add status: %s", body)
raise Exception("could not add status")
def add_batch_status(self, statuses):
data = []
for status in statuses:
data.append(",".join([
status[0].strftime("%Y%m%d"),
status[0].strftime("%H:%M"),
str(status[2]), str(status[1])]))
offset = 0
while offset < len(data):
entries = data[offset:offset + 30]
(status, reason, body) = self.send_request(
"/service/r2/addbatchstatus.jsp",
{"data": ";".join(entries)})
if status == http.OK:
offset += len(entries)
if offset < len(data):
time.sleep(10)
elif (status == http.BAD_REQUEST and 'Load in progress' in body):
time.sleep(20)
else:
logging.error("Failed to add status batch: %s", body)
raise Exception("could not add status batch")
def add_batch_output(self, data):
offset = 0
while offset < len(data):
entries = data[offset:offset + 30]
(status, reason, body) = self.send_request(
"/service/r2/addbatchoutput.jsp",
{"data": ";".join(entries)})
if status == http.OK:
offset += len(entries)
if offset < len(data):
time.sleep(10)
elif (status == http.BAD_REQUEST and 'Load in progress' in body):
time.sleep(20)
else:
logging.error("Failed to add output batch: %s", body)
raise Exception("could not add output batch")
def get_data_for_day(plant, date):
day = plant.day_overview(date)
if day.difference is None:
return None
# Find peak hour and power
peak = sunnyportal.responses.Power(None, 0)
for measure in day.power_measurements:
if measure.power > peak.power:
peak = measure
data = "%s,%s" % (day.date.strftime("%Y%m%d"), day.difference)
if peak.timestamp is not None:
data += ",,,%s,%s" % (peak.power, peak.timestamp.strftime("%H:%M"))
return data
def get_data_for_period(plant, start_date=None, end_date=None):
if start_date is None:
start_date = plant.all_data('year').start_timestamp.date()
if end_date is None:
end_date = date.today()
data = []
while start_date <= end_date:
res = get_data_for_day(plant, start_date)
if res:
data.append(res)
start_date = start_date + timedelta(days=1)
return data
def main():
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG)
parser = argparse.ArgumentParser(
description="Connect Sunny Portal to PVoutput.org")
parser.add_argument('config', help="Configuration file to use")
parser.add_argument('-s', '--status', help="Report current status",
action="store_true")
parser.add_argument('-o', '--output', help="Report last output(s)",
action="store_true")
parser.add_argument('-q', '--quiet', help="Silence output",
action="store_true")
parser.add_argument('-n', '--dry-run', help="Don't send any data",
action="store_true")
args = parser.parse_args()
if args.quiet:
logging.disable(logging.DEBUG)
config = configparser.ConfigParser()
config['sunnyportal'] = {}
config['pvoutput'] = {}
config.read(args.config)
modified = False
if not config['sunnyportal'].get('email'):
config['sunnyportal']['email'] = input("Sunny Portal e-mail: ")
modified = True
if not config['sunnyportal'].get('password'):
config['sunnyportal']['password'] = getpass("Sunny Portal password: ")
modified = True
if not config['pvoutput'].get('apikey'):
config['pvoutput']['apikey'] = input("PVOutput API key: ")
modified = True
if not config['pvoutput'].get('systemid'):
config['pvoutput']['systemid'] = input("PVOutput System Id: ")
modified = True
if modified:
with open(args.config, 'w') as configfile:
config.write(configfile)
client = sunnyportal.client.Client(
config['sunnyportal']['email'], config['sunnyportal']['password'])
plant = client.get_plants()[0]
pvoutput = PvOutput(config['pvoutput']['apikey'],
config['pvoutput']['systemid'],
args.dry_run)
if args.status:
day = plant.day_overview(date.today())
if day.power_measurements:
data = []
energy = day.difference
for measure in reversed(day.power_measurements):
data.append((measure.timestamp, measure.power, energy))
energy -= int(measure.power / 4)
pvoutput.add_batch_status(list(reversed(data[0:16])))
elif day.difference is not None:
pvoutput.add_status(day.date, day.difference)
if args.output:
start_date = date.today() - timedelta(days=7)
data = get_data_for_period(plant, start_date=start_date)
pvoutput.add_batch_output(data)
client.logout()
if __name__ == '__main__':
main()