Merge dependabot/npm_and_yarn/types/tar-6.1.12 into combined-prs-branch
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
CommitLineData
54510a60
JB
1import { EventEmitter } from 'node:events'
2
24d15716 3import _Ajv, { type ValidateFunction } from 'ajv'
66a7748d 4import _ajvFormats from 'ajv-formats'
e3018bc4 5
66a7748d
JB
6import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js'
7import { OCPPError } from '../../exception/index.js'
268a74bb
JB
8import type {
9 ClearCacheResponse,
10 HandleErrorParams,
11 IncomingRequestCommand,
268a74bb 12 JsonType,
66a7748d
JB
13 OCPPVersion
14} from '../../types/index.js'
15import { logger, setDefaultErrorParams } from '../../utils/index.js'
4c3f6c20
JB
16import { OCPPConstants } from './OCPPConstants.js'
17import { OCPPServiceUtils } from './OCPPServiceUtils.js'
66a7748d
JB
18type Ajv = _Ajv.default
19// eslint-disable-next-line @typescript-eslint/no-redeclare
20const Ajv = _Ajv.default
21const ajvFormats = _ajvFormats.default
c0560973 22
66a7748d 23const moduleName = 'OCPPIncomingRequestService'
e3018bc4 24
54510a60 25export 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 {
66a7748d 59 setDefaultErrorParams(params)
e7aeea18 60 logger.error(
60ddad53 61 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
66a7748d
JB
62 error
63 )
5199f9fd
JB
64 if (params.throwError === false && params.errorResponse != null) {
65 return params.errorResponse
e64c0923 66 }
5199f9fd 67 if (params.throwError === true && params.errorResponse == null) {
66a7748d 68 throw error
e0a50bcd 69 }
5199f9fd
JB
70 if (params.throwError === true && params.errorResponse != null) {
71 return params.errorResponse
717c1e56 72 }
47e22477
JB
73 }
74
e3018bc4
JB
75 protected validateIncomingRequestPayload<T extends JsonType>(
76 chargingStation: ChargingStation,
77 commandName: IncomingRequestCommand,
66a7748d 78 payload: T
e3018bc4 79 ): boolean {
5398cecf 80 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
66a7748d 81 return true
e3018bc4 82 }
d5490a13 83 const validate = this.payloadValidateFunctions.get(commandName)
24d15716 84 if (validate?.(payload) === true) {
66a7748d 85 return true
e3018bc4
JB
86 }
87 logger.error(
45988780 88 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
24d15716 89 validate?.errors
66a7748d 90 )
e3018bc4 91 throw new OCPPError(
24d15716 92 OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors),
e3018bc4
JB
93 'Incoming request PDU is invalid',
94 commandName,
24d15716 95 JSON.stringify(validate?.errors, undefined, 2)
66a7748d 96 )
e3018bc4
JB
97 }
98
66a7748d
JB
99 protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse {
100 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5199f9fd 101 if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) {
66a7748d 102 return OCPPConstants.OCPP_RESPONSE_ACCEPTED
26a17d93 103 }
66a7748d 104 return OCPPConstants.OCPP_RESPONSE_REJECTED
22e0d48e
JB
105 }
106
9429aa42
JB
107 // eslint-disable-next-line @typescript-eslint/no-unused-vars
108 public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
08f130a0 109 chargingStation: ChargingStation,
e7aeea18
JB
110 messageId: string,
111 commandName: IncomingRequestCommand,
66a7748d
JB
112 commandPayload: ReqType
113 ): Promise<void>
c0560973 114}