From 2cd1c515ccd4427f6b68c3680e1f2802f8eab42a Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Fri, 3 Nov 2023 15:14:01 +0100 Subject: [PATCH] chore: Refactor Creation of DataEntry --- .../eclipse/kuksa/DataBrokerTransporter.kt | 39 +++++----------- .../extension/DataEntryBuilderExtension.kt | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/extension/DataEntryBuilderExtension.kt diff --git a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt index a3b8c66a..c06c43d6 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt @@ -29,6 +29,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.eclipse.kuksa.extension.TAG +import org.eclipse.kuksa.extension.applyDatapoint import org.eclipse.kuksa.proto.v1.KuksaValV1 import org.eclipse.kuksa.proto.v1.KuksaValV1.SubscribeResponse import org.eclipse.kuksa.proto.v1.Types @@ -96,7 +97,17 @@ internal class DataBrokerTransporter( return withContext(defaultDispatcher) { val blockingStub = VALGrpc.newBlockingStub(managedChannel) - val entryUpdates = fields.map { createEntryUpdate(vssPath, it, updatedDatapoint) } + val entryUpdates = fields.map { field -> + val dataEntry = Types.DataEntry.newBuilder() + .setPath(vssPath) + .applyDatapoint(updatedDatapoint, field) + .build() + + KuksaValV1.EntryUpdate.newBuilder() + .setEntry(dataEntry) + .addFields(field) + .build() + } val request = KuksaValV1.SetRequest.newBuilder() .addAllUpdates(entryUpdates) @@ -172,30 +183,4 @@ internal class DataBrokerTransporter( return subscription } - - private fun createEntryUpdate( - vssPath: String, - field: Field, - updatedDatapoint: Types.Datapoint, - ): KuksaValV1.EntryUpdate { - val builder = Types.DataEntry.newBuilder() - .setPath(vssPath) - - when (field) { - Field.FIELD_ACTUATOR_TARGET -> { - builder.actuatorTarget = updatedDatapoint - } - - else -> { - builder.value = updatedDatapoint - } - } - - val dataEntry = builder.build() - - return KuksaValV1.EntryUpdate.newBuilder() - .setEntry(dataEntry) - .addFields(field) - .build() - } } diff --git a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/extension/DataEntryBuilderExtension.kt b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/extension/DataEntryBuilderExtension.kt new file mode 100644 index 00000000..d844a0a0 --- /dev/null +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/extension/DataEntryBuilderExtension.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.eclipse.kuksa.extension + +import org.eclipse.kuksa.proto.v1.Types +import org.eclipse.kuksa.proto.v1.Types.Field + +/** + * Applies the given [datapoint] to the given [Types.DataEntry.Builder]. If the [field] is set to + * [Field.FIELD_ACTUATOR_TARGET] it will set the datapoint using [Types.DataEntry.Builder.setActuatorTarget], + * otherwise it it will set the datapoint using [Types.DataEntry.Builder.setValue]. + */ +fun Types.DataEntry.Builder.applyDatapoint( + datapoint: Types.Datapoint, + field: Field, +): Types.DataEntry.Builder { + when (field) { + Field.FIELD_ACTUATOR_TARGET -> { + this.actuatorTarget = datapoint + } + + else -> { + this.value = datapoint + } + } + + return this +}