examples.rst 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. .. _examples:
  2. =====================
  3. Ready-to-Run Examples
  4. =====================
  5. This page contains examples for pyOpenADR:
  6. .. _client_example:
  7. Client Example
  8. ==============
  9. This example sets up a minimal OpenADR Client (Virtual End Node):
  10. .. code-block:: python3
  11. from pyopenadr import OpenADRClient
  12. import asyncio
  13. async def main():
  14. client = OpenADRClient(ven_name="Device001", vtn_url="http://localhost:8080/OpenADR2/Simple/2.0b")
  15. client.on_event = handle_event
  16. client.on_report = handle_report
  17. await client.run()
  18. async def handle_event(event):
  19. """
  20. This coroutine will be called
  21. whenever there is an event to be handled.
  22. """
  23. print("There is an event!")
  24. print(event)
  25. return 'optIn'
  26. async def handle_report(report):
  27. """
  28. This coroutine will be called
  29. whenever there is a report from the VTN.
  30. """
  31. print("There is a report!")
  32. print(report)
  33. loop = asyncio.get_event_loop()
  34. loop.create_task(main())
  35. loop.run_forever()
  36. .. _server_example:
  37. Server Example
  38. ==============
  39. .. _server_with_gui_example:
  40. Server with GUI Example
  41. =======================