Skip to content

Commit

Permalink
remove dependency and move code the main function (update routine)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7878 committed Nov 14, 2024
1 parent f806c56 commit f7367b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
25 changes: 24 additions & 1 deletion dbus_opendtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ def sign_of_life_all_services(services):
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()
Expand All @@ -152,7 +175,7 @@ def main():
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) # Update every 1000 ms
gobject.timeout_add(1000, update_all_services, services)

logging.info("Connected to dbus, and switching over to gobject.MainLoop() (= event based)")
mainloop = gobject.MainLoop()
Expand Down
17 changes: 5 additions & 12 deletions dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
# victron imports:
import dbus

if sys.version_info.major == 2:
import gobject
else:
from gi.repository import GLib as gobject

sys.path.insert(
1,
os.path.join(
Expand Down Expand Up @@ -143,7 +138,8 @@ def __init__(
onchangecallback=self._handlechangedvalue,
)

gobject.timeout_add(self._get_polling_interval(), self._update)
self.polling_interval = self._get_polling_interval()
self.last_polling = 0

@staticmethod
def get_ac_inverter_state(current):
Expand Down Expand Up @@ -519,10 +515,11 @@ def get_ts_last_success(self, meter_data):

def sign_of_life(self):
logging.debug("Last inverter #%d _update() call: %s", self.pvinverternumber, self._last_update)
logging.info("Last inverter #%d '/Ac/Power': %s", self.pvinverternumber, self._dbusservice["/Ac/Power"])
logging.info("[%s] Last inverter #%d '/Ac/Power': %s", self._servicename,
self.pvinverternumber, self._dbusservice["/Ac/Power"])
return True

def _update(self):
def update(self):
logging.debug("_update")
successful = False
try:
Expand Down Expand Up @@ -560,10 +557,6 @@ def _update(self):
else:
self.last_update_successful = False

# return true, otherwise add_timeout will be removed from GObject - see docs
# http://library.isr.ist.utl.pt/docs/pygtk2reference/gobject-functions.html#function-gobject--timeout-add
return True

def _update_index(self):
if self.dry_run:
return
Expand Down

0 comments on commit f7367b1

Please sign in to comment.