| 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< |
| 9 | string, |
| 10 | OCPPIncomingRequestService |
| 11 | >(); |
| 12 | |
| 13 | protected chargingStation: ChargingStation; |
| 14 | |
| 15 | protected constructor(chargingStation: ChargingStation) { |
| 16 | this.chargingStation = chargingStation; |
| 17 | } |
| 18 | |
| 19 | public static getInstance<T extends OCPPIncomingRequestService>( |
| 20 | this: new (chargingStation: ChargingStation) => T, |
| 21 | chargingStation: ChargingStation |
| 22 | ): T { |
| 23 | if (!OCPPIncomingRequestService.instances.has(chargingStation.id)) { |
| 24 | OCPPIncomingRequestService.instances.set(chargingStation.id, new this(chargingStation)); |
| 25 | } |
| 26 | return OCPPIncomingRequestService.instances.get(chargingStation.id) as T; |
| 27 | } |
| 28 | |
| 29 | protected handleIncomingRequestError<T>( |
| 30 | commandName: IncomingRequestCommand, |
| 31 | error: Error, |
| 32 | params: HandleErrorParams<T> = { throwError: true } |
| 33 | ): T { |
| 34 | logger.error( |
| 35 | this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', |
| 36 | commandName, |
| 37 | error |
| 38 | ); |
| 39 | if (!params?.throwError && params?.errorResponse) { |
| 40 | return params?.errorResponse; |
| 41 | } |
| 42 | if (params?.throwError && !params?.errorResponse) { |
| 43 | throw error; |
| 44 | } |
| 45 | if (params?.throwError && params?.errorResponse) { |
| 46 | return params?.errorResponse; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public abstract handleRequest( |
| 51 | messageId: string, |
| 52 | commandName: IncomingRequestCommand, |
| 53 | commandPayload: JsonType |
| 54 | ): Promise<void>; |
| 55 | } |