provision-ven.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. 'use strict';
  2. const { API } = require('@hw/edge-sdks');
  3. const logger = require('../../logger');
  4. const { companyId, company, nantumUrl } = require('../../config');
  5. const { request } = API({ company, logger });
  6. const { pki } = require('node-forge');
  7. const {
  8. calculatePartialFingerprintOfEscapedPemCertificate,
  9. } = require('../../modules/certificate');
  10. const { Ven } = require('../../client/ven');
  11. const { port } = require('../../config');
  12. async function generateEventForVen1(venId) {
  13. const eventStartMillis = new Date().getTime() + ( 60 * 60 * 1000 );
  14. const event = {
  15. 'cancelled': false,
  16. 'priority': 1,
  17. 'response_required': true,
  18. 'dis': `Test Event ${eventStartMillis}`,
  19. 'modification_number': 1,
  20. 'modification_reason': 'Updated market context',
  21. 'market_context': 'http://emix2',
  22. 'targets': [
  23. {
  24. 'dis': 'ven target',
  25. 'target_type': 'ven',
  26. 'value': '1234567'
  27. }
  28. ],
  29. 'signals': {
  30. 'event': [
  31. {
  32. 'signal_id': 'id1',
  33. 'signal_name': 'BID_LOAD',
  34. 'signal_type': 'setpoint',
  35. 'current_value': 45.5,
  36. 'duration_seconds': 1800,
  37. 'start_time': new Date(eventStartMillis).toISOString(),
  38. 'intervals': [
  39. {
  40. 'duration_seconds': 1740,
  41. 'signal_payloads': [
  42. 50
  43. ],
  44. 'uid': '0'
  45. },
  46. {
  47. 'duration_seconds': 60,
  48. 'signal_payloads': [
  49. 51
  50. ],
  51. 'uid': '1'
  52. }
  53. ],
  54. 'item_base': {
  55. 'type': 'power-real',
  56. 'dis': 'RealPower',
  57. 'units': 'W',
  58. 'si_scale_code': 'none',
  59. 'power_attributes': {
  60. 'hertz': 60,
  61. 'voltage': 120,
  62. 'ac': true
  63. }
  64. }
  65. }
  66. ]
  67. },
  68. 'active_period': {
  69. 'duration_seconds': 1800,
  70. 'notification_duration_seconds': 86400,
  71. 'ramp_up_duration_seconds': 3600,
  72. 'recovery_duration_seconds': 0,
  73. 'start_tolerance_duration_seconds': 0,
  74. 'start_time': new Date(eventStartMillis).toISOString()
  75. }
  76. };
  77. // create test event
  78. const newEvent = await request({
  79. method: 'POST',
  80. uri: `${nantumUrl}/oadr_utility_events`,
  81. body: event,
  82. });
  83. // create event ven mapping
  84. await request({
  85. method: 'POST',
  86. uri: `${nantumUrl}/oadr_utility_event_vens`,
  87. body: {
  88. oadr_utility_event_id: newEvent[0]._id,
  89. oadr_utility_ven_id: venId,
  90. },
  91. });
  92. }
  93. async function generateEventForVen2(venId) {
  94. const eventStartMillis = new Date().getTime() + ( 26 * 60 * 60 * 1000 );
  95. const event = {
  96. 'cancelled': false,
  97. 'priority': 1,
  98. 'response_required': true,
  99. 'dis': `Test Event ${eventStartMillis}`,
  100. 'modification_number': 0,
  101. 'market_context': 'http://emix2',
  102. 'signals': {
  103. 'event': [
  104. {
  105. 'signal_id': 'id1',
  106. 'signal_name': 'BID_LOAD',
  107. 'signal_type': 'setpoint',
  108. 'current_value': 47.5,
  109. 'duration_seconds': 1800,
  110. 'start_time': new Date(eventStartMillis).toISOString(),
  111. 'intervals': [
  112. {
  113. 'duration_seconds': 1740,
  114. 'signal_payloads': [
  115. 50
  116. ],
  117. 'uid': '0'
  118. },
  119. {
  120. 'duration_seconds': 60,
  121. 'signal_payloads': [
  122. 51
  123. ],
  124. 'uid': '1'
  125. }
  126. ],
  127. 'item_base': {
  128. 'type': 'power-real',
  129. 'dis': 'RealPower',
  130. 'units': 'W',
  131. 'si_scale_code': 'none',
  132. 'power_attributes': {
  133. 'hertz': 60,
  134. 'voltage': 120,
  135. 'ac': true
  136. }
  137. }
  138. }
  139. ]
  140. },
  141. 'active_period': {
  142. 'duration_seconds': 3600,
  143. 'notification_duration_seconds': 86400,
  144. 'ramp_up_duration_seconds': 3600,
  145. 'recovery_duration_seconds': 0,
  146. 'start_tolerance_duration_seconds': 0,
  147. 'start_time': new Date(eventStartMillis).toISOString()
  148. }
  149. };
  150. // create test event
  151. const newEvent = await request({
  152. method: 'POST',
  153. uri: `${nantumUrl}/oadr_utility_events`,
  154. body: event,
  155. });
  156. // create event ven mapping
  157. await request({
  158. method: 'POST',
  159. uri: `${nantumUrl}/oadr_utility_event_vens`,
  160. body: {
  161. oadr_utility_event_id: newEvent[0]._id,
  162. oadr_utility_ven_id: venId,
  163. },
  164. });
  165. }
  166. async function getVenClient() {
  167. const commonName = 'aabbccddeeff';
  168. const keypair = pki.rsa.generateKeyPair({ bits: 1024, e: 0x10001 });
  169. const cert = pki.createCertificate();
  170. cert.publicKey = keypair.publicKey;
  171. cert.serialNumber = '01';
  172. cert.validity.notBefore = new Date('2000-01-01T00:00:00.000Z');
  173. cert.validity.notAfter = new Date('2100-01-01T00:00:00.000Z');
  174. const attrs = [
  175. {
  176. name: 'commonName',
  177. value: commonName,
  178. },
  179. {
  180. name: 'countryName',
  181. value: 'US',
  182. },
  183. {
  184. shortName: 'ST',
  185. value: 'New York',
  186. },
  187. {
  188. name: 'localityName',
  189. value: 'New York',
  190. },
  191. {
  192. name: 'organizationName',
  193. value: 'Test',
  194. },
  195. {
  196. shortName: 'OU',
  197. value: 'Test',
  198. },
  199. ];
  200. cert.setSubject(attrs);
  201. cert.setIssuer(attrs);
  202. cert.sign(keypair.privateKey);
  203. const clientCrtPem = pki.certificateToPem(cert);
  204. const fingerprint = calculatePartialFingerprintOfEscapedPemCertificate(
  205. clientCrtPem,
  206. );
  207. const ven = new Ven(
  208. `http://127.0.0.1:${port}/${companyId}`,
  209. clientCrtPem,
  210. commonName,
  211. fingerprint,
  212. 'ven.js1',
  213. );
  214. // register VEN, this will create an oadr_utility_ven_registrations
  215. await ven.register();
  216. // retrieve the oadr_utility_ven_registration_id
  217. const registrations = await request({
  218. uri: `${nantumUrl}/oadr_utility_ven_registrations`,
  219. query: {
  220. client_certificate_fingerprint: fingerprint,
  221. },
  222. });
  223. const oadrUtilityVenRegistrationId = registrations[0]._id;
  224. // create the VEN tied to the ven registration
  225. const newVen = await request({
  226. method: 'POST',
  227. uri: `${nantumUrl}/oadr_utility_vens`,
  228. body: {
  229. dis: `VEN ${fingerprint}`,
  230. oadr_utility_ven_registration_id: oadrUtilityVenRegistrationId
  231. },
  232. });
  233. // generate 2 events
  234. await generateEventForVen1(newVen[0]._id);
  235. await generateEventForVen2(newVen[0]._id);
  236. return ven;
  237. }
  238. module.exports = {
  239. getVenClient
  240. };