def publish(self, topic, payload=None, qos=None, retain=None): """publish message using paho library """ logger.debug("Checking subscriptions") def not_finished_subcribing_to(): """Get topic names for topics which have not finished subcribing to""" return [i[0] for i in self._subscribed.values() if not i[1]] to_wait_for = not_finished_subcribing_to() if to_wait_for: elapsed = 0.0 while elapsed < self._connect_timeout: # TODO # configurable? time.sleep(0.25) elapsed += 0.25 to_wait_for = not_finished_subcribing_to() if not to_wait_for: logger.debug("Finished subcribing to all topics") break logger.debug( "Not finished subcribing to '%s' after %.2f seconds", to_wait_for, elapsed, ) if to_wait_for: logger.warning( "Did not finish subscribing to '%s' before publishing - going ahead anyway" ) else: logger.debug("Finished subcribing to all topics") logger.debug("Publishing on '%s'", topic) kwargs = {} if qos is not None: kwargs["qos"] = qos if retain is not None: kwargs["retain"] = retain msg = self._client.publish(topic, payload, **kwargs) if not msg.is_published: raise exceptions.MQTTError( "err {:s}: {:s}".format( _err_vals.get(msg.rc, "unknown"), paho.error_string(msg.rc) ) ) return msg
def publish(self, topic, payload, *args, **kwargs): """publish message using paho library """ logger.debug("Publishing on '%s'", topic) msg = self._client.publish(topic, payload, *args, **kwargs) if not msg.is_published: raise exceptions.MQTTError("err {:s}: {:s}".format(_err_vals.get(msg.rc, "unknown"), paho.error_string(msg.rc))) return msg
def publish(self, topic, payload=None, qos=None, retain=None): """publish message using paho library""" self._wait_for_subscriptions() logger.debug("Publishing on '%s'", topic) kwargs = {} if qos is not None: kwargs["qos"] = qos if retain is not None: kwargs["retain"] = retain msg = self._client.publish(topic, payload, **kwargs) if not msg.is_published: raise exceptions.MQTTError("err {:s}: {:s}".format( _err_vals.get(msg.rc, "unknown"), paho.error_string(msg.rc))) return msg