test_message_conversion.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from pyopenadr.utils import generate_id
  13. from pyopenadr.messaging import create_message, parse_message
  14. from pyopenadr import enums
  15. from pprint import pprint
  16. from termcolor import colored
  17. from datetime import datetime, timezone, timedelta
  18. DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  19. def create_dummy_event(ven_id):
  20. """
  21. Creates a dummy event
  22. """
  23. now = datetime.now(timezone.utc)
  24. event_id = generate_id()
  25. active_period = {"dtstart": (now + timedelta(minutes=1)),
  26. "duration": timedelta(minutes=9)}
  27. event_descriptor = {"event_id": event_id,
  28. "modification_number": 1,
  29. "modification_date_time": now,
  30. "priority": 1,
  31. "market_context": "http://MarketContext1",
  32. "created_date_time": now,
  33. "event_status": "near",
  34. "test_event": False,
  35. "vtn_comment": "This is an event"}
  36. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 0, "signal_payload": 8.0},
  37. {"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 10.0},
  38. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 12.0},
  39. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 14.0},
  40. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 16.0},
  41. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 18.0},
  42. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 20.0},
  43. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 10.0},
  44. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 20.0}],
  45. "signal_name": "LOAD_CONTROL",
  46. #"signal_name": "simple",
  47. #"signal_type": "level",
  48. "signal_type": "x-loadControlCapacity",
  49. "signal_id": generate_id(),
  50. "current_value": 0.0}]
  51. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  52. event = {'active_period': active_period,
  53. 'event_descriptor': event_descriptor,
  54. 'event_signals': event_signals,
  55. 'targets': event_targets,
  56. 'response_required': 'always'}
  57. return event
  58. def test_message(message_type, **data):
  59. message = create_message(message_type, **data)
  60. # print(message)
  61. parsed = parse_message(message)[1]
  62. if parsed == data:
  63. print(colored(f"pass {message_type}", "green"))
  64. else:
  65. pprint(data)
  66. print(message)
  67. pprint(parsed)
  68. print(colored(f"fail {message_type}", "red"))
  69. quit(1)
  70. test_message('oadrCanceledOpt', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, opt_id=generate_id())
  71. test_message('oadrCanceledPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, registration_id=generate_id(), ven_id='123ABC')
  72. test_message('oadrCanceledReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  73. test_message('oadrCanceledReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')
  74. test_message('oadrCancelOpt', request_id=generate_id(), ven_id='123ABC', opt_id=generate_id())
  75. test_message('oadrCancelPartyRegistration', request_id=generate_id(), ven_id='123ABC', registration_id=generate_id())
  76. test_message('oadrCancelReport', request_id=generate_id(), ven_id='123ABC', report_request_id=generate_id(), report_to_follow=True)
  77. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  78. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  79. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  80. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'}],
  81. ven_id='123ABC')
  82. test_message('oadrCreatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')
  83. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  84. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  85. 'event_id': generate_id(),
  86. 'modification_number': 1,
  87. 'opt_type': 'optIn'},
  88. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  89. 'event_id': generate_id(),
  90. 'modification_number': 1,
  91. 'opt_type': 'optOut'}],
  92. ven_id='123ABC')
  93. test_message('oadrCreatedPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  94. registration_id=generate_id(),
  95. ven_id='123ABC',
  96. profiles=[{'profile_name': '2.0b',
  97. 'transports': [{'transport_name': 'simpleHttp'}]}],
  98. vtn_id='VTN123')
  99. test_message('oadrCreatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  100. test_message('oadrCreateOpt', opt_id=generate_id(),
  101. opt_type='optIn',
  102. opt_reason='participating',
  103. created_date_time=datetime.now(timezone.utc),
  104. request_id=generate_id(),
  105. event_id=generate_id(),
  106. modification_number=1,
  107. targets=[{'ven_id': '123ABC'}],
  108. ven_id='VEN123')
  109. test_message('oadrCreatePartyRegistration', request_id=generate_id(), ven_id='123ABC', profile_name='2.0b', transport_name='simpleHttp', transport_address='http://localhost', report_only=False, xml_signature=False, ven_name='test', http_pull_model=True)
  110. test_message('oadrCreateReport', request_id=generate_id(),
  111. ven_id='123ABC',
  112. report_requests=[{'report_request_id': 'd2b7bade5f',
  113. 'report_specifier': {'granularity': timedelta(seconds=900),
  114. 'report_back_duration': timedelta(seconds=900),
  115. 'report_interval': {'dtstart': datetime(2019, 11, 19, 11, 0, 18, 672768, tzinfo=timezone.utc),
  116. 'duration': timedelta(seconds=7200),
  117. 'tolerance': {'tolerate': {'startafter': timedelta(seconds=300)}}},
  118. 'report_specifier_id': '9c8bdc00e7',
  119. 'specifier_payload': {'r_id': 'd6e2e07485',
  120. 'reading_type': 'Direct Read'}}}])
  121. test_message('oadrDistributeEvent', request_id=generate_id(), response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'}, events=[create_dummy_event(ven_id='123ABC')], vtn_id='VTN123')
  122. test_message('oadrPoll', ven_id='123ABC')
  123. test_message('oadrQueryRegistration', request_id=generate_id())
  124. test_message('oadrRegisteredReport', ven_id='VEN123', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  125. report_requests=[{'report_request_id': generate_id(),
  126. 'report_specifier': {'report_specifier_id': generate_id(),
  127. 'granularity': timedelta(minutes=15),
  128. 'report_back_duration': timedelta(minutes=15),
  129. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  130. 'duration': timedelta(hours=2),
  131. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  132. 'notification': timedelta(minutes=30),
  133. 'ramp_up': timedelta(minutes=15),
  134. 'recovery': timedelta(minutes=5)},
  135. 'specifier_payload': {'r_id': generate_id(),
  136. 'reading_type': 'Direct Read'}}},
  137. {'report_request_id': generate_id(),
  138. 'report_specifier': {'report_specifier_id': generate_id(),
  139. 'granularity': timedelta(minutes=15),
  140. 'report_back_duration': timedelta(minutes=15),
  141. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  142. 'duration': timedelta(hours=2),
  143. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  144. 'notification': timedelta(minutes=30),
  145. 'ramp_up': timedelta(minutes=15),
  146. 'recovery': timedelta(minutes=5)},
  147. 'specifier_payload': {'r_id': generate_id(),
  148. 'reading_type': 'Direct Read'}}}])
  149. test_message('oadrRequestEvent', request_id=generate_id(), ven_id='123ABC')
  150. test_message('oadrRequestReregistration', ven_id='123ABC')
  151. test_message('oadrRegisterReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  152. 'report_descriptions': {
  153. generate_id(): {
  154. 'report_subjects': [{'ven_id': '123ABC'}],
  155. 'report_data_sources': [{'ven_id': '123ABC'}],
  156. 'report_type': 'reading',
  157. 'reading_type': 'Direct Read',
  158. 'market_context': 'http://localhost',
  159. 'sampling_rate': {'min_period': timedelta(minutes=1), 'max_period': timedelta(minutes=1), 'on_change': True}}},
  160. 'report_request_id': generate_id(),
  161. 'report_specifier_id': generate_id(),
  162. 'report_name': 'HISTORY_USAGE',
  163. 'created_date_time': datetime.now(timezone.utc)}],
  164. ven_id='123ABC',
  165. report_request_id=generate_id())
  166. test_message('oadrRegisterReport', **{'request_id': '8a4f859883', 'reports': [{'report_id': generate_id(),
  167. 'duration': timedelta(seconds=7200),
  168. 'report_descriptions': {'resource1_status': {
  169. 'report_data_sources': [{'resource_id': 'resource1'}],
  170. 'report_type': 'x-resourceStatus',
  171. 'reading_type': 'x-notApplicable',
  172. 'market_context': 'http://MarketContext1',
  173. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  174. 'report_request_id': '0',
  175. 'report_specifier_id': '789ed6cd4e_telemetry_status',
  176. 'report_name': 'METADATA_TELEMETRY_STATUS',
  177. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  178. {'report_id': generate_id(),
  179. 'duration': timedelta(seconds=7200),
  180. 'report_descriptions': {'resource1_energy': {
  181. 'report_data_sources': [{'resource_id': 'resource1'}],
  182. 'report_type': 'usage',
  183. 'energy_real': {'item_description': 'RealEnergy',
  184. 'item_units': 'Wh',
  185. 'si_scale_code': 'n'},
  186. 'reading_type': 'Direct Read',
  187. 'market_context': 'http://MarketContext1',
  188. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  189. 'resource1_power': {
  190. 'report_data_sources': [{'resource_id': 'resource1'}],
  191. 'report_type': 'usage',
  192. 'power_real': {'item_description': 'RealPower',
  193. 'item_units': 'W',
  194. 'si_scale_code': 'n',
  195. 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}},
  196. 'reading_type': 'Direct Read',
  197. 'market_context': 'http://MarketContext1',
  198. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  199. 'report_request_id': '0',
  200. 'report_specifier_id': '789ed6cd4e_telemetry_usage',
  201. 'report_name': 'METADATA_TELEMETRY_USAGE',
  202. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  203. {'report_id': generate_id(),
  204. 'duration': timedelta(seconds=7200),
  205. 'report_descriptions': {'resource1_energy': {
  206. 'report_data_sources': [{'resource_id': 'resource1'}],
  207. 'report_type': 'usage',
  208. 'energy_real': {'item_description': 'RealEnergy',
  209. 'item_units': 'Wh',
  210. 'si_scale_code': 'n'},
  211. 'reading_type': 'Direct Read',
  212. 'market_context': 'http://MarketContext1',
  213. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  214. 'resource1_power': {
  215. 'report_data_sources': [{'resource_id': 'resource1'}],
  216. 'report_type': 'usage',
  217. 'power_real': {'item_description': 'RealPower',
  218. 'item_units': 'W', 'si_scale_code': 'n',
  219. 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}},
  220. 'reading_type': 'Direct Read',
  221. 'market_context': 'http://MarketContext1',
  222. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  223. 'report_request_id': '0',
  224. 'report_specifier_id': '789ed6cd4e_history_usage',
  225. 'report_name': 'METADATA_HISTORY_USAGE',
  226. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}], 'ven_id': 's3cc244ee6'})
  227. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC')
  228. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': None}, ven_id='123ABC')
  229. test_message('oadrUpdatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC', cancel_report={'request_id': generate_id(), 'report_request_id': [generate_id(), generate_id(), generate_id()], 'report_to_follow': False, 'ven_id': '123ABC'})
  230. test_message('oadrUpdateReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  231. 'report_name': enums.REPORT_NAME.values[0],
  232. 'created_date_time': datetime.now(timezone.utc),
  233. 'report_request_id': generate_id(),
  234. 'report_specifier_id': generate_id(),
  235. 'report_descriptions': {generate_id(): {'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  236. 'report_data_sources': [{'ven_id': '123ABC'}],
  237. 'report_type': enums.REPORT_TYPE.values[0],
  238. 'reading_type': enums.READING_TYPE.values[0],
  239. 'market_context': 'http://localhost',
  240. 'sampling_rate': {'min_period': timedelta(minutes=1),
  241. 'max_period': timedelta(minutes=2),
  242. 'on_change': False}}}}], ven_id='123ABC')
  243. # for report_name in enums.REPORT_NAME.values:
  244. # for reading_type in enums.READING_TYPE.values:
  245. # for report_type in enums.REPORT_TYPE.values:
  246. # test_message('oadrUpdateReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  247. # 'report_name': report_name,
  248. # 'created_date_time': datetime.now(timezone.utc),
  249. # 'report_request_id': generate_id(),
  250. # 'report_specifier_id': generate_id(),
  251. # 'report_descriptions': [{'r_id': generate_id(),
  252. # 'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  253. # 'report_data_sources': [{'ven_id': '123ABC'}],
  254. # 'report_type': report_type,
  255. # 'reading_type': reading_type,
  256. # 'market_context': 'http://localhost',
  257. # 'sampling_rate': {'min_period': timedelta(minutes=1),
  258. # 'max_period': timedelta(minutes=2),
  259. # 'on_change': False}}]}], ven_id='123ABC')