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';
13 OCPP20BootNotificationRequest
,
15 } from
'../../../types/ocpp/2.0/Requests';
16 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
17 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
18 import type { RequestParams
} from
'../../../types/ocpp/Requests';
19 import logger from
'../../../utils/Logger';
20 import Utils from
'../../../utils/Utils';
21 import type ChargingStation from
'../../ChargingStation';
22 import OCPPRequestService from
'../OCPPRequestService';
23 import type OCPPResponseService from
'../OCPPResponseService';
24 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
26 const moduleName
= 'OCPP20RequestService';
28 export default class OCPP20RequestService
extends OCPPRequestService
{
29 private jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
31 public constructor(ocppResponseService
: OCPPResponseService
) {
32 if (new.target
?.name
=== moduleName
) {
33 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
35 super(OCPPVersion
.VERSION_20
, ocppResponseService
);
36 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>([
38 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
42 path
.dirname(fileURLToPath(import.meta
.url
)),
43 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
47 ) as JSONSchemaType
<OCPP20BootNotificationRequest
>,
50 this.buildRequestPayload
.bind(this);
51 this.validatePayload
.bind(this);
54 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
55 chargingStation
: ChargingStation
,
56 commandName
: OCPP20RequestCommand
,
57 commandParams
?: JsonType
,
58 params
?: RequestParams
59 ): Promise
<ResponseType
> {
60 if (OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
61 const requestPayload
= this.buildRequestPayload
<RequestType
>(
66 this.validatePayload(chargingStation
, commandName
, requestPayload
);
67 return (await this.sendMessage(
73 )) as unknown
as ResponseType
;
75 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
77 ErrorType
.NOT_SUPPORTED
,
78 `Unsupported OCPP command '${commandName}'`,
84 private buildRequestPayload
<Request
extends JsonType
>(
85 chargingStation
: ChargingStation
,
86 commandName
: OCPP20RequestCommand
,
87 commandParams
?: JsonType
89 commandParams
= commandParams
as JsonObject
;
90 switch (commandName
) {
91 case OCPP20RequestCommand
.BOOT_NOTIFICATION
:
92 commandParams
.modem
= commandParams
.modem
as JsonObject
;
94 reason
: commandParams
?.reason
,
96 model
: commandParams
?.model
,
97 vendorName
: commandParams
?.vendorName
,
98 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
99 firmwareVersion
: commandParams
.firmwareVersion
,
101 ...(!Utils
.isUndefined(commandParams
?.serialNumber
) && {
102 serialNumber
: commandParams
.serialNumber
,
105 ...(!Utils
.isUndefined(commandParams
?.modem
?.iccid
) && {
106 iccid
: commandParams
.modem
.iccid
,
108 ...(!Utils
.isUndefined(commandParams
?.modem
?.imsi
) && {
109 imsi
: commandParams
.modem
.imsi
,
113 } as unknown
as Request
;
115 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
117 ErrorType
.NOT_SUPPORTED
,
118 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
119 `Unsupported OCPP command '${commandName}'`,
126 private validatePayload
<Request
extends JsonType
>(
127 chargingStation
: ChargingStation
,
128 commandName
: OCPP20RequestCommand
,
129 requestPayload
: Request
131 if (this.jsonSchemas
.has(commandName
)) {
132 return this.validateRequestPayload(
135 this.jsonSchemas
.get(commandName
),
140 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`