Skip to content

Commit

Permalink
Rename Item interface to Measurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhh committed Sep 13, 2019
1 parent dc7d11f commit 1c0c7a6
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 62 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {

configure(subprojects) {
group = 'org.strykeforce.thirdcoast'
version = '19.2.0'
version = '19.3.0'

apply plugin: 'java-library'
apply plugin: 'idea'
Expand Down Expand Up @@ -51,7 +51,7 @@ configure(subprojects) {

configure(subprojects - project(":deadeye")) {
apply plugin: "edu.wpi.first.GradleRIO"

dependencies {
compile wpi.deps.wpilib()
compile wpi.deps.vendor.java()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package org.strykeforce.thirdcoast.telemetry

import com.squareup.moshi.JsonWriter
import okio.BufferedSink
import org.strykeforce.thirdcoast.telemetry.item.Item
import org.strykeforce.thirdcoast.telemetry.item.Measurable
import java.io.IOException

/**
* An abstract base class intended to be subclassed by concrete implmentations of [Inventory].
* An abstract base class intended to be subclassed by concrete implementations of [Inventory].
*/
abstract class AbstractInventory(items: Collection<Item>) : Inventory {
abstract class AbstractInventory(items: Collection<Measurable>) : Inventory {

protected val items = items.sorted()

Expand All @@ -29,7 +29,7 @@ abstract class AbstractInventory(items: Collection<Item>) : Inventory {
override fun toString() = "AbstractInventory(items=$items)"
}

private fun JsonWriter.writeItems(items: List<Item>): JsonWriter {
private fun JsonWriter.writeItems(items: List<Measurable>): JsonWriter {
beginArray()
items.forEachIndexed { index, item ->
beginObject()
Expand All @@ -41,7 +41,7 @@ private fun JsonWriter.writeItems(items: List<Item>): JsonWriter {
return endArray()
}

private fun JsonWriter.writeMeasures(items: List<Item>): JsonWriter {
private fun JsonWriter.writeMeasures(items: List<Measurable>): JsonWriter {
beginArray()
items.associateBy({ it.type }, { it.measures }).forEach { type, measures ->
beginObject()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package org.strykeforce.thirdcoast.telemetry

import okio.BufferedSink
import org.strykeforce.thirdcoast.telemetry.item.Item
import org.strykeforce.thirdcoast.telemetry.item.Measurable
import java.io.IOException

/** Represents the inventory of robot hardware and subsystems that can have telemetry streaming. */
interface Inventory {

/**
* Gets an item by its inventory ID. The inventory ID is an index to an item in inventory and
* should **not** be confused with the device ID of the underlying device the `Item`
* represents.
*
* @param id the inventory ID to look up.
* @return the found Item.
*/
fun itemForId(id: Int): Item
/**
* Gets a measurable item by its inventory ID. The inventory ID is an index to a measurable item in inventory and
* should **not** be confused with the device ID of the underlying device the `Measurable` represents.
*
* @param id the inventory ID to look up.
* @return the found Measurable item.
*/
fun itemForId(id: Int): Measurable

/**
* Writes the grapher-format JSON inventory to the supplied sink.
*
* @param sink the sink to write to.
* @throws IOException if an IO error occurs during writing.
*/
@Throws(IOException::class)
fun writeInventory(sink: BufferedSink)
/**
* Writes the grapher-format JSON inventory to the supplied sink.
*
* @param sink the sink to write to.
* @throws IOException if an IO error occurs during writing.
*/
@Throws(IOException::class)
fun writeInventory(sink: BufferedSink)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.strykeforce.thirdcoast.telemetry

import org.strykeforce.thirdcoast.telemetry.item.Item
import org.strykeforce.thirdcoast.telemetry.item.Measurable

/** Default implementation of [Inventory] for a robot. */
class RobotInventory(items: Collection<Item>) : AbstractInventory(items)
class RobotInventory(items: Collection<Measurable>) : AbstractInventory(items)
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package org.strykeforce.thirdcoast.telemetry
import com.ctre.phoenix.motorcontrol.can.TalonSRX
import mu.KotlinLogging
import org.strykeforce.thirdcoast.swerve.SwerveDrive
import org.strykeforce.thirdcoast.telemetry.item.Item
import org.strykeforce.thirdcoast.telemetry.item.Measurable
import org.strykeforce.thirdcoast.telemetry.item.TalonItem
import java.util.*
import java.util.function.Function

private val logger = KotlinLogging.logger {}

/**
* The Telemetry service registers [Item] instances for data collection and controls the
* The Telemetry service registers [Measurable] instances for data collection and controls the
* starting and stopping of the service. When active, the services listens for incoming config
* messages via a HTTP REST service and sends data over UDP.
*/
Expand All @@ -21,7 +21,7 @@ class TelemetryService(private val telemetryControllerFactory: Function<Inventor
// when start is called. The inventory copies this collection into a List, using its index in
// this list as the inventory id.

private val items = LinkedHashSet<Item>()
private val items = LinkedHashSet<Measurable>()
private var telemetryController: TelemetryController? = null

/** Start the Telemetry service and listen for client connections. */
Expand Down Expand Up @@ -62,7 +62,7 @@ class TelemetryService(private val telemetryControllerFactory: Function<Inventor
* @param item the Item to register for data collection
* @throws IllegalStateException if TelemetryService is running.
*/
fun register(item: Item) {
fun register(item: Measurable) {
checkNotStarted()
if (items.add(item)) {
logger.info { "registered item ${item.description}" }
Expand All @@ -77,7 +77,7 @@ class TelemetryService(private val telemetryControllerFactory: Function<Inventor
* @param collection the collection of Items to register for data collection
* @throws IllegalStateException if TelemetryService is running.
*/
fun registerAll(collection: Collection<Item>) {
fun registerAll(collection: Collection<Measurable>) {
checkNotStarted()
items.addAll(collection)
logger.info { "registered all: $collection" }
Expand Down Expand Up @@ -108,7 +108,7 @@ class TelemetryService(private val telemetryControllerFactory: Function<Inventor
*
* @return an unmodifiable Set of Items.
*/
fun getItems(): Set<Item> {
fun getItems(): Set<Measurable> {
return Collections.unmodifiableSet(items)
}

Expand All @@ -117,7 +117,7 @@ class TelemetryService(private val telemetryControllerFactory: Function<Inventor
*
* @throws AssertionError if TelemetryService is running.
*/
fun remove(item: Item) {
fun remove(item: Measurable) {
checkNotStarted()
if (items.remove(item)) {
logger.info { "removed $item" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ClientHandler(private val port: Int, private val socket: DatagramSocket) {
private var scheduler: ScheduledExecutorService? = null

/**
* Start streaming the items specified in the subscription.
* Start streaming the `Measurable` items specified in the subscription.
*
* @param subscription Items to stream to client
* @param subscription items to stream to client
*/
fun start(subscription: Subscription) {
if (scheduler != null) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure
import org.strykeforce.thirdcoast.telemetry.grapher.Measure.*
import java.util.function.DoubleSupplier

/** Represents a [TalonSRX] telemetry-enable Item. */
/** Represents a [TalonSRX] telemetry-enable `Measurable` item. */
class CanifierItem @JvmOverloads constructor(
private val canifier: CANifier,
override val description: String = "CANifier ${canifier.deviceID}"
) : Item {
) : Measurable {

override val deviceId = canifier.deviceID
override val type = "canifier"
Expand Down Expand Up @@ -87,4 +87,4 @@ class CanifierItem @JvmOverloads constructor(
PWM3_PERIOD
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure.VALUE
import java.util.function.DoubleSupplier


/** Represents a [DigitalInput] telemetry-enable Item. */
/** Represents a [DigitalInput] telemetry-enable `Measurable` item. */
class DigitalInputItem @JvmOverloads constructor(
private val digitalInput: DigitalInput,
override val description: String = "Digital Input ${digitalInput.channel}"
) : Item {
) : Measurable {

override val deviceId = digitalInput.channel
override val type = "digitalInput"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure.VALUE
import java.util.function.DoubleSupplier


/** Represents a [DigitalInput] telemetry-enable Item. */
/** Represents a [DigitalInput] telemetry-enable `Measurable` item. */
class DigitalOutputItem @JvmOverloads constructor(
private val digitalOutput: DigitalOutput,
override val description: String = "Digital Output ${digitalOutput.channel}"
) : Item {
) : Measurable {

override val deviceId = digitalOutput.channel
override val type = "digitalOutput"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure
import java.util.function.DoubleSupplier

/**
* An item that can be graphed. These are used as `Set` elements and implementing classes
* An item that can be measured and graphed. These are used as `Set` elements and implementing classes
* should implement an identity-based version of equals and hashCode.
*
*
* The abstract base class implementing `Item` implements `Comparable` by comparing
* the results returned by `Item#deviceId()`.
* The abstract base class implementing `Measurable` implements `Comparable` by comparing
* the results returned by `Measurable#deviceId()`.
*/
interface Item : Comparable<Item> {
interface Measurable : Comparable<Measurable> {

/**
* Returns the underlying device id, for example, CAN bus address or PWM port.
Expand All @@ -29,7 +29,7 @@ interface Item : Comparable<Item> {
val description: String

/**
* `Set` of `Measure` items applicable to this item type.
* `Set` of `Measure` parameters applicable to this item type.
*/
val measures: Set<Measure>

Expand All @@ -39,11 +39,11 @@ interface Item : Comparable<Item> {
*/
fun measurementFor(measure: Measure): DoubleSupplier

override fun compareTo(other: Item): Int {
override fun compareTo(other: Measurable): Int {
val result = type.compareTo(other.type)
return if (result != 0) result else deviceId.compareTo(other.deviceId)
}

}

internal fun Boolean.toDouble() = if (this) 1.0 else 0.0
internal fun Boolean.toDouble() = if (this) 1.0 else 0.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure.ANGLE
import org.strykeforce.thirdcoast.telemetry.grapher.Measure.POSITION
import java.util.function.DoubleSupplier

/** Represents a [Servo] telemetry-enable Item. */
/** Represents a [Servo] telemetry-enable `Measurable` item. */
class ServoItem @JvmOverloads constructor(
private val servo: Servo,
override val description: String = "Servo ${servo.channel}"
) : Item {
) : Measurable {

override val deviceId = servo.channel
override val type = "servo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure
import org.strykeforce.thirdcoast.telemetry.grapher.Measure.*
import java.util.function.DoubleSupplier

/** Represents a [TalonSRX] telemetry-enable Item. */
/** Represents a [TalonSRX] telemetry-enable `Measurable` item. */
class TalonItem @JvmOverloads constructor(
private val talon: TalonSRX,
override val description: String = "TalonSRX ${talon.deviceID}"
) : Item {
) : Measurable {

override val deviceId = talon.deviceID
override val type = "talon"
Expand Down Expand Up @@ -94,4 +94,4 @@ class TalonItem @JvmOverloads constructor(
REVERSE_LIMIT_SWITCH_CLOSED
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import org.strykeforce.thirdcoast.telemetry.grapher.Measure
import org.strykeforce.thirdcoast.telemetry.grapher.Measure.VALUE
import java.util.function.DoubleSupplier

/** Represents a PWM ultrasonic rangefinder telemetry-enable `Measurable` item. */
class UltrasonicRangefinderItem @JvmOverloads constructor(
canId: Int,
private val pwmChannel: PWMChannel,
override val description: String = "Sensor ${canId * 10 + pwmChannel.value}"
) : Item {
) : Measurable {

override val deviceId = canId * 10 + pwmChannel.value
override val type = "sensor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.skyscreamer.jsonassert.JSONAssert;
import org.strykeforce.thirdcoast.telemetry.grapher.ResourceHelper;
import org.strykeforce.thirdcoast.telemetry.item.Item;
import org.strykeforce.thirdcoast.telemetry.item.Measurable;

import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -22,9 +22,9 @@
@ExtendWith(MockitoExtension.class)
class RobotInventoryTest {

@Mock private Item itemOne;
@Mock private Item itemTwo;
private Collection<Item> items;
@Mock private Measurable itemOne;
@Mock private Measurable itemTwo;
private Collection<Measurable> items;

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONParser;
import org.strykeforce.thirdcoast.telemetry.Inventory;
import org.strykeforce.thirdcoast.telemetry.item.Item;
import org.strykeforce.thirdcoast.telemetry.item.Measurable;

import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -24,7 +24,8 @@
@ExtendWith(MockitoExtension.class)
class SubscriptionTest {

@Mock Item itemZero, itemOne;
@Mock
Measurable itemZero, itemOne;
@Mock Inventory inventory;

@BeforeEach
Expand Down

0 comments on commit 1c0c7a6

Please sign in to comment.