From b2517634b14ab412a4a39cbf769a6d4292451bb9 Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Thu, 27 Jun 2024 16:44:18 -0400 Subject: [PATCH] Board (#45) --- .../com/viam/sdk/core/component/board/Board.kt | 15 +++++++++++++-- .../sdk/core/component/board/BoardRPCClient.kt | 6 ++++-- .../viam/sdk/core/component/board/BoardTest.kt | 3 ++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt b/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt index a3c1a3c50..011a73b8b 100644 --- a/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt +++ b/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt @@ -11,6 +11,8 @@ import com.viam.sdk.core.robot.RobotClient import java.util.* import java.util.stream.Stream import kotlin.time.Duration +import kotlin.time.toJavaDuration +import java.time.Duration as JDuration typealias Tick = StreamTicksResponse @@ -226,14 +228,23 @@ abstract class Board(name: String) : Component(SUBTYPE, named(name)) { * @param powerMode the power mode to set * @param duration if provided, the board will exit the given power mode after this duration */ - abstract fun setPowerMode(powerMode: PowerMode, duration: Duration, extra: Struct) + abstract fun setPowerMode(powerMode: PowerMode, duration: JDuration, extra: Struct) /** * Set the board to the indicated power mode. * @param powerMode the power mode to set * @param duration if provided, the board will exit the given power mode after this duration */ - fun setPowerMode(powerMode: PowerMode, duration: Duration) { + fun setPowerMode(powerMode: PowerMode, duration: JDuration) { return setPowerMode(powerMode, duration, Struct.getDefaultInstance()) } + + /** + * Set the board to the indicated power mode. + * @param powerMode the power mode to set + * @param duration if provided, the board will exit the given power mode after this duration + */ + fun setPowerMode(powerMode: PowerMode, duration: Duration, extra: Struct = Struct.getDefaultInstance()) { + return setPowerMode(powerMode, duration.toJavaDuration(), extra) + } } diff --git a/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/BoardRPCClient.kt b/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/BoardRPCClient.kt index 545a3a9c9..e84c94efa 100644 --- a/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/BoardRPCClient.kt +++ b/core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/BoardRPCClient.kt @@ -10,9 +10,10 @@ import com.viam.component.board.v1.BoardServiceGrpc.BoardServiceBlockingStub import com.viam.sdk.core.exception.MethodNotImplementedException import com.viam.sdk.core.rpc.Channel import com.viam.sdk.core.util.Durations +import java.time.Duration import java.util.* import kotlin.jvm.optionals.getOrDefault -import kotlin.time.Duration +import kotlin.time.toKotlinDuration /** * gRPC Client for a Board component @@ -99,7 +100,7 @@ class BoardRPCClient(name: String, channel: Channel) : Board(name) { powerMode: PowerMode, duration: Duration, extra: Struct ) { val request = SetPowerModeRequest.newBuilder().setName(this.name.name).setPowerMode(powerMode) - .setDuration(Durations.fromNanos(duration.inWholeNanoseconds)).setExtra(extra).build() + .setDuration(Durations.fromNanos(duration.toKotlinDuration().inWholeNanoseconds)).setExtra(extra).build() this.client.setPowerMode(request) } @@ -116,4 +117,5 @@ class BoardRPCClient(name: String, channel: Channel) : Board(name) { val response = this.client.getGeometries(request) return response.geometriesList } + } diff --git a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/board/BoardTest.kt b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/board/BoardTest.kt index 7d73824a8..0518b3171 100644 --- a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/board/BoardTest.kt +++ b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/board/BoardTest.kt @@ -14,6 +14,7 @@ import java.util.* import kotlin.random.Random import kotlin.time.DurationUnit import kotlin.time.toDuration +import kotlin.time.toJavaDuration class BoardTest { private lateinit var board: Board @@ -163,6 +164,6 @@ class BoardTest { val powerMode = PowerMode.POWER_MODE_OFFLINE_DEEP val powerModeDuration = Random.nextInt().toDuration(DurationUnit.NANOSECONDS) board.setPowerMode(powerMode, powerModeDuration) - verify(board).setPowerMode(powerMode, powerModeDuration) + verify(board).setPowerMode(powerMode, powerModeDuration.toJavaDuration(), Struct.getDefaultInstance()) } }