event_service.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. result = self.on_request_event(payload['ven_id'])
  23. if iscoroutine(result):
  24. result = await result
  25. if result is None:
  26. return 'oadrDistributeEvent', {'events': []}
  27. if isinstance(result, dict):
  28. return 'oadrDistributeEvent', {'events': [result]}
  29. if isinstance(result, list):
  30. return 'oadrDistributeEvent', {'events': result}
  31. else:
  32. raise TypeError("Event handler should return None, a dict or a list")
  33. @handler('oadrCreatedEvent')
  34. async def created_event(self, payload):
  35. """
  36. The VEN informs us that they created an EiEvent.
  37. """
  38. result = self.on_created_event(payload)
  39. if iscoroutine(result):
  40. result = await(result)
  41. return result