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
,
13 type OCPP20HeartbeatRequest
,
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 Utils from
'../../../utils/Utils';
20 import type ChargingStation from
'../../ChargingStation';
21 import OCPPRequestService from
'../OCPPRequestService';
22 import type OCPPResponseService from
'../OCPPResponseService';
23 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
25 const moduleName
= 'OCPP20RequestService';
27 export default class OCPP20RequestService
extends OCPPRequestService
{
28 protected jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
30 public constructor(ocppResponseService
: OCPPResponseService
) {
31 if (new.target
?.name
=== moduleName
) {
32 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
34 super(OCPPVersion
.VERSION_20
, ocppResponseService
);
35 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>([
37 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
41 path
.dirname(fileURLToPath(import.meta
.url
)),
42 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
46 ) as JSONSchemaType
<OCPP20BootNotificationRequest
>,
49 OCPP20RequestCommand
.HEARTBEAT
,
53 path
.dirname(fileURLToPath(import.meta
.url
)),
54 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
58 ) as JSONSchemaType
<OCPP20HeartbeatRequest
>,
61 this.buildRequestPayload
.bind(this);
64 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
65 chargingStation
: ChargingStation
,
66 commandName
: OCPP20RequestCommand
,
67 commandParams
?: JsonType
,
68 params
?: RequestParams
69 ): Promise
<ResponseType
> {
70 if (OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
71 const requestPayload
= this.buildRequestPayload
<RequestType
>(
76 return (await this.sendMessage(
82 )) as unknown
as ResponseType
;
84 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
86 ErrorType
.NOT_SUPPORTED
,
87 `Unsupported OCPP command '${commandName}'`,
93 private buildRequestPayload
<Request
extends JsonType
>(
94 chargingStation
: ChargingStation
,
95 commandName
: OCPP20RequestCommand
,
96 commandParams
?: JsonType
98 commandParams
= commandParams
as JsonObject
;
99 switch (commandName
) {
100 case OCPP20RequestCommand
.BOOT_NOTIFICATION
:
101 commandParams
.chargingStation
= commandParams
.chargingStation
as JsonObject
;
102 commandParams
.chargingStation
.modem
= commandParams
.chargingStation
.modem
as JsonObject
;
104 reason
: commandParams
?.reason
,
106 model
: commandParams
?.chargingStation
?.model
,
107 vendorName
: commandParams
?.chargingStation
?.vendorName
,
108 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.firmwareVersion
) && {
109 firmwareVersion
: commandParams
.chargingStation
?.firmwareVersion
,
111 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.serialNumber
) && {
112 serialNumber
: commandParams
.chargingStation
?.serialNumber
,
114 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
) && {
116 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.iccid
) && {
117 iccid
: commandParams
.chargingStation
.modem
.iccid
,
119 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.imsi
) && {
120 imsi
: commandParams
.chargingStation
.modem
.imsi
,
125 } as unknown
as Request
;
126 case OCPP20RequestCommand
.HEARTBEAT
:
127 return {} as unknown
as Request
;
129 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
131 ErrorType
.NOT_SUPPORTED
,
132 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
133 `Unsupported OCPP command '${commandName}'`,