forked from mpepping/solarman-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.py
225 lines (200 loc) · 7.01 KB
/
run.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
"""
Collect PV data from the SolarmanPV API and send Power+Energy data (W+kWh) to MQTT
"""
import argparse
import http.client
import json
import logging
import sys
import time
import mqtt
logging.basicConfig(level=logging.INFO)
def load_config(file):
"""
Load configuration
:return:
"""
with open(file, "r", encoding="utf-8") as config_file:
config = json.load(config_file)
return config
def today():
"""
Return date in YYYY-MM-DD
:return:
"""
date = time.strftime("%Y-%m-%d")
return date
def get_token(url, appid, secret, username, passhash):
"""
Get a token from the API
:return: access_token
"""
try:
conn = http.client.HTTPSConnection(url)
payload = json.dumps({
"appSecret": secret,
"email": username,
"password": passhash
})
headers = {
'Content-Type': 'application/json'
}
url = f"//account/v1.0/token?appId={appid}&language=en"
conn.request("POST", url, payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
logging.debug("Received token")
return data["access_token"]
except Exception as error: # pylint: disable=broad-except
logging.error("Unable to fetch token: %s", str(error))
sys.exit(1)
def get_station_realtime(url, stationid, token):
"""
Return station realtime data
:return: realtime data
"""
conn = http.client.HTTPSConnection(url)
payload = json.dumps({
"stationId": stationid
})
headers = {
'Content-Type': 'application/json',
'Authorization': "bearer " + token
}
conn.request("POST", "//station/v1.0/realTime?language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
return data
def get_device_current_data(url, device_sn, token):
"""
Return device current data
:return: current data
"""
conn = http.client.HTTPSConnection(url)
payload = json.dumps({
"deviceSn": device_sn
})
headers = {
'Content-Type': 'application/json',
'Authorization': "bearer " + token
}
conn.request("POST", "//device/v1.0/currentData?language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
return data
def restruct_and_separate_current_data(data):
"""
Return restructured and separated device current data
Original data is removed
:return: new current data
"""
new_data_list = {}
if data["dataList"]:
data_list = data["dataList"]
for i in data_list:
del i["key"]
name = i["name"]
name = name.replace(" ", "_")
del i["name"]
new_data_list[name] = i["value"]
del data["dataList"]
return new_data_list
def single_run(file):
"""
Output current watts and kilowatts
:return:
"""
config = load_config(file)
token = get_token(
config["url"],
config["appid"],
config["secret"],
config["username"],
config["passhash"]
)
station_data = get_station_realtime(config["url"], config["stationId"], token)
inverter_data = get_device_current_data(config["url"], config["inverterId"], token)
logger_data = get_device_current_data(config["url"], config["loggerId"], token)
inverter_data_list = restruct_and_separate_current_data(inverter_data)
logger_data_list = restruct_and_separate_current_data(logger_data)
if config["debug"]:
logging.info(json.dumps(station_data, indent=4, sort_keys=True))
logging.info(json.dumps(inverter_data, indent=4, sort_keys=True))
logging.info(json.dumps(inverter_data_list, indent=4, sort_keys=True))
logging.info(json.dumps(logger_data, indent=4, sort_keys=True))
logging.info(json.dumps(logger_data_list, indent=4, sort_keys=True))
discard = ["code", "msg", "requestId", "success"]
topic = config["mqtt"]["topic"]
_t = time.strftime("%Y-%m-%d %H:%M:%S")
inverter_device_state = inverter_data["deviceState"]
if inverter_device_state == 1:
logging.info("%s - Inverter DeviceState: %s -> Publishing MQTT...",
_t, inverter_device_state)
for i in station_data:
if station_data[i]:
if i not in discard:
mqtt.message(config["mqtt"], topic+"/station/" + i, station_data[i])
for i in inverter_data:
if inverter_data[i]:
if i not in discard:
mqtt.message(config["mqtt"], topic+"/inverter/" + i, inverter_data[i])
if inverter_data_list:
mqtt.message(config["mqtt"], topic+"/inverter/attributes", json.dumps(inverter_data_list))
for i in logger_data:
if logger_data[i]:
if i not in discard:
mqtt.message(config["mqtt"], topic+"/logger/" + i, logger_data[i])
if logger_data_list:
mqtt.message(config["mqtt"], topic+"/logger/attributes", json.dumps(logger_data_list))
else:
mqtt.message(config["mqtt"], topic+"/inverter/deviceState", inverter_data["deviceState"])
mqtt.message(config["mqtt"], topic+"/logger/deviceState", logger_data["deviceState"])
logging.info("%s - Inverter DeviceState: %s"
"-> Only status MQTT publish (probably offline due to nighttime shutdown)",
_t, inverter_device_state)
def daemon(file, interval):
"""
Run as a daemon process
:param file: Config file
:param interval: Run interval in seconds
:return:
"""
interval = int(interval)
logging.info("Starting daemonized with a %s seconds run interval", str(interval))
while True:
try:
single_run(file)
time.sleep(interval)
except Exception as error: # pylint: disable=broad-except
logging.error("Error on start: %s", str(error))
sys.exit(1)
def main():
"""
Main
:return:
"""
parser = argparse.ArgumentParser(description="Collect data from Trannergy / Solarman API")
parser.add_argument("-d", "--daemon",
action="store_true",
help="run as a service")
parser.add_argument("-s", "--single",
action="store_true",
help="single run and exit")
parser.add_argument("-i", "--interval",
default="300",
help="run interval in seconds (default 300 sec.)")
parser.add_argument("-f", "--file",
default="config.json",
help="config file (default ./config.json)")
parser.add_argument("-v", "--version",
action='version',
version='%(prog)s 0.0.1')
args = parser.parse_args()
if args.single:
single_run(args.file)
elif args.daemon:
daemon(args.file, args.interval)
else:
parser.print_help(sys.stderr)
if __name__ == '__main__':
main()