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 type OCPP20StatusNotificationRequest
,
16 } from
'../../../types/ocpp/2.0/Requests';
17 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
18 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
19 import type { RequestParams
} from
'../../../types/ocpp/Requests';
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 protected 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
,
39 this.parseJsonSchemaFile
<OCPP20BootNotificationRequest
>(
40 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
44 OCPP20RequestCommand
.HEARTBEAT
,
45 this.parseJsonSchemaFile
<OCPP20HeartbeatRequest
>(
46 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
50 OCPP20RequestCommand
.STATUS_NOTIFICATION
,
51 this.parseJsonSchemaFile
<OCPP20StatusNotificationRequest
>(
52 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
56 this.buildRequestPayload
.bind(this);
59 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
60 chargingStation
: ChargingStation
,
61 commandName
: OCPP20RequestCommand
,
62 commandParams
?: JsonType
,
63 params
?: RequestParams
64 ): Promise
<ResponseType
> {
65 if (OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
66 const requestPayload
= this.buildRequestPayload
<RequestType
>(
71 return (await this.sendMessage(
77 )) as unknown
as ResponseType
;
79 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
81 ErrorType
.NOT_SUPPORTED
,
82 `Unsupported OCPP command '${commandName}'`,
88 private buildRequestPayload
<Request
extends JsonType
>(
89 chargingStation
: ChargingStation
,
90 commandName
: OCPP20RequestCommand
,
91 commandParams
?: JsonType
93 commandParams
= commandParams
as JsonObject
;
94 switch (commandName
) {
95 case OCPP20RequestCommand
.BOOT_NOTIFICATION
:
96 commandParams
.chargingStation
= commandParams
.chargingStation
as JsonObject
;
97 commandParams
.chargingStation
.modem
= commandParams
.chargingStation
.modem
as JsonObject
;
99 reason
: commandParams
?.reason
,
101 model
: commandParams
?.chargingStation
?.model
,
102 vendorName
: commandParams
?.chargingStation
?.vendorName
,
103 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.firmwareVersion
) && {
104 firmwareVersion
: commandParams
.chargingStation
?.firmwareVersion
,
106 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.serialNumber
) && {
107 serialNumber
: commandParams
.chargingStation
?.serialNumber
,
109 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
) && {
111 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.iccid
) && {
112 iccid
: commandParams
.chargingStation
.modem
.iccid
,
114 ...(!Utils
.isUndefined(commandParams
?.chargingStation
?.modem
?.imsi
) && {
115 imsi
: commandParams
.chargingStation
.modem
.imsi
,
120 } as unknown
as Request
;
121 case OCPP20RequestCommand
.HEARTBEAT
:
122 return {} as unknown
as Request
;
123 case OCPP20RequestCommand
.STATUS_NOTIFICATION
:
125 timestamp
: commandParams
?.timestamp
,
126 connectorStatus
: commandParams
?.connectorStatus
,
127 evseId
: commandParams
?.evseId
,
128 connectorId
: commandParams
?.connectorId
,
129 } as unknown
as Request
;
131 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
133 ErrorType
.NOT_SUPPORTED
,
134 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
135 `Unsupported OCPP command '${commandName}'`,
142 private parseJsonSchemaFile
<T
extends JsonType
>(relativePath
: string): JSONSchemaType
<T
> {
145 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), relativePath
),
148 ) as JSONSchemaType
<T
>;