Commit | Line | Data |
---|---|---|
54510a60 JB |
1 | import { EventEmitter } from 'node:events' |
2 | ||
24d15716 | 3 | import _Ajv, { type ValidateFunction } from 'ajv' |
66a7748d | 4 | import _ajvFormats from 'ajv-formats' |
e3018bc4 | 5 | |
66a7748d JB |
6 | import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js' |
7 | import { OCPPError } from '../../exception/index.js' | |
268a74bb JB |
8 | import type { |
9 | ClearCacheResponse, | |
10 | HandleErrorParams, | |
11 | IncomingRequestCommand, | |
268a74bb | 12 | JsonType, |
66a7748d JB |
13 | OCPPVersion |
14 | } from '../../types/index.js' | |
15 | import { logger, setDefaultErrorParams } from '../../utils/index.js' | |
4c3f6c20 JB |
16 | import { OCPPConstants } from './OCPPConstants.js' |
17 | import { OCPPServiceUtils } from './OCPPServiceUtils.js' | |
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 { |
66a7748d JB |
26 | private static instance: OCPPIncomingRequestService | null = null |
27 | private readonly version: OCPPVersion | |
24d15716 | 28 | protected readonly ajv: Ajv |
d5490a13 | 29 | protected abstract payloadValidateFunctions: Map< |
24d15716 JB |
30 | IncomingRequestCommand, |
31 | ValidateFunction<JsonType> | |
32 | > | |
10068088 | 33 | |
66a7748d | 34 | protected constructor (version: OCPPVersion) { |
54510a60 | 35 | super() |
66a7748d | 36 | this.version = version |
45988780 | 37 | this.ajv = new Ajv({ |
98fc1389 | 38 | keywords: ['javaType'], |
66a7748d JB |
39 | multipleOfPrecision: 2 |
40 | }) | |
41 | ajvFormats(this.ajv) | |
ba9a56a6 JB |
42 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) |
43 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) | |
c0560973 JB |
44 | } |
45 | ||
08f130a0 | 46 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 47 | if (OCPPIncomingRequestService.instance === null) { |
66a7748d | 48 | OCPPIncomingRequestService.instance = new this() |
9f2e3130 | 49 | } |
66a7748d | 50 | return OCPPIncomingRequestService.instance as T |
9f2e3130 JB |
51 | } |
52 | ||
7b5dbe91 | 53 | protected handleIncomingRequestError<T extends JsonType>( |
08f130a0 | 54 | chargingStation: ChargingStation, |
e7aeea18 JB |
55 | commandName: IncomingRequestCommand, |
56 | error: Error, | |
66a7748d | 57 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false } |
51581a20 | 58 | ): T | undefined { |
30695dcf | 59 | params = setDefaultErrorParams(params) |
e7aeea18 | 60 | logger.error( |
60ddad53 | 61 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
66a7748d JB |
62 | error |
63 | ) | |
30695dcf | 64 | console.error('params set', params) |
5199f9fd JB |
65 | if (params.throwError === false && params.errorResponse != null) { |
66 | return params.errorResponse | |
e64c0923 | 67 | } |
5199f9fd | 68 | if (params.throwError === true && params.errorResponse == null) { |
66a7748d | 69 | throw error |
e0a50bcd | 70 | } |
5199f9fd JB |
71 | if (params.throwError === true && params.errorResponse != null) { |
72 | return params.errorResponse | |
717c1e56 | 73 | } |
47e22477 JB |
74 | } |
75 | ||
e3018bc4 JB |
76 | protected validateIncomingRequestPayload<T extends JsonType>( |
77 | chargingStation: ChargingStation, | |
78 | commandName: IncomingRequestCommand, | |
66a7748d | 79 | payload: T |
e3018bc4 | 80 | ): boolean { |
5398cecf | 81 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 82 | return true |
e3018bc4 | 83 | } |
d5490a13 | 84 | const validate = this.payloadValidateFunctions.get(commandName) |
24d15716 | 85 | if (validate?.(payload) === true) { |
66a7748d | 86 | return true |
e3018bc4 JB |
87 | } |
88 | logger.error( | |
45988780 | 89 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
24d15716 | 90 | validate?.errors |
66a7748d | 91 | ) |
e3018bc4 | 92 | throw new OCPPError( |
24d15716 | 93 | OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), |
e3018bc4 JB |
94 | 'Incoming request PDU is invalid', |
95 | commandName, | |
24d15716 | 96 | JSON.stringify(validate?.errors, undefined, 2) |
66a7748d | 97 | ) |
e3018bc4 JB |
98 | } |
99 | ||
66a7748d JB |
100 | protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse { |
101 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 102 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) { |
66a7748d | 103 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED |
26a17d93 | 104 | } |
66a7748d | 105 | return OCPPConstants.OCPP_RESPONSE_REJECTED |
22e0d48e JB |
106 | } |
107 | ||
9429aa42 JB |
108 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
109 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
08f130a0 | 110 | chargingStation: ChargingStation, |
e7aeea18 JB |
111 | messageId: string, |
112 | commandName: IncomingRequestCommand, | |
66a7748d JB |
113 | commandPayload: ReqType |
114 | ): Promise<void> | |
c0560973 | 115 | } |