2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj.smartdashboard;
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
import edu.wpi.first.networktables.BooleanArrayPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.BooleanArraySubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.BooleanArrayTopic;
|
|
|
|
|
import edu.wpi.first.networktables.BooleanPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.BooleanSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.BooleanTopic;
|
|
|
|
|
import edu.wpi.first.networktables.DoubleArrayPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.DoubleArraySubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.DoubleArrayTopic;
|
|
|
|
|
import edu.wpi.first.networktables.DoublePublisher;
|
|
|
|
|
import edu.wpi.first.networktables.DoubleSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.DoubleTopic;
|
|
|
|
|
import edu.wpi.first.networktables.FloatArrayPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.FloatArraySubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.FloatArrayTopic;
|
|
|
|
|
import edu.wpi.first.networktables.FloatPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.FloatSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.FloatTopic;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerArrayPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerArraySubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerArrayTopic;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.IntegerTopic;
|
2021-06-13 16:38:05 -07:00
|
|
|
import edu.wpi.first.networktables.NTSendableBuilder;
|
2020-12-29 22:45:16 -08:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
2022-12-05 23:08:40 -08:00
|
|
|
import edu.wpi.first.networktables.PubSubOption;
|
2022-10-08 10:01:31 -07:00
|
|
|
import edu.wpi.first.networktables.Publisher;
|
|
|
|
|
import edu.wpi.first.networktables.RawPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.RawSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.RawTopic;
|
|
|
|
|
import edu.wpi.first.networktables.StringArrayPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.StringArraySubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.StringArrayTopic;
|
|
|
|
|
import edu.wpi.first.networktables.StringPublisher;
|
|
|
|
|
import edu.wpi.first.networktables.StringSubscriber;
|
|
|
|
|
import edu.wpi.first.networktables.StringTopic;
|
|
|
|
|
import edu.wpi.first.networktables.Subscriber;
|
|
|
|
|
import edu.wpi.first.networktables.Topic;
|
2021-09-15 21:36:11 -07:00
|
|
|
import edu.wpi.first.util.function.BooleanConsumer;
|
2022-10-08 10:01:31 -07:00
|
|
|
import edu.wpi.first.util.function.FloatConsumer;
|
|
|
|
|
import edu.wpi.first.util.function.FloatSupplier;
|
2024-11-16 10:43:38 -05:00
|
|
|
import edu.wpi.first.wpilibj.RobotController;
|
2018-05-24 00:31:04 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2017-12-04 23:28:33 -08:00
|
|
|
import java.util.function.BooleanSupplier;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.function.DoubleConsumer;
|
|
|
|
|
import java.util.function.DoubleSupplier;
|
2022-10-08 10:01:31 -07:00
|
|
|
import java.util.function.LongConsumer;
|
|
|
|
|
import java.util.function.LongSupplier;
|
2017-12-04 23:28:33 -08:00
|
|
|
import java.util.function.Supplier;
|
2018-05-24 00:31:04 -04:00
|
|
|
|
2024-01-05 07:35:59 -08:00
|
|
|
/** Implementation detail for SendableBuilder. */
|
2021-06-13 16:38:05 -07:00
|
|
|
public class SendableBuilderImpl implements NTSendableBuilder {
|
2022-10-08 10:01:31 -07:00
|
|
|
@FunctionalInterface
|
|
|
|
|
private interface TimedConsumer<T> {
|
|
|
|
|
void accept(T value, long time);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2024-06-09 01:08:23 -04:00
|
|
|
private static final class Property<P extends Publisher, S extends Subscriber>
|
2022-10-08 10:01:31 -07:00
|
|
|
implements AutoCloseable {
|
2017-12-04 23:28:33 -08:00
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
@SuppressWarnings("PMD.AvoidCatchingGenericException")
|
2022-04-24 07:21:08 -07:00
|
|
|
public void close() {
|
2022-10-08 10:01:31 -07:00
|
|
|
try {
|
|
|
|
|
if (m_pub != null) {
|
|
|
|
|
m_pub.close();
|
|
|
|
|
}
|
|
|
|
|
if (m_sub != null) {
|
|
|
|
|
m_sub.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// ignore
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void update(boolean controllable, long time) {
|
|
|
|
|
if (controllable && m_sub != null && m_updateLocal != null) {
|
|
|
|
|
m_updateLocal.accept(m_sub);
|
2022-11-18 23:50:41 -08:00
|
|
|
}
|
|
|
|
|
if (m_pub != null && m_updateNetwork != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_updateNetwork.accept(m_pub, time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
P m_pub;
|
|
|
|
|
S m_sub;
|
|
|
|
|
TimedConsumer<P> m_updateNetwork;
|
|
|
|
|
Consumer<S> m_updateLocal;
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
private final List<Property<?, ?>> m_properties = new ArrayList<>();
|
2020-01-10 14:49:55 -08:00
|
|
|
private final List<Runnable> m_updateTables = new ArrayList<>();
|
2017-12-04 23:28:33 -08:00
|
|
|
private NetworkTable m_table;
|
2022-10-08 10:01:31 -07:00
|
|
|
private boolean m_controllable;
|
2018-07-28 14:04:46 -07:00
|
|
|
private boolean m_actuator;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
private BooleanPublisher m_controllablePub;
|
|
|
|
|
private StringPublisher m_typePub;
|
|
|
|
|
private BooleanPublisher m_actuatorPub;
|
|
|
|
|
|
|
|
|
|
private final List<AutoCloseable> m_closeables = new ArrayList<>();
|
|
|
|
|
|
2024-01-04 13:57:21 -08:00
|
|
|
/** Default constructor. */
|
|
|
|
|
public SendableBuilderImpl() {}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
@Override
|
|
|
|
|
@SuppressWarnings("PMD.AvoidCatchingGenericException")
|
|
|
|
|
public void close() {
|
|
|
|
|
if (m_controllablePub != null) {
|
|
|
|
|
m_controllablePub.close();
|
|
|
|
|
}
|
|
|
|
|
if (m_typePub != null) {
|
|
|
|
|
m_typePub.close();
|
|
|
|
|
}
|
|
|
|
|
if (m_actuatorPub != null) {
|
|
|
|
|
m_actuatorPub.close();
|
|
|
|
|
}
|
|
|
|
|
for (Property<?, ?> property : m_properties) {
|
|
|
|
|
property.close();
|
|
|
|
|
}
|
|
|
|
|
for (AutoCloseable closeable : m_closeables) {
|
|
|
|
|
try {
|
|
|
|
|
closeable.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Set the network table. Must be called prior to any Add* functions being called.
|
2019-08-03 18:00:43 -04:00
|
|
|
*
|
2017-12-04 23:28:33 -08:00
|
|
|
* @param table Network table
|
|
|
|
|
*/
|
|
|
|
|
public void setTable(NetworkTable table) {
|
|
|
|
|
m_table = table;
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllablePub = table.getBooleanTopic(".controllable").publish();
|
|
|
|
|
m_controllablePub.setDefault(false);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the network table.
|
2019-08-03 18:00:43 -04:00
|
|
|
*
|
2017-12-04 23:28:33 -08:00
|
|
|
* @return The network table
|
|
|
|
|
*/
|
2020-10-26 00:15:21 -07:00
|
|
|
@Override
|
2017-12-04 23:28:33 -08:00
|
|
|
public NetworkTable getTable() {
|
|
|
|
|
return m_table;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 13:36:01 -07:00
|
|
|
/**
|
|
|
|
|
* Return whether this sendable has an associated table.
|
2020-12-29 22:45:16 -08:00
|
|
|
*
|
2019-10-18 13:36:01 -07:00
|
|
|
* @return True if it has a table, false if not.
|
|
|
|
|
*/
|
2021-06-13 16:38:05 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean isPublished() {
|
2019-10-18 13:36:01 -07:00
|
|
|
return m_table != null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
/**
|
|
|
|
|
* Return whether this sendable should be treated as an actuator.
|
2019-08-03 18:00:43 -04:00
|
|
|
*
|
2018-07-28 14:04:46 -07:00
|
|
|
* @return True if actuator, false if not.
|
|
|
|
|
*/
|
|
|
|
|
public boolean isActuator() {
|
|
|
|
|
return m_actuator;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Update the network table values by calling the getters for all properties. */
|
2021-06-13 16:38:05 -07:00
|
|
|
@Override
|
|
|
|
|
public void update() {
|
2024-11-16 10:43:38 -05:00
|
|
|
long time = RobotController.getTime();
|
2022-10-08 10:01:31 -07:00
|
|
|
for (Property<?, ?> property : m_properties) {
|
|
|
|
|
property.update(m_controllable, time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2020-01-10 14:49:55 -08:00
|
|
|
for (Runnable updateTable : m_updateTables) {
|
|
|
|
|
updateTable.run();
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Hook setters for all properties. */
|
2017-12-26 17:18:02 -06:00
|
|
|
public void startListeners() {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllable = true;
|
|
|
|
|
if (m_controllablePub != null) {
|
|
|
|
|
m_controllablePub.set(true);
|
2018-12-07 22:38:22 -05:00
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Unhook setters for all properties. */
|
2017-12-26 17:18:02 -06:00
|
|
|
public void stopListeners() {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllable = false;
|
|
|
|
|
if (m_controllablePub != null) {
|
|
|
|
|
m_controllablePub.set(false);
|
2018-12-07 22:38:22 -05:00
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Clear properties. */
|
2021-06-13 16:38:05 -07:00
|
|
|
@Override
|
2019-10-18 13:36:01 -07:00
|
|
|
public void clearProperties() {
|
|
|
|
|
stopListeners();
|
2022-10-08 10:01:31 -07:00
|
|
|
for (Property<?, ?> property : m_properties) {
|
|
|
|
|
property.close();
|
|
|
|
|
}
|
2019-10-18 13:36:01 -07:00
|
|
|
m_properties.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
@Override
|
|
|
|
|
public void addCloseable(AutoCloseable closeable) {
|
|
|
|
|
m_closeables.add(closeable);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2019-08-03 18:00:43 -04:00
|
|
|
* Set the string representation of the named data type that will be used by the smart dashboard
|
|
|
|
|
* for this sendable.
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param type data type
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setSmartDashboardType(String type) {
|
2022-10-08 10:01:31 -07:00
|
|
|
if (m_typePub == null) {
|
2024-06-14 10:12:18 -04:00
|
|
|
m_typePub =
|
|
|
|
|
m_table
|
|
|
|
|
.getStringTopic(".type")
|
|
|
|
|
.publishEx(StringTopic.kTypeString, "{\"SmartDashboard\":\"" + type + "\"}");
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
|
|
|
|
m_typePub.set(type);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
/**
|
2022-12-26 14:32:13 -05:00
|
|
|
* Set a flag indicating if this sendable should be treated as an actuator. By default, this flag
|
2019-08-03 18:00:43 -04:00
|
|
|
* is false.
|
2018-07-28 14:04:46 -07:00
|
|
|
*
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param value true if actuator, false if not
|
2018-07-28 14:04:46 -07:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setActuator(boolean value) {
|
2022-10-08 10:01:31 -07:00
|
|
|
if (m_actuatorPub == null) {
|
|
|
|
|
m_actuatorPub = m_table.getBooleanTopic(".actuator").publish();
|
|
|
|
|
}
|
|
|
|
|
m_actuatorPub.set(value);
|
2018-07-28 14:04:46 -07:00
|
|
|
m_actuator = value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2019-08-03 18:00:43 -04:00
|
|
|
* Set the function that should be called to update the network table for things other than
|
2020-12-29 22:45:16 -08:00
|
|
|
* properties. Note this function is not passed the network table object; instead it should use
|
2022-10-08 10:01:31 -07:00
|
|
|
* the topics returned by getTopic().
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param func function
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setUpdateTable(Runnable func) {
|
2020-01-10 14:49:55 -08:00
|
|
|
m_updateTables.add(func);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Add a property without getters or setters. This can be used to get entry handles for the
|
2019-08-03 18:00:43 -04:00
|
|
|
* function called by setUpdateTable().
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param key property name
|
2017-12-04 23:28:33 -08:00
|
|
|
* @return Network table entry
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
public Topic getTopic(String key) {
|
|
|
|
|
return m_table.getTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a boolean property.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addBooleanProperty(String key, BooleanSupplier getter, BooleanConsumer setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
Property<BooleanPublisher, BooleanSubscriber> property = new Property<>();
|
|
|
|
|
BooleanTopic topic = m_table.getBooleanTopic(key);
|
|
|
|
|
if (getter != null) {
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.getAsBoolean(), time);
|
|
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub = topic.subscribe(false, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (boolean val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstBoolean(String key, boolean value) {
|
|
|
|
|
Property<BooleanPublisher, BooleanSubscriber> property = new Property<>();
|
|
|
|
|
BooleanTopic topic = m_table.getBooleanTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* Add an integer property.
|
|
|
|
|
*
|
|
|
|
|
* @param key property name
|
|
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addIntegerProperty(String key, LongSupplier getter, LongConsumer setter) {
|
|
|
|
|
Property<IntegerPublisher, IntegerSubscriber> property = new Property<>();
|
|
|
|
|
IntegerTopic topic = m_table.getIntegerTopic(key);
|
|
|
|
|
if (getter != null) {
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.getAsLong(), time);
|
|
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub = topic.subscribe(0, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (long val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstInteger(String key, long value) {
|
|
|
|
|
Property<IntegerPublisher, IntegerSubscriber> property = new Property<>();
|
|
|
|
|
IntegerTopic topic = m_table.getIntegerTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* Add a float property.
|
|
|
|
|
*
|
|
|
|
|
* @param key property name
|
|
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addFloatProperty(String key, FloatSupplier getter, FloatConsumer setter) {
|
|
|
|
|
Property<FloatPublisher, FloatSubscriber> property = new Property<>();
|
|
|
|
|
FloatTopic topic = m_table.getFloatTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.getAsFloat(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub = topic.subscribe(0.0f, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (float val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstFloat(String key, float value) {
|
|
|
|
|
Property<FloatPublisher, FloatSubscriber> property = new Property<>();
|
|
|
|
|
FloatTopic topic = m_table.getFloatTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Add a double property.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addDoubleProperty(String key, DoubleSupplier getter, DoubleConsumer setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
Property<DoublePublisher, DoubleSubscriber> property = new Property<>();
|
|
|
|
|
DoubleTopic topic = m_table.getDoubleTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.getAsDouble(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub = topic.subscribe(0.0, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (double val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstDouble(String key, double value) {
|
|
|
|
|
Property<DoublePublisher, DoubleSubscriber> property = new Property<>();
|
|
|
|
|
DoubleTopic topic = m_table.getDoubleTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Add a string property.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addStringProperty(String key, Supplier<String> getter, Consumer<String> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
Property<StringPublisher, StringSubscriber> property = new Property<>();
|
|
|
|
|
StringTopic topic = m_table.getStringTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub = topic.subscribe("", PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (String val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstString(String key, String value) {
|
|
|
|
|
Property<StringPublisher, StringSubscriber> property = new Property<>();
|
|
|
|
|
StringTopic topic = m_table.getStringTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Add a boolean array property.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
2020-12-29 22:45:16 -08:00
|
|
|
public void addBooleanArrayProperty(
|
|
|
|
|
String key, Supplier<boolean[]> getter, Consumer<boolean[]> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
Property<BooleanArrayPublisher, BooleanArraySubscriber> property = new Property<>();
|
|
|
|
|
BooleanArrayTopic topic = m_table.getBooleanArrayTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(new boolean[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (boolean[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstBooleanArray(String key, boolean[] value) {
|
|
|
|
|
Property<BooleanArrayPublisher, BooleanArraySubscriber> property = new Property<>();
|
|
|
|
|
BooleanArrayTopic topic = m_table.getBooleanArrayTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2022-10-08 10:01:31 -07:00
|
|
|
* Add an integer array property.
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
public void addIntegerArrayProperty(
|
|
|
|
|
String key, Supplier<long[]> getter, Consumer<long[]> setter) {
|
|
|
|
|
Property<IntegerArrayPublisher, IntegerArraySubscriber> property = new Property<>();
|
|
|
|
|
IntegerArrayTopic topic = m_table.getIntegerArrayTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(new long[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (long[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstIntegerArray(String key, long[] value) {
|
|
|
|
|
Property<IntegerArrayPublisher, IntegerArraySubscriber> property = new Property<>();
|
|
|
|
|
IntegerArrayTopic topic = m_table.getIntegerArrayTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2022-10-08 10:01:31 -07:00
|
|
|
* Add a float array property.
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
public void addFloatArrayProperty(
|
|
|
|
|
String key, Supplier<float[]> getter, Consumer<float[]> setter) {
|
|
|
|
|
Property<FloatArrayPublisher, FloatArraySubscriber> property = new Property<>();
|
|
|
|
|
FloatArrayTopic topic = m_table.getFloatArrayTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(new float[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (float[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstFloatArray(String key, float[] value) {
|
|
|
|
|
Property<FloatArrayPublisher, FloatArraySubscriber> property = new Property<>();
|
|
|
|
|
FloatArrayTopic topic = m_table.getFloatArrayTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2022-10-08 10:01:31 -07:00
|
|
|
* Add a double array property.
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
public void addDoubleArrayProperty(
|
|
|
|
|
String key, Supplier<double[]> getter, Consumer<double[]> setter) {
|
|
|
|
|
Property<DoubleArrayPublisher, DoubleArraySubscriber> property = new Property<>();
|
|
|
|
|
DoubleArrayTopic topic = m_table.getDoubleArrayTopic(key);
|
|
|
|
|
if (getter != null) {
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
|
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(new double[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (double[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstDoubleArray(String key, double[] value) {
|
|
|
|
|
Property<DoubleArrayPublisher, DoubleArraySubscriber> property = new Property<>();
|
|
|
|
|
DoubleArrayTopic topic = m_table.getDoubleArrayTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* Add a string array property.
|
|
|
|
|
*
|
|
|
|
|
* @param key property name
|
|
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addStringArrayProperty(
|
|
|
|
|
String key, Supplier<String[]> getter, Consumer<String[]> setter) {
|
|
|
|
|
Property<StringArrayPublisher, StringArraySubscriber> property = new Property<>();
|
|
|
|
|
StringArrayTopic topic = m_table.getStringArrayTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(new String[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (String[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:23:11 +03:00
|
|
|
@Override
|
|
|
|
|
public void publishConstStringArray(String key, String[] value) {
|
|
|
|
|
Property<StringArrayPublisher, StringArraySubscriber> property = new Property<>();
|
|
|
|
|
StringArrayTopic topic = m_table.getStringArrayTopic(key);
|
|
|
|
|
property.m_pub = topic.publish();
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2022-10-08 10:01:31 -07:00
|
|
|
* Add a raw property.
|
2017-12-04 23:28:33 -08:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param key property name
|
2022-10-08 10:01:31 -07:00
|
|
|
* @param typeString type string
|
2019-08-03 18:00:43 -04:00
|
|
|
* @param getter getter function (returns current value)
|
|
|
|
|
* @param setter setter function (sets new value)
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
@Override
|
2022-10-08 10:01:31 -07:00
|
|
|
public void addRawProperty(
|
|
|
|
|
String key, String typeString, Supplier<byte[]> getter, Consumer<byte[]> setter) {
|
|
|
|
|
Property<RawPublisher, RawSubscriber> property = new Property<>();
|
|
|
|
|
RawTopic topic = m_table.getRawTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter != null) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_pub = topic.publish(typeString);
|
|
|
|
|
property.m_updateNetwork = (pub, time) -> pub.set(getter.get(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
if (setter != null) {
|
2022-12-05 23:08:40 -08:00
|
|
|
property.m_sub =
|
|
|
|
|
topic.subscribe(typeString, new byte[] {}, PubSubOption.excludePublisher(property.m_pub));
|
2022-10-08 10:01:31 -07:00
|
|
|
property.m_updateLocal =
|
|
|
|
|
sub -> {
|
|
|
|
|
for (byte[] val : sub.readQueueValues()) {
|
|
|
|
|
setter.accept(val);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
2023-10-02 18:23:11 +03:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void publishConstRaw(String key, String typestring, byte[] value) {
|
|
|
|
|
Property<RawPublisher, RawSubscriber> property = new Property<>();
|
|
|
|
|
RawTopic topic = m_table.getRawTopic(key);
|
|
|
|
|
property.m_pub = topic.publish(typestring);
|
|
|
|
|
property.m_pub.set(value);
|
|
|
|
|
m_properties.add(property);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|