-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
113 lines (89 loc) · 2.95 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
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
from bs4 import BeautifulSoup
import json
import os
import paho.mqtt.client as mqtt
import requests
import time
SUBSYSTEM = "subsystem/inverters"
COMBOX_IP = "192.168.0.150"
COMBOX_USER = "admin"
COMBOX_PASS = os.environ["COMBOX_PASS"]
SESSION = requests.Session() #session to maintain login cookies
# Logic
# External and internal states
e_state = {
"state": "STOPPED",
"load": "0"
}
i_state = {
"target_state": "STOPPED"
}
# puts all the data in the list *fields* into a json object
# with two keys (device names XW8548-61 and XW8548-61), containing all the data
# then publishes it to subsystem/inverter
def send_inverter_json(fields):
inverter1_data = [x for x in fields if "400858" in x]
inverter2_data = [x for x in fields if "385892" in x]
inverter_json = {}
inverter1_json = {}
inverter2_json = {}
inverter_json = {
"state": "RUNNING",
"inverters": { "0": {}, "1": {} }
}
try:
for i in inverter1_data:
i = i.replace("(400858)", "")
halves = i.split("=")
inverter1_json[halves[0]] = halves[1]
inverter_json["inverters"]["0"] = {
"voltage": inverter1_json["XW.VDCIN"],
"wattage": inverter1_json["XW.PACLOAD2"],
"temp": inverter1_json["XW.TBATT"],
}
except:
pass
try:
for i in inverter2_data:
i = i.replace("(385892)", "")
halves = i.split("=")
inverter2_json[halves[0]] = halves[1]
inverter_json["inverters"]["1"] = {
"voltage": inverter2_json["XW.VDCIN"],
"wattage": inverter2_json["XW.PACLOAD2"],
"temp": inverter2_json["XW.TBATT"],
}
except:
pass
print inverter_json, "\n\n"
client.publish(SUBSYSTEM, json.dumps(inverter_json))
#scrape status from combox page and send it out
def logic_loop(client):
values_URL = "http://" + COMBOX_IP +"/gethandler.json?name=XBGATEWAY.VARLIST"
values = SESSION.get(values_URL).content
values = values.replace("�D;�A", "")
values_json = json.loads(values)
intermediate = values_json["values"]
intermediate = intermediate["XBGATEWAY.VARLIST"]
values = intermediate.split(";")
send_inverter_json(values)
time.sleep(0.1);
#needed for the interface, doesn't really do anything as of now
def on_message(client, unknown, message):
pass
# Setup scrape
login_page = None
while login_page == None or login_page.status_code == 301:
login_page = SESSION.post("http://" + COMBOX_IP + "/login.cgi", data = { "submit": "Log In", "login_username": COMBOX_USER, "login_password": COMBOX_PASS })
# print login_page.status_code
# main_page = SESSION.get("http://" + COMBOX_IP + "/main.html")
# print main_page.content
# Setup client
client = mqtt.Client()
client.connect("192.168.0.100", 1883)
client.loop_start()
client.on_message = on_message
client.subscribe(SUBSYSTEM + "/#")
while True:
logic_loop(client)
client.loop_stop()