-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·80 lines (67 loc) · 2.51 KB
/
main.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
#!/usr/bin/env python3
import solarModbus, solarWeb
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import os
DEYE_IP = ""
DEYE_USER = ""
DEYE_PASSWORD = ""
def buildSite(url, user, password):
# Start Time
st = time.time()
prom_output = "# Solar Exporter\n"
dataWeb = solarWeb.doTryWebWatt(url, user, password)
if (dataWeb != None):
prom_output += "watt {}\n".format(dataWeb.webdata_now_p)
prom_output += "today_e {}\n".format(dataWeb.webdata_today_e)
prom_output += "total_e {}\n".format(dataWeb.webdata_total_e)
prom_output += "online 1"
# Die erweiterten Daten nur holen, wenn das Geraet auch online ist
dataModbus = solarModbus.getSolarData(url)
if (dataModbus):
prom_output += "\nvoltage{panel=\"1\"} " + str(dataModbus.p1Voltage)
prom_output += "\ncurrent{panel=\"1\"} " + str(dataModbus.p1Current)
prom_output += "\nvoltage{panel=\"2\"} " + str(dataModbus.p2Voltage)
prom_output += "\ncurrent{panel=\"2\"} " + str(dataModbus.p2Current)
prom_output += "\ntemperature " + str(dataModbus.temperature)
else:
prom_output += "watt 0\nonline 0"
# End Time
et = time.time()
prom_output += "\nexecutiontime " + str(round (et - st))
return prom_output
class doWeb(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
if self.path == "/test":
self.wfile.write("ok".encode())
return
if self.path == "/favicon.ico":
return
self.wfile.write(buildSite(DEYE_IP, DEYE_USER, DEYE_PASSWORD).encode())
return
def log_message(self, format, *args):
return
if __name__ == '__main__':
DEYE_IP = os.getenv('DEYE_IP', "")
DEYE_USER = os.getenv('DEYE_USER', "admin")
DEYE_PASSWORD = os.getenv('DEYE_PASSWORD', "admin")
if (DEYE_IP==""):
print("No Config found, please set Environment variables like this:")
print("DEYE_IP=192.168.2.15")
print("DEYE_USER=admin")
print("DEYE_PASSWORD=admin")
exit(255)
print("DEYE_IP:", DEYE_IP)
print("DEYE_USER:", DEYE_USER)
print("DEYE_PASSWORD:", DEYE_PASSWORD)
httpdserver = HTTPServer(('0.0.0.0', 9942), doWeb)
try:
print("Start Webserver")
httpdserver.serve_forever()
except KeyboardInterrupt:
pass
httpdserver.server_close()
print("End Webserver")