client.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 OpenLEADR 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. The VTN Server will most like want to receive some reports like metering values or availability status from you.
  28. Providing reports
  29. -----------------
  30. If you tell OpenLEADR what reports you are able to provide, and give it a handler that will retrieve those reports from your own systems, OpenLEADR will make sure that the server receives the reports it asks for and at the requested interval.
  31. For example: you can provide 15-minute meter readings for an energy meter at your site. You have a coroutine set up like this:
  32. .. code-block:: python3
  33. async def get_metervalue():
  34. current_value = await meter.read()
  35. return current_value
  36. And you configure this report in OpenLEADR using an :ref:`oadrReportDescription` dict:
  37. .. code-block:: python3
  38. async def main():
  39. client = OpenADRClient(ven_name='MyVEN', vtn_url='https://localhost:8080/')
  40. report_description = {''}
  41. client.add_report({'report'})
  42. 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.
  43. .. _client_signing_messages:
  44. Signing outgoing messages
  45. =========================
  46. 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.
  47. If you want you client to automatically sign your outgoing messages, use the following configuration:
  48. .. code-block:: python3
  49. async def main():
  50. client = OpenADRClient(ven_name='MyVEN',
  51. vtn_url='https://localhost:8080/',
  52. cert='/path/to/cert.pem',
  53. key='/path/to/key.pem',
  54. passphrase='my-key-password')
  55. ...
  56. .. _client_validating_messages:
  57. Validating incoming messages
  58. ============================
  59. You can validate incoming messages against a public key.
  60. .. code-block:: python3
  61. async def main():
  62. client = OpenADRClient(ven_name='MyVEN',
  63. vtn_url='https://localhost:8080/',
  64. vtn_fingerprint='AA:BB:CC:DD:EE:FF:11:22:33:44')
  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.
  66. You should use both of the previous examples combined to secure both the incoming and the outgoing messages.