1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 import path from
'path';
5 import { fileURLToPath
} from
'url';
7 import { JSONSchemaType
} from
'ajv';
9 import OCPPError from
'../../../exception/OCPPError';
10 import { JsonObject
, JsonType
} from
'../../../types/JsonType';
11 import { OCPP16MeterValuesRequest
} from
'../../../types/ocpp/1.6/MeterValues';
13 DiagnosticsStatusNotificationRequest
,
14 OCPP16BootNotificationRequest
,
15 OCPP16HeartbeatRequest
,
17 OCPP16StatusNotificationRequest
,
18 } from
'../../../types/ocpp/1.6/Requests';
20 OCPP16AuthorizeRequest
,
21 OCPP16StartTransactionRequest
,
22 OCPP16StopTransactionRequest
,
23 } from
'../../../types/ocpp/1.6/Transaction';
24 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
25 import { RequestParams
} from
'../../../types/ocpp/Requests';
26 import Constants from
'../../../utils/Constants';
27 import logger from
'../../../utils/Logger';
28 import Utils from
'../../../utils/Utils';
29 import type ChargingStation from
'../../ChargingStation';
30 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
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 private 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(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
>,
145 public async requestHandler
<Request
extends JsonType
, Response
extends JsonType
>(
146 chargingStation
: ChargingStation
,
147 commandName
: OCPP16RequestCommand
,
148 commandParams
?: JsonType
,
149 params
?: RequestParams
150 ): Promise
<Response
> {
151 if (ChargingStationUtils
.isRequestCommandSupported(commandName
, chargingStation
)) {
152 const requestPayload
= this.buildRequestPayload
<Request
>(
157 this.validatePayload(chargingStation
, commandName
, requestPayload
);
158 return (await this.sendMessage(
160 Utils
.generateUUID(),
164 )) as unknown
as Response
;
167 ErrorType
.NOT_SUPPORTED
,
168 `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`,
174 private buildRequestPayload
<Request
extends JsonType
>(
175 chargingStation
: ChargingStation
,
176 commandName
: OCPP16RequestCommand
,
177 commandParams
?: JsonType
179 let connectorId
: number;
180 commandParams
= commandParams
as JsonObject
;
181 switch (commandName
) {
182 case OCPP16RequestCommand
.AUTHORIZE
:
184 ...(!Utils
.isUndefined(commandParams
?.idTag
)
185 ? { idTag
: commandParams
.idTag
}
186 : { idTag
: Constants
.DEFAULT_IDTAG
}),
187 } as unknown
as Request
;
188 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
190 chargePointModel
: commandParams
?.chargePointModel
,
191 chargePointVendor
: commandParams
?.chargePointVendor
,
192 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
193 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
195 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
196 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
198 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
199 firmwareVersion
: commandParams
.firmwareVersion
,
201 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
202 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
203 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
204 meterSerialNumber
: commandParams
.meterSerialNumber
,
206 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
207 meterType
: commandParams
.meterType
,
209 } as unknown
as Request
;
210 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
212 status: commandParams
?.diagnosticsStatus
,
213 } as unknown
as Request
;
214 case OCPP16RequestCommand
.HEARTBEAT
:
215 return {} as unknown
as Request
;
216 case OCPP16RequestCommand
.METER_VALUES
:
218 connectorId
: commandParams
?.connectorId
,
219 transactionId
: commandParams
?.transactionId
,
220 meterValue
: commandParams
?.meterValue
,
221 } as unknown
as Request
;
222 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
224 connectorId
: commandParams
?.connectorId
,
225 status: commandParams
?.status,
226 errorCode
: commandParams
?.errorCode
,
227 } as unknown
as Request
;
228 case OCPP16RequestCommand
.START_TRANSACTION
:
230 connectorId
: commandParams
?.connectorId
,
231 ...(!Utils
.isUndefined(commandParams
?.idTag
)
232 ? { idTag
: commandParams
?.idTag
}
233 : { idTag
: Constants
.DEFAULT_IDTAG
}),
234 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
235 commandParams
?.connectorId
as number
237 timestamp
: new Date().toISOString(),
238 } as unknown
as Request
;
239 case OCPP16RequestCommand
.STOP_TRANSACTION
:
240 connectorId
= chargingStation
.getConnectorIdByTransactionId(
241 commandParams
?.transactionId
as number
244 transactionId
: commandParams
?.transactionId
,
245 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
246 meterStop
: commandParams
?.meterStop
,
247 timestamp
: new Date().toISOString(),
248 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
249 ...(chargingStation
.getTransactionDataMeterValues() && {
250 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
251 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
252 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
255 commandParams
?.meterStop
as number
259 } as unknown
as Request
;
262 ErrorType
.NOT_SUPPORTED
,
263 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
264 `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`,
271 private validatePayload
<Request
extends JsonType
>(
272 chargingStation
: ChargingStation
,
273 commandName
: OCPP16RequestCommand
,
274 requestPayload
: Request
276 if (this.jsonSchemas
.has(commandName
)) {
277 return this.validateRequestPayload(
280 this.jsonSchemas
.get(commandName
),
285 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`