فهرست منبع

More updates to reports interface

Stan Janssen 5 سال پیش
والد
کامیت
98cd9c982e
4فایلهای تغییر یافته به همراه36 افزوده شده و 29 حذف شده
  1. 12 7
      pyopenadr/client.py
  2. 14 12
      pyopenadr/service/registration_service.py
  3. 1 1
      setup.py
  4. 9 9
      test/test_schema.py

+ 12 - 7
pyopenadr/client.py

@@ -11,11 +11,12 @@ import asyncio
 from asyncio import iscoroutine
 
 class OpenADRClient:
-    def __init__(self, ven_name, vtn_url):
+    def __init__(self, ven_name, vtn_url, debug=False):
         self.ven_name = ven_name
         self.vtn_url = vtn_url
         self.ven_id = None
         self.poll_frequency = None
+        self.debug = debug
 
     def run(self):
         """
@@ -117,8 +118,8 @@ class OpenADRClient:
                    'event_responses': [{'response_code': 200,
                                         'response_description': 'OK',
                                         'request_id': request_id,
-                                        'qualified_event_id': {'event_id': event_id,
-                                                               'modification_number': modification_number},
+                                        'event_id': event_id,
+                                        'modification_number': modification_number,
                                         'opt_type': opt_type}]}
         message = create_message('oadrCreatedEvent', **payload)
         response_type, response_payload = self._perform_request(service, message)
@@ -137,22 +138,26 @@ class OpenADRClient:
         return response_type, response_payload
 
     def _perform_request(self, service, message):
-        print(f"Sending {message}")
+        if self.debug:
+            print(f"Sending {message}")
         url = f"{self.vtn_url}/{service}"
         r = requests.post(url,
                           data=message)
         if r.status_code != HTTPStatus.OK:
             raise Exception(f"Received non-OK status in request: {r.status_code}")
-        print(r.content.decode('utf-8'))
+        if self.debug:
+            print(r.content.decode('utf-8'))
         return parse_message(r.content)
 
     async def _on_event(self, message):
-        print("ON_EVENT")
+        if self.debug:
+            print("ON_EVENT")
         result = self.on_event(message)
         if iscoroutine(result):
             result = await result
 
-        print(f"Now responding with {result}")
+        if self.debug:
+            print(f"Now responding with {result}")
         request_id = message['request_id']
         event_id = message['events'][0]['event_descriptor']['event_id']
         self.created_event(request_id, event_id, result)

+ 14 - 12
pyopenadr/service/registration_service.py

@@ -50,18 +50,20 @@ class RegistrationService(VTNService):
         """
         Return the profiles we support.
         """
-        request_id = payload['request_id']
-        response_type = "oadrCreatedPartyRegistration"
-        response_payload = {"request_id": request_id,
-                            "vtn_id": "elaadvtn",
-                            "profiles": [{"profile_name": "2.0a",
-                                          "transports": [{"transport_name": "simpleHttp"},
-                                                          {"transport_name": "xmpp"}]},
-                                         {"profile_name": "2.0b",
-                                          "transports": [{"transport_name": "simpleHttp"},
-                                                          {"transport_name": "xmpp"}]}],
-                            "requested_oadr_poll_freq": timedelta(seconds=10)}
-        return response_type, response_payload
+        if hasattr(self, 'on_query_registration'):
+            result = self.on_query_registration(payload)
+            if iscoroutine(result):
+                result = await result
+            return result
+
+        # If you don't provide a default handler, just give out the info
+        response_payload = {'response': {'response_code': 200, 'response_description': 'OK', 'request_id': payload['request_id']},
+                            'request_id': payload['request_id'],
+                            'vtn_id': 'ElaadVTN',
+                            'profiles': [{'profile_name': '2.0b',
+                                          'transports': {'transport_name': 'simpleHttp'}}],
+                            'requested_oadr_poll_freq': timedelta(seconds=5)}
+        return 'oadrCreatedPartyRegistration', response_payload
 
     @handler('oadrCreatePartyRegistration')
     async def create_party_registration(self, payload):

+ 1 - 1
setup.py

@@ -4,7 +4,7 @@ with open("README.md", "r") as fh:
     long_description = fh.read()
 
 setup(name="pyopenadr",
-      version="0.2.0",
+      version="0.2.1",
       description="Python library for dealing with OpenADR",
       long_description=long_description,
       long_description_content_type="text/markdown",

+ 9 - 9
test/test_schema.py

@@ -31,15 +31,15 @@ def create_dummy_event(ven_id):
                         "test_event": "false",
                         "vtn_comment": "This is an event"}
 
-    event_signals = [{"intervals": [{"duration": "PT1M", "uid": 1, "signal_payload": 8},
-                                    {"duration": "PT1M", "uid": 2, "signal_payload": 10},
-                                    {"duration": "PT1M", "uid": 3, "signal_payload": 12},
-                                    {"duration": "PT1M", "uid": 4, "signal_payload": 14},
-                                    {"duration": "PT1M", "uid": 5, "signal_payload": 16},
-                                    {"duration": "PT1M", "uid": 6, "signal_payload": 18},
-                                    {"duration": "PT1M", "uid": 7, "signal_payload": 20},
-                                    {"duration": "PT1M", "uid": 8, "signal_payload": 10},
-                                    {"duration": "PT1M", "uid": 9, "signal_payload": 20}],
+    event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
+                                    {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
+                                    {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
+                                    {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
+                                    {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
+                                    {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
+                                    {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
+                                    {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
+                                    {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
                     "signal_name": "LOAD_CONTROL",
                     #"signal_name": "simple",
                     #"signal_type": "level",