client.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ======
  2. Client
  3. ======
  4. An OpenADR Client (Virtual End Node or VEN) usually represents an entity that owns controllable devices. This can be electric vehicles, generators, wind turbines, refrigerated warehouses, et cetera. The client connects to a server, usualy representing a utility company, to discuss possible cooperation on energy usage throughout the day.
  5. In your application, you mostly only have to deal with two things: Events and Reports.
  6. .. _client_events:
  7. Dealing with Events
  8. ===================
  9. Events are informational or instructional messages from the server (VTN) which inform you of price changes, request load reduction, et cetera. Whenever there is an Event for your VEN, your ``on_event`` handler will be called with the event as its ``payload`` parameter.
  10. The Event consists of three main sections:
  11. 1. A time period for when this event is supposed to be active (``active_period``)
  12. 2. A list of Targets to which the Event applies (``target``). This can be the VEN as a whole, or specific groups, assets, geographic areas, et cetera that this VEN represents.
  13. 3. A list of Signals (``signals``), which form the content of the Event. This can be price signals, load reduction signals, et cetera. Each signal has a name, a type, multiple Intervals that contain the relative start times, and some payload value for the client to interpret.
  14. After you evaluate all these properties, you have only one decision to make: Opt In or Opt Out. Your handler must return either the string ``optIn`` or ``optOut``, and pyOpenADR will see to it that your response is correctly formatted for the server.
  15. Example implementation:
  16. .. code-block:: python3
  17. from openadr import OpenADRClient
  18. async def on_event(payload):
  19. # Check if we can opt in to this event
  20. start_time = payload['events'][0]['active_period']['dtstart']
  21. duration = payload['events'][0]['active_period']['duration']
  22. await can_we_do_this(from_time=payload[''])
  23. return 'optIn'
  24. .. _client_reports:
  25. Dealing with Reports
  26. ====================
  27. Receiving reports
  28. -----------------
  29. Upon registration, the server will tell you which reports it has available.
  30. Providing reports
  31. -----------------
  32. If you tell pyOpenADR what reports you are able to provide, and give it a handler that will retrieve those reports from your own systems, pyOpenADR will make sure that the server receives the reports it asks for and at the requested interval.
  33. For example: you can provide 15-minute meter readings for an energy meter at your site. You have a coroutine set up like this:
  34. .. code-block:: python3
  35. async def get_metervalue():
  36. current_value = await meter.read()
  37. return current_value
  38. And you configure this report in pyOpenADR using an :ref:`oadrReportDescription` dict:
  39. .. code-block:: python3
  40. async def main():
  41. client = OpenADRClient(ven_name='MyVEN', vtn_url='https://localhost:8080/')
  42. report_description = {''}
  43. client.add_report({'report'})
  44. The only thing you need to provide is the current value for the item you are reporting. PyOpenADR will format the complete :ref:`oadrReport` message for you.
  45. .. _client_signing_messages:
  46. Signing outgoing messages
  47. =========================
  48. You can sign your outgoing messages using a public-private key pair in PEM format. This allows the receiving party to verify that the messages are actually coming from you.
  49. If you want you client to automatically sign your outgoing messages, use the following configuration:
  50. .. code-block:: python3
  51. async def main():
  52. client = OpenADRClient(ven_name='MyVEN', vtn_url='https://localhost:8080/',
  53. cert='/path/to/cert.pem',
  54. key='/path/to/key.pem',
  55. passphrase='my-key-password')
  56. ...
  57. .. _client_validating_messages:
  58. Validating incoming messages
  59. ============================
  60. You can validate incoming messages against a public key.
  61. .. code-block:: python3
  62. async def main():
  63. client = OpenADRClient(ven_name='MyVEN', vtn_url='https://localhost:8080/',
  64. verification_cert='/path/to/cert.pem')
  65. This will automatically validate check that incoming messages are signed by the private key that belongs to the provided (public) certificate. If validation fails, you will see a Warning emitted, but the message will not be delivered to your handlers, protecting you from malicious messages being processed by your system. The sending party will see an error message returned.