Skip to content

Commit

Permalink
removed logging, moved sendable & SendableBuilder.java to Witty Lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPeled committed Aug 30, 2024
1 parent 0a7c72f commit e18c36d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 390 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/frcforftc/networktables/NT4Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public NT4Client(URI serverUri) {

@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("Connected to server");
// System.out.println("Connected to server");
}

@Override
public void onMessage(String message) {
try {
JsonNode data = objectMapper.readTree(message);
System.out.println(message);
// System.out.println(message);
processMessage(data);
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -61,7 +61,7 @@ void processMessage(JsonNode node) {

@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Disconnected from server");
// System.out.println("Disconnected from server");
}

@Override
Expand Down Expand Up @@ -268,6 +268,6 @@ private void decodeNT4Message(ByteBuffer buffer) throws IOException {

private void processMessage(long topicId, long timestamp, int dataType, Object dataValue) {
// Implement message processing logic here
System.out.println("Received data - Topic ID: " + topicId + ", Timestamp: " + timestamp + ", Data Type: " + dataType + ", Data Value: " + dataValue);
// System.out.println("Received data - Topic ID: " + topicId + ", Timestamp: " + timestamp + ", Data Type: " + dataType + ", Data Value: " + dataValue);
}
}
18 changes: 9 additions & 9 deletions src/main/java/org/frcforftc/networktables/NT4Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public static NT4Server createInstance(String address, int port) {
if (m_shutdownHookAdded) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
System.out.println("Shutting down server...");
// System.out.println("Shutting down server...");
m_server.stop(0);
} catch (InterruptedException e) {
System.err.println("Server shutdown interrupted");
// System.err.println("Server shutdown interrupted");
Thread.currentThread().interrupt();
}
}));
Expand All @@ -106,7 +106,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
setConnectionLostTimeout(Integer.MAX_VALUE);
m_connections.add(conn);
String subprotocol = handshake.getFieldValue("Sec-WebSocket-Protocol");
System.out.println("CLIENT CONNECTED with " + subprotocol);
// System.out.println("CLIENT CONNECTED with " + subprotocol);

