+
+ private async handleRequestGetDiagnostics(commandPayload: GetDiagnosticsRequest): Promise<GetDiagnosticsResponse> {
+ logger.debug(this.chargingStation.logPrefix() + ' ' + IncomingRequestCommand.GET_DIAGNOSTICS + ' request received: %j', commandPayload);
+ const uri = new url.URL(commandPayload.location);
+ if (uri.protocol.startsWith('ftp:')) {
+ let ftpClient: Client;
+ try {
+ const logFiles = fs.readdirSync(path.resolve(__dirname, '../../../../')).filter((file) => file.endsWith('.log')).map((file) => path.join('./', file));
+ const diagnosticsArchive = this.chargingStation.stationInfo.chargingStationId + '_logs.tar.gz';
+ tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive));
+ ftpClient = new Client();
+ const accessResponse = await ftpClient.access({
+ host: uri.host,
+ ...(uri.port !== '') && { port: Utils.convertToInt(uri.port) },
+ ...(uri.username !== '') && { user: uri.username },
+ ...(uri.password !== '') && { password: uri.password },
+ });
+ let uploadResponse: FTPResponse;
+ if (accessResponse.code === 220) {
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ ftpClient.trackProgress(async (info) => {
+ logger.info(`${this.chargingStation.logPrefix()} ${info.bytes / 1024} bytes transferred from diagnostics archive ${info.name}`);
+ await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploading);
+ });
+ uploadResponse = await ftpClient.uploadFrom(path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), uri.pathname + diagnosticsArchive);
+ if (uploadResponse.code === 226) {
+ await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploaded);
+ if (ftpClient) {
+ ftpClient.close();
+ }
+ return { fileName: diagnosticsArchive };
+ }
+ throw Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
+ }
+ throw Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
+ } catch (error) {
+ await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed);
+ this.handleIncomingRequestError(IncomingRequestCommand.GET_DIAGNOSTICS, error);
+ if (ftpClient) {
+ ftpClient.close();
+ }
+ return Constants.OCPP_RESPONSE_EMPTY;
+ }
+ } else {
+ logger.error(`${this.chargingStation.logPrefix()} Unsupported protocol ${uri.protocol} to transfer the diagnostic logs archive`);
+ await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed);
+ return Constants.OCPP_RESPONSE_EMPTY;
+ }
+ }