From 28d7a20cdc15277c414a5937fcfd9d7ff216fa14 Mon Sep 17 00:00:00 2001 From: Suthep Pomjaksilp Date: Tue, 26 Aug 2025 18:39:17 +0200 Subject: [PATCH] Rename new_data to emit_state --- docs/source/paradigms.rst | 2 +- examples/datasource_hero.py | 2 +- examples/datasource_observer.py | 2 +- src/heros/datasource/datasource.py | 44 +++++++++++++++--------------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/docs/source/paradigms.rst b/docs/source/paradigms.rst index 8f4de48..41d4b7f 100644 --- a/docs/source/paradigms.rst +++ b/docs/source/paradigms.rst @@ -33,5 +33,5 @@ Datasource ---------- A frequent use case for HERO is that of making data (like sensor data or status data) available to interested peers. -To cover this use case, a special class of HERO exists, the ``DatasourceHERO``. It provides a special event ``new_data`` that is always emitted when new data is available. RemoteHEROs connect to the emitting HERO will get noticed directly and react accordingly. +To cover this use case, a special class of HERO exists, the ``DatasourceHERO``. It provides a special event ``emit_state`` that is always emitted when new data is available. RemoteHEROs connect to the emitting HERO will get noticed directly and react accordingly. In addition also a ``DatasourceObsever`` class exist, that efficiently monitors the events of many HEROs in the network without fully instantiating the RemoteHEROs. diff --git a/examples/datasource_hero.py b/examples/datasource_hero.py index f62cf3f..3f494cd 100644 --- a/examples/datasource_hero.py +++ b/examples/datasource_hero.py @@ -22,7 +22,7 @@ class TestObject(PolledLocalDatasourceHERO): self.testme += 1 return "world" - def _new_data(self): + def _emit_state(self): self.testme += 1 return {"foo": (self.testme, "mW"), "bar": self.foovar} diff --git a/examples/datasource_observer.py b/examples/datasource_observer.py index ee08b09..f7100e9 100644 --- a/examples/datasource_observer.py +++ b/examples/datasource_observer.py @@ -9,7 +9,7 @@ def printer(obj_name, data): print(f"got message from {obj_name}: {data}") -obs.register_new_data_callback(printer) +obs.register_emit_state_callback(printer) while True: time.sleep(1) diff --git a/src/heros/datasource/datasource.py b/src/heros/datasource/datasource.py index 808f691..f4842b4 100644 --- a/src/heros/datasource/datasource.py +++ b/src/heros/datasource/datasource.py @@ -10,13 +10,13 @@ from .observables import ObservableProcessor class LocalDatasourceHERO(LocalHERO): """ A datasource is a HERO that can provide information on a standardized interface. - This interface is the event `new_data`. Instances in the zenoh network interested in the data provided - by data sources can simply subscribe to the key expression `@objects/realm/*/new_data` or use + This interface is the event `emit_state`. Instances in the zenoh network interested in the data provided + by data sources can simply subscribe to the key expression `@objects/realm/*/emit_state` or use the :class:`DatasourceObserver`. To make your class a LocalDatasourceHERO make it inherit this class. This class is meant for datasources that create asynchronous events on their own. When processing such an event - call `new_data` to publish the data from this datasource. + call `emit_state` to publish the data from this datasource. Args: name: name/identifier under which the object is available. Make sure this name is unique in the realm. @@ -32,7 +32,7 @@ class LocalDatasourceHERO(LocalHERO): return self.observable_processor(DatasourceReturnSet.from_data(data)) @event - def new_data(self, data): + def emit_state(self, data): return self._process_data(data) @@ -42,7 +42,7 @@ class DatasourceObserver(HEROPeer): In particular, this class provides an efficient way to listen to the data emitted by all datasource HEROs in the realm. By not instantiating the HEROs themselves but just subscribing to the topics for the datasource, this reduces the pressure on the backing zenoh network. If, however, only the data of a few HEROs should be observed, - it might make more sense to just instantiate the according RemoteHEROs and connect a callback to their `new_data` + it might make more sense to just instantiate the according RemoteHEROs and connect a callback to their `emit_state` signal. Args: @@ -54,15 +54,15 @@ class DatasourceObserver(HEROPeer): HEROPeer.__init__(self, *args, **kwargs) self._object_selector = object_selector - self._new_data_callbacks = [] + self._emit_state_callbacks = [] - zenoh_selector = "/".join([self._ns_objects, self._realm, object_selector, "new_data"]) - self._subscription = self._subscribe_selector(zenoh_selector, self._handle_new_data) + zenoh_selector = "/".join([self._ns_objects, self._realm, object_selector, "emit_state"]) + self._subscription = self._subscribe_selector(zenoh_selector, self._handle_emit_state) - def _handle_new_data(self, key_expr: str, data): - for cb in self._new_data_callbacks: + def _handle_emit_state(self, key_expr: str, data): + for cb in self._emit_state_callbacks: try: - object_name = object_name_from_keyexpr(str(key_expr), self._ns_objects, self._realm, "new_data") + object_name = object_name_from_keyexpr(str(key_expr), self._ns_objects, self._realm, "emit_state") try: data = DatasourceReturnSet([DatasourceReturnValue(**d) for d in data]) except Exception: @@ -71,24 +71,24 @@ class DatasourceObserver(HEROPeer): except Exception as e: log.error(f"Could not call callback {cb} for new data: {e}") - def register_new_data_callback(self, func: callable): + def register_emit_state_callback(self, func: callable): """ Register a callback that should be called when a new HERO joins the realm. Args: func: function to call when a new HERO joins the realm """ - if func not in self._new_data_callbacks: - self._new_data_callbacks.append(func) + if func not in self._emit_state_callbacks: + self._emit_state_callbacks.append(func) class PolledLocalDatasourceHERO(LocalDatasourceHERO): """ - This local HERO periodically triggers the event "new_data" to poll and publish data. + This local HERO periodically triggers the event "emit_state" to poll and publish data. This class is meant for datasources that do not generate events on their own an thus should be polled on a periodical basis. - To make your class a PolledLocalDatasourceHERO make it inherit this class an implement the method `_new_data`. + To make your class a PolledLocalDatasourceHERO make it inherit this class an implement the method `_emit_state`. The method will get called periodically and the return value will be published as an event. Note: @@ -98,7 +98,7 @@ class PolledLocalDatasourceHERO(LocalDatasourceHERO): Args: name: name/identifier under which the object is available. Make sure this name is unique in the realm. realm: realm the HERO should exist in. default is "heros" - interval: time interval in seconds between consecutive calls of `new_data` event + interval: time interval in seconds between consecutive calls of `emit_state` event """ def __init__(self, *args, loop, interval: float = 5, **kwargs): @@ -110,12 +110,12 @@ class PolledLocalDatasourceHERO(LocalDatasourceHERO): async def _trigger_datasource(self): while True: - self.new_data() + self.emit_state() await asyncio.sleep(self.datasource_interval) @event - def new_data(self): - return self._process_data(self._new_data()) + def emit_state(self): + return self._process_data(self._emit_state()) - def _new_data(self): - raise NotImplementedError("Implement _new_data() in a subclass of PolledLocalDatasourceHERO") + def _emit_state(self): + raise NotImplementedError("Implement _emit_state() in a subclass of PolledLocalDatasourceHERO") -- GitLab