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 Utils from
'../../../utils/Utils';
30 import type ChargingStation from
'../../ChargingStation';
31 import OCPPRequestService from
'../OCPPRequestService';
32 import type OCPPResponseService from
'../OCPPResponseService';
33 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
35 const moduleName
= 'OCPP16RequestService';
37 export default class OCPP16RequestService
extends OCPPRequestService
{
38 protected jsonSchemas
: Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>;
40 public constructor(ocppResponseService
: OCPPResponseService
) {
41 if (new.target
?.name
=== moduleName
) {
42 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
44 super(OCPPVersion
.VERSION_16
, ocppResponseService
);
45 this.jsonSchemas
= new Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>([
47 OCPP16RequestCommand
.AUTHORIZE
,
51 path
.dirname(fileURLToPath(import.meta
.url
)),
52 '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
56 ) as JSONSchemaType
<OCPP16AuthorizeRequest
>,
59 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
63 path
.dirname(fileURLToPath(import.meta
.url
)),
64 '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
68 ) as JSONSchemaType
<OCPP16BootNotificationRequest
>,
71 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
75 path
.dirname(fileURLToPath(import.meta
.url
)),
76 '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
80 ) as JSONSchemaType
<DiagnosticsStatusNotificationRequest
>,
83 OCPP16RequestCommand
.HEARTBEAT
,
87 path
.dirname(fileURLToPath(import.meta
.url
)),
88 '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
92 ) as JSONSchemaType
<OCPP16HeartbeatRequest
>,
95 OCPP16RequestCommand
.METER_VALUES
,
99 path
.dirname(fileURLToPath(import.meta
.url
)),
100 '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
104 ) as JSONSchemaType
<OCPP16MeterValuesRequest
>,
107 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
111 path
.dirname(fileURLToPath(import.meta
.url
)),
112 '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
116 ) as JSONSchemaType
<OCPP16StatusNotificationRequest
>,
119 OCPP16RequestCommand
.START_TRANSACTION
,
123 path
.dirname(fileURLToPath(import.meta
.url
)),
124 '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
128 ) as JSONSchemaType
<OCPP16StartTransactionRequest
>,
131 OCPP16RequestCommand
.STOP_TRANSACTION
,
135 path
.dirname(fileURLToPath(import.meta
.url
)),
136 '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
140 ) as JSONSchemaType
<OCPP16StopTransactionRequest
>,
143 OCPP16RequestCommand
.DATA_TRANSFER
,
147 path
.dirname(fileURLToPath(import.meta
.url
)),
148 '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
152 ) as JSONSchemaType
<OCPP16DataTransferRequest
>,
155 this.buildRequestPayload
.bind(this);
158 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
159 chargingStation
: ChargingStation
,
160 commandName
: OCPP16RequestCommand
,
161 commandParams
?: JsonType
,
162 params
?: RequestParams
163 ): Promise
<ResponseType
> {
164 if (OCPP16ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
165 const requestPayload
= this.buildRequestPayload
<RequestType
>(
170 return (await this.sendMessage(
172 Utils
.generateUUID(),
176 )) as unknown
as ResponseType
;
178 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
180 ErrorType
.NOT_SUPPORTED
,
181 `Unsupported OCPP command '${commandName}'`,
187 private buildRequestPayload
<Request
extends JsonType
>(
188 chargingStation
: ChargingStation
,
189 commandName
: OCPP16RequestCommand
,
190 commandParams
?: JsonType
192 let connectorId
: number;
193 let energyActiveImportRegister
: number;
194 commandParams
= commandParams
as JsonObject
;
195 switch (commandName
) {
196 case OCPP16RequestCommand
.AUTHORIZE
:
198 ...(!Utils
.isUndefined(commandParams
?.idTag
)
199 ? { idTag
: commandParams
.idTag
}
200 : { idTag
: Constants
.DEFAULT_IDTAG
}),
201 } as unknown
as Request
;
202 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
204 chargePointModel
: commandParams
?.chargePointModel
,
205 chargePointVendor
: commandParams
?.chargePointVendor
,
206 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
207 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
209 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
210 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
212 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
213 firmwareVersion
: commandParams
.firmwareVersion
,
215 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
216 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
217 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
218 meterSerialNumber
: commandParams
.meterSerialNumber
,
220 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
221 meterType
: commandParams
.meterType
,
223 } as unknown
as Request
;
224 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
226 status: commandParams
?.diagnosticsStatus
,
227 } as unknown
as Request
;
228 case OCPP16RequestCommand
.HEARTBEAT
:
229 return {} as unknown
as Request
;
230 case OCPP16RequestCommand
.METER_VALUES
:
232 connectorId
: commandParams
?.connectorId
,
233 transactionId
: commandParams
?.transactionId
,
234 meterValue
: commandParams
?.meterValue
,
235 } as unknown
as Request
;
236 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
238 connectorId
: commandParams
?.connectorId
,
239 status: commandParams
?.status,
240 errorCode
: commandParams
?.errorCode
,
241 } as unknown
as Request
;
242 case OCPP16RequestCommand
.START_TRANSACTION
:
244 connectorId
: commandParams
?.connectorId
,
245 ...(!Utils
.isUndefined(commandParams
?.idTag
)
246 ? { idTag
: commandParams
?.idTag
}
247 : { idTag
: Constants
.DEFAULT_IDTAG
}),
248 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
249 commandParams
?.connectorId
as number
251 timestamp
: new Date(),
252 } as unknown
as Request
;
253 case OCPP16RequestCommand
.STOP_TRANSACTION
:
254 connectorId
= chargingStation
.getConnectorIdByTransactionId(
255 commandParams
?.transactionId
as number
257 commandParams
?.meterStop
&&
258 (energyActiveImportRegister
=
259 chargingStation
.getEnergyActiveImportRegisterByTransactionId(
260 commandParams
?.transactionId
as number,
264 transactionId
: commandParams
?.transactionId
,
266 commandParams
?.idTag
??
267 chargingStation
.getTransactionIdTag(commandParams
?.transactionId
as number),
268 meterStop
: commandParams
?.meterStop
?? energyActiveImportRegister
,
269 timestamp
: new Date(),
270 reason
: commandParams
?.reason
,
271 ...(chargingStation
.getTransactionDataMeterValues() && {
272 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
273 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
274 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
277 (commandParams
?.meterStop
as number) ?? energyActiveImportRegister
281 } as unknown
as Request
;
282 case OCPP16RequestCommand
.DATA_TRANSFER
:
283 return commandParams
as unknown
as Request
;
285 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
287 ErrorType
.NOT_SUPPORTED
,
288 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
289 `Unsupported OCPP command '${commandName}'`,