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 { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
10 import OCPPError from
'../../../exception/OCPPError';
11 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
12 import type { OCPP16MeterValuesRequest
} from
'../../../types/ocpp/1.6/MeterValues';
14 type OCPP16BootNotificationRequest
,
15 type OCPP16DataTransferRequest
,
16 type OCPP16DiagnosticsStatusNotificationRequest
,
17 type OCPP16FirmwareStatusNotificationRequest
,
18 type OCPP16HeartbeatRequest
,
20 type OCPP16StatusNotificationRequest
,
21 } from
'../../../types/ocpp/1.6/Requests';
23 OCPP16AuthorizeRequest
,
24 OCPP16StartTransactionRequest
,
25 OCPP16StopTransactionRequest
,
26 } from
'../../../types/ocpp/1.6/Transaction';
27 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
28 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
29 import type { RequestParams
} from
'../../../types/ocpp/Requests';
30 import Constants from
'../../../utils/Constants';
31 import Utils from
'../../../utils/Utils';
32 import type ChargingStation from
'../../ChargingStation';
33 import OCPPConstants from
'../OCPPConstants';
34 import OCPPRequestService from
'../OCPPRequestService';
35 import type OCPPResponseService from
'../OCPPResponseService';
37 const moduleName
= 'OCPP16RequestService';
39 export default class OCPP16RequestService
extends OCPPRequestService
{
40 protected jsonSchemas
: Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>;
42 public constructor(ocppResponseService
: OCPPResponseService
) {
43 if (new.target
?.name
=== moduleName
) {
44 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
46 super(OCPPVersion
.VERSION_16
, ocppResponseService
);
47 this.jsonSchemas
= new Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonObject
>>([
49 OCPP16RequestCommand
.AUTHORIZE
,
50 this.parseJsonSchemaFile
<OCPP16AuthorizeRequest
>(
51 '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
55 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
56 this.parseJsonSchemaFile
<OCPP16BootNotificationRequest
>(
57 '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
61 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
62 this.parseJsonSchemaFile
<OCPP16DiagnosticsStatusNotificationRequest
>(
63 '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
67 OCPP16RequestCommand
.HEARTBEAT
,
68 this.parseJsonSchemaFile
<OCPP16HeartbeatRequest
>(
69 '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
73 OCPP16RequestCommand
.METER_VALUES
,
74 this.parseJsonSchemaFile
<OCPP16MeterValuesRequest
>(
75 '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
79 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
80 this.parseJsonSchemaFile
<OCPP16StatusNotificationRequest
>(
81 '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
85 OCPP16RequestCommand
.START_TRANSACTION
,
86 this.parseJsonSchemaFile
<OCPP16StartTransactionRequest
>(
87 '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
91 OCPP16RequestCommand
.STOP_TRANSACTION
,
92 this.parseJsonSchemaFile
<OCPP16StopTransactionRequest
>(
93 '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
97 OCPP16RequestCommand
.DATA_TRANSFER
,
98 this.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
99 '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
103 OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
104 this.parseJsonSchemaFile
<OCPP16FirmwareStatusNotificationRequest
>(
105 '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
109 this.buildRequestPayload
.bind(this);
112 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
113 chargingStation
: ChargingStation
,
114 commandName
: OCPP16RequestCommand
,
115 commandParams
?: JsonType
,
116 params
?: RequestParams
117 ): Promise
<ResponseType
> {
118 if (OCPP16ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
119 const requestPayload
= this.buildRequestPayload
<RequestType
>(
124 return (await this.sendMessage(
126 Utils
.generateUUID(),
130 )) as unknown
as ResponseType
;
132 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
134 ErrorType
.NOT_SUPPORTED
,
135 `Unsupported OCPP command '${commandName}'`,
141 private buildRequestPayload
<Request
extends JsonType
>(
142 chargingStation
: ChargingStation
,
143 commandName
: OCPP16RequestCommand
,
144 commandParams
?: JsonType
146 let connectorId
: number;
147 let energyActiveImportRegister
: number;
148 commandParams
= commandParams
as JsonObject
;
149 switch (commandName
) {
150 case OCPP16RequestCommand
.AUTHORIZE
:
152 ...(!Utils
.isUndefined(commandParams
?.idTag
)
153 ? { idTag
: commandParams
.idTag
}
154 : { idTag
: Constants
.DEFAULT_IDTAG
}),
155 } as unknown
as Request
;
156 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
158 chargePointModel
: commandParams
?.chargePointModel
,
159 chargePointVendor
: commandParams
?.chargePointVendor
,
160 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
161 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
163 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
164 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
166 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
167 firmwareVersion
: commandParams
.firmwareVersion
,
169 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
170 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
171 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
172 meterSerialNumber
: commandParams
.meterSerialNumber
,
174 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
175 meterType
: commandParams
.meterType
,
177 } as unknown
as Request
;
178 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
179 case OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
:
181 status: commandParams
?.status,
182 } as unknown
as Request
;
183 case OCPP16RequestCommand
.HEARTBEAT
:
184 return OCPPConstants
.OCPP_REQUEST_EMPTY
as unknown
as Request
;
185 case OCPP16RequestCommand
.METER_VALUES
:
187 connectorId
: commandParams
?.connectorId
,
188 transactionId
: commandParams
?.transactionId
,
189 meterValue
: commandParams
?.meterValue
,
190 } as unknown
as Request
;
191 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
193 connectorId
: commandParams
?.connectorId
,
194 status: commandParams
?.status,
195 errorCode
: commandParams
?.errorCode
,
196 ...(!Utils
.isUndefined(commandParams
?.info
) && {
197 info
: commandParams
?.info
,
199 ...(!Utils
.isUndefined(commandParams
?.timestamp
) && {
200 timestamp
: commandParams
?.timestamp
,
202 ...(!Utils
.isUndefined(commandParams
?.vendorId
) && {
203 vendorId
: commandParams
?.vendorId
,
205 ...(!Utils
.isUndefined(commandParams
?.vendorErrorCode
) && {
206 vendorErrorCode
: commandParams
?.vendorErrorCode
,
208 } as unknown
as Request
;
209 case OCPP16RequestCommand
.START_TRANSACTION
:
211 connectorId
: commandParams
?.connectorId
,
212 ...(!Utils
.isUndefined(commandParams
?.idTag
)
213 ? { idTag
: commandParams
?.idTag
}
214 : { idTag
: Constants
.DEFAULT_IDTAG
}),
216 commandParams
?.meterStart
??
217 chargingStation
.getEnergyActiveImportRegisterByConnectorId(
218 commandParams
?.connectorId
as number
220 timestamp
: commandParams
?.timestamp
?? new Date(),
221 ...(!Utils
.isUndefined(commandParams
?.reservationId
) && {
222 reservationId
: commandParams
?.reservationId
,
224 } as unknown
as Request
;
225 case OCPP16RequestCommand
.STOP_TRANSACTION
:
226 chargingStation
.getTransactionDataMeterValues() &&
227 Utils
.isNullOrUndefined(commandParams
?.transactionData
) &&
228 (connectorId
= chargingStation
.getConnectorIdByTransactionId(
229 commandParams
?.transactionId
as number
231 Utils
.isNullOrUndefined(commandParams
?.meterStop
) &&
232 (energyActiveImportRegister
=
233 chargingStation
.getEnergyActiveImportRegisterByTransactionId(
234 commandParams
?.transactionId
as number,
238 transactionId
: commandParams
?.transactionId
,
240 commandParams
?.idTag
??
241 chargingStation
.getTransactionIdTag(commandParams
?.transactionId
as number),
242 meterStop
: commandParams
?.meterStop
?? energyActiveImportRegister
,
243 timestamp
: commandParams
?.timestamp
?? new Date(),
244 ...(!Utils
.isUndefined(commandParams
?.reason
) && {
245 reason
: commandParams
?.reason
,
247 ...(!Utils
.isUndefined(commandParams
?.transactionData
)
248 ? { transactionData
: commandParams
?.transactionData
}
249 : chargingStation
.getTransactionDataMeterValues() && {
250 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
251 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
252 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
255 (commandParams
?.meterStop
as number) ?? energyActiveImportRegister
259 } as unknown
as Request
;
260 case OCPP16RequestCommand
.DATA_TRANSFER
:
261 return commandParams
as unknown
as Request
;
263 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
265 ErrorType
.NOT_SUPPORTED
,
266 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
267 `Unsupported OCPP command '${commandName}'`,
274 private parseJsonSchemaFile
<T
extends JsonType
>(relativePath
: string): JSONSchemaType
<T
> {
277 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), relativePath
),
280 ) as JSONSchemaType
<T
>;