| 1 | import type { JSONSchemaType } from 'ajv'; |
| 2 | import Ajv from 'ajv-draft-04'; |
| 3 | import ajvFormats from 'ajv-formats'; |
| 4 | |
| 5 | import OCPPError from '../../exception/OCPPError'; |
| 6 | import type { HandleErrorParams } from '../../types/Error'; |
| 7 | import type { JsonType } from '../../types/JsonType'; |
| 8 | import type { IncomingRequestCommand } from '../../types/ocpp/Requests'; |
| 9 | import logger from '../../utils/Logger'; |
| 10 | import type ChargingStation from '../ChargingStation'; |
| 11 | import { OCPPServiceUtils } from './OCPPServiceUtils'; |
| 12 | |
| 13 | const moduleName = 'OCPPIncomingRequestService'; |
| 14 | |
| 15 | export default abstract class OCPPIncomingRequestService { |
| 16 | private static instance: OCPPIncomingRequestService | null = null; |
| 17 | private ajv: Ajv; |
| 18 | |
| 19 | protected constructor() { |
| 20 | this.ajv = new Ajv(); |
| 21 | ajvFormats(this.ajv); |
| 22 | this.incomingRequestHandler.bind(this); |
| 23 | this.validateIncomingRequestPayload.bind(this); |
| 24 | } |
| 25 | |
| 26 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
| 27 | if (OCPPIncomingRequestService.instance === null) { |
| 28 | OCPPIncomingRequestService.instance = new this(); |
| 29 | } |
| 30 | return OCPPIncomingRequestService.instance as T; |
| 31 | } |
| 32 | |
| 33 | protected handleIncomingRequestError<T>( |
| 34 | chargingStation: ChargingStation, |
| 35 | commandName: IncomingRequestCommand, |
| 36 | error: Error, |
| 37 | params: HandleErrorParams<T> = { throwError: true } |
| 38 | ): T { |
| 39 | logger.error( |
| 40 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command %s error:`, |
| 41 | commandName, |
| 42 | error |
| 43 | ); |
| 44 | if (!params?.throwError && params?.errorResponse) { |
| 45 | return params?.errorResponse; |
| 46 | } |
| 47 | if (params?.throwError && !params?.errorResponse) { |
| 48 | throw error; |
| 49 | } |
| 50 | if (params?.throwError && params?.errorResponse) { |
| 51 | return params?.errorResponse; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | protected validateIncomingRequestPayload<T extends JsonType>( |
| 56 | chargingStation: ChargingStation, |
| 57 | commandName: IncomingRequestCommand, |
| 58 | schema: JSONSchemaType<T>, |
| 59 | payload: T |
| 60 | ): boolean { |
| 61 | if (!chargingStation.getPayloadSchemaValidation()) { |
| 62 | return true; |
| 63 | } |
| 64 | const validate = this.ajv.compile(schema); |
| 65 | if (validate(payload)) { |
| 66 | return true; |
| 67 | } |
| 68 | logger.error( |
| 69 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Incoming request PDU is invalid: %j`, |
| 70 | validate.errors |
| 71 | ); |
| 72 | throw new OCPPError( |
| 73 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
| 74 | 'Incoming request PDU is invalid', |
| 75 | commandName, |
| 76 | JSON.stringify(validate.errors, null, 2) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | public abstract incomingRequestHandler( |
| 81 | chargingStation: ChargingStation, |
| 82 | messageId: string, |
| 83 | commandName: IncomingRequestCommand, |
| 84 | commandPayload: JsonType |
| 85 | ): Promise<void>; |
| 86 | } |