NT4Server

class NT4Server @JvmOverloads constructor(val port: Int = DEFAULT_PORT, val host: String = "0.0.0.0", timeSource: TimeSource.WithComparableMarks = TimeSource.Monotonic, val origin: ComparableTimeMark = timeSource.markNow(), routes: Routing.() -> Unit? = null) : Closeable(source)

A NetworkTables 4 server that publishes values to dashboards such as AdvantageScope, Glass and Shuffleboard.

Publish a topic with publish, publishStruct or publishStructArray, which return its id, and then set values on it with the set* methods. A client that connects and subscribes is told about the topics it asked for and is sent their current values, and then every change after that.

val server = NT4Server()
server.start()
val voltage = server.publish("/Robot/Battery", NT4Type.DOUBLE)
server.setDouble(voltage, 12.4)

This is a publish-only server. It ignores publish, unpublish and setproperties messages, and values clients try to publish, so clients can read but not write. It sends every change to a subscriber, whatever the subscription's periodic and all options say, and it has no meta topics such as $clients. It answers the clock-synchronization messages that clients use to convert its timestamps.

Every timestamp is in microseconds since origin, as measured by timeSource. That is also the time base that clients synchronize to, so a log writer that stamps values with the same source and origin agrees with what dashboards see.

A client that can't keep up is disconnected, so it can reconnect and catch up, rather than making the robot wait or run out of memory.

All methods are safe to call from multiple threads, and the set* methods never block on the network.

Parameters

port

The port to listen on. The standard NetworkTables 4 port is DEFAULT_PORT, and 0 picks a free one, which boundPort reports after start.

host

The address to listen on. The default is every network interface.

timeSource

The clock that timestamps are measured on. A server created on its own can use the default. To line it up with a dev.nextftc.telemetry.wpilog.WPILogWriter, give them the same source and the same origin.

origin

The moment timestamp zero refers to, a mark from timeSource. The default is the moment the server is created. Share one origin between sinks that should agree on every timestamp.

routes

Adds Ktor routes to the server, for pages or downloads on the same port. They are installed after the server's own, so they can't replace / or /nt/.... It runs each time start is called.

Throws

if origin isn't from timeSource, or is in the future

Constructors

Link copied to clipboard
constructor(port: Int = DEFAULT_PORT, host: String = "0.0.0.0", timeSource: TimeSource.WithComparableMarks = TimeSource.Monotonic, origin: ComparableTimeMark = timeSource.markNow(), routes: Routing.() -> Unit? = null)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The port the server is listening on, which differs from port when that is 0.

Link copied to clipboard

How many dashboards are connected. Connections that only measure round-trip time aren't counted.

Link copied to clipboard
Link copied to clipboard

Whether start has been called without a stop since.

Link copied to clipboard
Link copied to clipboard
val port: Int

Functions

Link copied to clipboard
open override fun close()
Link copied to clipboard
fun now(): Long

Returns the current time as a timestamp in microseconds since origin.

Link copied to clipboard
fun publish(name: String, type: NT4Type, retained: Boolean = false, properties: Map<String, Any> = emptyMap()): Int

Publishes a topic of type. See the overload that takes a type string.

fun publish(name: String, type: String, retained: Boolean = false, properties: Map<String, Any> = emptyMap()): Int

Publishes a topic and returns its id, or returns the id of the topic with this name if it is already published.

Link copied to clipboard
fun publishStruct(name: String, schema: StructSchema<*>, retained: Boolean = false, properties: Map<String, Any> = emptyMap()): Int

Publishes a topic that holds single values of schema, and returns its id.

Link copied to clipboard
fun publishStructArray(name: String, schema: StructSchema<*>, retained: Boolean = false, properties: Map<String, Any> = emptyMap()): Int

Publishes a topic that holds arrays of schema values, and returns its id. Otherwise the same as publishStruct.

Link copied to clipboard
fun setBoolean(id: Int, value: Boolean, timestampMicros: Long = now())

Sets the value of the boolean topic id.

Link copied to clipboard
fun setBooleanArray(id: Int, value: BooleanArray, timestampMicros: Long = now())

Sets the value of the boolean[] topic id.

Link copied to clipboard
fun setDouble(id: Int, value: Double, timestampMicros: Long = now())

Sets the value of the double topic id.

Link copied to clipboard
fun setDoubleArray(id: Int, value: DoubleArray, timestampMicros: Long = now())

Sets the value of the double[] topic id.

Link copied to clipboard
fun setFloat(id: Int, value: Float, timestampMicros: Long = now())

Sets the value of the float topic id.

Link copied to clipboard
fun setFloatArray(id: Int, value: FloatArray, timestampMicros: Long = now())

Sets the value of the float[] topic id.

Link copied to clipboard
fun setInteger(id: Int, value: Long, timestampMicros: Long = now())

Sets the value of the int topic id.

Link copied to clipboard
fun setIntegerArray(id: Int, value: LongArray, timestampMicros: Long = now())

Sets the value of the int[] topic id.

Link copied to clipboard
fun setRaw(id: Int, value: ByteArray, timestampMicros: Long = now())

Sets the value of a binary topic id, such as a raw, msgpack, or struct topic. For a struct topic prefer setStruct, which checks the type and packs the value.

Link copied to clipboard
fun setString(id: Int, value: String, timestampMicros: Long = now())

Sets the value of the string or json topic id.

Link copied to clipboard
fun setStringArray(id: Int, value: List<String>, timestampMicros: Long = now())

Sets the value of the string[] topic id.

Link copied to clipboard
fun <T> setStruct(id: Int, schema: StructSchema<T>, value: T, timestampMicros: Long = now())

Sets the value of a topic published with publishStruct, packing value with schema.

Link copied to clipboard
fun <T> setStructArray(id: Int, schema: StructSchema<T>, values: List<T>, timestampMicros: Long = now())

Sets the value of a topic published with publishStructArray, packing values back to back with schema.

Link copied to clipboard
fun start()

Starts listening, without blocking, and returns once the port is bound.

Link copied to clipboard
fun stop()

Stops listening and disconnects every client. Topics and their values are kept.

Link copied to clipboard

Returns mark as a timestamp in microseconds since origin.

Link copied to clipboard
fun unpublish(id: Int)

Removes the topic id and tells the clients that were told about it. Does nothing if there is no such topic. The name can be published again, but only with the type it had.