server.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from aiohttp import web
  13. from pyopenadr.service import EventService, PollService, RegistrationService, ReportService, OptService, VTNService
  14. from pyopenadr.messaging import create_message, parse_message
  15. from functools import partial
  16. class OpenADRServer:
  17. _MAP = {'on_created_event': EventService,
  18. 'on_request_event': EventService,
  19. 'on_register_report': ReportService,
  20. 'on_create_report': ReportService,
  21. 'on_created_report': ReportService,
  22. 'on_request_report': ReportService,
  23. 'on_update_report': ReportService,
  24. 'on_poll': PollService,
  25. 'on_query_registration': RegistrationService,
  26. 'on_create_party_registration': RegistrationService,
  27. 'on_cancel_party_registration': RegistrationService}
  28. def __init__(self, vtn_id, cert=None, key=None, passphrase=None, verification_cert=None):
  29. self.app = web.Application()
  30. self.services = {'event_service': EventService(vtn_id),
  31. 'report_service': ReportService(vtn_id),
  32. 'poll_service': PollService(vtn_id),
  33. 'opt_service': OptService(vtn_id),
  34. 'registration_service': RegistrationService(vtn_id)}
  35. self.app.add_routes([web.post(f"/OpenADR2/Simple/2.0b/{s.__service_name__}", s.handler) for s in self.services.values()])
  36. # Configure message signing
  37. if cert and key:
  38. with open(cert, "rb") as file:
  39. cert = file.read()
  40. with open(key, "rb") as file:
  41. key = file.read()
  42. if verification_cert:
  43. with open(verification_cert, "rb") as file:
  44. verification_cert = file.read()
  45. VTNService._create_message = partial(create_message, cert=cert, key=key, passphrase=passphrase)
  46. VTNService._parse_message = partial(parse_message, cert=verification_cert)
  47. self.__setattr__ = self.add_handler
  48. def run(self):
  49. """
  50. Starts the asyncio-loop and runs the server in it. This function is
  51. blocking. For other ways to run the server in a more flexible context,
  52. please refer to the `aiohttp documentation
  53. <https://docs.aiohttp.org/en/stable/web_advanced.html#aiohttp-web-app-runners>`_.
  54. """
  55. web.run_app(self.app)
  56. def add_handler(self, name, func):
  57. """
  58. Add a handler to the OpenADRServer.
  59. """
  60. print("Called add_handler", name, func)
  61. if name in self._MAP:
  62. setattr(self._MAP[name], name, staticmethod(func))
  63. else:
  64. raise NameError(f"Unknown handler {name}. Correct handler names are: {self._MAP.keys()}")