Ver Fonte

Improved poll wrapper
In addition to retrieving a message_type, message_payload tuple, you can now return None, a single Event or a list of Events in your on_poll handler.

Stan Janssen há 4 anos atrás
pai
commit
e548ee20fb
1 ficheiros alterados com 11 adições e 1 exclusões
  1. 11 1
      openleadr/service/poll_service.py

+ 11 - 1
openleadr/service/poll_service.py

@@ -16,6 +16,7 @@
 
 from . import service, handler, VTNService
 from asyncio import iscoroutine
+import warnings
 
 # ╔══════════════════════════════════════════════════════════════════════════╗
 # ║                             POLLING SERVICE                              ║
@@ -107,4 +108,13 @@ class PollService(VTNService):
         result = self.on_poll(ven_id=payload['ven_id'])
         if iscoroutine(result):
             result = await result
-        return result
+        if result is None:
+            return result
+        if isinstance(result, tuple):
+            return result
+        if isinstance(result, list):
+            return 'oadrDistributeEvent', result
+        if isinstance(result, dict) and 'event_descriptor' in result:
+            return 'oadrDistributeEvent', {'events': [result]}
+        warnings.warn(f"Could not determine type of message in response to oadrPoll: {result}")
+        return 'oadrResponse', result