| 1 | import type ChargingStation from '../ChargingStation'; |
| 2 | import { HandleErrorParams } from '../../types/Error'; |
| 3 | import { IncomingRequestCommand } from '../../types/ocpp/Requests'; |
| 4 | import { JsonType } from '../../types/JsonType'; |
| 5 | import logger from '../../utils/Logger'; |
| 6 | |
| 7 | export default abstract class OCPPIncomingRequestService { |
| 8 | private static readonly instances: Map<string, OCPPIncomingRequestService> = new Map<string, OCPPIncomingRequestService>(); |
| 9 | protected chargingStation: ChargingStation; |
| 10 | |
| 11 | protected constructor(chargingStation: ChargingStation) { |
| 12 | this.chargingStation = chargingStation; |
| 13 | } |
| 14 | |
| 15 | public static getInstance<T extends OCPPIncomingRequestService>(this: new (chargingStation: ChargingStation) => T, chargingStation: ChargingStation): T { |
| 16 | if (!OCPPIncomingRequestService.instances.has(chargingStation.id)) { |
| 17 | OCPPIncomingRequestService.instances.set(chargingStation.id, new this(chargingStation)); |
| 18 | } |
| 19 | return OCPPIncomingRequestService.instances.get(chargingStation.id) as T; |
| 20 | } |
| 21 | |
| 22 | protected handleIncomingRequestError<T>(commandName: IncomingRequestCommand, error: Error, params: HandleErrorParams<T> = { throwError: true }): T { |
| 23 | logger.error(this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', commandName, error); |
| 24 | if (!params?.throwError && params?.errorResponse) { |
| 25 | return params?.errorResponse; |
| 26 | } |
| 27 | if (params?.throwError && !params?.errorResponse) { |
| 28 | throw error; |
| 29 | } |
| 30 | if (params?.throwError && params?.errorResponse) { |
| 31 | return params?.errorResponse; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public abstract handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: JsonType): Promise<void>; |
| 36 | } |