test_message_conversion.py 27 KB

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