- public async sendMessage(messageId: string, commandParams: any, messageType: MessageType = MessageType.CALL_RESULT_MESSAGE,
- commandName: RequestCommand | IncomingRequestCommand): Promise<any> {
- // eslint-disable-next-line @typescript-eslint/no-this-alias
- const self = this;
- // Send a message through wsConnection
- return new Promise((resolve: (value?: any | PromiseLike<any>) => void, reject: (reason?: any) => void) => {
- let messageToSend: string;
- // Type of message
- switch (messageType) {
- // Request
- case MessageType.CALL_MESSAGE:
- // Build request
- this.chargingStation.requests[messageId] = [responseCallback, rejectCallback, commandParams] as Request;
- messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
- break;
- // Response
- case MessageType.CALL_RESULT_MESSAGE:
- // Build response
- messageToSend = JSON.stringify([messageType, messageId, commandParams]);
- break;
- // Error Message
- case MessageType.CALL_ERROR_MESSAGE:
- // Build Error Message
- messageToSend = JSON.stringify([messageType, messageId, commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : '', commandParams.details ? commandParams.details : {}]);
- break;
- }
- // Check if wsConnection opened and charging station registered
- if (this.chargingStation.isWebSocketOpen() && (this.chargingStation.isRegistered() || commandName === RequestCommand.BOOT_NOTIFICATION)) {
+ public static getInstance<T extends OCPPRequestService>(this: new (chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) => T, chargingStation: ChargingStation, ocppResponseService: OCPPResponseService): T {
+ if (!OCPPRequestService.instances.has(chargingStation.id)) {
+ OCPPRequestService.instances.set(chargingStation.id, new this(chargingStation, ocppResponseService));
+ }
+ return OCPPRequestService.instances.get(chargingStation.id) as T;
+ }
+
+ public async sendResult(messageId: string, messagePayload: JsonType, commandName: IncomingRequestCommand): Promise<ResponseType> {
+ try {
+ // Send result message
+ return await this.internalSendMessage(messageId, messagePayload, MessageType.CALL_RESULT_MESSAGE, commandName);
+ } catch (error) {
+ this.handleRequestError(commandName, error as Error);
+ }
+ }
+
+ public async sendError(messageId: string, ocppError: OCPPError, commandName: IncomingRequestCommand): Promise<ResponseType> {
+ try {
+ // Send error message
+ return await this.internalSendMessage(messageId, ocppError, MessageType.CALL_ERROR_MESSAGE, commandName);
+ } catch (error) {
+ this.handleRequestError(commandName, error as Error);
+ }
+ }
+
+ protected async sendMessage(messageId: string, messagePayload: JsonType, commandName: RequestCommand, params: SendParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false
+ }): Promise<ResponseType> {
+ try {
+ return await this.internalSendMessage(messageId, messagePayload, MessageType.CALL_MESSAGE, commandName, params);
+ } catch (error) {
+ this.handleRequestError(commandName, error as Error, { throwError: false });
+ }
+ }
+
+ private async internalSendMessage(messageId: string, messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand,
+ params: SendParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false
+ }): Promise<ResponseType> {
+ if ((this.chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION)
+ || (!this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInUnknownState())
+ || this.chargingStation.isInAcceptedState() || (this.chargingStation.isInPendingState() && params.triggerMessage)) {
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
+ const self = this;
+ // Send a message through wsConnection
+ return Utils.promiseWithTimeout(new Promise((resolve, reject) => {
+ const messageToSend = this.buildMessageToSend(messageId, messagePayload, messageType, commandName, responseCallback, rejectCallback);