Преглед изворни кода

Add support for event callbacks to server.add_raw_event method.

Adds the same callback interface to the server.add_raw_event method that we had for the server.add_event method.

This was reported in #35.

Signed-off-by: Stan Janssen <stan.janssen@elaad.nl>
Stan Janssen пре 4 година
родитељ
комит
92336dc7b7
2 измењених фајлова са 51 додато и 2 уклоњено
  1. 6 1
      openleadr/server.py
  2. 45 1
      test/test_queues.py

+ 6 - 1
openleadr/server.py

@@ -233,16 +233,21 @@ class OpenADRServer:
         self.services['event_service'].pending_events[event_id] = (event, callback)
         return event_id
 
-    def add_raw_event(self, ven_id, event):
+    def add_raw_event(self, ven_id, event, callback):
         """
         Add a new event to the queue for a specific VEN.
         :param str ven_id: The ven_id to which this event should be distributed.
         :param dict event: The event (as a dict or as a objects.Event instance)
                            that contains the event details.
+        :param callable callback: A callback that will receive the opt status for this event.
+                                  This callback receives ven_id, event_id, opt_type as its arguments.
         """
         if ven_id not in self.message_queues:
             self.message_queues[ven_id] = deque()
+        event_id = utils.getmember(utils.getmember(event, 'event_descriptor'), 'event_id')
         self.message_queues[ven_id].append(event)
+        self.services['event_service'].pending_events[event_id] = (event, callback)
+        return event_id
 
     def add_handler(self, name, func):
         """

+ 45 - 1
test/test_queues.py

@@ -1,4 +1,4 @@
-from openleadr import OpenADRClient, OpenADRServer, enable_default_logging
+from openleadr import OpenADRClient, OpenADRServer, enable_default_logging, objects, utils
 import pytest
 import asyncio
 import datetime
@@ -197,3 +197,47 @@ async def test_request_event():
     message_type, message_payload = await client.request_event()
     assert message_type == 'oadrResponse'
     await server.stop()
+
+
+@pytest.mark.asyncio
+async def test_raw_event():
+    now = datetime.datetime.now(datetime.timezone.utc)
+    server = OpenADRServer(vtn_id='myvtn')
+    server.add_handler('on_create_party_registration', on_create_party_registration)
+    event = objects.Event(event_descriptor=objects.EventDescriptor(event_id='event001',
+                                                                   modification_number=0,
+                                                                   event_status='far',
+                                                                   market_context='http://marketcontext01'),
+                          event_signals=[objects.EventSignal(signal_id='signal001',
+                                                             signal_type='level',
+                                                             signal_name='simple',
+                                                             intervals=[objects.Interval(dtstart=now,
+                                                                                         duration=datetime.timedelta(minutes=10),
+                                                                                         signal_payload=1)]),
+                                        objects.EventSignal(signal_id='signal002',
+                                                            signal_type='price',
+                                                            signal_name='ELECTRICITY_PRICE',
+                                                            intervals=[objects.Interval(dtstart=now,
+                                                                                        duration=datetime.timedelta(minutes=10),
+                                                                                        signal_payload=1)])],
+                          targets=[objects.Target(ven_id='ven123')])
+    loop = asyncio.get_event_loop()
+    event_callback_future = loop.create_future()
+    server.add_raw_event(ven_id='ven123', event=event, callback=partial(event_callback, future=event_callback_future))
+
+    on_event_future = loop.create_future()
+    client = OpenADRClient(ven_name='myven',
+                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
+    client.add_handler('on_event', partial(on_event_opt_in, future=on_event_future))
+
+    await server.run_async()
+    await client.run()
+    event = await on_event_future
+    assert len(event['event_signals']) == 2
+
+    result = await event_callback_future
+    assert result == 'optIn'
+
+    await client.stop()
+    await server.stop()
+