-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Implement websocketOptions as arg to CS connection (#58)
Feat: Implement websocketOptions as arg to CS connection
- Loading branch information
1 parent
7672b1a
commit b9ee039
Showing
7 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,92 @@ | ||
import { ActionName, Request, Response } from '../messages'; | ||
import { OCPPApplicationError, OCPPRequestError } from '../errors/index'; | ||
import { validateMessageRequest } from '../messages/validation'; | ||
import { EitherAsync, Left, Right } from 'purify-ts'; | ||
import * as soap from 'soap'; | ||
import * as path from 'path'; | ||
import { chargePointActions } from '../messages/cp'; | ||
import { soapCentralSystemActions } from '../messages/cs'; | ||
import * as uuid from 'uuid'; | ||
import { ActionName, Request, Response } from "../messages"; | ||
import { OCPPApplicationError, OCPPRequestError } from "../errors/index"; | ||
import { validateMessageRequest } from "../messages/validation"; | ||
import { EitherAsync, Left, Right } from "purify-ts"; | ||
import * as soap from "soap"; | ||
import * as path from "path"; | ||
import { chargePointActions } from "../messages/cp"; | ||
import { soapCentralSystemActions } from "../messages/cs"; | ||
import * as uuid from "uuid"; | ||
|
||
type SOAPResponse = [Error | null, any, any, any, any]; | ||
|
||
export default class SOAPConnection { | ||
private respondedActions: ActionName<'v1.5-soap'>[]; | ||
private respondedActions: ActionName<"v1.5-soap">[]; | ||
constructor( | ||
private readonly soapClient: soap.Client, | ||
private readonly connectedTo: 'cp' | 'cs', | ||
private readonly connectedTo: "cp" | "cs", | ||
private readonly chargePointId: string, | ||
private readonly endpoint: string, | ||
private readonly endpoint: string | ||
) { | ||
this.respondedActions = connectedTo === 'cp' ? soapCentralSystemActions : chargePointActions; | ||
this.respondedActions = | ||
connectedTo === "cp" ? soapCentralSystemActions : chargePointActions; | ||
} | ||
|
||
static async connect(endpoint: string, connectedTo: 'cp' | 'cs', chargePointId: string): Promise<SOAPConnection> { | ||
const wsdlPath = path.resolve(__dirname, ( | ||
connectedTo === 'cs' | ||
? '../messages/soap/ocpp_centralsystemservice_1.5_final.wsdl' | ||
: '../messages/soap/ocpp_chargepointservice_1.5_final.wsdl' | ||
)); | ||
const soapClient = await soap.createClientAsync(wsdlPath, { endpoint, forceSoap12Headers: true }); | ||
static async connect( | ||
endpoint: string, | ||
connectedTo: "cp" | "cs", | ||
chargePointId: string | ||
): Promise<SOAPConnection> { | ||
const wsdlPath = path.resolve( | ||
__dirname, | ||
connectedTo === "cs" | ||
? "../messages/soap/ocpp_centralsystemservice_1.5_final.wsdl" | ||
: "../messages/soap/ocpp_chargepointservice_1.5_final.wsdl" | ||
); | ||
const soapClient = await soap.createClientAsync(wsdlPath, { | ||
endpoint, | ||
forceSoap12Headers: true, | ||
}); | ||
return new SOAPConnection(soapClient, connectedTo, chargePointId, endpoint); | ||
} | ||
|
||
public sendRequest<T extends ActionName<'v1.5-soap'>>(action: T, { action: _, ocppVersion: __, ...payload }: Request<T, 'v1.5-soap'>): EitherAsync<OCPPRequestError, Response<T, 'v1.5-soap'>> { | ||
public sendRequest<T extends ActionName<"v1.5-soap">>( | ||
action: T, | ||
{ action: _, ocppVersion: __, ...payload }: Request<T, "v1.5-soap"> | ||
): EitherAsync<OCPPRequestError, Response<T, "v1.5-soap">> { | ||
return EitherAsync.fromPromise(async () => { | ||
const validateResult = validateMessageRequest(action, payload, this.respondedActions); | ||
const validateResult = validateMessageRequest( | ||
action, | ||
payload, | ||
this.respondedActions | ||
); | ||
if (validateResult.isLeft()) | ||
return Left(new OCPPApplicationError(validateResult.extract().toString())) | ||
|
||
const xmlNs = this.connectedTo === 'cp' ? 'urn://Ocpp/Cp/2012/06/' : 'urn://Ocpp/Cs/2012/06/'; | ||
this.soapClient.addSoapHeader({ chargeBoxIdentity: this.chargePointId }, '', 'ocpp', xmlNs); | ||
this.soapClient.addSoapHeader({ | ||
'Action': '/' + action, | ||
'MessageID': uuid.v4(), | ||
'To': this.endpoint, | ||
}, '', 'wsa5', 'http://www.w3.org/2005/08/addressing'); | ||
return Left( | ||
new OCPPApplicationError(validateResult.extract().toString()) | ||
); | ||
|
||
const [err, result, _rawResponse, _soapHeader, _rawRequest] = await new Promise(resolve => { | ||
const [serviceKey, portKey] = this.connectedTo === 'cp' | ||
? ['ChargePointService', 'ChargePointServiceSoap12'] | ||
: ['CentralSystemService', 'CentralSystemServiceSoap12']; | ||
this.soapClient[serviceKey][portKey][action](payload, (...args: any) => resolve(args)) | ||
const xmlNs = | ||
this.connectedTo === "cp" | ||
? "urn://Ocpp/Cp/2012/06/" | ||
: "urn://Ocpp/Cs/2012/06/"; | ||
this.soapClient.addSoapHeader( | ||
{ chargeBoxIdentity: this.chargePointId }, | ||
"", | ||
"ocpp", | ||
xmlNs | ||
); | ||
this.soapClient.addSoapHeader( | ||
{ | ||
Action: "/" + action, | ||
MessageID: uuid.v4(), | ||
To: this.endpoint, | ||
}, | ||
"", | ||
"wsa5", | ||
"http://www.w3.org/2005/08/addressing" | ||
); | ||
|
||
const [err, result, _rawResponse, _soapHeader, _rawRequest] = await new Promise<SOAPResponse>((resolve) => { | ||
const [serviceKey, portKey] = | ||
this.connectedTo === "cp" | ||
? ["ChargePointService", "ChargePointServiceSoap12"] | ||
: ["CentralSystemService", "CentralSystemServiceSoap12"]; | ||
this.soapClient[serviceKey][portKey][action](payload, (...args: any[]) => | ||
resolve(args as SOAPResponse) | ||
); | ||
}); | ||
if (err) | ||
return Left(new OCPPRequestError(err.toString())) | ||
if (err) return Left(new OCPPRequestError(err.toString())); | ||
return Right(result); | ||
}) | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters