1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import type { JSONSchemaType
} from
'ajv'
5 import { OCPP16Constants
} from
'./OCPP16Constants.js'
6 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils.js'
7 import type { ChargingStation
} from
'../../../charging-station/index.js'
8 import { OCPPError
} from
'../../../exception/index.js'
13 type OCPP16AuthorizeRequest
,
14 type OCPP16BootNotificationRequest
,
15 OCPP16ChargePointStatus
,
16 type OCPP16DataTransferRequest
,
17 type OCPP16DiagnosticsStatusNotificationRequest
,
18 type OCPP16FirmwareStatusNotificationRequest
,
19 type OCPP16HeartbeatRequest
,
20 type OCPP16MeterValuesRequest
,
22 type OCPP16StartTransactionRequest
,
23 type OCPP16StatusNotificationRequest
,
24 type OCPP16StopTransactionRequest
,
27 } from
'../../../types/index.js'
28 import { Constants
, generateUUID
} from
'../../../utils/index.js'
29 import { OCPPRequestService
} from
'../OCPPRequestService.js'
30 import type { OCPPResponseService
} from
'../OCPPResponseService.js'
32 const moduleName
= 'OCPP16RequestService'
34 export class OCPP16RequestService
extends OCPPRequestService
{
35 protected jsonSchemas
: Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonType
>>
37 public constructor (ocppResponseService
: OCPPResponseService
) {
38 // if (new.target.name === moduleName) {
39 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
41 super(OCPPVersion
.VERSION_16
, ocppResponseService
)
42 this.jsonSchemas
= new Map
<OCPP16RequestCommand
, JSONSchemaType
<JsonType
>>([
44 OCPP16RequestCommand
.AUTHORIZE
,
45 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16AuthorizeRequest
>(
46 'assets/json-schemas/ocpp/1.6/Authorize.json',
52 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
53 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16BootNotificationRequest
>(
54 'assets/json-schemas/ocpp/1.6/BootNotification.json',
60 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
61 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DiagnosticsStatusNotificationRequest
>(
62 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json',
68 OCPP16RequestCommand
.HEARTBEAT
,
69 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16HeartbeatRequest
>(
70 'assets/json-schemas/ocpp/1.6/Heartbeat.json',
76 OCPP16RequestCommand
.METER_VALUES
,
77 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16MeterValuesRequest
>(
78 'assets/json-schemas/ocpp/1.6/MeterValues.json',
84 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
85 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16StatusNotificationRequest
>(
86 'assets/json-schemas/ocpp/1.6/StatusNotification.json',
92 OCPP16RequestCommand
.START_TRANSACTION
,
93 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16StartTransactionRequest
>(
94 'assets/json-schemas/ocpp/1.6/StartTransaction.json',
100 OCPP16RequestCommand
.STOP_TRANSACTION
,
101 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16StopTransactionRequest
>(
102 'assets/json-schemas/ocpp/1.6/StopTransaction.json',
108 OCPP16RequestCommand
.DATA_TRANSFER
,
109 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
110 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
116 OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
117 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16FirmwareStatusNotificationRequest
>(
118 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json',
124 this.buildRequestPayload
= this.buildRequestPayload
.bind(this)
127 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
128 chargingStation
: ChargingStation
,
129 commandName
: OCPP16RequestCommand
,
130 commandParams
?: JsonType
,
131 params
?: RequestParams
132 ): Promise
<ResponseType
> {
133 // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc.
134 if (OCPP16ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
)) {
135 return (await this.sendMessage(
138 this.buildRequestPayload
<RequestType
>(chargingStation
, commandName
, commandParams
),
143 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
145 ErrorType
.NOT_SUPPORTED
,
146 `Unsupported OCPP command '${commandName}'`,
152 private buildRequestPayload
<Request
extends JsonType
>(
153 chargingStation
: ChargingStation
,
154 commandName
: OCPP16RequestCommand
,
155 commandParams
?: JsonType
157 let connectorId
: number | undefined
158 let energyActiveImportRegister
: number
159 commandParams
= commandParams
as JsonObject
160 switch (commandName
) {
161 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
162 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
163 case OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
:
164 case OCPP16RequestCommand
.METER_VALUES
:
165 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
166 case OCPP16RequestCommand
.DATA_TRANSFER
:
167 return commandParams
as unknown
as Request
168 case OCPP16RequestCommand
.AUTHORIZE
:
170 idTag
: Constants
.DEFAULT_IDTAG
,
172 } as unknown
as Request
173 case OCPP16RequestCommand
.HEARTBEAT
:
174 return OCPP16Constants
.OCPP_REQUEST_EMPTY
as unknown
as Request
175 case OCPP16RequestCommand
.START_TRANSACTION
:
177 idTag
: Constants
.DEFAULT_IDTAG
,
178 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
179 commandParams
.connectorId
as number,
182 timestamp
: new Date(),
183 ...(OCPP16ServiceUtils
.hasReservation(
185 commandParams
.connectorId
as number,
186 commandParams
.idTag
as string
188 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
189 reservationId
: chargingStation
.getReservationBy(
191 chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
193 : (commandParams
.connectorId
as number)
197 } as unknown
as Request
198 case OCPP16RequestCommand
.STOP_TRANSACTION
:
199 chargingStation
.stationInfo
?.transactionDataMeterValues
=== true &&
200 (connectorId
= chargingStation
.getConnectorIdByTransactionId(
201 commandParams
.transactionId
as number
203 energyActiveImportRegister
= chargingStation
.getEnergyActiveImportRegisterByTransactionId(
204 commandParams
.transactionId
as number,
208 idTag
: chargingStation
.getTransactionIdTag(commandParams
.transactionId
as number),
209 meterStop
: energyActiveImportRegister
,
210 timestamp
: new Date(),
211 ...(chargingStation
.stationInfo
?.transactionDataMeterValues
=== true && {
212 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
213 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
214 chargingStation
.getConnectorStatus(connectorId
!)!.transactionBeginMeterValue
!,
215 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
217 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
219 energyActiveImportRegister
224 } as unknown
as Request
226 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
228 ErrorType
.NOT_SUPPORTED
,
229 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
230 `Unsupported OCPP command '${commandName}'`,