44import com .squareup .moshi .Moshi ;
55import edu .wpi .first .networktables .NetworkTable ;
66import edu .wpi .first .networktables .NetworkTableInstance ;
7- import okio .BufferedSource ;
8- import org .jetbrains .annotations .NotNull ;
9- import org .jetbrains .annotations .Nullable ;
10- import org .slf4j .Logger ;
11- import org .slf4j .LoggerFactory ;
12-
137import java .io .IOException ;
148import java .lang .reflect .Constructor ;
159import java .lang .reflect .InvocationTargetException ;
1610import java .util .Objects ;
1711import java .util .regex .Pattern ;
12+ import okio .BufferedSource ;
13+ import org .jetbrains .annotations .NotNull ;
14+ import org .jetbrains .annotations .Nullable ;
15+ import org .slf4j .Logger ;
16+ import org .slf4j .LoggerFactory ;
1817
1918/**
2019 * Represents a connection to a Deadeye camera. It provides methods to configure and control the
@@ -34,30 +33,58 @@ public class Deadeye<T extends TargetData> {
3433 /**
3534 * Initialize a connection to a Deadeye camera.
3635 *
37- * @param id the camera id.
36+ * @param id the camera id.
3837 * @param cls the appropriate TargetData (or subclass) class object
3938 */
4039 public Deadeye (String id , Class <T > cls ) {
41- this (id , cls , NetworkTableInstance .getDefault ());
40+ this (id , cls , NetworkTableInstance .getDefault (), null );
41+ }
42+
43+ /**
44+ * Initialize a connection to a Deadeye camera and override the detected client IP address.
45+ *
46+ * @param id the camera id.
47+ * @param cls the appropriate TargetData (or subclass) class object
48+ * @param linkAddress override IP address to send client data to
49+ */
50+ public Deadeye (String id , Class <T > cls , @ Nullable String linkAddress ) {
51+ this (id , cls , NetworkTableInstance .getDefault (), linkAddress );
4252 }
4353
54+
4455 /**
4556 * Initialize a connection to a Deadeye camera using a specified NetworkTables instance. This is
4657 * primarily used for testing.
4758 *
48- * @param id the camera id.
59+ * @param id the camera id.
4960 * @param cls the appropriate TargetData (or subclass) class object
5061 * @param nti the NetworkTables instance to connect through
5162 */
52- @ SuppressWarnings ("unchecked" )
5363 public Deadeye (String id , Class <T > cls , NetworkTableInstance nti ) {
64+ this (id , cls , nti , null );
65+ }
66+
67+ /**
68+ * Initialize a connection to a Deadeye camera using a specified NetworkTables instance and
69+ * override the client IP address. This is primarily used for testing.
70+ *
71+ * @param id the camera id.
72+ * @param cls the appropriate TargetData (or subclass) class object
73+ * @param nti the NetworkTables instance to connect through
74+ * @param linkAddress override IP address to send target data to
75+ */
76+ @ SuppressWarnings ("unchecked" )
77+ public Deadeye (@ NotNull String id , @ NotNull Class <T > cls , @ NotNull NetworkTableInstance nti ,
78+ @ Nullable String linkAddress ) {
5479 if (!Pattern .matches ("^[A-Za-z][0-4]$" , id )) {
5580 throw new IllegalArgumentException (id );
5681 }
5782
5883 if (link == null ) {
5984 synchronized (Link .class ) {
60- if (link == null ) link = new Link (nti );
85+ if (link == null ) {
86+ link = new Link (nti , linkAddress );
87+ }
6188 }
6289 }
6390
@@ -70,7 +97,9 @@ public Deadeye(String id, Class<T> cls, NetworkTableInstance nti) {
7097
7198 if (!link .isAlive ()) {
7299 synchronized (Link .class ) {
73- if (!link .isAlive ()) link .start ();
100+ if (!link .isAlive ()) {
101+ link .start ();
102+ }
74103 }
75104 }
76105
@@ -87,6 +116,11 @@ public Deadeye(String id, Class<T> cls, NetworkTableInstance nti) {
87116 }
88117 }
89118
119+ static JsonAdapter <Info > getInfoJsonAdapter () {
120+ Moshi moshi = new Moshi .Builder ().build ();
121+ return moshi .adapter (Info .class );
122+ }
123+
90124 /**
91125 * Get TargetDataListener.
92126 *
@@ -107,7 +141,9 @@ public void setTargetDataListener(TargetDataListener<T> targetDataListener) {
107141
108142 public void handleTargetData (BufferedSource source ) throws IOException {
109143 targetData = jsonAdapter .fromJson (source );
110- if (targetDataListener != null ) targetDataListener .onTargetData (targetData );
144+ if (targetDataListener != null ) {
145+ targetDataListener .onTargetData (targetData );
146+ }
111147 }
112148
113149 /**
@@ -135,8 +171,11 @@ public boolean getEnabled() {
135171 * @param enabled true to turn camera on.
136172 */
137173 public void setEnabled (boolean enabled ) {
138- if (enabled ) table .getEntry ("On" ).setBoolean (true );
139- else table .getEntry ("Off" ).setBoolean (true );
174+ if (enabled ) {
175+ table .getEntry ("On" ).setBoolean (true );
176+ } else {
177+ table .getEntry ("Off" ).setBoolean (true );
178+ }
140179 }
141180
142181 /**
@@ -157,8 +196,11 @@ public boolean getLightEnabled() {
157196 */
158197 public void setLightEnabled (boolean enabled ) {
159198 NetworkTable light = table .getSubTable ("Light" );
160- if (enabled ) light .getEntry ("On" ).setBoolean (true );
161- else light .getEntry ("Off" ).setBoolean (true );
199+ if (enabled ) {
200+ light .getEntry ("On" ).setBoolean (true );
201+ } else {
202+ light .getEntry ("Off" ).setBoolean (true );
203+ }
162204 }
163205
164206 /**
@@ -189,16 +231,12 @@ public Info getInfo() {
189231 return info ;
190232 }
191233
192- static JsonAdapter <Info > getInfoJsonAdapter () {
193- Moshi moshi = new Moshi .Builder ().build ();
194- return moshi .adapter (Info .class );
195- }
196-
197234 Link getLink () {
198235 return link ;
199236 }
200237
201238 static class Info {
239+
202240 private final boolean logging ;
203241 private final String pipeline ;
204242 private final String version ;
@@ -211,8 +249,12 @@ public Info(boolean logging, String pipeline, String version) {
211249
212250 @ Override
213251 public boolean equals (Object o ) {
214- if (this == o ) return true ;
215- if (o == null || getClass () != o .getClass ()) return false ;
252+ if (this == o ) {
253+ return true ;
254+ }
255+ if (o == null || getClass () != o .getClass ()) {
256+ return false ;
257+ }
216258 Info info = (Info ) o ;
217259 return logging == info .logging
218260 && pipeline .equals (info .pipeline )
0 commit comments