| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 'use strict';
- const {
- serialize: serializeCreatePartyRegistration,
- } = require('../xml/create-party-registration');
- const {
- parse: parseCreatedPartyRegistration,
- } = require('../xml/created-party-registration');
- const axios = require('axios');
- const { escape } = require('querystring');
- class Ven {
- constructor(
- endpoint,
- clientCertPem,
- commonName,
- venId,
- venName,
- registrationId,
- ) {
- if (!endpoint.endsWith('/')) endpoint += '/';
- this.endpoint = endpoint;
- this.clientCertHeader = escape(clientCertPem);
- this.venId = venId;
- this.registrationId = registrationId;
- this.venName = venName;
- this.commonName = commonName;
- }
- async register() {
- const message = {
- requestId: '2233',
- registrationId: this.registrationId,
- venId: this.venId,
- oadrProfileName: '2.0b',
- oadrTransportName: 'simplehttp',
- oadrReportOnly: false,
- oadrXmlSignature: false,
- oadrVenName: this.venName,
- oadrHttpPullModel: true,
- };
- const xml = serializeCreatePartyRegistration(message);
- const config = {
- headers: {
- 'Content-Type': 'application/xml',
- // these next 2 headers are provided manually for now, simulating what nginx is doing.
- SSL_CLIENT_S_DN_CN: this.commonName || 'no_client_cert',
- SSL_CLIENT_CERTIFICATE: this.clientCertHeader,
- },
- };
- // axios will automatically reject HTTP response codes outside the 200-299 range
- const httpResponse = await axios.post(
- `${this.endpoint}OpenADR2/Simple/2.0b/EiRegisterParty`,
- xml,
- config,
- );
- const registrationResponse = await parseCreatedPartyRegistration(
- httpResponse.data,
- );
- // but OpenADR provides its own response code in the XML envelope, we need to check that
- if (
- registrationResponse.responseCode < 200 ||
- registrationResponse.responseCode >= 300
- ) {
- throw new Error(
- 'Error during registration. ResponseCode=' +
- registrationResponse.responseCode +
- ', ResponseDescription=' +
- registrationResponse.responseDescription,
- );
- }
- // track registrationId for subsequent requests
- this.registrationId = registrationResponse.registrationId;
- return registrationResponse;
- }
- }
- module.exports = {
- Ven,
- };
|