From 3c2428b0efa73d75aee00b4632011b6f7caf7de3 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Thu, 2 Nov 2023 13:13:19 +0100 Subject: [PATCH 1/6] feature: Correctly trigger Actuator Target Events Closes: #29 Signed-Off-By: Andre Weber --- .../org/eclipse/kuksa/DataBrokerConnection.kt | 17 +++++--- .../eclipse/kuksa/DataBrokerTransporter.kt | 42 ++++++++++++++----- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt index b8af74a7..cf241f8d 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt @@ -34,6 +34,7 @@ import org.eclipse.kuksa.proto.v1.KuksaValV1.GetResponse import org.eclipse.kuksa.proto.v1.KuksaValV1.SetResponse import org.eclipse.kuksa.proto.v1.Types import org.eclipse.kuksa.proto.v1.Types.Datapoint +import org.eclipse.kuksa.proto.v1.Types.Field import org.eclipse.kuksa.subscription.DataBrokerSubscriber import org.eclipse.kuksa.vsscore.model.VssProperty import org.eclipse.kuksa.vsscore.model.VssSpecification @@ -117,7 +118,7 @@ class DataBrokerConnection internal constructor( @JvmOverloads fun subscribe( specification: T, - fields: List = listOf(Types.Field.FIELD_VALUE), + fields: List = listOf(Field.FIELD_VALUE), listener: VssSpecificationListener, ) { fields.forEach { field -> @@ -130,7 +131,7 @@ class DataBrokerConnection internal constructor( */ fun unsubscribe( specification: T, - fields: List = listOf(Types.Field.FIELD_VALUE), + fields: List = listOf(Field.FIELD_VALUE), listener: VssSpecificationListener, ) { fields.forEach { field -> @@ -160,7 +161,7 @@ class DataBrokerConnection internal constructor( @JvmOverloads suspend fun fetch( specification: T, - fields: List = listOf(Types.Field.FIELD_VALUE), + fields: List = listOf(Field.FIELD_VALUE), ): T { return withContext(dispatcher) { try { @@ -204,18 +205,22 @@ class DataBrokerConnection internal constructor( /** * Only a [VssProperty] can be updated because they have an actual value. When provided with any parent - * [VssSpecification] then this [update] method will find all [VssProperty] children and updates them instead. + * [VssSpecification] then this [update] method will find all [VssProperty] children and updates their corresponding + * [fields] instead. * Compared to [update] with only one [Property] and [Datapoint], here multiple [SetResponse] will be returned * because a [VssSpecification] may consists of multiple values which may need to be updated. * * @throws DataBrokerException in case the connection to the DataBroker is no longer active * @throws IllegalArgumentException if the [VssProperty] could not be converted to a [Datapoint]. */ - suspend fun update(vssSpecification: VssSpecification): List { + suspend fun update( + vssSpecification: VssSpecification, + fields: List = listOf(Field.FIELD_VALUE), + ): List { val responses = mutableListOf() vssSpecification.vssProperties.forEach { vssProperty -> - val property = Property(vssProperty.vssPath) + val property = Property(vssProperty.vssPath, fields) val response = update(property, vssProperty.datapoint) responses.add(response) } 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 7d62d590..77f19c05 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt @@ -28,6 +28,7 @@ import io.grpc.stub.StreamObserver import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.eclipse.kuksa.extension.TAG import org.eclipse.kuksa.proto.v1.KuksaValV1 import org.eclipse.kuksa.proto.v1.KuksaValV1.SubscribeResponse import org.eclipse.kuksa.proto.v1.Types @@ -94,16 +95,11 @@ internal class DataBrokerTransporter( ): KuksaValV1.SetResponse { return withContext(defaultDispatcher) { val blockingStub = VALGrpc.newBlockingStub(managedChannel) - val dataEntry = Types.DataEntry.newBuilder() - .setPath(vssPath) - .setValue(updatedDatapoint) - .build() - val entryUpdate = KuksaValV1.EntryUpdate.newBuilder() - .setEntry(dataEntry) - .addAllFields(fields) - .build() + + val entryUpdates = fields.map { createEntryUpdate(vssPath, it, updatedDatapoint) } + val request = KuksaValV1.SetRequest.newBuilder() - .addUpdates(entryUpdate) + .addAllUpdates(entryUpdates) .build() return@withContext try { @@ -162,7 +158,7 @@ internal class DataBrokerTransporter( } override fun onCompleted() { - Log.d("TAG", "onCompleted() called") + Log.d(TAG, "onCompleted() called") } } @@ -176,4 +172,30 @@ 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 + } + } + + builder.build() + + return KuksaValV1.EntryUpdate.newBuilder() + .setEntry(builder.build()) + .addFields(field) + .build() + } } From 654f1711a32b424a037736d81014febf071a1549 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Thu, 2 Nov 2023 13:13:59 +0100 Subject: [PATCH 2/6] chore: Remove FIELD_PATH from Field Types --- .../databroker/viewmodel/VssPropertiesViewModel.kt | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/VssPropertiesViewModel.kt b/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/VssPropertiesViewModel.kt index fb5c23c6..cf153143 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/VssPropertiesViewModel.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/VssPropertiesViewModel.kt @@ -48,9 +48,7 @@ class VSSPropertiesViewModel : ViewModel() { private set val valueTypes: List = ValueCase.values().toList() - val fieldTypes: List = Field.values().toList() - .filterNot { it.toString().contains(EXCLUDED_FIELD_TYPES) } - .minus(listOf(Field.FIELD_UNSPECIFIED, Field.UNRECOGNIZED)) // Not relevant field types + val fieldTypes: List = listOf(Field.FIELD_VALUE, Field.FIELD_ACTUATOR_TARGET) val datapoint: Datapoint get() = vssProperties.valueType.createDatapoint(vssProperties.value) @@ -62,10 +60,6 @@ class VSSPropertiesViewModel : ViewModel() { fun updateVssProperties(vssProperties: VSSProperties = VSSProperties()) { this.vssProperties = vssProperties } - - companion object { - const val EXCLUDED_FIELD_TYPES = "METADATA" - } } @Immutable @@ -73,5 +67,5 @@ data class VSSProperties( val vssPath: String = "Vehicle.Speed", val valueType: ValueCase = ValueCase.VALUE_NOT_SET, val value: String = "130", - val fieldType: Field = Field.FIELD_UNSPECIFIED, + val fieldType: Field = Field.FIELD_VALUE, ) From 422260872a3a748e2c18aeb7951e14fad7e9dec4 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Thu, 2 Nov 2023 13:14:49 +0100 Subject: [PATCH 3/6] chore: Exclude node_module Folder from Detekt --- buildSrc/src/main/kotlin/detekt.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/detekt.gradle.kts b/buildSrc/src/main/kotlin/detekt.gradle.kts index 0c52bece..dffee6c0 100644 --- a/buildSrc/src/main/kotlin/detekt.gradle.kts +++ b/buildSrc/src/main/kotlin/detekt.gradle.kts @@ -26,7 +26,7 @@ tasks.withType().configureEach { parallel = true setSource(projectDir) include("**/*.kt", "**/*.kts") - exclude("**/resources/**", "**/build/**") + exclude("**/resources/**", "**/build/**", "**/node_modules/**") config.setFrom(project.file("$rootDir/config/detekt/config.yml")) baseline.set(baselineFile) From fc1092ae844494871e90723b0a6b431dcb9b821f Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Fri, 3 Nov 2023 14:38:26 +0100 Subject: [PATCH 4/6] chore: Remove Redundant Line --- .../main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 77f19c05..a3b8c66a 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerTransporter.kt @@ -191,10 +191,10 @@ internal class DataBrokerTransporter( } } - builder.build() + val dataEntry = builder.build() return KuksaValV1.EntryUpdate.newBuilder() - .setEntry(builder.build()) + .setEntry(dataEntry) .addFields(field) .build() } From 2cd1c515ccd4427f6b68c3680e1f2802f8eab42a Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Fri, 3 Nov 2023 15:14:01 +0100 Subject: [PATCH 5/6] 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 +} From 1ae3ad4bb0443e7718df5f862d6d395977119c94 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Fri, 3 Nov 2023 15:21:24 +0100 Subject: [PATCH 6/6] chore: Change List to Collection --- .../src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt index cf241f8d..6d143183 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/DataBrokerConnection.kt @@ -161,7 +161,7 @@ class DataBrokerConnection internal constructor( @JvmOverloads suspend fun fetch( specification: T, - fields: List = listOf(Field.FIELD_VALUE), + fields: Collection = listOf(Field.FIELD_VALUE), ): T { return withContext(dispatcher) { try {