| 1 | import { AsyncResource } from 'async_hooks'; |
| 2 | |
| 3 | import Ajv, { type JSONSchemaType } from 'ajv'; |
| 4 | import ajvFormats from 'ajv-formats'; |
| 5 | |
| 6 | import { OCPPConstants } from './OCPPConstants'; |
| 7 | import { OCPPServiceUtils } from './OCPPServiceUtils'; |
| 8 | import { OCPPError } from '../../exception'; |
| 9 | import type { |
| 10 | ClearCacheResponse, |
| 11 | HandleErrorParams, |
| 12 | IncomingRequestCommand, |
| 13 | JsonObject, |
| 14 | JsonType, |
| 15 | OCPPVersion, |
| 16 | } from '../../types'; |
| 17 | import { logger } from '../../utils/Logger'; |
| 18 | import type { ChargingStation } from '../ChargingStation'; |
| 19 | import { ChargingStationUtils } from '../ChargingStationUtils'; |
| 20 | |
| 21 | const moduleName = 'OCPPIncomingRequestService'; |
| 22 | |
| 23 | export abstract class OCPPIncomingRequestService extends AsyncResource { |
| 24 | private static instance: OCPPIncomingRequestService | null = null; |
| 25 | private readonly version: OCPPVersion; |
| 26 | private readonly ajv: Ajv; |
| 27 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
| 28 | |
| 29 | protected constructor(version: OCPPVersion) { |
| 30 | super(moduleName); |
| 31 | this.version = version; |
| 32 | this.ajv = new Ajv({ |
| 33 | keywords: ['javaType'], |
| 34 | multipleOfPrecision: 2, |
| 35 | }); |
| 36 | ajvFormats(this.ajv); |
| 37 | this.incomingRequestHandler.bind(this); |
| 38 | this.validateIncomingRequestPayload.bind(this); |
| 39 | } |
| 40 | |
| 41 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
| 42 | if (OCPPIncomingRequestService.instance === null) { |
| 43 | OCPPIncomingRequestService.instance = new this(); |
| 44 | } |
| 45 | return OCPPIncomingRequestService.instance as T; |
| 46 | } |
| 47 | |
| 48 | protected handleIncomingRequestError<T>( |
| 49 | chargingStation: ChargingStation, |
| 50 | commandName: IncomingRequestCommand, |
| 51 | error: Error, |
| 52 | params: HandleErrorParams<T> = { throwError: true } |
| 53 | ): T | undefined { |
| 54 | logger.error( |
| 55 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
| 56 | error |
| 57 | ); |
| 58 | if (!params?.throwError && params?.errorResponse) { |
| 59 | return params?.errorResponse; |
| 60 | } |
| 61 | if (params?.throwError && !params?.errorResponse) { |
| 62 | throw error; |
| 63 | } |
| 64 | if (params?.throwError && params?.errorResponse) { |
| 65 | return params?.errorResponse; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected validateIncomingRequestPayload<T extends JsonType>( |
| 70 | chargingStation: ChargingStation, |
| 71 | commandName: IncomingRequestCommand, |
| 72 | schema: JSONSchemaType<T>, |
| 73 | payload: T |
| 74 | ): boolean { |
| 75 | if (chargingStation.getPayloadSchemaValidation() === false) { |
| 76 | return true; |
| 77 | } |
| 78 | const validate = this.ajv.compile(schema); |
| 79 | if (validate(payload)) { |
| 80 | return true; |
| 81 | } |
| 82 | logger.error( |
| 83 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
| 84 | validate.errors |
| 85 | ); |
| 86 | throw new OCPPError( |
| 87 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
| 88 | 'Incoming request PDU is invalid', |
| 89 | commandName, |
| 90 | JSON.stringify(validate.errors, null, 2) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { |
| 95 | chargingStation.authorizedTagsCache.deleteAuthorizedTags( |
| 96 | ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) |
| 97 | ); |
| 98 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED; |
| 99 | } |
| 100 | |
| 101 | public abstract incomingRequestHandler( |
| 102 | chargingStation: ChargingStation, |
| 103 | messageId: string, |
| 104 | commandName: IncomingRequestCommand, |
| 105 | commandPayload: JsonType |
| 106 | ): Promise<void>; |
| 107 | } |