-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar_reader_v2.py
230 lines (176 loc) · 5.45 KB
/
solar_reader_v2.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
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
import requests
import signal
import mysql.connector
from mysql.connector import Error
import threading
import time
import logging
import sys
running = True
poll_rate = 3
scrapers = None
database = None
mycursor = None
"""
Online URLs
'auth_url', 'https://webportal-backend.delios-srl.it/authenticate',
'api_url', 'https://webportal-backend.delios-srl.it/machine_logs/get_machine_log_general',
"""
auth_payload = {
'email': 'john.timmins@icloud.com',
'password': 'j2Pe25fdZecx'
}
sites_to_scrape = [
{
'auth_url': 'https://webportal-backend.delios-srl.it/authenticate',
'api_url': 'https://webportal-backend.delios-srl.it/machine_logs/get_machine_log_general',
},
]
def intialise_database():
global mycursor, database
try:
database = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="lol12345",
)
mycursor = database.cursor()
with open('init_database.sql') as f:
mycursor.execute(f.read())
database.close()
except:
print('failed to create database')
quit()
try:
connect_to_database()
with open('init_table.sql') as f:
mycursor.execute(f.read())
database.commit()
except:
print('failed to create and intialise data table, but data base might exist')
quit()
def main():
if len(sys.argv) < 2:
print('no valid argument passed. Use these...')
print('init -> to intialise solar reader database')
print('run -> to start program')
quit()
elif sys.argv[1] == 'help':
print('init -> to intialise solar reader')
print('run -> to start program')
quit()
elif sys.argv[1] == 'init':
print('setting up database...')
intialise_database()
quit()
elif sys.argv[1] != 'run':
print('no valid argument passed. Use these...')
print('init -> to intialise solar reader database')
print('run -> to start program')
quit()
init()
while running:
try:
run()
except Exception as e:
print(str(e))
if str(e) == 'POLLING_FAILED':
try:
re_login_scrapers()
except:
print('failed to log back in')
if str(e) == 'STORING_FAILED':
try:
connect_to_database()
except:
print('failed to connect to database')
time.sleep(poll_rate)
def re_login_scrapers():
global scrapers
for scraper in scrapers:
scraper.login()
def init():
global scrapers
try:
connect_to_database()
except:
print('failed to connect to database')
quit()
try:
scrapers = create_scrapers()
except:
print('failed to intialise scraper(s)')
quit()
def run():
global scrapers
data_list = []
for scraper in scrapers:
try:
data = scraper.poll_api()
data_list.append(data)
except:
raise Exception('POLLING_FAILED')
try:
store(format_data(data_list))
except:
raise Exception('STORING_FAILED')
class Scraper():
def __init__(self, auth_url, api_url):
self.api_url = api_url
self.auth_url = auth_url
self.session = requests.Session()
self.login()
def login(self):
response = self.session.post(self.auth_url, data=auth_payload, timeout=10)
token = response.json()['token']
self.data_request_payload = {"plant_id":548,"machine_id":""}
self.session.headers['Authorization'] = 'Bearer ' + token
def poll_api(self):
return self.session.post(self.api_url, data=self.data_request_payload, timeout=10).json()
def create_scrapers():
new_scrapers = []
for site in sites_to_scrape:
scraper = Scraper(site['auth_url'], site['api_url'])
new_scrapers.append(scraper)
return new_scrapers
def connect_to_database():
global database, mycursor
try:
database = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="lol12345",
database="solar"
)
mycursor = database.cursor()
except Error as e:
print("Failed to connect to database")
def format_data(data_list):
formated_data = {}
if(len(data_list) == 1):
return data_list[0]
elif(len(data_list) == 0):
raise("no data to format...")
for data in data_list:
for key in data:
if key in formated_data:
formated_data[key] = formated_data[key] + data[key]
else:
formated_data[key] = data[key]
formated_data['percentbattery'] = formated_data['percentbattery'] / len(data_list)
formated_data['energy_battery_char'] = formated_data['energy_battery_char'] / len(data_list)
formated_data['energy_battery_discha'] = formated_data['energy_battery_discha'] / len(data_list)
formated_data['self_sufficiency'] = formated_data['self_sufficiency'] / len(data_list)
return formated_data
def store(data):
for key in data:
mycursor.execute("update data set value=" + str(data[key]) + " where type='" + key + "';")
database.commit()
def signal_handler(signal, frame):
global running
running = False
quit()
signal.signal(signal.SIGINT, signal_handler)
# MAIN START
main()
quit()