event_service.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 . import service, handler, VTNService
  13. from datetime import datetime, timedelta, timezone
  14. from asyncio import iscoroutine
  15. @service('EiEvent')
  16. class EventService(VTNService):
  17. @handler('oadrRequestEvent')
  18. async def request_event(self, payload):
  19. """
  20. The VEN requests us to send any events we have.
  21. """
  22. try:
  23. result = self.on_request_event(payload['ven_id'])
  24. if iscoroutine(result):
  25. result = await result
  26. except OpenADRError as err:
  27. response_type = 'oadrResponse'
  28. response_payload = {'request_id': payload['request_id'],
  29. 'response_code': err.status,
  30. 'response_description': err.description,
  31. 'ven_id': payload['ven_id']}
  32. return response_type, response_payload
  33. else:
  34. return result
  35. @handler('oadrCreatedEvent')
  36. async def created_event(self, payload):
  37. """
  38. The VEN informs us that they created an EiEvent.
  39. """
  40. result = self.on_created_event(payload)
  41. if iscoroutine(result):
  42. result = await(result)
  43. return result