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