Skip to content

Commit

Permalink
chore: Refactor Creation of DataEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Nov 3, 2023
1 parent fc1092a commit 2cd1c51
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2cd1c51

Please sign in to comment.