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 type { OCPP20ClearCacheResponse
} from
'../../../types/ocpp/2.0/Responses';
16 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
17 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
18 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
19 import logger from
'../../../utils/Logger';
20 import type ChargingStation from
'../../ChargingStation';
21 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
22 import OCPPConstants from
'../OCPPConstants';
23 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
24 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
26 const moduleName
= 'OCPP20IncomingRequestService';
28 export default class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
29 private incomingRequestHandlers
: Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>;
30 private jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
32 public constructor() {
33 if (new.target
?.name
=== moduleName
) {
34 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
36 super(OCPPVersion
.VERSION_20
);
37 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
38 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
40 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
42 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
46 path
.dirname(fileURLToPath(import.meta
.url
)),
47 '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json'
51 ) as JSONSchemaType
<OCPP20ClearCacheRequest
>,
54 this.validatePayload
.bind(this);
57 public async incomingRequestHandler(
58 chargingStation
: ChargingStation
,
60 commandName
: OCPP20IncomingRequestCommand
,
61 commandPayload
: JsonType
63 let response
: JsonType
;
65 chargingStation
.getOcppStrictCompliance() === true &&
66 chargingStation
.isInPendingState() === true /* &&
67 (commandName === OCPP20IncomingRequestCommand.REMOTE_START_TRANSACTION ||
68 commandName === OCPP20IncomingRequestCommand.REMOTE_STOP_TRANSACTION ) */
71 ErrorType
.SECURITY_ERROR
,
72 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
76 )} while the charging station is in pending state on the central server`,
82 chargingStation
.isRegistered() === true ||
83 (chargingStation
.getOcppStrictCompliance() === false &&
84 chargingStation
.isInUnknownState() === true)
87 this.incomingRequestHandlers
.has(commandName
) === true &&
88 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
91 this.validatePayload(chargingStation
, commandName
, commandPayload
);
92 // Call the method to build the response
93 response
= await this.incomingRequestHandlers
.get(commandName
)(
100 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
108 ErrorType
.NOT_IMPLEMENTED
,
109 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
120 ErrorType
.SECURITY_ERROR
,
121 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
125 )} while the charging station is not registered on the central server.`,
130 // Send the built response
131 await chargingStation
.ocppRequestService
.sendResponse(
139 private validatePayload(
140 chargingStation
: ChargingStation
,
141 commandName
: OCPP20IncomingRequestCommand
,
142 commandPayload
: JsonType
144 if (this.jsonSchemas
.has(commandName
) === true) {
145 return this.validateIncomingRequestPayload(
148 this.jsonSchemas
.get(commandName
),
153 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
158 private handleRequestClearCache(chargingStation
: ChargingStation
): OCPP20ClearCacheResponse
{
159 chargingStation
.authorizedTagsCache
.deleteAuthorizedTags(
160 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
162 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;