def subscriber_main(domain_id, sample_count): participant = dds.DomainParticipant(domain_id) wsqc_type = dds.QosProvider("waitset_cond.xml").type("wssc_lib", "Foo") topic = dds.DynamicData.Topic(participant, "Example Foo", wsqc_type) reader_qos = dds.QosProvider.default.datareader_qos reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos) # Get the StatusCondition associated with the reader and set the mask to get liveliness updates status_condition = dds.StatusCondition(reader) status_condition.enabled_statuses = dds.StatusMask.LIVELINESS_CHANGED # Create a ReadCondition to get any data read_condition = dds.ReadCondition(reader, dds.DataState.any_data) # Create WaitSet and attach conditions waitset = dds.WaitSet() waitset += status_condition waitset += read_condition count = Counter() while (sample_count == 0) or (count.value < sample_count): active = waitset.wait(4.0) # Check conditions after wait to see if anything triggered if status_condition in active: status_handler(reader) if read_condition in active: rc_handler(reader, count)
def run_example(domain_id, sample_count): # A DomainParticipant allows an application to begin communicating in # a DDS domain. Typically there is one DomainParticipant per application. # Create a DomainParticipant with default Qos with dds.DomainParticipant(domain_id) as participant: # A QosProvider is used here to get the type from the file # HelloWorld.xml provider = dds.QosProvider(FILE) provider_type = provider.type("HelloWorld") # A Topic has a name and a datatype. Create a Topic named # "HelloWorld Topic" with type HelloWorld topic = dds.DynamicData.Topic( participant, "Example HelloWorld", provider_type ) # A Subscriber allows an application to create one or more DataReaders # Subscriber QoS is configured in USER_QOS_PROFILES.xml subscriber = dds.Subscriber(participant) # This DataReader will read data of type HelloWorld on Topic # "HelloWorld Topic". DataReader QoS is configured in # USER_QOS_PROFILES.xml reader = dds.DynamicData.DataReader(subscriber, topic) # Obtain the DataReader's Status Condition status_condition = dds.StatusCondition(reader) # Enable the 'data available' status. status_condition.enabled_statuses = dds.StatusMask.data_available() # Initialize samples_read to zero samples_read = 0 # Associate a handler with the status condition. This will run when the # condition is triggered, in the context of the dispatch call (see below) def handler(_): # condition argument is not used nonlocal samples_read nonlocal reader samples_read += process_data(reader) status_condition.set_handler(handler) # Create a WaitSet and attach the StatusCondition waitset = dds.WaitSet() waitset += status_condition # Catch control c interrupt try: while samples_read < sample_count: # Dispatch will call the handlers associated to the WaitSet conditions # when they activate print(f"Hello World subscriber sleeping for 4 seconds...") waitset.dispatch(dds.Duration(4)) # Wait up to 4s each time except KeyboardInterrupt: pass
def run_example(domain_id, sample_count, sensor_id): # A DomainParticipant allows an application to begin communicating in # a DDS domain. Typically there is one DomainParticipant per application. # Create a DomainParticipant with default Qos participant = dds.DomainParticipant(domain_id) # A Topic has a name and a datatype. Create a Topic named # "ChocolateTemperature" with type Temperature temperature_type = dds.QosProvider(FILE).type("Temperature") topic = dds.DynamicData.Topic(participant, "ChocolateTemperature", temperature_type) # A Subscriber allows an application to create one or more DataReaders # Subscriber QoS is configured in USER_QOS_PROFILES.xml subscriber = dds.Subscriber(participant) # This DataReader reads data of type Temperature on Topic # "ChocolateTemperature". DataReader QoS is configured in # USER_QOS_PROFILES.xml reader = dds.DynamicData.DataReader(subscriber, topic) # Obtain the DataReader's Status Condition status_condition = dds.StatusCondition(reader) # Enable the 'data available' status. status_condition.enabled_statuses = dds.StatusMask.data_available() # Associate a handler with the status condition. This will run when the # condition is triggered, in the context of the dispatch call (see below) samples_read = 0 def handler(_): nonlocal samples_read nonlocal reader samples_read += process_data(reader) status_condition.set_handler(handler) # Create a WaitSet and attach the StatusCondition waitset = dds.WaitSet() waitset += status_condition try: # Dispatch will call the handlers associated to the WaitSet conditions # when they activate while sample_count is None or samples_read < sample_count: print("ChocolateTemperature subcriber sleeping for 4 sec...") waitset.dispatch(dds.Duration(4)) # Wait up to 4s each time except KeyboardInterrupt: pass
def run_example(domain_id, sensor_id): # A DomainParticipant allows an application to begin communicating in # a DDS domain. Typically there is one DomainParticipant per application. # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml participant = dds.DomainParticipant(domain_id) # A Topic has a name and a datatype. Create Topics. # Topic names are constants defined in the XML file. temperature_topic = dds.DynamicData.Topic(participant, CHOCOLATE_TEMPERATURE_TOPIC, TEMPERATURE_TYPE) lot_state_topic = dds.DynamicData.Topic(participant, CHOCOLATE_LOT_STATE_TOPIC, CHOCOLATE_LOT_TYPE) # A Publisher allows an application to create one or more DataWriters # Publisher QoS is configured in USER_QOS_PROFILES.xml publisher = dds.Publisher(participant) # Create DataWriters of Topics "ChocolateTemperature" & "ChocolateLotState" # DataWriter QoS is configured in USER_QOS_PROFILES.xml temperature_writer = dds.DynamicData.DataWriter(publisher, temperature_topic) lot_state_writer = dds.DynamicData.DataWriter(publisher, lot_state_topic) # A Subscriber allows an application to create one or more DataReaders # Subscriber QoS is configured in USER_QOS_PROFILES.xml subscriber = dds.Subscriber(participant) # Create DataReader of Topic "ChocolateLotState". # DataReader QoS is configured in USER_QOS_PROFILES.xml lot_state_reader = dds.DynamicData.DataReader(subscriber, lot_state_topic) # Obtain the DataReader's Status Condition status_condition = dds.StatusCondition(lot_state_reader) # Enable the 'data available' status. status_condition.enabled_statuses = dds.StatusMask.data_available() # Associate a handler with the status condition. This will run when the # condition is triggered, in the context of the dispatch call (see below) def handler(_): nonlocal lot_state_reader nonlocal lot_state_writer process_lot(lot_state_reader, lot_state_writer) # Create a WaitSet and attach the StatusCondition status_condition.set_handler(handler) waitset = dds.WaitSet() waitset += status_condition # Create a thread to periodically publish the temperature print(f"ChocolateTemperature Sensor with ID: {sensor_id} starting") temperature_thread = threading.Thread(target=publish_temperature, args=(temperature_writer, sensor_id)) temperature_thread.start() try: while True: # Wait for ChocolateLotState print("Waiting for lot") waitset.dispatch(dds.Duration(10)) # Wait up to 10s for update except KeyboardInterrupt: pass temperature_thread.join()
def run_example(domain_id, lots_to_process, sensor_id): # A DomainParticipant allows an application to begin communicating in # a DDS domain. Typically there is one DomainParticipant per application. # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml participant = dds.DomainParticipant(domain_id) # A Topic has a name and a datatype. Create a Topic with type # ChocolateLotState. Topic name is a constant defined in the XML file. topic = dds.DynamicData.Topic(participant, CHOCOLATE_LOT_STATE_TOPIC, CHOCOLATE_LOT_TYPE) # Exercise #4.1: Add a Topic for Temperature to this application temperature_topic = dds.DynamicData.Topic(participant, CHOCOLATE_TEMPERATURE_TOPIC, provider.type("Temperature")) # A Publisher allows an application to create one or more DataWriters # Publisher QoS is configured in USER_QOS_PROFILES.xml publisher = dds.Publisher(participant) # This DataWriter writes data on Topic "ChocolateLotState" # DataWriter QoS is configured in USER_QOS_PROFILES.xml writer = dds.DynamicData.DataWriter(publisher, topic) # A Subscriber allows an application to create one or more DataReaders # Subscriber QoS is configured in USER_QOS_PROFILES.xml subscriber = dds.Subscriber(participant) # Create DataReader of Topic "ChocolateLotState". # DataReader QoS is configured in USER_QOS_PROFILES.xml reader = dds.DynamicData.DataReader(subscriber, topic) # Exercise #4.2: Add a DataReader for Temperature to this application temperature_reader = dds.DynamicData.DataReader(subscriber, temperature_topic) # Obtain the DataReader's Status Condition status_condition = dds.StatusCondition(reader) # Enable the 'data available' status. status_condition.enabled_statuses = dds.StatusMask.data_available() # Associate a handler with the status condition. This will run when the # condition is triggered, in the context of the dispatch call (see below) lots_processed = 0 def handler(_): nonlocal lots_processed nonlocal reader lots_processed += monitor_lot_state(reader) status_condition.set_handler(handler) temperature_status_condition = dds.StatusCondition(reader) temperature_status_condition.enabled_statuses = ( dds.StatusMask.data_available()) def temperature_handler(_): nonlocal temperature_reader monitor_lot_state(temperature_reader) temperature_status_condition.set_handler(temperature_handler) # Create a WaitSet and attach the StatusCondition waitset = dds.WaitSet() waitset += status_condition # Exercise #4.3: Add the new DataReader's StatusCondition to the Waitset waitset += temperature_status_condition # Create a thread to periodically publish the temperature start_lot_thread = threading.Thread(target=publish_start_lot, args=(writer, lots_to_process)) start_lot_thread.start() try: while lots_to_process is None or lots_processed < lots_to_process: # Dispatch will call the handlers associated to the WaitSet conditions # when they activate waitset.dispatch(dds.Duration(4)) # Wait for up to 4s each time except KeyboardInterrupt: pass start_lot_thread.join()
def __init__(self, basedir, registry, listener, profile_file, participant_config, writers={}, readers={}, types={}, queries={}, dds_peers=[], process_events=UvnDefaults["dds"]["process_events"], router_cfg=None): threading.Thread.__init__(self, daemon=True) # set thread name self.name = f"{participant_config}" self.basedir = basedir self.registry = registry self.listener = listener self._process_events = process_events self._participant_config = participant_config self._profile_file = str(profile_file) self._writers = dict(writers) # self._writers.update({ # "dns": UvnDefaults["dds"]["writer"]["dns"] # }) self._readers = dict(readers) self._readers.update({ "cell_info": UvnDefaults["dds"]["reader"]["cell_info"], "dns": UvnDefaults["dds"]["reader"]["dns"] }) self._types = dict(types) self._queries = dict(queries) self._dds_peers = set(dds_peers) self._types.update({ "uvn_info": UvnDefaults["dds"]["types"]["uvn_info"], "cell_info": UvnDefaults["dds"]["types"]["cell_info"], "ip_address": UvnDefaults["dds"]["types"]["ip_address"], "deployment": UvnDefaults["dds"]["types"]["deployment"], "dns_db": UvnDefaults["dds"]["types"]["dns_db"], "dns_rec": UvnDefaults["dds"]["types"]["dns_rec"], "cell_site": UvnDefaults["dds"]["types"]["cell_site"], "cell_peer": UvnDefaults["dds"]["types"]["cell_peer"] }) qos_provider = self._create_qos_provider(self._profile_file) self.participant = self._create_participant(qos_provider, self._participant_config) self.types = self._create_types(qos_provider, self._types) (self.writers, self.readers) = self._create_endpoints(self.participant, self._writers, self._readers) self.writer_conditions = self._create_writer_conditions(self.writers) self.reader_conditions = self._create_reader_conditions(self.readers) self.data_conditions = self._create_data_conditions(self.readers) self.exit_condition = dds.GuardCondition() self.waitset = dds.WaitSet() self.waitset += self.exit_condition for c in itertools.chain(self.writer_conditions.values(), self.reader_conditions.values(), self.data_conditions.values()): self.waitset += c logger.activity("DDS types: {}", self._types) logger.activity("DDS readers: {}", self._readers) logger.activity("DDS writers: {}", self._writers) # Start routing service if requested # if router_cfg: # self._rs = self._create_rs(*router_cfg) # self._rs_cfg = router_cfg # else: # self._rs = None # Keep track of "private" ports from discovered peers. # These are the address on which the peer can be reach within # (one or more of) its own private LANs attached to the UVN. # Each port is added as a peer, and once all cells are active, # and have a private port assigned, then routing service will # be disabled, to stop receiving recasted message from the # registry and other peers. # If a cell loses liveliness, then routing service will be # reenabled, until a connection can be re-established. # The agent keeps track of the livelness of remote peers by # monitoring the liveliness of a topic which is not shared # across the DDS routing service network. self._private_ports = {}
def subscriber_main(domain_id, sample_count): participant = dds.DomainParticipant(domain_id) wsqc_type = dds.QosProvider("waitset_query_cond.xml").type( "wsqc_lib", "waitset_query_cond" ) topic = dds.DynamicData.Topic(participant, "Example waitset_query_cond", wsqc_type) reader_qos = dds.QosProvider.default.datareader_qos reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos) # Query against even samples at the start query_parameters = ["'EVEN'"] query = dds.Query(reader, "name MATCH %0", query_parameters) query_condition = dds.QueryCondition(query, dds.DataState.any_data) # Create a WaitSet and attach the QueryCondition waitset = dds.WaitSet() waitset += query_condition print( textwrap.dedent( """\ >>> Timeout: {} sec >>> Query conditions: {} \t %0 = {} ---------------------------------- """.format( 1, query_condition.expression, query_parameters[0] ) ) ) count = 0 while (sample_count == 0) or (count < sample_count): time.sleep(1) if count == 7: # Update the query after 7 seconds query_parameters[0] = "'ODD'" query_condition.parameters = query_parameters print( textwrap.dedent( """ CHANGING THE QUERY CONDITION >>> Query conditions: {} \t %%0 = {} >>> We keep one sample in the history ------------------------------------- """.format( query_condition.expression, query_parameters[0] ) ) ) active = waitset.wait((1, 0)) # Check to see if the query was triggered if query_condition in active: query_handler(reader, query_condition) count += 1