def __del__(self, CorbaConsumer=PahoPublisher): self._rtcout.RTC_PARANOID("~OutPortPahoPublisher()") print("[disconnecting from MQTT broker start]") PahoPublisher.paho_disconnect(self) print("[disconnecting from MQTT broker end]") PahoPublisher.__del__(self) return
def put(self, data): self._rtcout.RTC_PARANOID("put()") try: rpdata = repr(data) PahoPublisher.paho_pub(self, rpdata) return self.PORT_OK except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) return self.CONNECTION_LOST
def __init__(self): global called PahoPublisher.__init__(self) self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf( "OutPortPahoPublisher") self._properties = None thread = threading.Thread(target=self.catch_signal) thread.daemon = True thread.start() called = True return
def subscribePahoPublisher(self, properties): self._rtcout.RTC_TRACE("subscribePahoPublisher()") PN_HOST = "host" PN_PORT = "port" PN_KPALV = "kpalv" PN_TOPIC = "topic" PN_QOS = "qos" PN_ID = "id" PN_CS = "cs" index0 = self.findProp(properties, PN_HOST) index1 = self.findProp(properties, PN_PORT) index2 = self.findProp(properties, PN_KPALV) index3 = self.findProp(properties, PN_TOPIC) index4 = self.findProp(properties, PN_QOS) index5 = self.findProp(properties, PN_ID) index6 = self.findProp(properties, PN_CS) tmp_host = "localhost" tmp_port = 1883 str_port = "1883" tmp_kpalv = 60 str_kpalv = "60" tmp_topic = "test" tmp_qos = 0 str_qos = "0" tmp_id = "" tmp_cs = True str_cs = "True" if index0 < 0: print("Server address not found. Default server address '" + tmp_host + "' is used.") else: try: tmp_host = any.from_any(properties[index0].value, keep_structs=True) print("Server address: " + tmp_host) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not tmp_host: self._rtcout.RTC_ERROR("Server address has no string") return False if index1 < 0: print("Port number not found. Default port '" + str(tmp_port) + "' is used.") else: try: str_port = any.from_any(properties[index1].value, keep_structs=True) tmp_port = int(str_port) if tmp_port < 0 or tmp_port > 65535: tmp_port = 1883 print("Port: " + str(tmp_port)) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not str_port: self._rtcout.RTC_ERROR("Port number has no string") return False if index2 < 0: print("Keepalive not found. Default keepalve '" + str(tmp_kpalv) + "' is used.") else: try: str_kpalv = any.from_any(properties[index2].value, keep_structs=True) tmp_kpalv = int(str_kpalv) if tmp_kpalv < 0 or tmp_kpalv > 86400: tmp_kpalv = 60 print("keepalive: " + str(tmp_kpalv)) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not str_kpalv: self._rtcout.RTC_ERROR("Keepalive has no string") return False if index3 < 0: print("Topic not found. Default Topic '" + tmp_topic + "' is used.") else: try: tmp_topic = any.from_any(properties[index3].value, keep_structs=True) print("Topic: " + tmp_topic) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not tmp_topic: self._rtcout.RTC_ERROR("Topic has no string") return False if index4 < 0: print("QoS not found. Default QoS '" + str(tmp_qos) + "' is used.") else: try: str_qos = any.from_any(properties[index4].value, keep_structs=True) tmp_qos = int(str_qos) if tmp_qos < 0 or tmp_qos > 2: tmp_qos = 0 print("QoS: " + str(tmp_qos)) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not str_qos: self._rtcout.RTC_ERROR("QoS has no string") return False if index5 < 0: print("Client ID not found. Random number ID is used.") else: try: tmp_id = any.from_any(properties[index5].value, keep_structs=True) print("Client ID: " + tmp_id) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if index6 < 0: print("CleanSession not found. Default clean_session '" + str(tmp_cs) + "' is used.") else: try: str_cs = any.from_any(properties[index6].value, keep_structs=True) if str_cs == "False" or str_cs == "false" or str_cs == "FALSE" or str_cs == "f" or str_cs == "F" or str_cs == "0": tmp_cs = False print("Clean session: " + str(tmp_cs)) except: self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception()) if not str_cs: self._rtcout.RTC_ERROR("Clean session has no string") return False print("[connecting to MQTT broker start]") PahoPublisher.paho_initialize(self, tmp_id, tmp_cs, tmp_topic, tmp_qos) PahoPublisher.paho_connect(self, tmp_host, tmp_port, tmp_kpalv) print("[connecting to MQTT broker end]") return True