Exemple #1
0
 def subscribe(self, topic, qos, callback):
     if (topic is None or qos is None):
         self._log.error("subscribe: None type inputs detected.")
         raise TypeError("None type inputs detected.")
     # Return subscribe succeeded/failed
     ret = False
     self._subscribeLock.acquire()
     # Subscribe
     # Register callback
     if (callback is not None):
         self._pahoClient.message_callback_add(topic, callback)
     (rc, mid) = self._pahoClient.subscribe(topic,
                                            qos)  # Throw exception...
     self._log.debug("Started a subscribe request " + str(mid))
     TenmsCount = 0
     while (TenmsCount != self._mqttOperationTimeout * 100
            and not self._subscribeSent):
         TenmsCount += 1
         time.sleep(0.01)
     if (self._subscribeSent):
         ret = rc == 0
         if (ret):
             self._subscribePool[topic] = (qos, callback)
             self._log.debug("Subscribe request " + str(mid) +
                             " succeeded. Time consumption: " +
                             str(float(TenmsCount) * 10) + "ms.")
         else:
             if (callback is not None):
                 self._pahoClient.message_callback_remove(topic)
             self._log.error("Subscribe request " + str(mid) +
                             " failed with code: " + str(rc))
             self._log.debug("Callback cleaned up.")
             self._subscribeLock.release(
             )  # Release the lock when exception is raised
             raise subscribeError(rc)
     else:
         # Subscribe timeout
         if (callback is not None):
             self._pahoClient.message_callback_remove(topic)
         self._log.error("No feedback detected for subscribe request " +
                         str(mid) + ". Timeout and failed.")
         self._log.debug("Callback cleaned up.")
         self._subscribeLock.release(
         )  # Release the lock when exception is raised
         raise subscribeTimeoutException()
     self._subscribeSent = False
     self._log.debug(
         "Recover subscribe context for the next request: subscribeSent: " +
         str(self._subscribeSent))
     self._subscribeLock.release()
     return ret
Exemple #2
0
 def subscribe(self, topic, qos, message_callback=None):
     self._logger.info("Performing sync subscribe...")
     ret = False
     if ClientStatus.STABLE != self._client_status.get_status():
         self._handle_offline_request(RequestTypes.SUBSCRIBE, (topic, qos, message_callback))
     else:
         event = Event()
         rc, mid = self._subscribe_async(topic, qos, self._create_blocking_ack_callback(event), message_callback)
         if not event.wait(self._operation_timeout_sec):
             self._internal_async_client.remove_event_callback(mid)
             self._logger.error("Subscribe timed out")
             raise subscribeTimeoutException()
         ret = True
     return ret
 def subscribe(self, topic, qos, message_callback=None):
     self._logger.info("Performing sync subscribe...")
     ret = False
     if ClientStatus.STABLE != self._client_status.get_status():
         self._handle_offline_request(RequestTypes.SUBSCRIBE, (topic, qos, message_callback))
     else:
         event = Event()
         rc, mid = self._subscribe_async(topic, qos, self._create_blocking_ack_callback(event), message_callback)
         if not event.wait(self._operation_timeout_sec):
             self._internal_async_client.remove_event_callback(mid)
             self._logger.error("Subscribe timed out")
             raise subscribeTimeoutException()
         ret = True
     return ret
 def subscribe(self, topic, qos, callback):
     if(topic is None or qos is None):
         self._log.error("subscribe: None type inputs detected.")
         raise TypeError("None type inputs detected.")
     # Return subscribe succeeded/failed
     ret = False
     self._subscribeLock.acquire()
     # Subscribe
     # Register callback
     if(callback is not None):
         self._pahoClient.message_callback_add(topic, callback)
     (rc, mid) = self._pahoClient.subscribe(topic, qos)  # Throw exception...
     self._log.debug("Started a subscribe request " + str(mid))
     TenmsCount = 0
     while(TenmsCount != self._mqttOperationTimeout * 100 and not self._subscribeSent):
         TenmsCount += 1
         time.sleep(0.01)
     if(self._subscribeSent):
         ret = rc == 0
         if(ret):
             self._subscribePool[topic] = (qos, callback)
             self._log.debug("Subscribe request " + str(mid) + " succeeded. Time consumption: " + str(float(TenmsCount) * 10) + "ms.")
         else:
             if(callback is not None):
                 self._pahoClient.message_callback_remove(topic)
             self._log.error("Subscribe request " + str(mid) + " failed with code: " + str(rc))
             self._log.debug("Callback cleaned up.")
             self._subscribeLock.release()  # Release the lock when exception is raised
             raise subscribeError(rc)
     else:
         # Subscribe timeout
         if(callback is not None):
             self._pahoClient.message_callback_remove(topic)
         self._log.error("No feedback detected for subscribe request " + str(mid) + ". Timeout and failed.")
         self._log.debug("Callback cleaned up.")
         self._subscribeLock.release()  # Release the lock when exception is raised
         raise subscribeTimeoutException()
     self._subscribeSent = False
     self._log.debug("Recover subscribe context for the next request: subscribeSent: " + str(self._subscribeSent))
     self._subscribeLock.release()
     return ret