1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
4 import path from
'path';
5 import { fileURLToPath
} from
'url';
7 import type { JSONSchemaType
} from
'ajv';
9 import OCPPError from
'../../../exception/OCPPError';
10 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
12 OCPP20ClearCacheRequest
,
13 OCPP20IncomingRequestCommand
,
14 } from
'../../../types/ocpp/2.0/Requests';
15 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
16 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
17 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
18 import logger from
'../../../utils/Logger';
19 import type ChargingStation from
'../../ChargingStation';
20 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
21 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
23 const moduleName
= 'OCPP20IncomingRequestService';
25 export default class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
26 protected jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
27 private incomingRequestHandlers
: Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>;
29 public constructor() {
30 if (new.target
?.name
=== moduleName
) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
33 super(OCPPVersion
.VERSION_20
);
34 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
35 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
37 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
39 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
43 path
.dirname(fileURLToPath(import.meta
.url
)),
44 '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json'
48 ) as JSONSchemaType
<OCPP20ClearCacheRequest
>,
51 this.validatePayload
.bind(this);
54 public async incomingRequestHandler(
55 chargingStation
: ChargingStation
,
57 commandName
: OCPP20IncomingRequestCommand
,
58 commandPayload
: JsonType
60 let response
: JsonType
;
62 chargingStation
.getOcppStrictCompliance() === true &&
63 chargingStation
.isInPendingState() === true /* &&
64 (commandName === OCPP20IncomingRequestCommand.REMOTE_START_TRANSACTION ||
65 commandName === OCPP20IncomingRequestCommand.REMOTE_STOP_TRANSACTION ) */
68 ErrorType
.SECURITY_ERROR
,
69 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
73 )} while the charging station is in pending state on the central server`,
79 chargingStation
.isRegistered() === true ||
80 (chargingStation
.getOcppStrictCompliance() === false &&
81 chargingStation
.isInUnknownState() === true)
84 this.incomingRequestHandlers
.has(commandName
) === true &&
85 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
88 this.validatePayload(chargingStation
, commandName
, commandPayload
);
89 // Call the method to build the response
90 response
= await this.incomingRequestHandlers
.get(commandName
)(
97 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
105 ErrorType
.NOT_IMPLEMENTED
,
106 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
117 ErrorType
.SECURITY_ERROR
,
118 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
122 )} while the charging station is not registered on the central server.`,
127 // Send the built response
128 await chargingStation
.ocppRequestService
.sendResponse(
136 private validatePayload(
137 chargingStation
: ChargingStation
,
138 commandName
: OCPP20IncomingRequestCommand
,
139 commandPayload
: JsonType
141 if (this.jsonSchemas
.has(commandName
) === true) {
142 return this.validateIncomingRequestPayload(
145 this.jsonSchemas
.get(commandName
),
150 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`