Add helper to get connector id for a transaction id
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
CommitLineData
c8eeb62b
JB
1// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
2
e7aeea18
JB
3import {
4 ChangeAvailabilityRequest,
5 ChangeConfigurationRequest,
6 ClearChargingProfileRequest,
7 GetConfigurationRequest,
8 GetDiagnosticsRequest,
9 MessageTrigger,
10 OCPP16AvailabilityType,
11 OCPP16IncomingRequestCommand,
94a464f9 12 OCPP16RequestCommand,
e7aeea18
JB
13 OCPP16TriggerMessageRequest,
14 RemoteStartTransactionRequest,
15 RemoteStopTransactionRequest,
16 ResetRequest,
17 SetChargingProfileRequest,
18 UnlockConnectorRequest,
19} from '../../../types/ocpp/1.6/Requests';
20import {
21 ChangeAvailabilityResponse,
22 ChangeConfigurationResponse,
23 ClearChargingProfileResponse,
24 GetConfigurationResponse,
25 GetDiagnosticsResponse,
26 OCPP16TriggerMessageResponse,
27 SetChargingProfileResponse,
28 UnlockConnectorResponse,
29} from '../../../types/ocpp/1.6/Responses';
30import {
31 ChargingProfilePurposeType,
32 OCPP16ChargingProfile,
33} from '../../../types/ocpp/1.6/ChargingProfile';
47e22477 34import { Client, FTPResponse } from 'basic-ftp';
e7aeea18
JB
35import {
36 OCPP16AuthorizationStatus,
2e3d65ae 37 OCPP16AuthorizeResponse,
e7454a1f 38 OCPP16StartTransactionResponse,
e7aeea18 39 OCPP16StopTransactionReason,
68c993d5 40 OCPP16StopTransactionResponse,
e7aeea18 41} from '../../../types/ocpp/1.6/Transaction';
c0560973 42
73b9adec 43import type ChargingStation from '../../ChargingStation';
c0560973 44import Constants from '../../../utils/Constants';
9ccca265 45import { DefaultResponse } from '../../../types/ocpp/Responses';
c0560973 46import { ErrorType } from '../../../types/ocpp/ErrorType';
58144adb 47import { IncomingRequestHandler } from '../../../types/ocpp/Requests';
d1888640 48import { JsonType } from '../../../types/JsonType';
93b4a429 49import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
c0560973 50import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
47e22477 51import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
68c993d5 52import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
c0560973
JB
53import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
54import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
e58068fd 55import OCPPError from '../../../exception/OCPPError';
c0560973 56import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
a3868ec4 57import { URL } from 'url';
c0560973 58import Utils from '../../../utils/Utils';
47e22477 59import fs from 'fs';
9f2e3130 60import logger from '../../../utils/Logger';
47e22477
JB
61import path from 'path';
62import tar from 'tar';
c0560973 63
2a115f87 64const moduleName = 'OCPP16IncomingRequestService';
909dcf2d 65
c0560973 66export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
58144adb
JB
67 private incomingRequestHandlers: Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>;
68
9f2e3130 69 public constructor(chargingStation: ChargingStation) {
909dcf2d 70 if (new.target?.name === moduleName) {
06127450 71 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
9f2e3130 72 }
58144adb
JB
73 super(chargingStation);
74 this.incomingRequestHandlers = new Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>([
75 [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)],
76 [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
77 [OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, this.handleRequestUnlockConnector.bind(this)],
e7aeea18
JB
78 [
79 OCPP16IncomingRequestCommand.GET_CONFIGURATION,
80 this.handleRequestGetConfiguration.bind(this),
81 ],
82 [
83 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
84 this.handleRequestChangeConfiguration.bind(this),
85 ],
86 [
87 OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
88 this.handleRequestSetChargingProfile.bind(this),
89 ],
90 [
91 OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
92 this.handleRequestClearChargingProfile.bind(this),
93 ],
94 [
95 OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
96 this.handleRequestChangeAvailability.bind(this),
97 ],
98 [
99 OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
100 this.handleRequestRemoteStartTransaction.bind(this),
101 ],
102 [
103 OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
104 this.handleRequestRemoteStopTransaction.bind(this),
105 ],
734d790d 106 [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)],
e7aeea18 107 [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)],
58144adb
JB
108 ]);
109 }
110
e7aeea18
JB
111 public async handleRequest(
112 messageId: string,
113 commandName: OCPP16IncomingRequestCommand,
114 commandPayload: JsonType
115 ): Promise<void> {
d1888640 116 let result: JsonType;
e7aeea18
JB
117 if (
118 this.chargingStation.getOcppStrictCompliance() &&
119 this.chargingStation.isInPendingState() &&
120 (commandName === OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION ||
121 commandName === OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION)
122 ) {
123 throw new OCPPError(
124 ErrorType.SECURITY_ERROR,
125 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
126 commandPayload,
127 null,
128 2
129 )} while the charging station is in pending state on the central server`,
130 commandName
131 );
caad9d6b 132 }
e7aeea18
JB
133 if (
134 this.chargingStation.isRegistered() ||
135 (!this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInUnknownState())
136 ) {
124f3553
JB
137 if (this.incomingRequestHandlers.has(commandName)) {
138 try {
139 // Call the method to build the result
140 result = await this.incomingRequestHandlers.get(commandName)(commandPayload);
141 } catch (error) {
142 // Log
9f2e3130 143 logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
124f3553
JB
144 throw error;
145 }
146 } else {
147 // Throw exception
e7aeea18
JB
148 throw new OCPPError(
149 ErrorType.NOT_IMPLEMENTED,
150 `${commandName} is not implemented to handle request payload ${JSON.stringify(
151 commandPayload,
152 null,
153 2
154 )}`,
155 commandName
156 );
c0560973
JB
157 }
158 } else {
e7aeea18
JB
159 throw new OCPPError(
160 ErrorType.SECURITY_ERROR,
161 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
162 commandPayload,
163 null,
164 2
165 )} while the charging station is not registered on the central server.`,
166 commandName
167 );
c0560973 168 }
de3dbcf5
JB
169 // Send the built result
170 await this.chargingStation.ocppRequestService.sendResult(messageId, result, commandName);
c0560973
JB
171 }
172
173 // Simulate charging station restart
174 private handleRequestReset(commandPayload: ResetRequest): DefaultResponse {
71623267
JB
175 // eslint-disable-next-line @typescript-eslint/no-misused-promises
176 setImmediate(async (): Promise<void> => {
e7aeea18
JB
177 await this.chargingStation.stop(
178 (commandPayload.type + 'Reset') as OCPP16StopTransactionReason
179 );
c0560973 180 await Utils.sleep(this.chargingStation.stationInfo.resetTime);
71623267 181 this.chargingStation.start();
c0560973 182 });
e7aeea18
JB
183 logger.info(
184 `${this.chargingStation.logPrefix()} ${
185 commandPayload.type
186 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
187 this.chargingStation.stationInfo.resetTime
188 )}`
189 );
c0560973
JB
190 return Constants.OCPP_RESPONSE_ACCEPTED;
191 }
192
193 private handleRequestClearCache(): DefaultResponse {
194 return Constants.OCPP_RESPONSE_ACCEPTED;
195 }
196
e7aeea18
JB
197 private async handleRequestUnlockConnector(
198 commandPayload: UnlockConnectorRequest
199 ): Promise<UnlockConnectorResponse> {
c0560973
JB
200 const connectorId = commandPayload.connectorId;
201 if (connectorId === 0) {
e7aeea18
JB
202 logger.error(
203 this.chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString()
204 );
c0560973
JB
205 return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED;
206 }
734d790d
JB
207 if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
208 const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId;
68c993d5
JB
209 if (
210 this.chargingStation.getBeginEndMeterValues() &&
211 this.chargingStation.getOcppStrictCompliance() &&
212 !this.chargingStation.getOutOfOrderEndMeterValues()
213 ) {
214 // FIXME: Implement OCPP version agnostic helpers
215 const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
216 this.chargingStation,
217 connectorId,
218 this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
219 );
3a33b6a9
JB
220 await this.chargingStation.ocppRequestService.sendMessageHandler(
221 OCPP16RequestCommand.METER_VALUES,
222 {
223 connectorId,
224 transactionId,
225 meterValue: transactionEndMeterValue,
226 }
68c993d5
JB
227 );
228 }
229 const stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
230 OCPP16RequestCommand.STOP_TRANSACTION,
231 {
232 transactionId,
233 meterStop:
234 this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
235 idTag: this.chargingStation.getTransactionIdTag(transactionId),
236 reason: OCPP16StopTransactionReason.UNLOCK_COMMAND,
237 }
238 )) as OCPP16StopTransactionResponse;
c0560973
JB
239 if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
240 return Constants.OCPP_RESPONSE_UNLOCKED;
241 }
242 return Constants.OCPP_RESPONSE_UNLOCK_FAILED;
243 }
93b4a429
JB
244 await this.chargingStation.ocppRequestService.sendMessageHandler(
245 OCPP16RequestCommand.STATUS_NOTIFICATION,
246 {
247 connectorId,
248 status: OCPP16ChargePointStatus.AVAILABLE,
249 errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
250 }
e7aeea18 251 );
734d790d 252 this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
c0560973
JB
253 return Constants.OCPP_RESPONSE_UNLOCKED;
254 }
255
e7aeea18
JB
256 private handleRequestGetConfiguration(
257 commandPayload: GetConfigurationRequest
258 ): GetConfigurationResponse {
c0560973
JB
259 const configurationKey: OCPPConfigurationKey[] = [];
260 const unknownKey: string[] = [];
261 if (Utils.isEmptyArray(commandPayload.key)) {
262 for (const configuration of this.chargingStation.configuration.configurationKey) {
263 if (Utils.isUndefined(configuration.visible)) {
264 configuration.visible = true;
265 }
266 if (!configuration.visible) {
267 continue;
268 }
269 configurationKey.push({
270 key: configuration.key,
271 readonly: configuration.readonly,
272 value: configuration.value,
273 });
274 }
275 } else {
276 for (const key of commandPayload.key) {
277 const keyFound = this.chargingStation.getConfigurationKey(key);
278 if (keyFound) {
279 if (Utils.isUndefined(keyFound.visible)) {
280 keyFound.visible = true;
281 }
282 if (!keyFound.visible) {
283 continue;
284 }
285 configurationKey.push({
286 key: keyFound.key,
287 readonly: keyFound.readonly,
288 value: keyFound.value,
289 });
290 } else {
291 unknownKey.push(key);
292 }
293 }
294 }
295 return {
296 configurationKey,
297 unknownKey,
298 };
299 }
300
e7aeea18
JB
301 private handleRequestChangeConfiguration(
302 commandPayload: ChangeConfigurationRequest
303 ): ChangeConfigurationResponse {
c0560973
JB
304 // JSON request fields type sanity check
305 if (!Utils.isString(commandPayload.key)) {
e7aeea18
JB
306 logger.error(
307 `${this.chargingStation.logPrefix()} ${
78085c42 308 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
e7aeea18
JB
309 } request key field is not a string:`,
310 commandPayload
311 );
c0560973
JB
312 }
313 if (!Utils.isString(commandPayload.value)) {
e7aeea18
JB
314 logger.error(
315 `${this.chargingStation.logPrefix()} ${
78085c42 316 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
e7aeea18
JB
317 } request value field is not a string:`,
318 commandPayload
319 );
c0560973
JB
320 }
321 const keyToChange = this.chargingStation.getConfigurationKey(commandPayload.key, true);
322 if (!keyToChange) {
323 return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED;
324 } else if (keyToChange && keyToChange.readonly) {
325 return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED;
326 } else if (keyToChange && !keyToChange.readonly) {
327 const keyIndex = this.chargingStation.configuration.configurationKey.indexOf(keyToChange);
328 let valueChanged = false;
e7aeea18
JB
329 if (
330 this.chargingStation.configuration.configurationKey[keyIndex].value !== commandPayload.value
331 ) {
c0560973
JB
332 this.chargingStation.configuration.configurationKey[keyIndex].value = commandPayload.value;
333 valueChanged = true;
334 }
335 let triggerHeartbeatRestart = false;
336 if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) {
e7aeea18
JB
337 this.chargingStation.setConfigurationKeyValue(
338 OCPP16StandardParametersKey.HeartbeatInterval,
339 commandPayload.value
340 );
c0560973
JB
341 triggerHeartbeatRestart = true;
342 }
343 if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) {
e7aeea18
JB
344 this.chargingStation.setConfigurationKeyValue(
345 OCPP16StandardParametersKey.HeartBeatInterval,
346 commandPayload.value
347 );
c0560973
JB
348 triggerHeartbeatRestart = true;
349 }
350 if (triggerHeartbeatRestart) {
351 this.chargingStation.restartHeartbeat();
352 }
353 if (keyToChange.key === OCPP16StandardParametersKey.WebSocketPingInterval && valueChanged) {
354 this.chargingStation.restartWebSocketPing();
355 }
356 if (keyToChange.reboot) {
357 return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED;
358 }
359 return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED;
360 }
361 }
362
e7aeea18
JB
363 private handleRequestSetChargingProfile(
364 commandPayload: SetChargingProfileRequest
365 ): SetChargingProfileResponse {
734d790d 366 if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) {
e7aeea18
JB
367 logger.error(
368 `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
369 commandPayload.connectorId
370 }`
371 );
c0560973
JB
372 return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
373 }
e7aeea18
JB
374 if (
375 commandPayload.csChargingProfiles.chargingProfilePurpose ===
376 ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE &&
377 commandPayload.connectorId !== 0
378 ) {
c0560973
JB
379 return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
380 }
e7aeea18
JB
381 if (
382 commandPayload.csChargingProfiles.chargingProfilePurpose ===
383 ChargingProfilePurposeType.TX_PROFILE &&
384 (commandPayload.connectorId === 0 ||
385 !this.chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted)
386 ) {
c0560973
JB
387 return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
388 }
e7aeea18
JB
389 this.chargingStation.setChargingProfile(
390 commandPayload.connectorId,
391 commandPayload.csChargingProfiles
392 );
393 logger.debug(
394 `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`,
395 this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
396 );
c0560973
JB
397 return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED;
398 }
399
e7aeea18
JB
400 private handleRequestClearChargingProfile(
401 commandPayload: ClearChargingProfileRequest
402 ): ClearChargingProfileResponse {
734d790d 403 if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) {
e7aeea18
JB
404 logger.error(
405 `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
406 commandPayload.connectorId
407 }`
408 );
c0560973
JB
409 return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
410 }
e7aeea18
JB
411 if (
412 commandPayload.connectorId &&
413 !Utils.isEmptyArray(
414 this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
415 )
416 ) {
734d790d 417 this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles = [];
e7aeea18
JB
418 logger.debug(
419 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
420 this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
421 );
c0560973
JB
422 return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
423 }
424 if (!commandPayload.connectorId) {
425 let clearedCP = false;
734d790d 426 for (const connectorId of this.chargingStation.connectors.keys()) {
e7aeea18
JB
427 if (
428 !Utils.isEmptyArray(this.chargingStation.getConnectorStatus(connectorId).chargingProfiles)
429 ) {
430 this.chargingStation
431 .getConnectorStatus(connectorId)
432 .chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => {
433 let clearCurrentCP = false;
434 if (chargingProfile.chargingProfileId === commandPayload.id) {
435 clearCurrentCP = true;
436 }
437 if (
438 !commandPayload.chargingProfilePurpose &&
439 chargingProfile.stackLevel === commandPayload.stackLevel
440 ) {
441 clearCurrentCP = true;
442 }
443 if (
444 !chargingProfile.stackLevel &&
445 chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose
446 ) {
447 clearCurrentCP = true;
448 }
449 if (
450 chargingProfile.stackLevel === commandPayload.stackLevel &&
451 chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose
452 ) {
453 clearCurrentCP = true;
454 }
455 if (clearCurrentCP) {
456 this.chargingStation.getConnectorStatus(
457 commandPayload.connectorId
458 ).chargingProfiles[index] = {} as OCPP16ChargingProfile;
459 logger.debug(
460 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
461 this.chargingStation.getConnectorStatus(commandPayload.connectorId)
462 .chargingProfiles
463 );
464 clearedCP = true;
465 }
466 });
c0560973
JB
467 }
468 }
469 if (clearedCP) {
470 return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
471 }
472 }
473 return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
474 }
475
e7aeea18
JB
476 private async handleRequestChangeAvailability(
477 commandPayload: ChangeAvailabilityRequest
478 ): Promise<ChangeAvailabilityResponse> {
c0560973 479 const connectorId: number = commandPayload.connectorId;
734d790d 480 if (!this.chargingStation.getConnectorStatus(connectorId)) {
e7aeea18
JB
481 logger.error(
482 `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
483 );
c0560973
JB
484 return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
485 }
e7aeea18
JB
486 const chargePointStatus: OCPP16ChargePointStatus =
487 commandPayload.type === OCPP16AvailabilityType.OPERATIVE
488 ? OCPP16ChargePointStatus.AVAILABLE
489 : OCPP16ChargePointStatus.UNAVAILABLE;
c0560973
JB
490 if (connectorId === 0) {
491 let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
734d790d
JB
492 for (const id of this.chargingStation.connectors.keys()) {
493 if (this.chargingStation.getConnectorStatus(id)?.transactionStarted) {
c0560973
JB
494 response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
495 }
734d790d 496 this.chargingStation.getConnectorStatus(id).availability = commandPayload.type;
c0560973 497 if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
93b4a429
JB
498 await this.chargingStation.ocppRequestService.sendMessageHandler(
499 OCPP16RequestCommand.STATUS_NOTIFICATION,
500 {
501 connectorId: id,
502 status: chargePointStatus,
503 errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
504 }
e7aeea18 505 );
734d790d 506 this.chargingStation.getConnectorStatus(id).status = chargePointStatus;
c0560973
JB
507 }
508 }
509 return response;
e7aeea18
JB
510 } else if (
511 connectorId > 0 &&
512 (this.chargingStation.getConnectorStatus(0).availability ===
513 OCPP16AvailabilityType.OPERATIVE ||
514 (this.chargingStation.getConnectorStatus(0).availability ===
515 OCPP16AvailabilityType.INOPERATIVE &&
516 commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))
517 ) {
734d790d
JB
518 if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
519 this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
c0560973
JB
520 return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
521 }
734d790d 522 this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
93b4a429
JB
523 await this.chargingStation.ocppRequestService.sendMessageHandler(
524 OCPP16RequestCommand.STATUS_NOTIFICATION,
525 { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR }
e7aeea18 526 );
734d790d 527 this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus;
c0560973
JB
528 return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
529 }
530 return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
531 }
532
e7aeea18
JB
533 private async handleRequestRemoteStartTransaction(
534 commandPayload: RemoteStartTransactionRequest
535 ): Promise<DefaultResponse> {
a7fc8211
JB
536 const transactionConnectorId: number = commandPayload.connectorId;
537 if (transactionConnectorId) {
93b4a429
JB
538 await this.chargingStation.ocppRequestService.sendMessageHandler(
539 OCPP16RequestCommand.STATUS_NOTIFICATION,
540 {
541 connectorId: transactionConnectorId,
542 status: OCPP16ChargePointStatus.PREPARING,
543 errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
544 }
e7aeea18
JB
545 );
546 this.chargingStation.getConnectorStatus(transactionConnectorId).status =
547 OCPP16ChargePointStatus.PREPARING;
548 if (
549 this.chargingStation.isChargingStationAvailable() &&
550 this.chargingStation.isConnectorAvailable(transactionConnectorId)
551 ) {
e060fe58 552 // Check if authorized
a7fc8211
JB
553 if (this.chargingStation.getAuthorizeRemoteTxRequests()) {
554 let authorized = false;
e7aeea18
JB
555 if (
556 this.chargingStation.getLocalAuthListEnabled() &&
557 this.chargingStation.hasAuthorizedTags() &&
558 this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)
559 ) {
560 this.chargingStation.getConnectorStatus(transactionConnectorId).localAuthorizeIdTag =
561 commandPayload.idTag;
562 this.chargingStation.getConnectorStatus(transactionConnectorId).idTagLocalAuthorized =
563 true;
36f6a92e 564 authorized = true;
71068fb9 565 } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) {
2e3d65ae
JB
566 this.chargingStation.getConnectorStatus(transactionConnectorId).authorizeIdTag =
567 commandPayload.idTag;
568 const authorizeResponse: OCPP16AuthorizeResponse =
569 (await this.chargingStation.ocppRequestService.sendMessageHandler(
570 OCPP16RequestCommand.AUTHORIZE,
571 {
572 idTag: commandPayload.idTag,
573 }
574 )) as OCPP16AuthorizeResponse;
a7fc8211
JB
575 if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
576 authorized = true;
a7fc8211 577 }
71068fb9 578 } else {
e7aeea18
JB
579 logger.warn(
580 `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
581 );
a7fc8211
JB
582 }
583 if (authorized) {
584 // Authorization successful, start transaction
e7aeea18
JB
585 if (
586 this.setRemoteStartTransactionChargingProfile(
587 transactionConnectorId,
588 commandPayload.chargingProfile
589 )
590 ) {
591 this.chargingStation.getConnectorStatus(
592 transactionConnectorId
593 ).transactionRemoteStarted = true;
594 if (
595 (
e7454a1f
JB
596 (await this.chargingStation.ocppRequestService.sendMessageHandler(
597 OCPP16RequestCommand.START_TRANSACTION,
598 {
599 connectorId: transactionConnectorId,
600 idTag: commandPayload.idTag,
601 }
602 )) as OCPP16StartTransactionResponse
e7aeea18
JB
603 ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
604 ) {
605 logger.debug(
606 this.chargingStation.logPrefix() +
607 ' Transaction remotely STARTED on ' +
608 this.chargingStation.stationInfo.chargingStationId +
609 '#' +
610 transactionConnectorId.toString() +
611 ' for idTag ' +
612 commandPayload.idTag
613 );
e060fe58
JB
614 return Constants.OCPP_RESPONSE_ACCEPTED;
615 }
e7aeea18
JB
616 return this.notifyRemoteStartTransactionRejected(
617 transactionConnectorId,
618 commandPayload.idTag
619 );
e060fe58 620 }
e7aeea18
JB
621 return this.notifyRemoteStartTransactionRejected(
622 transactionConnectorId,
623 commandPayload.idTag
624 );
a7fc8211 625 }
e7aeea18
JB
626 return this.notifyRemoteStartTransactionRejected(
627 transactionConnectorId,
628 commandPayload.idTag
629 );
36f6a92e 630 }
a7fc8211 631 // No authorization check required, start transaction
e7aeea18
JB
632 if (
633 this.setRemoteStartTransactionChargingProfile(
634 transactionConnectorId,
635 commandPayload.chargingProfile
636 )
637 ) {
638 this.chargingStation.getConnectorStatus(transactionConnectorId).transactionRemoteStarted =
639 true;
640 if (
641 (
e7454a1f
JB
642 (await this.chargingStation.ocppRequestService.sendMessageHandler(
643 OCPP16RequestCommand.START_TRANSACTION,
644 {
645 connectorId: transactionConnectorId,
646 idTag: commandPayload.idTag,
647 }
648 )) as OCPP16StartTransactionResponse
e7aeea18
JB
649 ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
650 ) {
651 logger.debug(
652 this.chargingStation.logPrefix() +
653 ' Transaction remotely STARTED on ' +
654 this.chargingStation.stationInfo.chargingStationId +
655 '#' +
656 transactionConnectorId.toString() +
657 ' for idTag ' +
658 commandPayload.idTag
659 );
e060fe58
JB
660 return Constants.OCPP_RESPONSE_ACCEPTED;
661 }
e7aeea18
JB
662 return this.notifyRemoteStartTransactionRejected(
663 transactionConnectorId,
664 commandPayload.idTag
665 );
e060fe58 666 }
e7aeea18
JB
667 return this.notifyRemoteStartTransactionRejected(
668 transactionConnectorId,
669 commandPayload.idTag
670 );
c0560973 671 }
e7aeea18
JB
672 return this.notifyRemoteStartTransactionRejected(
673 transactionConnectorId,
674 commandPayload.idTag
675 );
c0560973 676 }
57939a9d 677 return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
a7fc8211
JB
678 }
679
e7aeea18
JB
680 private async notifyRemoteStartTransactionRejected(
681 connectorId: number,
682 idTag: string
683 ): Promise<DefaultResponse> {
684 if (
685 this.chargingStation.getConnectorStatus(connectorId).status !==
686 OCPP16ChargePointStatus.AVAILABLE
687 ) {
93b4a429
JB
688 await this.chargingStation.ocppRequestService.sendMessageHandler(
689 OCPP16RequestCommand.STATUS_NOTIFICATION,
690 {
691 connectorId,
692 status: OCPP16ChargePointStatus.AVAILABLE,
693 errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
694 }
e7aeea18
JB
695 );
696 this.chargingStation.getConnectorStatus(connectorId).status =
697 OCPP16ChargePointStatus.AVAILABLE;
e060fe58 698 }
e7aeea18
JB
699 logger.warn(
700 this.chargingStation.logPrefix() +
701 ' Remote starting transaction REJECTED on connector Id ' +
702 connectorId.toString() +
703 ', idTag ' +
704 idTag +
705 ', availability ' +
706 this.chargingStation.getConnectorStatus(connectorId).availability +
707 ', status ' +
708 this.chargingStation.getConnectorStatus(connectorId).status
709 );
c0560973
JB
710 return Constants.OCPP_RESPONSE_REJECTED;
711 }
712
e7aeea18
JB
713 private setRemoteStartTransactionChargingProfile(
714 connectorId: number,
715 cp: OCPP16ChargingProfile
716 ): boolean {
a7fc8211
JB
717 if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) {
718 this.chargingStation.setChargingProfile(connectorId, cp);
e7aeea18
JB
719 logger.debug(
720 `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`,
721 this.chargingStation.getConnectorStatus(connectorId).chargingProfiles
722 );
a7fc8211
JB
723 return true;
724 } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) {
e7aeea18
JB
725 logger.warn(
726 `${this.chargingStation.logPrefix()} Not allowed to set ${
727 cp.chargingProfilePurpose
728 } charging profile(s) at remote start transaction`
729 );
a7fc8211 730 return false;
e060fe58
JB
731 } else if (!cp) {
732 return true;
a7fc8211
JB
733 }
734 }
735
e7aeea18
JB
736 private async handleRequestRemoteStopTransaction(
737 commandPayload: RemoteStopTransactionRequest
738 ): Promise<DefaultResponse> {
c0560973 739 const transactionId = commandPayload.transactionId;
734d790d 740 for (const connectorId of this.chargingStation.connectors.keys()) {
e7aeea18
JB
741 if (
742 connectorId > 0 &&
743 this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
744 ) {
93b4a429
JB
745 await this.chargingStation.ocppRequestService.sendMessageHandler(
746 OCPP16RequestCommand.STATUS_NOTIFICATION,
747 {
748 connectorId,
749 status: OCPP16ChargePointStatus.FINISHING,
750 errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
751 }
e7aeea18
JB
752 );
753 this.chargingStation.getConnectorStatus(connectorId).status =
754 OCPP16ChargePointStatus.FINISHING;
68c993d5
JB
755 if (
756 this.chargingStation.getBeginEndMeterValues() &&
757 this.chargingStation.getOcppStrictCompliance() &&
758 !this.chargingStation.getOutOfOrderEndMeterValues()
759 ) {
760 // FIXME: Implement OCPP version agnostic helpers
761 const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
762 this.chargingStation,
763 connectorId,
764 this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
765 );
3a33b6a9
JB
766 await this.chargingStation.ocppRequestService.sendMessageHandler(
767 OCPP16RequestCommand.METER_VALUES,
768 {
769 connectorId,
770 transactionId,
771 meterValue: transactionEndMeterValue,
772 }
68c993d5
JB
773 );
774 }
775 await this.chargingStation.ocppRequestService.sendMessageHandler(
776 OCPP16RequestCommand.STOP_TRANSACTION,
777 {
778 transactionId,
779 meterStop:
780 this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
781 idTag: this.chargingStation.getTransactionIdTag(transactionId),
782 }
e7aeea18 783 );
c0560973
JB
784 return Constants.OCPP_RESPONSE_ACCEPTED;
785 }
786 }
e7aeea18
JB
787 logger.info(
788 this.chargingStation.logPrefix() +
789 ' Trying to remote stop a non existing transaction ' +
790 transactionId.toString()
791 );
c0560973
JB
792 return Constants.OCPP_RESPONSE_REJECTED;
793 }
47e22477 794
e7aeea18
JB
795 private async handleRequestGetDiagnostics(
796 commandPayload: GetDiagnosticsRequest
797 ): Promise<GetDiagnosticsResponse> {
798 logger.debug(
799 this.chargingStation.logPrefix() +
800 ' ' +
801 OCPP16IncomingRequestCommand.GET_DIAGNOSTICS +
802 ' request received: %j',
803 commandPayload
804 );
a3868ec4 805 const uri = new URL(commandPayload.location);
47e22477
JB
806 if (uri.protocol.startsWith('ftp:')) {
807 let ftpClient: Client;
808 try {
e7aeea18
JB
809 const logFiles = fs
810 .readdirSync(path.resolve(__dirname, '../../../../'))
811 .filter((file) => file.endsWith('.log'))
812 .map((file) => path.join('./', file));
813 const diagnosticsArchive =
814 this.chargingStation.stationInfo.chargingStationId + '_logs.tar.gz';
47e22477
JB
815 tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive));
816 ftpClient = new Client();
817 const accessResponse = await ftpClient.access({
818 host: uri.host,
e8191622
JB
819 ...(!Utils.isEmptyString(uri.port) && { port: Utils.convertToInt(uri.port) }),
820 ...(!Utils.isEmptyString(uri.username) && { user: uri.username }),
821 ...(!Utils.isEmptyString(uri.password) && { password: uri.password }),
47e22477
JB
822 });
823 let uploadResponse: FTPResponse;
824 if (accessResponse.code === 220) {
825 // eslint-disable-next-line @typescript-eslint/no-misused-promises
826 ftpClient.trackProgress(async (info) => {
e7aeea18
JB
827 logger.info(
828 `${this.chargingStation.logPrefix()} ${
829 info.bytes / 1024
830 } bytes transferred from diagnostics archive ${info.name}`
831 );
bad85ee4
JB
832 await this.chargingStation.ocppRequestService.sendMessageHandler(
833 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
834 {
835 status: OCPP16DiagnosticsStatus.Uploading,
836 }
e7aeea18 837 );
47e22477 838 });
e7aeea18
JB
839 uploadResponse = await ftpClient.uploadFrom(
840 path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive),
841 uri.pathname + diagnosticsArchive
842 );
47e22477 843 if (uploadResponse.code === 226) {
bad85ee4
JB
844 await this.chargingStation.ocppRequestService.sendMessageHandler(
845 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
846 {
847 status: OCPP16DiagnosticsStatus.Uploaded,
848 }
e7aeea18 849 );
47e22477
JB
850 if (ftpClient) {
851 ftpClient.close();
852 }
853 return { fileName: diagnosticsArchive };
854 }
e7aeea18
JB
855 throw new OCPPError(
856 ErrorType.GENERIC_ERROR,
857 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
858 uploadResponse?.code && '|' + uploadResponse?.code.toString()
859 }`,
860 OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
861 );
47e22477 862 }
e7aeea18
JB
863 throw new OCPPError(
864 ErrorType.GENERIC_ERROR,
865 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
866 uploadResponse?.code && '|' + uploadResponse?.code.toString()
867 }`,
868 OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
869 );
47e22477 870 } catch (error) {
bad85ee4
JB
871 await this.chargingStation.ocppRequestService.sendMessageHandler(
872 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
873 {
874 status: OCPP16DiagnosticsStatus.UploadFailed,
875 }
e7aeea18 876 );
47e22477
JB
877 if (ftpClient) {
878 ftpClient.close();
879 }
e7aeea18
JB
880 return this.handleIncomingRequestError(
881 OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
882 error as Error,
883 { errorResponse: Constants.OCPP_RESPONSE_EMPTY }
884 );
47e22477
JB
885 }
886 } else {
e7aeea18
JB
887 logger.error(
888 `${this.chargingStation.logPrefix()} Unsupported protocol ${
889 uri.protocol
890 } to transfer the diagnostic logs archive`
891 );
bad85ee4
JB
892 await this.chargingStation.ocppRequestService.sendMessageHandler(
893 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
894 {
895 status: OCPP16DiagnosticsStatus.UploadFailed,
896 }
e7aeea18 897 );
47e22477
JB
898 return Constants.OCPP_RESPONSE_EMPTY;
899 }
900 }
802cfa13 901
e7aeea18
JB
902 private handleRequestTriggerMessage(
903 commandPayload: OCPP16TriggerMessageRequest
904 ): OCPP16TriggerMessageResponse {
802cfa13
JB
905 try {
906 switch (commandPayload.requestedMessage) {
907 case MessageTrigger.BootNotification:
908 setTimeout(() => {
e7aeea18 909 this.chargingStation.ocppRequestService
6a8b180d
JB
910 .sendMessageHandler(
911 OCPP16RequestCommand.BOOT_NOTIFICATION,
912 {
913 chargePointModel:
914 this.chargingStation.getBootNotificationRequest().chargePointModel,
915 chargePointVendor:
916 this.chargingStation.getBootNotificationRequest().chargePointVendor,
917 chargeBoxSerialNumber:
918 this.chargingStation.getBootNotificationRequest().chargeBoxSerialNumber,
919 firmwareVersion:
920 this.chargingStation.getBootNotificationRequest().firmwareVersion,
921 chargePointSerialNumber:
922 this.chargingStation.getBootNotificationRequest().chargePointSerialNumber,
923 iccid: this.chargingStation.getBootNotificationRequest().iccid,
924 imsi: this.chargingStation.getBootNotificationRequest().imsi,
925 meterSerialNumber:
926 this.chargingStation.getBootNotificationRequest().meterSerialNumber,
927 meterType: this.chargingStation.getBootNotificationRequest().meterType,
928 },
929 { skipBufferingOnError: true, triggerMessage: true }
e7aeea18
JB
930 )
931 .catch(() => {
932 /* This is intentional */
933 });
802cfa13
JB
934 }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
935 return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
936 case MessageTrigger.Heartbeat:
937 setTimeout(() => {
e7aeea18 938 this.chargingStation.ocppRequestService
94a464f9 939 .sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { triggerMessage: true })
e7aeea18
JB
940 .catch(() => {
941 /* This is intentional */
942 });
802cfa13
JB
943 }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
944 return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
945 default:
946 return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
947 }
948 } catch (error) {
e7aeea18
JB
949 return this.handleIncomingRequestError(
950 OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
951 error as Error,
952 { errorResponse: Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }
953 );
802cfa13
JB
954 }
955 }
c0560973 956}