-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58635d3
commit d409cc5
Showing
13 changed files
with
353 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
core/src/main/java/edu/wpi/grip/core/operations/network/MapNetworkReceiverFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package edu.wpi.grip.core.operations.network; | ||
|
||
|
||
/** | ||
* A factory to create {@link NetworkReceiver NetworkRecievers}. | ||
*/ | ||
@FunctionalInterface | ||
public interface MapNetworkReceiverFactory { | ||
NetworkReceiver create(String path); | ||
} |
34 changes: 34 additions & 0 deletions
34
core/src/main/java/edu/wpi/grip/core/operations/network/NetworkReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package edu.wpi.grip.core.operations.network; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
/** | ||
* Manages the interface between the {@link PublishAnnotatedOperation} and the actual network | ||
* protocol implemented by a specific {@link Manager}. | ||
*/ | ||
public abstract class NetworkReceiver implements AutoCloseable { | ||
|
||
protected final String path; | ||
|
||
/** | ||
* Create a new NetworkReceiver with the specified path. | ||
* | ||
* @param path The path of the object to get | ||
*/ | ||
public NetworkReceiver(String path) { | ||
checkArgument(!path.isEmpty(), "Name cannot be an empty string"); | ||
this.path = path; | ||
} | ||
|
||
/** | ||
* Get the value of the object. | ||
* | ||
* @return The value of this NetworkReceiver | ||
*/ | ||
public abstract Object getValue(); | ||
|
||
/** | ||
* Close the network reciever. This should not throw an exception. | ||
*/ | ||
public abstract void close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
core/src/main/java/edu/wpi/grip/core/sources/NetworkTableEntrySource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package edu.wpi.grip.core.sources; | ||
|
||
import edu.wpi.grip.core.Source; | ||
import edu.wpi.grip.core.events.SourceRemovedEvent; | ||
import edu.wpi.grip.core.operations.network.MapNetworkReceiverFactory; | ||
import edu.wpi.grip.core.operations.network.NetworkReceiver; | ||
import edu.wpi.grip.core.sockets.OutputSocket; | ||
import edu.wpi.grip.core.sockets.SocketHints; | ||
import edu.wpi.grip.core.util.ExceptionWitness; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.eventbus.Subscribe; | ||
import com.google.inject.assistedinject.Assisted; | ||
import com.google.inject.assistedinject.AssistedInject; | ||
import com.google.inject.name.Named; | ||
import com.thoughtworks.xstream.annotations.XStreamAlias; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Provides a way to get a {@link Types Type} from a NetworkTable that GRIP is connected to. | ||
*/ | ||
@XStreamAlias("grip:NetworkValue") | ||
public class NetworkTableEntrySource extends Source { | ||
|
||
private static final String PATH_PROPERTY = "networktable_path"; | ||
private static final String TYPE_PROPERTY = "BOOLEAN"; | ||
|
||
private final OutputSocket output; | ||
private final String path; | ||
private final Types type; | ||
private NetworkReceiver networkReceiver; | ||
|
||
public interface Factory { | ||
NetworkTableEntrySource create(Properties properties); | ||
|
||
NetworkTableEntrySource create(String path, Types type); | ||
} | ||
|
||
public enum Types { | ||
BOOLEAN, NUMBER, STRING; | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString().charAt(0) + super.toString().substring(1).toLowerCase(); | ||
} | ||
|
||
} | ||
|
||
@AssistedInject | ||
NetworkTableEntrySource( | ||
ExceptionWitness.Factory exceptionWitnessFactory, | ||
OutputSocket.Factory osf, | ||
@Named("ntManager") MapNetworkReceiverFactory ntManager, | ||
@Assisted Properties properties) { | ||
this(exceptionWitnessFactory, | ||
osf, | ||
ntManager, | ||
properties.getProperty(PATH_PROPERTY), | ||
Types.valueOf(properties.getProperty(TYPE_PROPERTY))); | ||
} | ||
|
||
@AssistedInject | ||
NetworkTableEntrySource( | ||
ExceptionWitness.Factory exceptionWitnessFactory, | ||
OutputSocket.Factory osf, | ||
@Named("ntManager") MapNetworkReceiverFactory ntManager, | ||
@Assisted String path, | ||
@Assisted Types type) { | ||
super(exceptionWitnessFactory); | ||
this.path = path; | ||
this.type = type; | ||
networkReceiver = ntManager.create(path); | ||
|
||
switch (type) { | ||
case BOOLEAN: | ||
output = osf.create( | ||
SocketHints.Outputs.createBooleanSocketHint(Types.BOOLEAN.toString(), false)); | ||
break; | ||
case NUMBER: | ||
output = osf.create( | ||
SocketHints.Outputs.createNumberSocketHint(Types.NUMBER.toString(), 0.0)); | ||
break; | ||
case STRING: | ||
output = osf.create( | ||
SocketHints.Outputs.createStringSocketHint(Types.STRING.toString(), "")); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Invalid NetworkTable source type"); | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return path; | ||
} | ||
|
||
@Override | ||
protected List<OutputSocket> createOutputSockets() { | ||
return ImmutableList.of( | ||
output | ||
); | ||
} | ||
|
||
@Override | ||
protected boolean updateOutputSockets() { | ||
try { | ||
output.setValue(networkReceiver.getValue()); | ||
} catch (ClassCastException ex) { | ||
getExceptionWitness().flagException(ex, getName() + " is not of type " | ||
+ output.getSocketHint().getTypeLabel()); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Properties getProperties() { | ||
Properties properties = new Properties(); | ||
properties.setProperty(PATH_PROPERTY, path); | ||
properties.setProperty(TYPE_PROPERTY, type.toString().toUpperCase()); | ||
return properties; | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
updateOutputSockets(); | ||
} | ||
|
||
@Subscribe | ||
public void onSourceRemovedEvent(SourceRemovedEvent event) { | ||
if (event.getSource() == this) { | ||
networkReceiver.close(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.