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