]>
Commit | Line | Data |
---|---|---|
24d15716 | 1 | import _Ajv, { type ValidateFunction } from 'ajv' |
66a7748d | 2 | import _ajvFormats from 'ajv-formats' |
0749233f | 3 | import { EventEmitter } from 'node:events' |
e3018bc4 | 4 | |
268a74bb JB |
5 | import type { |
6 | ClearCacheResponse, | |
268a74bb | 7 | IncomingRequestCommand, |
268a74bb | 8 | JsonType, |
d1f5bfd8 | 9 | OCPPVersion, |
66a7748d | 10 | } from '../../types/index.js' |
0749233f JB |
11 | |
12 | import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js' | |
13 | import { OCPPError } from '../../exception/index.js' | |
2d4928a7 | 14 | import { logger } from '../../utils/index.js' |
4c3f6c20 | 15 | import { OCPPConstants } from './OCPPConstants.js' |
01b82de5 | 16 | import { ajvErrorsToErrorType } from './OCPPServiceUtils.js' |
0c58e0c2 | 17 | |
66a7748d JB |
18 | type Ajv = _Ajv.default |
19 | // eslint-disable-next-line @typescript-eslint/no-redeclare | |
20 | const Ajv = _Ajv.default | |
21 | const ajvFormats = _ajvFormats.default | |
c0560973 | 22 | |
66a7748d | 23 | const moduleName = 'OCPPIncomingRequestService' |
e3018bc4 | 24 | |
54510a60 | 25 | export abstract class OCPPIncomingRequestService extends EventEmitter { |
0749233f | 26 | private static instance: null | OCPPIncomingRequestService = null |
24d15716 | 27 | protected readonly ajv: Ajv |
d5490a13 | 28 | protected abstract payloadValidateFunctions: Map< |
d1f5bfd8 JB |
29 | IncomingRequestCommand, |
30 | ValidateFunction<JsonType> | |
24d15716 | 31 | > |
10068088 | 32 | |
0749233f JB |
33 | private readonly version: OCPPVersion |
34 | ||
66a7748d | 35 | protected constructor (version: OCPPVersion) { |
54510a60 | 36 | super() |
66a7748d | 37 | this.version = version |
45988780 | 38 | this.ajv = new Ajv({ |
98fc1389 | 39 | keywords: ['javaType'], |
d1f5bfd8 | 40 | multipleOfPrecision: 2, |
66a7748d JB |
41 | }) |
42 | ajvFormats(this.ajv) | |
ba9a56a6 JB |
43 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) |
44 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) | |
c0560973 JB |
45 | } |
46 | ||
08f130a0 | 47 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
678ffc93 | 48 | OCPPIncomingRequestService.instance ??= new this() |
66a7748d | 49 | return OCPPIncomingRequestService.instance as T |
9f2e3130 JB |
50 | } |
51 | ||
c4a89082 JB |
52 | // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-unnecessary-type-parameters |
53 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
54 | chargingStation: ChargingStation, | |
55 | messageId: string, | |
56 | commandName: IncomingRequestCommand, | |
57 | commandPayload: ReqType | |
58 | ): Promise<void> | |
59 | ||
0749233f JB |
60 | protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse { |
61 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
62 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) { | |
63 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED | |
64 | } | |
65 | return OCPPConstants.OCPP_RESPONSE_REJECTED | |
66 | } | |
67 | ||
01841aad | 68 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters |
e3018bc4 JB |
69 | protected validateIncomingRequestPayload<T extends JsonType>( |
70 | chargingStation: ChargingStation, | |
71 | commandName: IncomingRequestCommand, | |
66a7748d | 72 | payload: T |
e3018bc4 | 73 | ): boolean { |
5398cecf | 74 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 75 | return true |
e3018bc4 | 76 | } |
d5490a13 | 77 | const validate = this.payloadValidateFunctions.get(commandName) |
24d15716 | 78 | if (validate?.(payload) === true) { |
66a7748d | 79 | return true |
e3018bc4 JB |
80 | } |
81 | logger.error( | |
45988780 | 82 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
24d15716 | 83 | validate?.errors |
66a7748d | 84 | ) |
e3018bc4 | 85 | throw new OCPPError( |
01b82de5 | 86 | ajvErrorsToErrorType(validate?.errors), |
e3018bc4 JB |
87 | 'Incoming request PDU is invalid', |
88 | commandName, | |
24d15716 | 89 | JSON.stringify(validate?.errors, undefined, 2) |
66a7748d | 90 | ) |
e3018bc4 | 91 | } |
c0560973 | 92 | } |