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 type OCPP20BootNotificationRequest
,
14 } from
'../../../types/ocpp/2.0/Requests';
15 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
16 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
17 import type { RequestParams
} from
'../../../types/ocpp/Requests';
18 import Utils from
'../../../utils/Utils';
19 import type ChargingStation from
'../../ChargingStation';
20 import OCPPRequestService from
'../OCPPRequestService';
21 import type OCPPResponseService from
'../OCPPResponseService';
22 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
24 const moduleName
= 'OCPP20RequestService';
26 export default class OCPP20RequestService
extends OCPPRequestService
{
27 protected jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
29 public constructor(ocppResponseService
: OCPPResponseService
) {
30 if (new.target
?.name
=== moduleName
) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
33 super(OCPPVersion
.VERSION_20
, ocppResponseService
);
34 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>([
36 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
40 path
.dirname(fileURLToPath(import.meta
.url
)),
41 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
45 ) as JSONSchemaType
<OCPP20BootNotificationRequest
>,
48 this.buildRequestPayload
.bind(this);
51 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
52 chargingStation
: ChargingStation
,
53 commandName
: OCPP20RequestCommand
,
54 commandParams
?: JsonType
,
55 params
?: RequestParams
56 ): Promise
<ResponseType
> {
57 if (OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
58 const requestPayload
= this.buildRequestPayload
<RequestType
>(
63 return (await this.sendMessage(
69 )) as unknown
as ResponseType
;
71 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
73 ErrorType
.NOT_SUPPORTED
,
74 `Unsupported OCPP command '${commandName}'`,
80 private buildRequestPayload
<Request
extends JsonType
>(
81 chargingStation
: ChargingStation
,
82 commandName
: OCPP20RequestCommand
,
83 commandParams
?: JsonType
85 commandParams
= commandParams
as JsonObject
;
86 switch (commandName
) {
87 case OCPP20RequestCommand
.BOOT_NOTIFICATION
:
88 commandParams
.chargingStation
= commandParams
.chargingStation
as JsonObject
;
89 commandParams
.chargingStation
.modem
= commandParams
.chargingStation
.modem
as JsonObject
;
91 reason
: commandParams
?.reason
,
93 model
: commandParams
?.chargingStation
?.model
,
94 vendorName
: commandParams
?.chargingStation
?.vendorName
,
95 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.firmwareVersion
) && {
96 firmwareVersion
: commandParams
.chargingStation
?.firmwareVersion
,
98 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.serialNumber
) && {
99 serialNumber
: commandParams
.chargingStation
?.serialNumber
,
101 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
) && {
103 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.iccid
) && {
104 iccid
: commandParams
.chargingStation
.modem
.iccid
,
106 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.imsi
) && {
107 imsi
: commandParams
.chargingStation
.modem
.imsi
,
112 } as unknown
as Request
;
114 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
116 ErrorType
.NOT_SUPPORTED
,
117 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
118 `Unsupported OCPP command '${commandName}'`,