created-party-registration.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* eslint-disable indent */
  2. 'use strict';
  3. const { parseXML, childAttr, required } = require('./parser');
  4. const { create, fragment } = require('xmlbuilder2');
  5. const oadrPayloadNs = 'http://www.w3.org/2000/09/xmldsig#';
  6. const oadrNs = 'http://openadr.org/oadr-2.0b/2012/07';
  7. const energyInteropNs = 'http://docs.oasis-open.org/ns/energyinterop/201110';
  8. const energyInteropPayloadsNs =
  9. 'http://docs.oasis-open.org/ns/energyinterop/201110/payloads';
  10. const calendarNs = 'urn:ietf:params:xml:ns:icalendar-2.0';
  11. function parseEiResponse(response) {
  12. return {
  13. code: required(childAttr(response, 'responseCode'), 'responseCode'),
  14. description: childAttr(response, 'responseDescription'),
  15. requestId: required(childAttr(response, 'requestID'), 'requestID'),
  16. };
  17. }
  18. function parseDuration(response) {
  19. return required(childAttr(response, 'duration'), 'duration');
  20. }
  21. async function parse(input) {
  22. const json = await parseXML(input);
  23. const o =
  24. json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'][
  25. 'oadrCreatedPartyRegistration'
  26. ][0]['$$'];
  27. const { code, description, requestId } = parseEiResponse(
  28. o['eiResponse'][0]['$$'],
  29. );
  30. const result = {
  31. responseCode: code,
  32. responseDescription: description,
  33. responseRequestId: requestId,
  34. };
  35. if (code < 200 || code >= 300) {
  36. return result;
  37. }
  38. const registrationId = childAttr(o, 'registrationID');
  39. if (registrationId != null) result.registrationId = registrationId;
  40. const venId = childAttr(o, 'venID');
  41. if (venId != null) result.venId = venId;
  42. const vtnId = childAttr(o, 'vtnID');
  43. if (vtnId != null) result.vtnId = vtnId;
  44. const oadrRequestedOadrPollFreq = childAttr(o, 'oadrRequestedOadrPollFreq');
  45. if (oadrRequestedOadrPollFreq != null) {
  46. const oadrRequestedOadrPollFreqDuration = parseDuration(
  47. oadrRequestedOadrPollFreq['$$'],
  48. );
  49. if (oadrRequestedOadrPollFreqDuration != null)
  50. result.pollFreqDuration = oadrRequestedOadrPollFreqDuration;
  51. }
  52. return result;
  53. }
  54. function serializeEiResponse(code, description, requestId) {
  55. const descriptionFrag =
  56. description != null
  57. ? fragment()
  58. .ele(energyInteropNs, 'ei:responseDescription')
  59. .txt(description)
  60. : fragment();
  61. return fragment()
  62. .ele(energyInteropNs, 'ei:responseCode')
  63. .txt(code)
  64. .up()
  65. .import(descriptionFrag)
  66. .ele(energyInteropPayloadsNs, 'pyld:requestID')
  67. .txt(requestId)
  68. .up();
  69. }
  70. function serializeDuration(duration) {
  71. return duration != null
  72. ? fragment()
  73. .ele(calendarNs, 'cal:duration')
  74. .txt(duration)
  75. : fragment();
  76. }
  77. function validate(obj) {
  78. if (!obj.responseCode) {
  79. throw new Error('Missing responseCode');
  80. }
  81. if (!obj.responseRequestId) {
  82. throw new Error('Missing responseRequestId');
  83. }
  84. }
  85. function serialize(obj) {
  86. validate(obj);
  87. const registrationId =
  88. obj.registrationId != null
  89. ? fragment()
  90. .ele(energyInteropNs, 'ei:registrationID')
  91. .txt(obj.registrationId)
  92. : fragment();
  93. const venId =
  94. obj.venId != null
  95. ? fragment()
  96. .ele(energyInteropNs, 'ei:venID')
  97. .txt(obj.venId)
  98. : fragment();
  99. const vtnId =
  100. obj.vtnId != null
  101. ? fragment()
  102. .ele(energyInteropNs, 'ei:vtnID')
  103. .txt(obj.vtnId)
  104. : fragment();
  105. const doc = create({
  106. namespaceAlias: {
  107. ns: oadrPayloadNs,
  108. oadr2b: oadrNs,
  109. ei: energyInteropNs,
  110. pyld: energyInteropPayloadsNs,
  111. cal: calendarNs,
  112. },
  113. })
  114. .ele('@oadr2b', 'oadr2b:oadrPayload')
  115. .ele('oadr2b:oadrSignedObject')
  116. .ele('oadr2b:oadrCreatedPartyRegistration')
  117. .att('@ei', 'ei:schemaVersion', '2.0b')
  118. .ele('@ei', 'ei:eiResponse')
  119. .import(
  120. serializeEiResponse(
  121. obj.responseCode,
  122. obj.responseDescription,
  123. obj.responseRequestId,
  124. ),
  125. )
  126. .up()
  127. .import(registrationId)
  128. .import(venId)
  129. .import(vtnId)
  130. .ele('oadr2b:oadrProfiles')
  131. .ele('oadr2b:oadrProfile')
  132. .ele('oadr2b:oadrProfileName')
  133. .txt('2.0b')
  134. .up()
  135. .ele('oadr2b:oadrTransports')
  136. .ele('oadr2b:oadrTransport')
  137. .ele('oadr2b:oadrTransportName')
  138. .txt('simpleHttp')
  139. .up()
  140. .up()
  141. .up()
  142. .up()
  143. .up()
  144. .ele('oadr2b:oadrRequestedOadrPollFreq')
  145. .import(serializeDuration(obj.pollFreqDuration))
  146. .doc();
  147. return doc.end({ headless: true, prettyPrint: false });
  148. }
  149. module.exports = {
  150. parse,
  151. serialize,
  152. };