for (String s : subprotocol.split(", ")) {
if (s.equals("v4.1.networktables.first.wpi.edu")) {
Expand Down Expand Up @@ -188,7 +188,7 @@ public void onError(WebSocket conn, Exception ex) {

@Override
public void onStart() {
System.out.println("Server started successfully!");
// System.out.println("Server started successfully!");
}

/**
Expand Down Expand Up @@ -374,7 +374,7 @@ public NetworkTablesMessage decodeNT4Message(ByteBuffer buffer) throws IOExcepti
throw new IOException("Unknown data type: " + dataType);
}
} catch (MessageInsufficientBufferException | IOException e) {
System.err.println("Error decoding data value: " + e.getMessage());
// System.err.println("Error decoding data value: " + e.getMessage());
e.printStackTrace();
}
if (m_publisherUIDSMap.containsKey(topicID)) {
Expand All @@ -384,7 +384,7 @@ public NetworkTablesMessage decodeNT4Message(ByteBuffer buffer) throws IOExcepti
return new NetworkTablesMessage(topicID, stamp, dataType, dataValue);
}
} catch (MessageInsufficientBufferException | IOException e) {
System.err.println("Error decoding NT4 message: " + e.getMessage());
// System.err.println("Error decoding NT4 message: " + e.getMessage());
e.printStackTrace();
}

Expand Down Expand Up @@ -430,7 +430,7 @@ private void handleUnAnnounce(JsonNode data) {
private void handleSubscribe(WebSocket conn, JsonNode data) throws IOException {
String topic = data.get("params").get("topics").get(0).asText().substring(1); // Removes the root "/" from the topic path
if (m_entries.containsKey(topic)) {
System.out.println("SUBSCRIBED: " + topic);
// System.out.println("SUBSCRIBED: " + topic);
m_clientSubscriptions.computeIfAbsent(topic, k -> new CopyOnWriteArraySet<>()).add(conn);
conn.send(encodeNT4Message(System.currentTimeMillis(), m_entries.get(topic).getId(), 0, NetworkTablesValueType.getFromString(m_entries.get(topic).getValue().getType()).id, m_entries.get(topic).getValue().getAs()));
if (topic.contains(".type")) {
Expand All @@ -448,8 +448,8 @@ private void handleSubscribe(WebSocket conn, JsonNode data) throws IOException {
}
}
}
System.out.println("FAILED TO SUBSCRIBE TO " + topic + " AVAILABLE TOPICS ARE:");
System.out.println(m_entries.keySet());
// System.out.println("FAILED TO SUBSCRIBE TO " + topic + " AVAILABLE TOPICS ARE:");
// System.out.println(m_entries.keySet());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public <T> NetworkTablesValue(Supplier<T> getter, Consumer<T> setter, NetworkTab
this.m_type = type.typeString;
}

public <T> NetworkTablesValue(Supplier<T> getter, Consumer<T> setter, String type) {
this.m_setter = setter;
this.m_value = getter;
this.m_type = type;
}

/**
* Retrieves the value as a specific type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static NetworkTablesValueType getFromString(@NonNull String s) {
return val;
}
}

return NetworkTablesValueType.Unknown;
}

Expand Down
76 changes: 3 additions & 73 deletions src/main/java/org/frcforftc/networktables/TestingPlayground.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.frcforftc.networktables;

import org.frcforftc.networktables.sendable.Sendable;
import org.frcforftc.networktables.sendable.SendableBuilder;


import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -12,76 +11,7 @@ public static void main(String[] args) {
inst.startNT4Server("localhost", 5810);
inst.putNumber("test1", 1);
inst.putStringArray("test2", new String[]{"t", "b", "c"});

SendableBuilder builder = new SendableBuilder(new TestSendable()) {
@Override
public void publishDoubleProperty(String key, Supplier<Double> getter, Consumer<Double> setter) {

}

@Override
public void publishDoubleArrayProperty(String key, Supplier<Double[]> getter, Consumer<Double[]> setter) {

}

@Override
public void publishBooleanProperty(String key, Supplier<Boolean> getter, Consumer<Boolean> setter) {

}

@Override
public void publishBooleanArrayProperty(String key, Supplier<Boolean[]> getter, Consumer<Boolean[]> setter) {

}

@Override
public void publishStringProperty(String key, Supplier<String> getter, Consumer<String> setter) {

}

@Override
public void publishStringArrayProperty(String key, Supplier<String[]> getter, Consumer<String[]> setter) {

}

@Override
public void publishRawProperty(String key, Supplier<Byte[]> getter, Consumer<Byte[]> setter) {

}

@Override
public void publishIntProperty(String key, Supplier<Integer> getter, Consumer<Integer> setter) {

}

@Override
public void publishFloatProperty(String key, Supplier<Float> getter, Consumer<Float> setter) {

}

@Override
public void publishIntArrayProperty(String key, Supplier<Integer[]> getter, Consumer<Integer[]> setter) {

}

@Override
public void publishFloatArrayProperty(String key, Supplier<Float[]> getter, Consumer<Float[]> setter) {

}
};

builder.post("Field", (String topic, Object value) -> inst.getServer().putTopic(topic, value));
}

public static class TestSendable implements Sendable {
double[] robotPose = {1, 2, 3};

@Override
public void initSendable(SendableBuilder builder) {
builder.setSmartDashboardType("Field2d");
builder.addDoubleArrayProperty("Robot", () -> robotPose, (double[] newValue) -> {
this.robotPose = newValue;
});
}
inst.putString("test/value1", "Hello");
inst.putString("test/value2", "Hello again");
}
}

This file was deleted.

Loading

0 comments on commit e18c36d

Please sign in to comment.