-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_collector.py
More file actions
299 lines (260 loc) · 10.4 KB
/
data_collector.py
File metadata and controls
299 lines (260 loc) · 10.4 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import argparse
import calendar
import datetime
import json
import logging
import pathlib
import sqlite3
import sys
from typing import Dict
from typing import List
from typing import Tuple
import requests
from dateutil import relativedelta
from apscheduler.schedulers.background import BlockingScheduler
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
DATABASE_NAME = "data/solar_data.sqlite"
scheduler = BlockingScheduler()
SCHEDULER_INTERVAL = 12
# Based on https://github.com/Antonji-py/solar-providers-manager
class GrowattApi:
def __init__(self, username, password):
session = requests.Session()
session.headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
self.login(session, username, password)
self.session = session
def __fetch_url(self, action: str = "login") -> str:
return {
"login": "http://server-api.growatt.com/login",
"list_plants": "http://server-api.growatt.com/selectPlant/getPlantList",
"plant_devices": "http://server-api.growatt.com/panel/getDevicesByPlantList",
"get_daily_logs_tlx": "http://server-api.growatt.com/device/getTLXHistory",
"get_daily_logs_inv": "http://server-api.growatt.com/device/getInverterHistor",
"get_monthly_energy": "http://server-api.growatt.com/energy/compare/getDevicesMonthChart",
"get_daily_energy": "http://server-api.growatt.com/energy/compare/getDevicesDayChart",
}.get(action)
def login(self, session: requests.Session, username: str, password: str) -> None:
data = {"account": username, "password": password, "validateCode": ""}
response = session.post(self.__fetch_url("login"), data=data)
if response.json()["result"] != 1:
logger.error("[Growatt] Error while logging in")
raise Exception("Error logging in")
logger.info("[Growatt] Successfully logged in")
def get_plants(self) -> List[Dict]:
current_page, pages, plants_list = 0, -1, []
while current_page != pages:
current_page += 1
response = self.session.post(
self.__fetch_url("list_plants"),
data={
"currPage": current_page,
"plantType": "-1",
"orderType": 0,
"plantName": "",
},
)
response_json = response.json()
pages = response_json["pages"]
plants_list.extend(
[
{"id": plant["id"], "plant_name": plant["plantName"]}
for plant in response_json["datas"]
]
)
return plants_list
def get_plant_devices(self, plant_id: str) -> List[Dict]:
current_page, pages, devices = 0, -1, []
while current_page != pages:
current_page += 1
response = self.session.post(
self.__fetch_url("plant_devices"),
data={"currPage": current_page, "plantId": plant_id},
)
response_json = response.json()
if response_json["result"] != 1:
return response_json
pages = response_json["obj"]["pages"]
devices.extend([device for device in response_json["obj"]["datas"]])
return devices
def get_daily_logs(
self, device_id: str, date: str, device_type="tlx"
) -> List[Dict]:
"""device type can be tlx or inv"""
url = self.__fetch_url(f"get_daily_logs_{device_type}")
device_key = "invSn" if device_type == "inv" else "tlxSn"
logs, start_index, have_next = [], 0, True
while have_next:
response = self.session.post(
url,
data={
device_key: device_id,
"startDate": date,
"endDate": date,
"start": start_index,
},
)
response_json = response.json()
for log in response_json["obj"]["datas"]:
logs.append(log)
start_index = response_json["obj"]["start"]
have_next = response_json["obj"]["haveNext"]
logs.reverse()
return logs
def get_monthly_energy_data(self, plant_id: str, date: str) -> List[Dict]:
url = self.__fetch_url("get_monthly_energy")
response = self.session.post(
url,
data={
"plantId": plant_id,
"jsonData": json.dumps(
[{"type": "plant", "sn": plant_id, "params": "energy,autoEnergy"}]
),
"date": date,
},
)
return response.json()
def get_daily_energy_data(self, plant_id: str, date: str) -> List[Dict]:
url = self.__fetch_url("get_daily_energy")
response = self.session.post(
url,
data={
"plantId": plant_id,
"jsonData": json.dumps(
[{"type": "plant", "sn": plant_id, "params": "energy,autoEnergy"}]
),
"date": date,
},
)
return response.json()
class Job:
PAC = "pac"
KWH = "kwh"
def __init__(self, conf_path):
self.conf = self.__load_conf(conf_path)
self.__validate_conf(self.conf)
self.api = GrowattApi(self.conf.get("username"), self.conf.get("password"))
def __load_conf(self, conf_path) -> Dict:
conf = {}
with open(conf_path, "r") as file:
conf = json.load(file)
return conf
def __validate_conf(self, conf: Dict):
if not conf.get("username") and not conf.get("password"):
Exception("username/password missing in conf")
if not conf.get("plant_id"):
Exception("plant_id missing in conf")
def diff_month(self, current, previous):
return (current.year - previous.year) * 12 + current.month - previous.month
def backfill_data(self):
self.__create_table() # first create table before starting operations
start_date = datetime.datetime.strptime(
self.conf.get("start_date"), "%Y-%m-%d"
).date()
days = (datetime.datetime.now().date() - start_date).days + 1
months = self.diff_month(datetime.datetime.now().date(), start_date) + 1
logger.info(f"Backfilling Pac for {days} days")
for count in range(days):
self._insert(
self.get_time_series_data_pac(
start_date + datetime.timedelta(days=count)
),
table_name=self.PAC,
)
logger.info(f"Backfilling Kwh for {months} months")
for count in range(months):
self._insert(
self.get_time_series_data_kwh(
start_date + relativedelta.relativedelta(months=count)
),
table_name=self.KWH,
)
def get_time_series_data_pac(self, date: datetime.date) -> List[Tuple]:
data = self.api.get_daily_energy_data(
self.conf.get("plant_id"),
f"{date.year}-{date.month}-{date.day}",
)
date = datetime.datetime.combine(date, datetime.datetime.min.time())
return list(
zip(
# converting to string type to enter DB
# 5 minutes, 288 times ~24 hours
[
(date + datetime.timedelta(minutes=5 * count)).timestamp()
for count in range(288)
],
list(
map(
lambda x: 0 if not x else x,
data.get("obj")[0]["datas"]["pac"],
)
),
)
)
def get_time_series_data_kwh(self, date: datetime.date) -> List[Tuple]:
data = self.api.get_monthly_energy_data(
self.conf.get("plant_id"),
f"{date.year}-{date.month}",
)
date = datetime.datetime.combine(date, datetime.datetime.min.time()).replace(
day=1
)
return list(
zip(
# daily data
[
(date + datetime.timedelta(days=count)).timestamp()
for count in range(calendar.monthrange(date.year, date.month)[1])
],
list(
map(
lambda x: 0 if not x else x,
data.get("obj")[0]["datas"]["energy"],
)
),
)
)
def run(self, backfill=False):
if backfill:
self.backfill_data()
logger.info("Backfilling completed")
sys.exit(0)
today = datetime.datetime.now()
self._insert(self.get_time_series_data_pac(today.date()), table_name=self.PAC)
# last date
self._insert(
self.get_time_series_data_kwh(today.date())[-1], table_name=self.KWH
)
def __create_table(self):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
cursor.execute(f"CREATE TABLE {self.PAC}(timestamp PRIMARY KEY, watt)")
cursor.execute(f"CREATE TABLE {self.KWH}(timestamp PRIMARY KEY, kilowatthour)")
def _insert(self, time_series_data: List[Tuple], table_name: str) -> None:
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
cursor.executemany(
f"INSERT OR REPLACE INTO {table_name} VALUES(?, ?)", time_series_data
)
connection.commit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--conf", action="append", default="config.json")
args = parser.parse_args()
job = Job(args.conf).run(backfill=not pathlib.Path(DATABASE_NAME).exists())
# job.run(not pathlib.Path(DATABASE_NAME).exists())
scheduler.add_job(
job.run,
trigger="interval",
hours=SCHEDULER_INTERVAL,
coalesce=True,
args=(not pathlib.Path(DATABASE_NAME).exists(),),
)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()