| 1 | import * as url from 'url'; |
| 2 | |
| 3 | import { ChangeAvailabilityRequest, ChangeConfigurationRequest, ClearChargingProfileRequest, GetConfigurationRequest, GetDiagnosticsRequest, OCPP16AvailabilityType, OCPP16IncomingRequestCommand, RemoteStartTransactionRequest, RemoteStopTransactionRequest, ResetRequest, SetChargingProfileRequest, UnlockConnectorRequest } from '../../../types/ocpp/1.6/Requests'; |
| 4 | import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, GetConfigurationResponse, GetDiagnosticsResponse, SetChargingProfileResponse, UnlockConnectorResponse } from '../../../types/ocpp/1.6/Responses'; |
| 5 | import { ChargingProfilePurposeType, OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; |
| 6 | import { Client, FTPResponse } from 'basic-ftp'; |
| 7 | import { IncomingRequestCommand, RequestCommand } from '../../../types/ocpp/Requests'; |
| 8 | import { OCPP16AuthorizationStatus, OCPP16StopTransactionReason } from '../../../types/ocpp/1.6/Transaction'; |
| 9 | |
| 10 | import Constants from '../../../utils/Constants'; |
| 11 | import { DefaultResponse } from '../../../types/ocpp/Responses'; |
| 12 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 13 | import { MessageType } from '../../../types/ocpp/MessageType'; |
| 14 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; |
| 15 | import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; |
| 16 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
| 17 | import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; |
| 18 | import OCPPError from '../../OCPPError'; |
| 19 | import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; |
| 20 | import Utils from '../../../utils/Utils'; |
| 21 | import fs from 'fs'; |
| 22 | import logger from '../../../utils/Logger'; |
| 23 | import path from 'path'; |
| 24 | import tar from 'tar'; |
| 25 | |
| 26 | export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { |
| 27 | public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> { |
| 28 | let response; |
| 29 | const methodName = `handleRequest${commandName}`; |
| 30 | // Call |
| 31 | if (typeof this[methodName] === 'function') { |
| 32 | try { |
| 33 | // Call the method to build the response |
| 34 | response = await this[methodName](commandPayload); |
| 35 | } catch (error) { |
| 36 | // Log |
| 37 | logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error); |
| 38 | // Send back an error response to inform backend |
| 39 | await this.chargingStation.ocppRequestService.sendError(messageId, error, commandName); |
| 40 | throw error; |
| 41 | } |
| 42 | } else { |
| 43 | // Throw exception |
| 44 | await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented`, {}), commandName); |
| 45 | throw new Error(`${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload)}`); |
| 46 | } |
| 47 | // Send the built response |
| 48 | await this.chargingStation.ocppRequestService.sendMessage(messageId, response, MessageType.CALL_RESULT_MESSAGE, commandName); |
| 49 | } |
| 50 | |
| 51 | // Simulate charging station restart |
| 52 | private handleRequestReset(commandPayload: ResetRequest): DefaultResponse { |
| 53 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 54 | setImmediate(async (): Promise<void> => { |
| 55 | await this.chargingStation.stop(commandPayload.type + 'Reset' as OCPP16StopTransactionReason); |
| 56 | await Utils.sleep(this.chargingStation.stationInfo.resetTime); |
| 57 | this.chargingStation.start(); |
| 58 | }); |
| 59 | logger.info(`${this.chargingStation.logPrefix()} ${commandPayload.type} reset command received, simulating it. The station will be back online in ${Utils.milliSecondsToHHMMSS(this.chargingStation.stationInfo.resetTime)}`); |
| 60 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 61 | } |
| 62 | |
| 63 | private handleRequestClearCache(): DefaultResponse { |
| 64 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 65 | } |
| 66 | |
| 67 | private async handleRequestUnlockConnector(commandPayload: UnlockConnectorRequest): Promise<UnlockConnectorResponse> { |
| 68 | const connectorId = commandPayload.connectorId; |
| 69 | if (connectorId === 0) { |
| 70 | logger.error(this.chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString()); |
| 71 | return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; |
| 72 | } |
| 73 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { |
| 74 | const transactionId = this.chargingStation.getConnector(connectorId).transactionId; |
| 75 | const stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, |
| 76 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), |
| 77 | this.chargingStation.getTransactionIdTag(transactionId), |
| 78 | OCPP16StopTransactionReason.UNLOCK_COMMAND); |
| 79 | if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 80 | return Constants.OCPP_RESPONSE_UNLOCKED; |
| 81 | } |
| 82 | return Constants.OCPP_RESPONSE_UNLOCK_FAILED; |
| 83 | } |
| 84 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); |
| 85 | this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
| 86 | return Constants.OCPP_RESPONSE_UNLOCKED; |
| 87 | } |
| 88 | |
| 89 | private handleRequestGetConfiguration(commandPayload: GetConfigurationRequest): GetConfigurationResponse { |
| 90 | const configurationKey: OCPPConfigurationKey[] = []; |
| 91 | const unknownKey: string[] = []; |
| 92 | if (Utils.isEmptyArray(commandPayload.key)) { |
| 93 | for (const configuration of this.chargingStation.configuration.configurationKey) { |
| 94 | if (Utils.isUndefined(configuration.visible)) { |
| 95 | configuration.visible = true; |
| 96 | } |
| 97 | if (!configuration.visible) { |
| 98 | continue; |
| 99 | } |
| 100 | configurationKey.push({ |
| 101 | key: configuration.key, |
| 102 | readonly: configuration.readonly, |
| 103 | value: configuration.value, |
| 104 | }); |
| 105 | } |
| 106 | } else { |
| 107 | for (const key of commandPayload.key) { |
| 108 | const keyFound = this.chargingStation.getConfigurationKey(key); |
| 109 | if (keyFound) { |
| 110 | if (Utils.isUndefined(keyFound.visible)) { |
| 111 | keyFound.visible = true; |
| 112 | } |
| 113 | if (!keyFound.visible) { |
| 114 | continue; |
| 115 | } |
| 116 | configurationKey.push({ |
| 117 | key: keyFound.key, |
| 118 | readonly: keyFound.readonly, |
| 119 | value: keyFound.value, |
| 120 | }); |
| 121 | } else { |
| 122 | unknownKey.push(key); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return { |
| 127 | configurationKey, |
| 128 | unknownKey, |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | private handleRequestChangeConfiguration(commandPayload: ChangeConfigurationRequest): ChangeConfigurationResponse { |
| 133 | // JSON request fields type sanity check |
| 134 | if (!Utils.isString(commandPayload.key)) { |
| 135 | logger.error(`${this.chargingStation.logPrefix()} ${RequestCommand.CHANGE_CONFIGURATION} request key field is not a string:`, commandPayload); |
| 136 | } |
| 137 | if (!Utils.isString(commandPayload.value)) { |
| 138 | logger.error(`${this.chargingStation.logPrefix()} ${RequestCommand.CHANGE_CONFIGURATION} request value field is not a string:`, commandPayload); |
| 139 | } |
| 140 | const keyToChange = this.chargingStation.getConfigurationKey(commandPayload.key, true); |
| 141 | if (!keyToChange) { |
| 142 | return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; |
| 143 | } else if (keyToChange && keyToChange.readonly) { |
| 144 | return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED; |
| 145 | } else if (keyToChange && !keyToChange.readonly) { |
| 146 | const keyIndex = this.chargingStation.configuration.configurationKey.indexOf(keyToChange); |
| 147 | let valueChanged = false; |
| 148 | if (this.chargingStation.configuration.configurationKey[keyIndex].value !== commandPayload.value) { |
| 149 | this.chargingStation.configuration.configurationKey[keyIndex].value = commandPayload.value; |
| 150 | valueChanged = true; |
| 151 | } |
| 152 | let triggerHeartbeatRestart = false; |
| 153 | if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) { |
| 154 | this.chargingStation.setConfigurationKeyValue(OCPP16StandardParametersKey.HeartbeatInterval, commandPayload.value); |
| 155 | triggerHeartbeatRestart = true; |
| 156 | } |
| 157 | if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) { |
| 158 | this.chargingStation.setConfigurationKeyValue(OCPP16StandardParametersKey.HeartBeatInterval, commandPayload.value); |
| 159 | triggerHeartbeatRestart = true; |
| 160 | } |
| 161 | if (triggerHeartbeatRestart) { |
| 162 | this.chargingStation.restartHeartbeat(); |
| 163 | } |
| 164 | if (keyToChange.key === OCPP16StandardParametersKey.WebSocketPingInterval && valueChanged) { |
| 165 | this.chargingStation.restartWebSocketPing(); |
| 166 | } |
| 167 | if (keyToChange.reboot) { |
| 168 | return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; |
| 169 | } |
| 170 | return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private handleRequestSetChargingProfile(commandPayload: SetChargingProfileRequest): SetChargingProfileResponse { |
| 175 | if (!this.chargingStation.getConnector(commandPayload.connectorId)) { |
| 176 | logger.error(`${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); |
| 177 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
| 178 | } |
| 179 | if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && commandPayload.connectorId !== 0) { |
| 180 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
| 181 | } |
| 182 | if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE && (commandPayload.connectorId === 0 || !this.chargingStation.getConnector(commandPayload.connectorId)?.transactionStarted)) { |
| 183 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
| 184 | } |
| 185 | this.chargingStation.setChargingProfile(commandPayload.connectorId, commandPayload.csChargingProfiles); |
| 186 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
| 187 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
| 188 | } |
| 189 | |
| 190 | private handleRequestClearChargingProfile(commandPayload: ClearChargingProfileRequest): ClearChargingProfileResponse { |
| 191 | if (!this.chargingStation.getConnector(commandPayload.connectorId)) { |
| 192 | logger.error(`${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); |
| 193 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
| 194 | } |
| 195 | if (commandPayload.connectorId && !Utils.isEmptyArray(this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles)) { |
| 196 | this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles = []; |
| 197 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
| 198 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
| 199 | } |
| 200 | if (!commandPayload.connectorId) { |
| 201 | let clearedCP = false; |
| 202 | for (const connector in this.chargingStation.connectors) { |
| 203 | if (!Utils.isEmptyArray(this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles)) { |
| 204 | this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { |
| 205 | let clearCurrentCP = false; |
| 206 | if (chargingProfile.chargingProfileId === commandPayload.id) { |
| 207 | clearCurrentCP = true; |
| 208 | } |
| 209 | if (!commandPayload.chargingProfilePurpose && chargingProfile.stackLevel === commandPayload.stackLevel) { |
| 210 | clearCurrentCP = true; |
| 211 | } |
| 212 | if (!chargingProfile.stackLevel && chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose) { |
| 213 | clearCurrentCP = true; |
| 214 | } |
| 215 | if (chargingProfile.stackLevel === commandPayload.stackLevel && chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose) { |
| 216 | clearCurrentCP = true; |
| 217 | } |
| 218 | if (clearCurrentCP) { |
| 219 | this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles[index] = {} as OCPP16ChargingProfile; |
| 220 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
| 221 | clearedCP = true; |
| 222 | } |
| 223 | }); |
| 224 | } |
| 225 | } |
| 226 | if (clearedCP) { |
| 227 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
| 228 | } |
| 229 | } |
| 230 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
| 231 | } |
| 232 | |
| 233 | private async handleRequestChangeAvailability(commandPayload: ChangeAvailabilityRequest): Promise<ChangeAvailabilityResponse> { |
| 234 | const connectorId: number = commandPayload.connectorId; |
| 235 | if (!this.chargingStation.getConnector(connectorId)) { |
| 236 | logger.error(`${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`); |
| 237 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; |
| 238 | } |
| 239 | const chargePointStatus: OCPP16ChargePointStatus = commandPayload.type === OCPP16AvailabilityType.OPERATIVE ? OCPP16ChargePointStatus.AVAILABLE : OCPP16ChargePointStatus.UNAVAILABLE; |
| 240 | if (connectorId === 0) { |
| 241 | let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; |
| 242 | for (const connector in this.chargingStation.connectors) { |
| 243 | if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { |
| 244 | response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
| 245 | } |
| 246 | this.chargingStation.getConnector(Utils.convertToInt(connector)).availability = commandPayload.type; |
| 247 | if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { |
| 248 | await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), chargePointStatus); |
| 249 | this.chargingStation.getConnector(Utils.convertToInt(connector)).status = chargePointStatus; |
| 250 | } |
| 251 | } |
| 252 | return response; |
| 253 | } else if (connectorId > 0 && (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.OPERATIVE || (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.INOPERATIVE && commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))) { |
| 254 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { |
| 255 | this.chargingStation.getConnector(connectorId).availability = commandPayload.type; |
| 256 | return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
| 257 | } |
| 258 | this.chargingStation.getConnector(connectorId).availability = commandPayload.type; |
| 259 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, chargePointStatus); |
| 260 | this.chargingStation.getConnector(connectorId).status = chargePointStatus; |
| 261 | return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; |
| 262 | } |
| 263 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; |
| 264 | } |
| 265 | |
| 266 | private async handleRequestRemoteStartTransaction(commandPayload: RemoteStartTransactionRequest): Promise<DefaultResponse> { |
| 267 | const transactionConnectorId: number = commandPayload.connectorId; |
| 268 | if (transactionConnectorId) { |
| 269 | await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.PREPARING); |
| 270 | this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING; |
| 271 | if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorId)) { |
| 272 | // Check if authorized |
| 273 | if (this.chargingStation.getAuthorizeRemoteTxRequests()) { |
| 274 | let authorized = false; |
| 275 | if (this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags() |
| 276 | && this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)) { |
| 277 | authorized = true; |
| 278 | } |
| 279 | if (!authorized || (authorized && this.chargingStation.getMayAuthorizeAtRemoteStart())) { |
| 280 | const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(transactionConnectorId, commandPayload.idTag); |
| 281 | if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 282 | authorized = true; |
| 283 | } else { |
| 284 | authorized = false; |
| 285 | } |
| 286 | } |
| 287 | if (authorized) { |
| 288 | // Authorization successful, start transaction |
| 289 | if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { |
| 290 | if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 291 | logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); |
| 292 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 293 | } |
| 294 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 295 | } |
| 296 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 297 | } |
| 298 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 299 | } |
| 300 | // No authorization check required, start transaction |
| 301 | if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { |
| 302 | if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 303 | logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); |
| 304 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 305 | } |
| 306 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 307 | } |
| 308 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 309 | } |
| 310 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 311 | } |
| 312 | return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
| 313 | } |
| 314 | |
| 315 | private async notifyRemoteStartTransactionRejected(connectorId: number, idTag: string): Promise<DefaultResponse> { |
| 316 | if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { |
| 317 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); |
| 318 | this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
| 319 | } |
| 320 | logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', status ' + this.chargingStation.getConnector(connectorId).status); |
| 321 | return Constants.OCPP_RESPONSE_REJECTED; |
| 322 | } |
| 323 | |
| 324 | private setRemoteStartTransactionChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): boolean { |
| 325 | if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { |
| 326 | this.chargingStation.setChargingProfile(connectorId, cp); |
| 327 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnector(connectorId).chargingProfiles); |
| 328 | return true; |
| 329 | } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { |
| 330 | logger.warn(`${this.chargingStation.logPrefix()} Not allowed to set ${cp.chargingProfilePurpose} charging profile(s) at remote start transaction`); |
| 331 | return false; |
| 332 | } else if (!cp) { |
| 333 | return true; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private async handleRequestRemoteStopTransaction(commandPayload: RemoteStopTransactionRequest): Promise<DefaultResponse> { |
| 338 | const transactionId = commandPayload.transactionId; |
| 339 | for (const connector in this.chargingStation.connectors) { |
| 340 | if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === transactionId) { |
| 341 | await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), OCPP16ChargePointStatus.FINISHING); |
| 342 | this.chargingStation.getConnector(Utils.convertToInt(connector)).status = OCPP16ChargePointStatus.FINISHING; |
| 343 | await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), |
| 344 | this.chargingStation.getTransactionIdTag(transactionId)); |
| 345 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 346 | } |
| 347 | } |
| 348 | logger.info(this.chargingStation.logPrefix() + ' Trying to remote stop a non existing transaction ' + transactionId.toString()); |
| 349 | return Constants.OCPP_RESPONSE_REJECTED; |
| 350 | } |
| 351 | |
| 352 | private async handleRequestGetDiagnostics(commandPayload: GetDiagnosticsRequest): Promise<GetDiagnosticsResponse> { |
| 353 | logger.debug(this.chargingStation.logPrefix() + ' ' + IncomingRequestCommand.GET_DIAGNOSTICS + ' request received: %j', commandPayload); |
| 354 | const uri = new url.URL(commandPayload.location); |
| 355 | if (uri.protocol.startsWith('ftp:')) { |
| 356 | let ftpClient: Client; |
| 357 | try { |
| 358 | const logFiles = fs.readdirSync(path.resolve(__dirname, '../../../../')).filter((file) => file.endsWith('.log')).map((file) => path.join('./', file)); |
| 359 | const diagnosticsArchive = this.chargingStation.stationInfo.chargingStationId + '_logs.tar.gz'; |
| 360 | tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive)); |
| 361 | ftpClient = new Client(); |
| 362 | const accessResponse = await ftpClient.access({ |
| 363 | host: uri.host, |
| 364 | ...(uri.port !== '') && { port: Utils.convertToInt(uri.port) }, |
| 365 | ...(uri.username !== '') && { user: uri.username }, |
| 366 | ...(uri.password !== '') && { password: uri.password }, |
| 367 | }); |
| 368 | let uploadResponse: FTPResponse; |
| 369 | if (accessResponse.code === 220) { |
| 370 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 371 | ftpClient.trackProgress(async (info) => { |
| 372 | logger.info(`${this.chargingStation.logPrefix()} ${info.bytes / 1024} bytes transferred from diagnostics archive ${info.name}`); |
| 373 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploading); |
| 374 | }); |
| 375 | uploadResponse = await ftpClient.uploadFrom(path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), uri.pathname + diagnosticsArchive); |
| 376 | if (uploadResponse.code === 226) { |
| 377 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploaded); |
| 378 | if (ftpClient) { |
| 379 | ftpClient.close(); |
| 380 | } |
| 381 | return { fileName: diagnosticsArchive }; |
| 382 | } |
| 383 | throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`); |
| 384 | } |
| 385 | throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`); |
| 386 | } catch (error) { |
| 387 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed); |
| 388 | this.handleIncomingRequestError(IncomingRequestCommand.GET_DIAGNOSTICS, error); |
| 389 | if (ftpClient) { |
| 390 | ftpClient.close(); |
| 391 | } |
| 392 | return Constants.OCPP_RESPONSE_EMPTY; |
| 393 | } |
| 394 | } else { |
| 395 | logger.error(`${this.chargingStation.logPrefix()} Unsupported protocol ${uri.protocol} to transfer the diagnostic logs archive`); |
| 396 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed); |
| 397 | return Constants.OCPP_RESPONSE_EMPTY; |
| 398 | } |
| 399 | } |
| 400 | } |