@@ -149,7 +149,9 @@ def __init__(self, protocol: str, address: str, port: int,
149149 if kwargs .get ('enable_mqtt' ):
150150 if kwargs .get ('mqtt_port' ) is not None :
151151 self ._mqtt_port = kwargs .get ('mqtt_port' )
152- self ._mqtt_client = MQTTCommClient (url = self .address , port = self ._mqtt_port )
152+ self ._mqtt_client = MQTTCommClient (url = self .address , port = self ._mqtt_port , client_id_suffix = uuid .uuid4 ().hex ,)
153+ self ._mqtt_client .connect ()
154+ self ._mqtt_client .start ()
153155 # self._mqtt_client = MQTTCommClient(url=self.address + self.server_root, port=self._mqtt_port,
154156 # username=username, password=password, )
155157
@@ -249,6 +251,7 @@ class Status(Enum):
249251 STOPPING = "stopping"
250252 STOPPED = "stopped"
251253
254+
252255class StreamableModes (Enum ):
253256 PUSH = "push"
254257 PULL = "pull"
@@ -340,7 +343,12 @@ def init_mqtt(self):
340343 logging .warning (f"No MQTT client configured for streamable resource { self ._id } ." )
341344 return
342345
343- self .get_mqtt_topic ()
346+ self ._mqtt_client .set_on_subscribe (self ._default_on_subscribe )
347+
348+ # self.get_mqtt_topic()
349+
350+ def _default_on_subscribe (self , client , userdata , mid , granted_qos , properties ):
351+ print ("OSH Subscribed: " + str (mid )+ " " + str (granted_qos ))
344352
345353 def get_mqtt_topic (self , subresource : APIResourceTypes | None = None ):
346354 """
@@ -468,7 +476,7 @@ def subscribe_mqtt(self, topic: str, qos: int = 0):
468476 if self ._mqtt_client is None :
469477 logging .warning (f"No MQTT client configured for streamable resource { self ._id } ." )
470478 return
471- self ._mqtt_client .subscribe (topic , qos = qos , msg_callback = self ._message_handler )
479+ self ._mqtt_client .subscribe (topic , qos = qos , msg_callback = self ._mqtt_sub_callback )
472480
473481 def _publish_mqtt (self , topic , payload ):
474482 if self ._mqtt_client is None :
@@ -502,6 +510,11 @@ def publish(self, payload):
502510 pass
503511
504512
513+ def _mqtt_sub_callback (self , client , userdata , msg ):
514+ print (f"Received MQTT message on topic { msg .topic } : { msg .payload } " )
515+ self ._msg_reader_queue .put_nowait (msg .payload )
516+
517+
505518class System (StreamableResource [SystemResource ]):
506519 name : str
507520 label : str
@@ -534,8 +547,10 @@ def __init__(self, name: str, label: str, urn: str, parent_node: Node, **kwargs)
534547 # self.underlying_resource = self._sys_resource
535548
536549 def discover_datastreams (self ) -> list [DatastreamResource ]:
537- res = self ._parent_node .get_api_helper ().retrieve_resource (
538- APIResourceTypes .DATASTREAM , req_headers = {})
550+ # res = self._parent_node.get_api_helper().retrieve_resource(
551+ # APIResourceTypes.DATASTREAM, req_headers={})
552+ res = self ._parent_node .get_api_helper ().get_resource (APIResourceTypes .SYSTEM , self ._resource_id ,
553+ APIResourceTypes .DATASTREAM )
539554 datastream_json = res .json ()['items' ]
540555 ds_resources = []
541556
@@ -702,19 +717,20 @@ def insert_observation_dict(self, obs_data: dict):
702717 def start (self ):
703718 super ().start ()
704719 if self ._mqtt_client is not None :
705- self ._mqtt_client .connect ()
706- self ._mqtt_client .start ()
707- self .subscribe_mqtt (self ._topic )
720+ # self._mqtt_client.connect()
721+
708722 if self ._connection_mode is StreamableModes .PULL or self ._connection_mode is StreamableModes .BIDIRECTIONAL :
709723 self ._mqtt_client .subscribe (self ._topic , msg_callback = self ._mqtt_sub_callback )
724+ else :
725+ try :
726+ loop = asyncio .get_event_loop ()
727+ loop .create_task (self ._write_to_mqtt ())
728+ except Exception as e :
729+ # TODO: Use logging instead of print
730+ print (traceback .format_exc ())
731+ print (f"Error starting MQTT write task: { e } " )
710732
711- try :
712- loop = asyncio .get_event_loop ()
713- loop .create_task (self ._write_to_mqtt ())
714- except Exception as e :
715- # TODO: Use logging instead of print
716- print (traceback .format_exc ())
717- print (f"Error starting MQTT write task: { e } " )
733+ # self._mqtt_client.start()
718734
719735 def init_mqtt (self ):
720736 super ().init_mqtt ()
@@ -731,9 +747,9 @@ def _queue_push(self, msg):
731747 def _queue_pop (self ):
732748 return self ._msg_reader_queue .get_nowait ()
733749
734- def _mqtt_sub_callback (self , client , userdata , msg ):
735- print (f"MQTT Message received on topic { msg .topic } : { msg .payload } " )
736- self ._queue_push (msg .payload )
750+ # def _mqtt_sub_callback(self, client, userdata, msg):
751+ # print(f"MQTT Message received on topic {msg.topic}: {msg.payload}")
752+ # self._queue_push(msg.payload)
737753
738754 def insert (self , data : dict ):
739755 # self._queue_push(data)
0 commit comments