Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c8eeb62b | 2 | |
6c1761d4 | 3 | import type { JSONSchemaType } from 'ajv'; |
b52c969d | 4 | |
78202038 | 5 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
268a74bb | 6 | import { OCPPError } from '../../../exception'; |
b52c969d | 7 | import { |
268a74bb JB |
8 | ErrorType, |
9 | type JsonObject, | |
10 | type JsonType, | |
11 | type OCPP16AuthorizeRequest, | |
27782dbc JB |
12 | type OCPP16BootNotificationRequest, |
13 | type OCPP16DataTransferRequest, | |
c9a4f9ea | 14 | type OCPP16DiagnosticsStatusNotificationRequest, |
e9a4164c | 15 | type OCPP16FirmwareStatusNotificationRequest, |
27782dbc | 16 | type OCPP16HeartbeatRequest, |
268a74bb | 17 | type OCPP16MeterValuesRequest, |
b52c969d | 18 | OCPP16RequestCommand, |
268a74bb | 19 | type OCPP16StartTransactionRequest, |
27782dbc | 20 | type OCPP16StatusNotificationRequest, |
268a74bb JB |
21 | type OCPP16StopTransactionRequest, |
22 | OCPPVersion, | |
23 | type RequestParams, | |
24 | } from '../../../types'; | |
25 | import { Constants } from '../../../utils/Constants'; | |
26 | import { Utils } from '../../../utils/Utils'; | |
27 | import type { ChargingStation } from '../../ChargingStation'; | |
28 | import { OCPPConstants } from '../OCPPConstants'; | |
29 | import { OCPPRequestService } from '../OCPPRequestService'; | |
30 | import type { OCPPResponseService } from '../OCPPResponseService'; | |
c0560973 | 31 | |
909dcf2d JB |
32 | const moduleName = 'OCPP16RequestService'; |
33 | ||
268a74bb | 34 | export class OCPP16RequestService extends OCPPRequestService { |
b3fc3ff5 | 35 | protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>; |
b52c969d | 36 | |
08f130a0 | 37 | public constructor(ocppResponseService: OCPPResponseService) { |
909dcf2d | 38 | if (new.target?.name === moduleName) { |
06127450 | 39 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
9f2e3130 | 40 | } |
d270cc87 | 41 | super(OCPPVersion.VERSION_16, ocppResponseService); |
b52c969d JB |
42 | this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([ |
43 | [ | |
44 | OCPP16RequestCommand.AUTHORIZE, | |
130783a7 | 45 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeRequest>( |
1b271a54 JB |
46 | '../../../assets/json-schemas/ocpp/1.6/Authorize.json', |
47 | moduleName, | |
48 | 'constructor' | |
e9a4164c | 49 | ), |
b52c969d JB |
50 | ], |
51 | [ | |
52 | OCPP16RequestCommand.BOOT_NOTIFICATION, | |
130783a7 | 53 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationRequest>( |
1b271a54 JB |
54 | '../../../assets/json-schemas/ocpp/1.6/BootNotification.json', |
55 | moduleName, | |
56 | 'constructor' | |
e9a4164c | 57 | ), |
b52c969d JB |
58 | ], |
59 | [ | |
60 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, | |
130783a7 | 61 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>( |
1b271a54 JB |
62 | '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', |
63 | moduleName, | |
64 | 'constructor' | |
e9a4164c | 65 | ), |
b52c969d JB |
66 | ], |
67 | [ | |
68 | OCPP16RequestCommand.HEARTBEAT, | |
130783a7 | 69 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16HeartbeatRequest>( |
1b271a54 JB |
70 | '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json', |
71 | moduleName, | |
72 | 'constructor' | |
e9a4164c | 73 | ), |
b52c969d JB |
74 | ], |
75 | [ | |
76 | OCPP16RequestCommand.METER_VALUES, | |
130783a7 | 77 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16MeterValuesRequest>( |
1b271a54 JB |
78 | '../../../assets/json-schemas/ocpp/1.6/MeterValues.json', |
79 | moduleName, | |
80 | 'constructor' | |
e9a4164c | 81 | ), |
b52c969d JB |
82 | ], |
83 | [ | |
84 | OCPP16RequestCommand.STATUS_NOTIFICATION, | |
130783a7 | 85 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StatusNotificationRequest>( |
1b271a54 JB |
86 | '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json', |
87 | moduleName, | |
88 | 'constructor' | |
e9a4164c | 89 | ), |
b52c969d JB |
90 | ], |
91 | [ | |
92 | OCPP16RequestCommand.START_TRANSACTION, | |
130783a7 | 93 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StartTransactionRequest>( |
1b271a54 JB |
94 | '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json', |
95 | moduleName, | |
96 | 'constructor' | |
e9a4164c | 97 | ), |
b52c969d JB |
98 | ], |
99 | [ | |
100 | OCPP16RequestCommand.STOP_TRANSACTION, | |
130783a7 | 101 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StopTransactionRequest>( |
1b271a54 JB |
102 | '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json', |
103 | moduleName, | |
104 | 'constructor' | |
e9a4164c | 105 | ), |
b52c969d | 106 | ], |
91a7d3ea JB |
107 | [ |
108 | OCPP16RequestCommand.DATA_TRANSFER, | |
130783a7 | 109 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferRequest>( |
1b271a54 JB |
110 | '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json', |
111 | moduleName, | |
112 | 'constructor' | |
e9a4164c | 113 | ), |
91a7d3ea | 114 | ], |
c9a4f9ea JB |
115 | [ |
116 | OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, | |
130783a7 | 117 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>( |
1b271a54 JB |
118 | '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', |
119 | moduleName, | |
120 | 'constructor' | |
e9a4164c | 121 | ), |
c9a4f9ea | 122 | ], |
b52c969d | 123 | ]); |
9952c548 | 124 | this.buildRequestPayload.bind(this); |
9f2e3130 JB |
125 | } |
126 | ||
6c1761d4 | 127 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( |
08f130a0 | 128 | chargingStation: ChargingStation, |
94a464f9 | 129 | commandName: OCPP16RequestCommand, |
5cc4b63b | 130 | commandParams?: JsonType, |
be9b0d50 | 131 | params?: RequestParams |
6c1761d4 | 132 | ): Promise<ResponseType> { |
ed6cfcff | 133 | if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { |
f22266fd | 134 | return (await this.sendMessage( |
08f130a0 | 135 | chargingStation, |
94a464f9 | 136 | Utils.generateUUID(), |
18bf8274 | 137 | this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams), |
94a464f9 JB |
138 | commandName, |
139 | params | |
6c1761d4 | 140 | )) as unknown as ResponseType; |
94a464f9 | 141 | } |
e909d2a7 | 142 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
94a464f9 JB |
143 | throw new OCPPError( |
144 | ErrorType.NOT_SUPPORTED, | |
6c8f5d90 | 145 | `Unsupported OCPP command '${commandName}'`, |
94a464f9 | 146 | commandName, |
7369e417 | 147 | commandParams |
94a464f9 | 148 | ); |
c0560973 JB |
149 | } |
150 | ||
5cc4b63b | 151 | private buildRequestPayload<Request extends JsonType>( |
08f130a0 | 152 | chargingStation: ChargingStation, |
78085c42 | 153 | commandName: OCPP16RequestCommand, |
5cc4b63b | 154 | commandParams?: JsonType |
f22266fd | 155 | ): Request { |
68c993d5 | 156 | let connectorId: number; |
cf058664 | 157 | let energyActiveImportRegister: number; |
5cc4b63b | 158 | commandParams = commandParams as JsonObject; |
78085c42 | 159 | switch (commandName) { |
78085c42 | 160 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
78085c42 | 161 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
22e0d48e | 162 | case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION: |
78085c42 | 163 | case OCPP16RequestCommand.METER_VALUES: |
78085c42 | 164 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
36c462a4 JB |
165 | case OCPP16RequestCommand.DATA_TRANSFER: |
166 | return commandParams as unknown as Request; | |
167 | case OCPP16RequestCommand.AUTHORIZE: | |
78085c42 | 168 | return { |
36c462a4 JB |
169 | idTag: Constants.DEFAULT_IDTAG, |
170 | ...commandParams, | |
f22266fd | 171 | } as unknown as Request; |
36c462a4 JB |
172 | case OCPP16RequestCommand.HEARTBEAT: |
173 | return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request; | |
78085c42 JB |
174 | case OCPP16RequestCommand.START_TRANSACTION: |
175 | return { | |
36c462a4 JB |
176 | idTag: Constants.DEFAULT_IDTAG, |
177 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( | |
18bf8274 JB |
178 | commandParams?.connectorId as number, |
179 | true | |
36c462a4 JB |
180 | ), |
181 | timestamp: new Date(), | |
182 | ...commandParams, | |
f22266fd | 183 | } as unknown as Request; |
78085c42 | 184 | case OCPP16RequestCommand.STOP_TRANSACTION: |
f1e731bd | 185 | chargingStation.getTransactionDataMeterValues() && |
f1e731bd JB |
186 | (connectorId = chargingStation.getConnectorIdByTransactionId( |
187 | commandParams?.transactionId as number | |
188 | )); | |
36c462a4 JB |
189 | energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId( |
190 | commandParams?.transactionId as number, | |
191 | true | |
192 | ); | |
78085c42 | 193 | return { |
36c462a4 JB |
194 | idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number), |
195 | meterStop: energyActiveImportRegister, | |
196 | timestamp: new Date(), | |
197 | ...(chargingStation.getTransactionDataMeterValues() && { | |
198 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( | |
199 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, | |
200 | OCPP16ServiceUtils.buildTransactionEndMeterValue( | |
201 | chargingStation, | |
202 | connectorId, | |
203 | energyActiveImportRegister | |
204 | ) | |
205 | ), | |
78085c42 | 206 | }), |
36c462a4 | 207 | ...commandParams, |
f22266fd | 208 | } as unknown as Request; |
78085c42 | 209 | default: |
e909d2a7 | 210 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
78085c42 JB |
211 | throw new OCPPError( |
212 | ErrorType.NOT_SUPPORTED, | |
213 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
6c8f5d90 | 214 | `Unsupported OCPP command '${commandName}'`, |
78085c42 | 215 | commandName, |
7369e417 | 216 | commandParams |
78085c42 JB |
217 | ); |
218 | } | |
219 | } | |
c0560973 | 220 | } |