Selaa lähdekoodia

Simplified the get_next_event_from_deque utility

Signed-off-by: Stan Janssen <stan.janssen@elaad.nl>
Stan Janssen 4 vuotta sitten
vanhempi
commit
e2f148b9af
3 muutettua tiedostoa jossa 17 lisäystä ja 12 poistoa
  1. 10 3
      openleadr/service/report_service.py
  2. 6 9
      openleadr/utils.py
  3. 1 0
      test/test_utils.py

+ 10 - 3
openleadr/service/report_service.py

@@ -68,6 +68,7 @@ class ReportService(VTNService):
         super().__init__(vtn_id)
         self.report_callbacks = {}
         self.message_queues = message_queues
+        self.registered_reports = {}
 
     @handler('oadrRegisterReport')
     async def register_report(self, payload):
@@ -80,6 +81,7 @@ class ReportService(VTNService):
                 'min_sampling_interval' in args, 'max_sampling_interval' in args,
                 'unit' in args, 'scale' in args]):
             for report in payload['reports']:
+                self.registered_reports
                 if report['report_name'] == 'METADATA_TELEMETRY_STATUS':
                     result = [self.on_register_report(ven_id=payload['ven_id'],
                                                       resource_id=rd['report_subject']['resource_id'],
@@ -98,11 +100,16 @@ class ReportService(VTNService):
                                                       min_sampling_interval=rd['sampling_rate']['min_period'],
                                                       max_sampling_interval=rd['sampling_rate']['max_period'])
                               for rd in report['report_descriptions']]
+                elif report['report_name'] in ('METADATA_HISTORY_USAGE', 'METADATA_HISTORY_GREENBUTTON'):
+                    if payload['ven_id'] not in self.available_reports:
+                        self.available_reports[payload['ven_id']] = []
+                    self.registered_reports[payload['ven_id']].append(report)
                 else:
-                    logger.warning("Reports other than TELEMETRY_USAGE and TELEMETRY_STATUS are "
-                                   f"not yet supported. Skipping report {report['report_name']}.")
+                    logger.warning("Reports other than TELEMETRY_USAGE, TELEMETRY_STATUS, "
+                                   "HISTORY_USAGE and HISTORY_GREENBUTTON are not yet supported. "
+                                   f"Skipping report with name {report['report_name']}.")
                     report_requests.append(None)
-                    break
+                    continue
 
                 if iscoroutine(result[0]):
                     result = await gather(*result)

+ 6 - 9
openleadr/utils.py

@@ -632,14 +632,11 @@ def get_next_event_from_deque(deque):
     unused_elements = []
     event = None
     for i in range(len(deque)):
-        try:
-            msg = deque.popleft()
-            if isinstance(msg, objects.Event) or (isinstance(msg, dict) and 'event_descriptor' in msg):
-                event = msg
-                break
-            else:
-                unused_elements.append(msg)
-        except IndexError:
-            pass
+        msg = deque.popleft()
+        if isinstance(msg, objects.Event) or (isinstance(msg, dict) and 'event_descriptor' in msg):
+            event = msg
+            break
+        else:
+            unused_elements.append(msg)
     deque.extend(unused_elements)
     return event

+ 1 - 0
test/test_utils.py

@@ -153,6 +153,7 @@ def test_get_event_from_deque():
     assert d.popleft() is msg_one
     assert d.popleft() is msg_two
     assert d.popleft() is msg_three
+    assert len(d) == 0
     assert utils.get_next_event_from_deque(d) is None