- index.html: remove duplicated sections/IDs, restore card CSS, add fetch timeout and error handling, label relay and lithium type values - app.py: close inverter connection in finally, socket timeout, lock plus 2s cache, debug off, friendly missing-config error, HTTP 502 on failure - app.py: fix register scaling per Deye Modbus V118 (battery temp +1000 offset, charge efficiency 0.1%, block 5 off-by-one for 312-325) - read_registers.py: drop hard-coded IP/serial (env or config.py), use argparse type validators, unify INVERTER_SLAVE_ID, document limits - extract_pdf.py: main guard, argparse, output next to script, pypdf in docs/requirements.txt; pin requirements Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
155 lines
6.3 KiB
Python
155 lines
6.3 KiB
Python
import threading
|
|
import time
|
|
|
|
from flask import Flask, jsonify, render_template
|
|
from pysolarmanv5 import PySolarmanV5
|
|
try:
|
|
import config
|
|
except ImportError:
|
|
raise SystemExit(
|
|
"config.py not found. Copy config.py.example to config.py and fill in your inverter details."
|
|
)
|
|
|
|
app = Flask(__name__)
|
|
|
|
SOCKET_TIMEOUT = getattr(config, "INVERTER_SOCKET_TIMEOUT", 5)
|
|
CACHE_TTL = 2.0 # seconds; the logger only tolerates one session at a time
|
|
|
|
_lock = threading.Lock()
|
|
_cache = {"time": 0.0, "data": None}
|
|
|
|
|
|
def to_signed(val):
|
|
return val if val < 32768 else val - 65536
|
|
|
|
|
|
def get_inverter_data():
|
|
"""
|
|
Connects to the inverter and reads voltage registers.
|
|
Returns a dictionary with the data.
|
|
"""
|
|
inverter = None
|
|
try:
|
|
inverter = PySolarmanV5(
|
|
config.INVERTER_IP,
|
|
config.INVERTER_SERIAL,
|
|
port=config.INVERTER_PORT,
|
|
mb_slave_id=config.INVERTER_SLAVE_ID,
|
|
socket_timeout=SOCKET_TIMEOUT,
|
|
verbose=False
|
|
)
|
|
|
|
# Reading blocks:
|
|
# Block 1: 150 - 216 (67 registers) - Voltages, Currents, Battery, PV, Control
|
|
# Block 2: 600 - 627 (28 registers) - BMS Data
|
|
# Block 3: 245 (1 register) - Max Grid Output
|
|
# Block 4: 292 - 293 (2 registers) - Peak Shaving
|
|
# Block 5: 312 - 325 (14 registers) - Lithium battery / BMS limits
|
|
|
|
block1 = inverter.read_holding_registers(register_addr=150, quantity=67)
|
|
block2 = inverter.read_holding_registers(register_addr=600, quantity=28)
|
|
block3 = inverter.read_holding_registers(register_addr=245, quantity=1)
|
|
block4 = inverter.read_holding_registers(register_addr=292, quantity=2)
|
|
block5 = inverter.read_holding_registers(register_addr=312, quantity=14)
|
|
|
|
data = {}
|
|
|
|
# Block 1 Parsing (150-216)
|
|
data["input_voltage"] = round(block1[0] * 0.1, 1) # 150
|
|
data["output_voltage"] = round(block1[4] * 0.1, 1) # 154
|
|
data["load_voltage"] = round(block1[7] * 0.1, 1) # 157
|
|
|
|
data["grid_current"] = round(to_signed(block1[10]) * 0.01, 2) # 160
|
|
data["grid_clamp_current"] = round(to_signed(block1[12]) * 0.01, 2) # 162
|
|
data["output_current"] = round(to_signed(block1[14]) * 0.01, 2) # 164
|
|
|
|
data["load_current"] = round(to_signed(block1[29]) * 0.01, 2) # 179
|
|
|
|
data["batt_temp"] = round((block1[32] - 1000) * 0.1, 1) # 182 (offset +1000, 0.1C)
|
|
data["batt_voltage"] = round(block1[33] * 0.01, 2) # 183
|
|
data["batt_soc"] = block1[34] # 184
|
|
data["batt_charge_status"] = block1[35] # 185
|
|
data["pv1_power"] = block1[36] # 186
|
|
data["pv2_power"] = block1[37] # 187
|
|
|
|
data["batt_power"] = to_signed(block1[40]) # 190
|
|
data["batt_current"] = round(to_signed(block1[41]) * 0.01, 2) # 191
|
|
|
|
data["grid_relay_status"] = block1[44] # 194
|
|
data["gen_relay_status"] = block1[45] # 195
|
|
|
|
data["max_charge_current"] = block1[60] # 210
|
|
data["max_discharge_current"] = block1[61] # 211
|
|
data["batt_charge_efficiency"] = round(block1[66] * 0.1, 1) # 216 (0.1%)
|
|
|
|
# Block 2 Parsing (BMS 600-627)
|
|
data["bms1_soc"] = block2[3] # 603
|
|
data["bms1_charge_voltage"] = round(block2[6] * 0.01, 2) # 606
|
|
data["bms1_charge_current"] = round(block2[7] * 0.1, 1) # 607
|
|
|
|
data["bms2_soc"] = block2[17] # 617
|
|
data["bms2_charge_voltage"] = round(block2[20] * 0.01, 2) # 620
|
|
data["bms2_charge_current"] = round(block2[21] * 0.1, 1) # 621
|
|
|
|
# Block 3 Parsing (245)
|
|
data["max_grid_output_power"] = block3[0] # 245
|
|
|
|
# Block 4 Parsing (292-293)
|
|
data["gen_peak_shaving_power"] = block4[0] # 292
|
|
data["grid_peak_shaving_power"] = block4[1] # 293
|
|
|
|
# Block 5 Parsing (312-325, index = register - 312)
|
|
data["charge_voltage"] = round(block5[0] * 0.01, 2) # 312
|
|
data["discharge_voltage"] = round(block5[1] * 0.01, 2) # 313
|
|
data["charge_current_limit"] = block5[2] # 314
|
|
data["discharge_current_limit"] = block5[3] # 315
|
|
data["real_time_capacity"] = block5[4] # 316
|
|
data["real_time_voltage"] = round(block5[5] * 0.01, 2) # 317
|
|
data["real_time_current"] = to_signed(block5[6]) # 318
|
|
data["real_time_temp"] = round((block5[7] - 1000) * 0.1, 1) # 319 (offset +1000)
|
|
data["max_charge_current_limit"] = block5[8] # 320
|
|
data["max_discharge_current_limit"] = block5[9] # 321
|
|
# 322-324 skipped (alarm / fault / flags)
|
|
data["lithium_battery_type"] = block5[13] # 325
|
|
|
|
data["status"] = "success"
|
|
return data
|
|
|
|
except Exception as e:
|
|
return {
|
|
"status": "error",
|
|
"message": str(e)
|
|
}
|
|
finally:
|
|
if inverter is not None:
|
|
try:
|
|
inverter.disconnect()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def get_cached_data():
|
|
"""Serialize inverter access and reuse a recent successful reading."""
|
|
with _lock:
|
|
now = time.monotonic()
|
|
if _cache["data"] is not None and now - _cache["time"] < CACHE_TTL:
|
|
return _cache["data"]
|
|
data = get_inverter_data()
|
|
if data.get("status") == "success":
|
|
_cache["data"] = data
|
|
_cache["time"] = time.monotonic()
|
|
return data
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/api/data')
|
|
def api_data():
|
|
data = get_cached_data()
|
|
status = 200 if data.get("status") == "success" else 502
|
|
return jsonify(data), status
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=False, host='0.0.0.0')
|