-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
91 lines (67 loc) · 3.03 KB
/
data.py
File metadata and controls
91 lines (67 loc) · 3.03 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
import argparse
import logging
import asyncio
from typing import Set, Type
import time
from solax import discover, RealTimeAPI, Inverter
from solax.discovery import DiscoveryError
from solax.inverter import InverterError
from config import SOLAX_HOST, SOLAX_PORT, SOLAX_PASSWORD
from my_inverter import X1LiteLV
logging.basicConfig(level=logging.DEBUG)
INVERTER = X1LiteLV
async def real_time_api(ip_address, port=80, pwd="", inverters=None):
if inverters is None:
inverters = {}
i = await discover(ip_address, port, pwd, return_when=asyncio.FIRST_COMPLETED, inverters=inverters)
return RealTimeAPI(i)
async def work(host, password, port=80, inverters=None):
if inverters is None:
inverters = {}
r = await real_time_api(host, port=port, pwd=password, inverters=inverters)
return await r.get_data()
def display_data(data, num_columns = 4):
# Calculate max key and value lengths dynamically
max_key_length = max(len(key) for key in data.keys()) + 2
max_value_length = max(len(str(value)) for value in data.values()) + 2
# Convert dictionary into a list of key-value pairs
items = list(data.items())
# Split data into approximately equal chunks per column
rows_per_column = len(items) // num_columns + (1 if len(items) % num_columns > 0 else 0)
columns = [items[i * rows_per_column:(i + 1) * rows_per_column] for i in range(num_columns)]
# Print results
for row_index in range(rows_per_column):
row = []
for col in columns:
if row_index < len(col):
row.append(f"{col[row_index][0]:<{max_key_length}}: {col[row_index][1]:<{max_value_length}}")
else:
row.append(" " * (max_key_length + max_value_length + 2)) # Placeholder for empty columns
print(" | ".join(row))
def run_periodically(url, password, interval):
while True:
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
info = loop.run_until_complete(work(url, password, inverters={INVERTER}))
display_data(info.data)
except (InverterError, DiscoveryError) as e:
print(e)
# Wait for the defined interval
time.sleep(interval)
# Main function to parse arguments and run the script
def main():
# Argument parser for command-line arguments
parser = argparse.ArgumentParser(description="Periodically perform curl requests for realtime data.")
parser.add_argument("--host", help="The IP of the Solax server.", default=SOLAX_HOST)
parser.add_argument("--password", help="The password to include in the POST request.", default=SOLAX_PASSWORD)
parser.add_argument("--interval", type=int, default=60, help="Interval in seconds between requests (default is 60).")
args = parser.parse_args()
# Extract parameters from command-line arguments
host = args.host
password = args.password
interval = args.interval
# Start the periodic execution
run_periodically(host, password, interval)
if __name__ == '__main__':
main()