| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 'use strict';
- const { API } = require('@hw/edge-sdks');
- const logger = require('../../logger');
- const { companyId, company, nantumUrl } = require('../../config');
- const { request } = API({ company, logger });
- const { pki } = require('node-forge');
- const {
- calculatePartialFingerprintOfEscapedPemCertificate,
- } = require('../../modules/certificate');
- const { Ven } = require('../../client/ven');
- const { port } = require('../../config');
- async function generateEventForVen1(venId) {
- const eventStartMillis = new Date().getTime() + ( 60 * 60 * 1000 );
- const event = {
- 'cancelled': false,
- 'priority': 1,
- 'response_required': true,
- 'dis': `Test Event ${eventStartMillis}`,
- 'modification_number': 1,
- 'modification_reason': 'Updated market context',
- 'market_context': 'http://emix2',
- 'targets': [
- {
- 'dis': 'ven target',
- 'target_type': 'ven',
- 'value': '1234567'
- }
- ],
- 'signals': {
- 'event': [
- {
- 'signal_id': 'id1',
- 'signal_name': 'BID_LOAD',
- 'signal_type': 'setpoint',
- 'current_value': 45.5,
- 'duration_seconds': 1800,
- 'start_time': new Date(eventStartMillis).toISOString(),
- 'intervals': [
- {
- 'duration_seconds': 1740,
- 'signal_payloads': [
- 50
- ],
- 'uid': '0'
- },
- {
- 'duration_seconds': 60,
- 'signal_payloads': [
- 51
- ],
- 'uid': '1'
- }
- ],
- 'item_base': {
- 'type': 'power-real',
- 'dis': 'RealPower',
- 'units': 'W',
- 'si_scale_code': 'none',
- 'power_attributes': {
- 'hertz': 60,
- 'voltage': 120,
- 'ac': true
- }
- }
- }
- ]
- },
- 'active_period': {
- 'duration_seconds': 1800,
- 'notification_duration_seconds': 86400,
- 'ramp_up_duration_seconds': 3600,
- 'recovery_duration_seconds': 0,
- 'start_tolerance_duration_seconds': 0,
- 'start_time': new Date(eventStartMillis).toISOString()
- }
- };
- // create test event
- const newEvent = await request({
- method: 'POST',
- uri: `${nantumUrl}/oadr_utility_events`,
- body: event,
- });
- // create event ven mapping
- await request({
- method: 'POST',
- uri: `${nantumUrl}/oadr_utility_event_vens`,
- body: {
- oadr_utility_event_id: newEvent[0]._id,
- oadr_utility_ven_id: venId,
- },
- });
- }
- async function generateEventForVen2(venId) {
- const eventStartMillis = new Date().getTime() + ( 26 * 60 * 60 * 1000 );
- const event = {
- 'cancelled': false,
- 'priority': 1,
- 'response_required': true,
- 'dis': `Test Event ${eventStartMillis}`,
- 'modification_number': 0,
- 'market_context': 'http://emix2',
- 'signals': {
- 'event': [
- {
- 'signal_id': 'id1',
- 'signal_name': 'BID_LOAD',
- 'signal_type': 'setpoint',
- 'current_value': 47.5,
- 'duration_seconds': 1800,
- 'start_time': new Date(eventStartMillis).toISOString(),
- 'intervals': [
- {
- 'duration_seconds': 1740,
- 'signal_payloads': [
- 50
- ],
- 'uid': '0'
- },
- {
- 'duration_seconds': 60,
- 'signal_payloads': [
- 51
- ],
- 'uid': '1'
- }
- ],
- 'item_base': {
- 'type': 'power-real',
- 'dis': 'RealPower',
- 'units': 'W',
- 'si_scale_code': 'none',
- 'power_attributes': {
- 'hertz': 60,
- 'voltage': 120,
- 'ac': true
- }
- }
- }
- ]
- },
- 'active_period': {
- 'duration_seconds': 3600,
- 'notification_duration_seconds': 86400,
- 'ramp_up_duration_seconds': 3600,
- 'recovery_duration_seconds': 0,
- 'start_tolerance_duration_seconds': 0,
- 'start_time': new Date(eventStartMillis).toISOString()
- }
- };
- // create test event
- const newEvent = await request({
- method: 'POST',
- uri: `${nantumUrl}/oadr_utility_events`,
- body: event,
- });
- // create event ven mapping
- await request({
- method: 'POST',
- uri: `${nantumUrl}/oadr_utility_event_vens`,
- body: {
- oadr_utility_event_id: newEvent[0]._id,
- oadr_utility_ven_id: venId,
- },
- });
- }
- async function getVenClient() {
- const commonName = 'aabbccddeeff';
- const keypair = pki.rsa.generateKeyPair({ bits: 1024, e: 0x10001 });
- const cert = pki.createCertificate();
- cert.publicKey = keypair.publicKey;
- cert.serialNumber = '01';
- cert.validity.notBefore = new Date('2000-01-01T00:00:00.000Z');
- cert.validity.notAfter = new Date('2100-01-01T00:00:00.000Z');
- const attrs = [
- {
- name: 'commonName',
- value: commonName,
- },
- {
- name: 'countryName',
- value: 'US',
- },
- {
- shortName: 'ST',
- value: 'New York',
- },
- {
- name: 'localityName',
- value: 'New York',
- },
- {
- name: 'organizationName',
- value: 'Test',
- },
- {
- shortName: 'OU',
- value: 'Test',
- },
- ];
- cert.setSubject(attrs);
- cert.setIssuer(attrs);
- cert.sign(keypair.privateKey);
- const clientCrtPem = pki.certificateToPem(cert);
- const fingerprint = calculatePartialFingerprintOfEscapedPemCertificate(
- clientCrtPem,
- );
- const ven = new Ven(
- `http://127.0.0.1:${port}/${companyId}`,
- clientCrtPem,
- commonName,
- fingerprint,
- 'ven.js1',
- );
- // register VEN, this will create an oadr_utility_ven_registrations
- await ven.register();
- // retrieve the oadr_utility_ven_registration_id
- const registrations = await request({
- uri: `${nantumUrl}/oadr_utility_ven_registrations`,
- query: {
- client_certificate_fingerprint: fingerprint,
- },
- });
- const oadrUtilityVenRegistrationId = registrations[0]._id;
- // create the VEN tied to the ven registration
- const newVen = await request({
- method: 'POST',
- uri: `${nantumUrl}/oadr_utility_vens`,
- body: {
- dis: `VEN ${fingerprint}`,
- oadr_utility_ven_registration_id: oadrUtilityVenRegistrationId
- },
- });
- // generate 2 events
- await generateEventForVen1(newVen[0]._id);
- await generateEventForVen2(newVen[0]._id);
- return ven;
- }
- module.exports = {
- getVenClient
- };
|