From 8530f7538ff8b743084ea9a945eb501f18bf1a7b Mon Sep 17 00:00:00 2001 From: Armin Date: Mon, 24 Jun 2024 14:38:10 +0200 Subject: [PATCH] Upgrade to serializer 4.0.0-alpha --- build.gradle.kts | 2 +- .../de/cyface/uploader/DefaultUploader.kt | 65 +++++++++---------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 226f9e7..6063a16 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,7 +62,7 @@ version = "0.0.0" // Automatically overwritten by CI // Versions of dependencies extra["slf4jVersion"] = "2.0.7" // TODO: Check if we can remove this after we port the new auth from android-backend to this library -extra["cyfaceSerializationVersion"] = "3.4.0-alpha2" // FIXME +extra["cyfaceSerializationVersion"] = "4.0.0-alpha2" // FIXME extra["googleApiClientVersion"] = "2.2.0" // transmission protocol extra["gradleWrapperVersion"] = "7.6.1" diff --git a/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt b/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt index ead4cca..f699eb3 100644 --- a/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt +++ b/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt @@ -73,7 +73,7 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { @Suppress("unused", "CyclomaticComplexMethod", "LongMethod") // Part of the API override fun uploadMeasurement( jwtToken: String, - metaData: RequestMetaData, + metaData: RequestMetaData, file: File, progressListener: UploadProgressListener ): Result { @@ -83,13 +83,13 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { override fun uploadAttachment( jwtToken: String, - metaData: RequestMetaData, + metaData: RequestMetaData, file: File, fileName: String, progressListener: UploadProgressListener, ): Result { - val measurementId = metaData.measurementIdentifier.toLong() - val deviceId = metaData.deviceIdentifier + val measurementId = metaData.identifier.measurementId.toLong() + val deviceId = metaData.identifier.deviceId val endpoint = attachmentsEndpoint(deviceId, measurementId) return uploadFile(jwtToken, metaData, file, endpoint, progressListener) } @@ -103,9 +103,9 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { } @Throws(UploadFailed::class) - private fun uploadFile( + private fun uploadFile( jwtToken: String, - metaData: RequestMetaData, + metaData: RequestMetaData, file: File, endpoint: URL, progressListener: UploadProgressListener @@ -149,9 +149,9 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { } } - private fun initializeUploader( + private fun initializeUploader( jwtToken: String, - metaData: RequestMetaData, + metaData: RequestMetaData, fileInputStream: FileInputStream, file: File ): MediaHttpUploader { @@ -212,6 +212,7 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { // InterruptedIOException while reading the response. Try again later. throw UploadFailed(SynchronisationException(exception)) } + is IOException -> handleIOException(exception) // File is too large to be uploaded. Handle in caller (e.g. skip the upload). // The max size is currently static and set to 100 MB which should be about 44 hours of 100 Hz measurement. @@ -310,15 +311,9 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { } } - internal class ProgressHandler(progressListener: UploadProgressListener) : + internal class ProgressHandler(private val progressListener: UploadProgressListener) : MediaHttpUploaderProgressListener { - private val progressListener: UploadProgressListener - - init { - this.progressListener = progressListener - } - @Throws(IOException::class) override fun progressChanged(uploader: MediaHttpUploader) { LOGGER.debug("progress: ${uploader.progress}, uploaded: ${uploader.numBytesUploaded} Bytes") @@ -410,40 +405,44 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { * @param metaData The metadata to convert. * @return The meta data as `HttpContent`. */ - fun preRequestBody(metaData: RequestMetaData): Map { + fun preRequestBody(metaData: RequestMetaData): + Map { val attributes: MutableMap = HashMap() // Location meta data - metaData.startLocation?.let { startLocation -> + metaData.measurementMetaData.startLocation?.let { startLocation -> attributes["startLocLat"] = startLocation.latitude.toString() attributes["startLocLon"] = startLocation.longitude.toString() attributes["startLocTS"] = startLocation.timestamp.toString() } - metaData.endLocation?.let { endLocation -> + metaData.measurementMetaData.endLocation?.let { endLocation -> attributes["endLocLat"] = endLocation.latitude.toString() attributes["endLocLon"] = endLocation.longitude.toString() attributes["endLocTS"] = endLocation.timestamp.toString() } - attributes["locationCount"] = metaData.locationCount.toString() + attributes["locationCount"] = metaData.measurementMetaData.locationCount.toString() // Attachment meta data - if (metaData.attachmentIdentifier != null) { - attributes["attachmentId"] = metaData.attachmentIdentifier.toString() + when (metaData.identifier) { + is RequestMetaData.AttachmentIdentifier -> { + val identifier = metaData.identifier as RequestMetaData.AttachmentIdentifier + attributes["attachmentId"] = identifier.attachmentId + } } - attributes["logCount"] = metaData.logCount.toString() - attributes["imageCount"] = metaData.imageCount.toString() - attributes["videoCount"] = metaData.videoCount.toString() - attributes["filesSize"] = metaData.filesSize.toString() + attributes["logCount"] = metaData.attachmentMetaData.logCount.toString() + attributes["imageCount"] = metaData.attachmentMetaData.imageCount.toString() + attributes["videoCount"] = metaData.attachmentMetaData.videoCount.toString() + attributes["filesSize"] = metaData.attachmentMetaData.filesSize.toString() // Remaining meta data - attributes["deviceId"] = metaData.deviceIdentifier - attributes["measurementId"] = metaData.measurementIdentifier - attributes["deviceType"] = metaData.deviceType - attributes["osVersion"] = metaData.operatingSystemVersion - attributes["appVersion"] = metaData.applicationVersion - attributes["length"] = metaData.length.toString() - attributes["modality"] = metaData.modality - attributes["formatVersion"] = metaData.formatVersion.toString() + attributes["deviceId"] = metaData.identifier.deviceId + attributes["measurementId"] = metaData.identifier.measurementId + attributes["deviceType"] = metaData.deviceMetaData.deviceType + attributes["osVersion"] = metaData.deviceMetaData.operatingSystemVersion + attributes["appVersion"] = metaData.applicationMetaData.applicationVersion + attributes["length"] = metaData.measurementMetaData.length.toString() + attributes["modality"] = metaData.measurementMetaData.modality + attributes["formatVersion"] = metaData.applicationMetaData.formatVersion.toString() return attributes }