-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbus-renogy-dcc.py
executable file
·237 lines (195 loc) · 8.18 KB
/
dbus-renogy-dcc.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
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
# If editing then use
# svc -d /service/dbus-renogy-dcc and
# svc -u /service/dbus-renogy-dcc
# to stop and restart the service
import logging
import os
import platform
import sys
from pprint import pformat
from typing import Tuple
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import renogy
# Victron packages
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "./ext/velib_python"))
from vedbus import VeDbusService
from ve_utils import exit_on_error
# Init logging
logger = logging.getLogger("dbus-renogy-dcc")
logger.setLevel(logging.INFO)
GLIB_INTERVAL = 5000
DEVICE_RETRIES = 5
class NoDeviceFoundError(Exception):
pass
class Driver:
device_connected = False
device_response_error_count = 0
def __init__(self, connection: str, device_instance: int = 0):
self.connection = connection
self.device_instance = device_instance
self._bus = (
dbus.SessionBus()
if "DBUS_SESSION_BUS_ADDRESS" in os.environ
else dbus.SystemBus()
)
self.slave_address, data = self.discover_slave_address()
self.init_dbus(data)
def discover_slave_address(self) -> Tuple[int, dict]:
slave_address = 0
print("Discovering slave address")
while slave_address < 255:
print(f"Trying slave address: {slave_address}")
try:
data = renogy.Device(
port=self.connection,
slaveaddress=slave_address,
).all()
print(f"Slave address found: {slave_address}")
return slave_address, data
except:
slave_address += 1
raise NoDeviceFoundError("No device found on any slave address")
def init_dbus(self, data: dict):
self.dbus = VeDbusService(
f"com.victronenergy.solarcharger.{self.connection.split('/')[-1]}",
self._bus,
)
# Create the management paths
self.dbus.add_path("/Mgmt/ProcessName", __file__)
self.dbus.add_path(
"/Mgmt/ProcessVersion",
"Unkown version, and running on Python " + platform.python_version(),
)
self.dbus.add_path("/Mgmt/Connection", self.connection)
self.dbus.add_path("/DeviceInstance", self.device_instance)
self.dbus.add_path("/ProductId", 0)
self.dbus.add_path("/ProductName", data["product_model"])
self.dbus.add_path("/Serial", data["product_serial_number"])
self.dbus.add_path("/FirmwareVersion", data["product_software_version"])
self.dbus.add_path("/HardwareVersion", data["product_hardware_version"])
self.dbus.add_path("/Connected", 1)
power_solar = data["voltage_solar"] * data["current_solar"]
power_alternator = data["voltage_alternator"] * data["current_alternator"]
power_total = power_solar + power_alternator
# Create the paths for the solar charger
self.dbus.add_path("/Pv/0/V", data["voltage_solar"])
self.dbus.add_path("/Pv/0/P", power_solar)
self.dbus.add_path("/Pv/0/MppOperationMode", 2)
# Create the paths for the DC/DC charger
self.dbus.add_path("/Pv/1/V", data["voltage_alternator"])
self.dbus.add_path("/Pv/1/P", power_alternator)
self.dbus.add_path("/Pv/1/MppOperationMode", 2)
# Create the paths for the battery
self.dbus.add_path("/Dc/0/Voltage", data["battery_voltage"])
self.dbus.add_path(
"/Dc/0/Current", round(power_total / data["battery_voltage"], 2)
)
# Create the device paths
self.dbus.add_path("/Yield/Power", power_total)
self.dbus.add_path("/MppOperationMode", 2)
# /MppOperationMode 0 = Off
# 1 = Voltage or Current limited
# 2 = MPPT Tracker active
if data["charge_state"]["mppt_charging"]:
self.dbus.add_path("/State", 3)
elif data["charge_state"]["boost"]:
self.dbus.add_path("/State", 4)
elif data["charge_state"]["float"]:
self.dbus.add_path("/State", 5)
elif data["charge_state"]["equalization"]:
self.dbus.add_path("/State", 7)
# /State 0=Off
# 2=Fault
# 3=Bulk
# 4=Absorption
# 5=Float
# 6=Storage
# 7=Equalize
# 252=External control
self.dbus.add_path("/Yield/User", round(data["power_daily"] / 1000, 3))
self.dbus.add_path("/Yield/System", round(data["power_total"] / 1000, 2))
def update(self) -> bool:
try:
data = renogy.Device(
port=self.connection,
slaveaddress=self.slave_address,
).all()
self.device_response_error_count = 0
# If the device was previously not connected, then set it to connected
if not self.device_connected:
self.device_connected = True
self.dbus["/Connected"] = 1
except:
logger.exception("Device unresponsive. Retrying")
self.device_response_error_count += 1
# If we hit our error limit, then set the device to disconnected
if (
self.device_connected
and self.device_response_error_count > DEVICE_RETRIES
):
self.device_connected = False
self.dbus["/Connected"] = 0
raise
return True
logger.info(f"Device data: {pformat(data)}")
self.dbus["/ProductName"] = data["product_model"]
self.dbus["/HardwareVersion"] = data["product_hardware_version"]
self.dbus["/FirmwareVersion"] = data["product_software_version"]
self.dbus["/Serial"] = data["product_serial_number"]
power_solar = data["voltage_solar"] * data["current_solar"]
power_alternator = data["voltage_alternator"] * data["current_alternator"]
power_total = power_solar + power_alternator
if data["charge_state"]["no_charging"]:
# 0 = Off
self.dbus["/MppOperationMode"] = 0
elif data["charge_state"]["current_limited"]:
# 1 = Voltage or Current limited
self.dbus["/MppOperationMode"] = 1
else:
# 2 = MPPT Tracker active
self.dbus["/MppOperationMode"] = 2
self.dbus["/Pv/0/V"] = data["voltage_solar"]
self.dbus["/Pv/0/P"] = power_solar
self.dbus["/Pv/0/MppOperationMode"] = 2
self.dbus["/Pv/1/V"] = data["voltage_alternator"]
self.dbus["/Pv/1/P"] = power_alternator
self.dbus["/Yield/Power"] = round(power_total, 1)
# solar_service["/MppOperationMode"] =
self.dbus["/Dc/0/Voltage"] = data["battery_voltage"]
self.dbus["/Dc/0/Current"] = round(power_total / data["battery_voltage"], 2)
if data["charge_state"]["mppt_charging"]:
self.dbus["/State"] = 3
elif data["charge_state"]["boost"]:
self.dbus["/State"] = 4
elif data["charge_state"]["float"]:
self.dbus["/State"] = 5
elif data["charge_state"]["equalization"]:
self.dbus["/State"] = 7
self.dbus["/Yield/User"] = round(data["power_daily"] / 1000, 3)
self.dbus["/Yield/System"] = round(data["power_total"] / 1000, 2)
return True
def main() -> None:
logger.info(__file__ + " is starting up")
# Get the first argument as the connection
if len(sys.argv) < 2:
logger.error("No connection provided. Exiting.")
sys.exit(1)
connection = sys.argv[1]
# Setup the dbus main loop
DBusGMainLoop(set_as_default=True)
# Create the device
device = Driver(connection=connection)
# Do a first update so that all the readings appear.
device.update()
# Setup periodic updates
GLib.timeout_add(GLIB_INTERVAL, exit_on_error, device.update)
# Run the main loop
mainloop = GLib.MainLoop()
logger.info("Connected to dbus, and switching over to GLib.MainLoop().")
mainloop.run()
if __name__ == "__main__":
main()