client.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. async def on_event(event):
  18. # Check if we can opt in to this event
  19. first_signal = event['event_signals'][0]
  20. intervals = first_signal['intervals']
  21. target = event['target']
  22. ...
  23. return 'optIn'
  24. An example event dict might look like this:
  25. .. code-block:: python3
  26. {
  27. 'event_id': '123786-129831',
  28. 'active_period': {'dtstart': datetime.datetime(2020, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
  29. 'duration': datetime.timedelta(minutes=30)}
  30. 'event_signals': [{'signal_name': 'simple',
  31. 'signal_type': 'level',
  32. 'intervals': [{'dtstart': datetime.datetime(2020, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
  33. 'duration': datetime.timedelta(minutes=10),
  34. 'signal_payload': 1},
  35. {'dtstart': datetime.datetime(2020, 1, 1, 12, 10, 0, tzinfo=timezone.utc),
  36. 'duration': datetime.timedelta(minutes=10),
  37. 'signal_payload': 0},
  38. {'dtstart': datetime.datetime(2020, 1, 1, 12, 20, 0, tzinfo=timezone.utc),
  39. 'duration': datetime.timedelta(minutes=10),
  40. 'signal_payload': 1}],
  41. 'targets': [{'resource_id': 'Device001'}],
  42. 'targets_by_type': {'resource_id': ['Device001']}
  43. }
  44. Please note that you can access the targets in two ways, which may be useful if there are more than one target:
  45. 1. As a list of Target dicts
  46. 2. As a dictionary of targets, grouped by target type.
  47. For example:
  48. .. code-block:: python3
  49. {
  50. 'event_id': 'event123',
  51. # ...
  52. # As a list of Target dicts
  53. 'targets': [{'resource_id': 'resource01'},
  54. {'resource_id': 'resource02'},
  55. {'group_id': 'group01'},
  56. {'group_id': 'group02'}],
  57. # Grouped by target type
  58. 'targets_by_type': {'resource_id': ['resource01', 'resource02'],
  59. 'group_id': ['group01', 'group02']}
  60. }
  61. It is up to you which you want to use.
  62. .. _client_reports:
  63. Dealing with Reports
  64. ====================
  65. The VTN Server will most likely want to receive some reports like metering values or availability status from you.
  66. You can easily add reporting capabilities to your OpenADRClient object using the ``client.add_report`` method. In this method, you supply a callback function that will retrieve the current value for that measurement, as well as the resource_id, the measurement (like 'voltage', 'power', 'temperature', et cetera), optionally a unit and scale, and a sampling rate at which you can support this metervalue.
  67. OpenLEADR will then offer this report to the VTN, and if they request this report from you, your callback function will automatically be called when needed.
  68. Please see the :ref:`reporting` section for detailed information.
  69. .. _client_signing_messages:
  70. Signing outgoing messages
  71. =========================
  72. 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.
  73. If you want you client to automatically sign your outgoing messages, use the following configuration:
  74. .. code-block:: python3
  75. async def main():
  76. client = OpenADRClient(ven_name='MyVEN',
  77. vtn_url='https://localhost:8080/',
  78. cert='/path/to/cert.pem',
  79. key='/path/to/key.pem',
  80. passphrase='my-key-password')
  81. ...
  82. .. _client_validating_messages:
  83. Validating incoming messages
  84. ============================
  85. You can validate incoming messages against a public key.
  86. .. code-block:: python3
  87. async def main():
  88. client = OpenADRClient(ven_name='MyVEN',
  89. vtn_url='https://localhost:8080/',
  90. vtn_fingerprint='AA:BB:CC:DD:EE:FF:11:22:33:44')
  91. 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.
  92. You should use both of the previous examples combined to secure both the incoming and the outgoing messages.