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';
11 import type { OCPP16MeterValuesRequest
} from
'../../../types/ocpp/1.6/MeterValues';
13 type DiagnosticsStatusNotificationRequest
,
14 type OCPP16BootNotificationRequest
,
15 type OCPP16DataTransferRequest
,
16 type OCPP16HeartbeatRequest
,
18 type OCPP16StatusNotificationRequest
,
19 } from
'../../../types/ocpp/1.6/Requests';
21 OCPP16AuthorizeRequest
,
22 OCPP16StartTransactionRequest
,
23 OCPP16StopTransactionRequest
,
24 } from
'../../../types/ocpp/1.6/Transaction';
25 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
26 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
27 import type { RequestParams
} from
'../../../types/ocpp/Requests';
28 import Constants from
'../../../utils/Constants';
29 import logger from
'../../../utils/Logger';
30 import Utils from
'../../../utils/Utils';
31 import type ChargingStation from
'../../ChargingStation';
32 import OCPPRequestService from
'../OCPPRequestService';
33 import type OCPPResponseService from
'../OCPPResponseService';
34 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
36 const moduleName
= 'OCPP16RequestService';
38 export default class OCPP16RequestService
extends OCPPRequestService
{
39 private jsonSchemas
: Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>;
41 public constructor(ocppResponseService
: OCPPResponseService
) {
42 if (new.target
?.name
=== moduleName
) {
43 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
45 super(OCPPVersion
.VERSION_16
, ocppResponseService
);
46 this.jsonSchemas
= new Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>([
48 OCPP16RequestCommand
.AUTHORIZE
,
52 path
.dirname(fileURLToPath(import.meta
.url
)),
53 '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
57 ) as JSONSchemaType
<OCPP16AuthorizeRequest
>,
60 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
64 path
.dirname(fileURLToPath(import.meta
.url
)),
65 '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
69 ) as JSONSchemaType
<OCPP16BootNotificationRequest
>,
72 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
76 path
.dirname(fileURLToPath(import.meta
.url
)),
77 '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
81 ) as JSONSchemaType
<DiagnosticsStatusNotificationRequest
>,
84 OCPP16RequestCommand
.HEARTBEAT
,
88 path
.dirname(fileURLToPath(import.meta
.url
)),
89 '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
93 ) as JSONSchemaType
<OCPP16HeartbeatRequest
>,
96 OCPP16RequestCommand
.METER_VALUES
,
100 path
.dirname(fileURLToPath(import.meta
.url
)),
101 '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
105 ) as JSONSchemaType
<OCPP16MeterValuesRequest
>,
108 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
112 path
.dirname(fileURLToPath(import.meta
.url
)),
113 '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
117 ) as JSONSchemaType
<OCPP16StatusNotificationRequest
>,
120 OCPP16RequestCommand
.START_TRANSACTION
,
124 path
.dirname(fileURLToPath(import.meta
.url
)),
125 '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
129 ) as JSONSchemaType
<OCPP16StartTransactionRequest
>,
132 OCPP16RequestCommand
.STOP_TRANSACTION
,
136 path
.dirname(fileURLToPath(import.meta
.url
)),
137 '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
141 ) as JSONSchemaType
<OCPP16StopTransactionRequest
>,
144 OCPP16RequestCommand
.DATA_TRANSFER
,
148 path
.dirname(fileURLToPath(import.meta
.url
)),
149 '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
153 ) as JSONSchemaType
<OCPP16DataTransferRequest
>,
156 this.buildRequestPayload
.bind(this);
159 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
160 chargingStation
: ChargingStation
,
161 commandName
: OCPP16RequestCommand
,
162 commandParams
?: JsonType
,
163 params
?: RequestParams
164 ): Promise
<ResponseType
> {
165 if (OCPP16ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
166 const requestPayload
= this.buildRequestPayload
<RequestType
>(
171 return (await this.sendMessage(
173 Utils
.generateUUID(),
177 )) as unknown
as ResponseType
;
179 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
181 ErrorType
.NOT_SUPPORTED
,
182 `Unsupported OCPP command '${commandName}'`,
188 protected getRequestPayloadValidationSchema(
189 chargingStation
: ChargingStation
,
190 commandName
: OCPP16RequestCommand
191 ): JSONSchemaType
<JsonObject
> | false {
192 if (this.jsonSchemas
.has(commandName
) === true) {
193 return this.jsonSchemas
.get(commandName
);
196 `${chargingStation.logPrefix()} ${moduleName}.getPayloadValidationSchema: No JSON schema found for command ${commandName} PDU validation`
201 private buildRequestPayload
<Request
extends JsonType
>(
202 chargingStation
: ChargingStation
,
203 commandName
: OCPP16RequestCommand
,
204 commandParams
?: JsonType
206 let connectorId
: number;
207 let energyActiveImportRegister
: number;
208 commandParams
= commandParams
as JsonObject
;
209 switch (commandName
) {
210 case OCPP16RequestCommand
.AUTHORIZE
:
212 ...(!Utils
.isUndefined(commandParams
?.idTag
)
213 ? { idTag
: commandParams
.idTag
}
214 : { idTag
: Constants
.DEFAULT_IDTAG
}),
215 } as unknown
as Request
;
216 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
218 chargePointModel
: commandParams
?.chargePointModel
,
219 chargePointVendor
: commandParams
?.chargePointVendor
,
220 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
221 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
223 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
224 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
226 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
227 firmwareVersion
: commandParams
.firmwareVersion
,
229 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
230 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
231 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
232 meterSerialNumber
: commandParams
.meterSerialNumber
,
234 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
235 meterType
: commandParams
.meterType
,
237 } as unknown
as Request
;
238 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
240 status: commandParams
?.diagnosticsStatus
,
241 } as unknown
as Request
;
242 case OCPP16RequestCommand
.HEARTBEAT
:
243 return {} as unknown
as Request
;
244 case OCPP16RequestCommand
.METER_VALUES
:
246 connectorId
: commandParams
?.connectorId
,
247 transactionId
: commandParams
?.transactionId
,
248 meterValue
: commandParams
?.meterValue
,
249 } as unknown
as Request
;
250 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
252 connectorId
: commandParams
?.connectorId
,
253 status: commandParams
?.status,
254 errorCode
: commandParams
?.errorCode
,
255 } as unknown
as Request
;
256 case OCPP16RequestCommand
.START_TRANSACTION
:
258 connectorId
: commandParams
?.connectorId
,
259 ...(!Utils
.isUndefined(commandParams
?.idTag
)
260 ? { idTag
: commandParams
?.idTag
}
261 : { idTag
: Constants
.DEFAULT_IDTAG
}),
262 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
263 commandParams
?.connectorId
as number
265 timestamp
: new Date(),
266 } as unknown
as Request
;
267 case OCPP16RequestCommand
.STOP_TRANSACTION
:
268 connectorId
= chargingStation
.getConnectorIdByTransactionId(
269 commandParams
?.transactionId
as number
271 commandParams
?.meterStop
&&
272 (energyActiveImportRegister
=
273 chargingStation
.getEnergyActiveImportRegisterByTransactionId(
274 commandParams
?.transactionId
as number,
278 transactionId
: commandParams
?.transactionId
,
280 commandParams
?.idTag
??
281 chargingStation
.getTransactionIdTag(commandParams
?.transactionId
as number),
282 meterStop
: commandParams
?.meterStop
?? energyActiveImportRegister
,
283 timestamp
: new Date(),
284 reason
: commandParams
?.reason
,
285 ...(chargingStation
.getTransactionDataMeterValues() && {
286 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
287 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
288 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
291 (commandParams
?.meterStop
as number) ?? energyActiveImportRegister
295 } as unknown
as Request
;
296 case OCPP16RequestCommand
.DATA_TRANSFER
:
297 return commandParams
as unknown
as Request
;
299 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
301 ErrorType
.NOT_SUPPORTED
,
302 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
303 `Unsupported OCPP command '${commandName}'`,