poll_service.py 9.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from . import api, handler, VTNService
  2. from asyncio import iscoroutine
  3. # ╔══════════════════════════════════════════════════════════════════════════╗
  4. # ║ POLLING SERVICE ║
  5. # ╚══════════════════════════════════════════════════════════════════════════╝
  6. #
  7. # oadrPoll is a service independent polling mechanism used by VENs in a PULL
  8. # model to request pending service operations from the VTN. The VEN queries
  9. # the poll endpoint and the VTN re- sponds with the same message that it would
  10. # have “pushed” had it been a PUSH VEN. If there are multiple messages pending
  11. # a “push,” the VEN will continue to query the poll endpoint until there are
  12. # no new messages and the VTN responds with an eiResponse payload.
  13. #
  14. # ┌──────────────────────────────────────────────────────────────────────────┐
  15. # │ The VEN can poll for any messages that we have for them. If we have no │
  16. # │ (more) messages, we send a generic oadrResponse: │
  17. # │ ┌────┐ ┌────┐ │
  18. # │ │VEN │ │VTN │ │
  19. # │ └─┬──┘ └─┬──┘ │
  20. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  21. # │ │ │ │
  22. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  23. # │ │ │ │
  24. # │ │
  25. # └──────────────────────────────────────────────────────────────────────────┘
  26. # ┌──────────────────────────────────────────────────────────────────────────┐
  27. # │ If we have an Event, we expect the following: │
  28. # │ │
  29. # │ ┌────┐ ┌────┐ │
  30. # │ │VEN │ │VTN │ │
  31. # │ └─┬──┘ └─┬──┘ │
  32. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  33. # │ │ │ │
  34. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ oadrCreateEvent() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  35. # │ │ │ │
  36. # │ │───────────────────────oadrCreatedEvent()───────────────────────▶│ │
  37. # │ │ │ │
  38. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  39. # │ │ │ │
  40. # │ │
  41. # └──────────────────────────────────────────────────────────────────────────┘
  42. # ┌──────────────────────────────────────────────────────────────────────────┐
  43. # │ For Reports: │
  44. # │ │
  45. # │ ┌────┐ ┌────┐ │
  46. # │ │VEN │ │VTN │ │
  47. # │ └─┬──┘ └─┬──┘ │
  48. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  49. # │ │ │ │
  50. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrCreateReport() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  51. # │ │ │ │
  52. # │ │───────────────────────oadrCreatedReport()──────────────────────▶│ │
  53. # │ │ │ │
  54. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  55. # │ │ │ │
  56. # │ │
  57. # └──────────────────────────────────────────────────────────────────────────┘
  58. # ┌──────────────────────────────────────────────────────────────────────────┐
  59. # │ If re-registration is neccessary: │
  60. # │ │
  61. # │ ┌────┐ ┌────┐ │
  62. # │ │VEN │ │VTN │ │
  63. # │ └─┬──┘ └─┬──┘ │
  64. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  65. # │ │ │ │
  66. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  67. # │ │ │ │
  68. # │ │─────────────────────────oadrResponse()─────────────────────────▶│ │
  69. # │ │ │ │
  70. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ HTTP 200─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  71. # │ │ │ │
  72. # │ │
  73. # │ │──────────────────oadrCreatePartyRegistration()─────────────────▶│ │
  74. # │ │ │ │
  75. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  76. # │ │ │ │
  77. # │ │
  78. # └──────────────────────────────────────────────────────────────────────────┘
  79. @api.route('/OpenADR2/Simple/2.0b/OadrPoll')
  80. class PollService(VTNService):
  81. @handler('oadrPoll')
  82. async def poll(self, payload):
  83. """
  84. Retrieve the messages that we have for this VEN in order.
  85. The backend get_next_message
  86. """
  87. result = self.on_poll(ven_id=payload['ven_id'])
  88. if iscoroutine(result):
  89. result = await result
  90. return result