def _process_queue(self, future):
     assert future == self.pending_task
     exc = future.exception()
     if exc:
         raise FatalError("mqtt request failed with exception: %s" % exc)
     if self.pending_message:
         self._dispatch_next(self.pending_message)
         self.pending_message = None
     if len(self.request_queue) == 0:
         self.pending_task = None
         #print("Completed last task")
     else:
         x = self.request_queue.popleft()
         if x is not None:
             self.pending_message = x
             self.pending_task = \
                 self.scheduler._schedule_coroutine(self.client.publish(self.topic,
                                                                        self._to_message(x)),
                                                    self._process_queue)
             #print("enqueuing message %s on %s (from request_q)" %
             #      (repr(x), self.topic))
         else:
             e = self.pending_error
             self.pending_task = \
                 self.scheduler._schedule_coroutine(self.client.disconnect(),
                                                    lambda f: self._dispatch_error(e) if e is not None
                                                    else lambda f: self._dispatch_completed)
示例#2
0
 def on_next(self, x):
     if x.val == 'ON':
         print("Turning ON!")
     elif x.val == 'OFF':
         print("Turning OFF!")
     else:
         raise FatalError("Unexpected event value for actuator: %s" % x.val)
 def _observe_event_loop(self):
     print("starting event loop")
     for i in range(4):
         if self.stop_requested:
             break
         self._dispatch_next(i)
         time.sleep(1)
     raise FatalError("testing the fatal error")
 def __init__(self, filename, has_header_row=True):
     super().__init__() # Make sure the publisher class is initialized
     self.filename = filename
     self.file = open(filename, 'r', newline='')
     self.reader = csv.reader(self.file)
     if has_header_row:
         # swallow up the header row so it is not passed as data
         try:
             self.reader.__next__()
         except Exception as e:
             raise FatalError("Problem reading header row of csv file %s: %s" %
                              (filename, e))
 def _process_event(self, future):
     assert future == self.pending_task
     #print("_process_event state=%s" % self.state)
     exc = future.exception()
     if exc and isinstance(exc, asyncio.TimeoutError) and\
        self.state==QueueReader.ACTIVE_STATE:
         # we timeout every few seconds to check for stop requests
         if not self._process_stop_request():
             self._start_task(self.client.deliver_message(self.timeout),
                              QueueReader.ACTIVE_STATE)
     elif exc:
         raise FatalError("mqtt request failed with exception: %s" % exc)
     elif self.state == QueueReader.CONNECTING_STATE:
         if not self._process_stop_request():
             self._start_task(
                 self.client.subscribe([
                     (self.topic, self.qos),
                 ]), QueueReader.SUBSCRIBING_STATE)
     elif self.state == QueueReader.SUBSCRIBING_STATE:
         if not self._process_stop_request():
             self._start_task(self.client.deliver_message(self.timeout),
                              QueueReader.ACTIVE_STATE)
     elif self.state == QueueReader.ACTIVE_STATE:
         result = future.result()
         assert isinstance(result, hbmqtt.session.ApplicationMessage)
         message = str(result.data, encoding='utf-8')
         self._dispatch_next(json.loads(message))
         if not self._process_stop_request():
             self._start_task(self.client.deliver_message(self.timeout),
                              QueueReader.ACTIVE_STATE)
     elif self.state == QueueReader.UNSUBSCRIBING_STATE:
         self._start_task(self.client.disconnect(),
                          QueueReader.DISCONNECTING_STATE)
     elif self.state == QueueReader.DISCONNECTING_STATE:
         self._dispatch_completed()
         self.state = QueueReader.FINAL_STATE
         self.pending_task = None
         self.scheduler._remove_from_active_schedules(self)
         print("QueueReader in FINAL state")
     elif self.state == QueueReader.FINAL_STATE:
         raise Exception(
             "_process_event should not be called in final state")
     else:
         raise Exception("_process_event: invalidate state %s" % self.state)
示例#6
0
 def on_timeout_completed(self):
     raise FatalError("%s.on_timeout_completed should not be called" % self)
示例#7
0
 def on_timeout_error(self, e):
     raise FatalError("%s.on_timeout_error should not be called" % self)
示例#8
0
 def on_timeout_completed(self):
     """This won't get called, as the timeout component does not propate
     any completions. We just use the primary event stream to figure out when
     things are done and clear any pending timeouts at that time.
     """
     raise FatalError("%s.on_timeout_completed should not be called" % self)
示例#9
0
 def on_timeout_error(self, e):
     """This won't get called, as the Timeout component does not republish any
     errors it receives.
     """
     raise FatalError("%s.on_timeout_error should not be called" % self)