-
Notifications
You must be signed in to change notification settings - Fork 36
/
dbus_opendtu.py
193 lines (151 loc) · 6.14 KB
/
dbus_opendtu.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
#!/usr/bin/env python
'''module to read data from dtu/template and show in VenusOS'''
from imports import *
def getConfig():
"""
Reads the configuration from a config.ini file and sets up logging.
The function reads the configuration file located in the same directory as the script.
It configures the logging level based on the value specified in the configuration file.
Returns:
configparser.ConfigParser: The configuration object containing the parsed configuration.
"""
# configure logging
config = configparser.ConfigParser()
config.read(f"{(os.path.dirname(os.path.realpath(__file__)))}/config.ini")
logging_level = config["DEFAULT"]["Logging"].upper()
logging.basicConfig(
format="%(levelname)s %(message)s",
level=logging_level,
)
return config
def get_DbusServices(config):
"""
Retrieves and registers D-Bus services based on the provided configuration.
Args:
config (dict): Configuration dictionary containing the necessary settings.
Returns:
list: A list of registered DbusService instances.
"""
services = []
# region Get the configuration values
try:
number_of_inverters = int(config["DEFAULT"]["NumberOfInvertersToQuery"])
except (KeyError, ValueError) as ex:
logging.warning("NumberOfInvertersToQuery: %s", ex)
logging.warning("NumberOfInvertersToQuery not set, using default")
number_of_inverters = 0
try:
number_of_templates = int(config["DEFAULT"]["NumberOfTemplates"])
except (KeyError, ValueError) as ex:
logging.warning("NumberOfTemplates: %s", ex)
logging.warning("NumberOfTemplates not set, using default")
number_of_templates = 0
try:
dtuvariant = config["DEFAULT"]["DTU"]
except KeyError:
logging.critical("DTU key not found in configuration")
return
# endregion
# region Register the inverters
if dtuvariant != constants.DTUVARIANT_TEMPLATE:
logging.info("Registering dtu devices")
servicename = get_config_value(config, "Servicename", "INVERTER", 0, "com.victronenergy.pvinverter")
service = DbusService(
servicename=servicename,
actual_inverter=0,
)
services.append(service)
if number_of_inverters == 0:
# pylint: disable=W0621
number_of_inverters = service.get_number_of_inverters()
# If there are no inverters or templates, return an empty list
if number_of_inverters == 0 and number_of_templates == 0:
logging.critical("No inverters or templates to query")
return [] # Empty list
if number_of_inverters > 1:
# start our main-service if there are more than 1 inverter
for actual_inverter in range(number_of_inverters - 1):
servicename = get_config_value(
config,
"Servicename",
"INVERTER",
actual_inverter + 1,
"com.victronenergy.pvinverter"
)
services.append(DbusService(
servicename=servicename,
actual_inverter=actual_inverter + 1,
))
# endregion
# region Register the templates
for actual_template in range(number_of_templates):
logging.critical("Registering Templates")
servicename = get_config_value(
config,
"Servicename",
"TEMPLATE",
actual_template,
"com.victronenergy.pvinverter"
)
services.append(DbusService(
servicename=servicename,
actual_inverter=actual_template,
istemplate=True,
))
# endregion
return services
def sign_of_life_all_services(services):
"""
Sends a 'sign of life' signal to all services in the provided list.
Args:
services (list): A list of service objects. Each service object must have a 'sign_of_life' method.
Returns:
bool: Always returns True to keep the timeout active.
"""
for service in services:
service.sign_of_life()
return True
def update_all_services(services):
"""
Updates all services in the provided list.
Args:
services (list): A list of service objects.
Each service object must have an 'update' method and
a 'polling_interval' and a 'polling_last_polling' attribute.
Returns:
bool: Always returns True to keep the timeout active.
"""
if sys.version_info.major == 2:
current_time = gobject.get_current_time()
else:
current_time = gobject.get_real_time() // 1000
for service in services:
if current_time - service.last_polling >= service.polling_interval:
service.update()
service.last_polling = current_time
return True
def main():
""" Main function """
config = getConfig()
signofliveinterval = int(get_config_value(config, "SignOfLifeLog", "DEFAULT", "", 1))
logging.debug("SignOfLifeLog: %d", signofliveinterval)
# TODO: I think it is better to run the tests inside CI/CD pipeline instead of running it here
# tests.run_tests()
try:
logging.info("Start")
from dbus.mainloop.glib import DBusGMainLoop # pylint: disable=E0401,C0415
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
services = get_DbusServices(config)
logging.info("Registered %d services", len(services))
# Use a single timeout to call sign_of_life for all services
gobject.timeout_add(signofliveinterval * 60 * 1000, sign_of_life_all_services, services)
# Use another timeout to update all services
gobject.timeout_add(1000, update_all_services, services)
logging.info("Connected to dbus, and switching over to gobject.MainLoop() (= event based)")
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as error: # pylint: disable=W0718
logging.critical("Error at %s", "main", exc_info=error)
if __name__ == "__main__":
main()