1717
1818package org .photonvision .vision .frame .consumer ;
1919
20- import edu .wpi .first .math .MathUtil ;
2120import edu .wpi .first .networktables .IntegerEntry ;
2221import edu .wpi .first .networktables .IntegerSubscriber ;
2322import edu .wpi .first .networktables .NetworkTable ;
2423import edu .wpi .first .networktables .StringSubscriber ;
24+ import edu .wpi .first .wpilibj .DriverStation .MatchType ;
2525import java .io .File ;
2626import java .text .DateFormat ;
2727import java .text .SimpleDateFormat ;
3838public class FileSaveFrameConsumer implements Consumer <CVMat > {
3939 private final Logger logger = new Logger (FileSaveFrameConsumer .class , LogGroup .General );
4040
41- // match type's values from the FMS.
42- private static final String [] matchTypes = {"N/A" , "P" , "Q" , "E" , "EV" };
43-
4441 // Formatters to generate unique, timestamped file names
4542 private static final String FILE_PATH = ConfigManager .getInstance ().getImageSavePath ().toString ();
46- private static final String FILE_EXTENSION = ".jpg" ;
43+ static final String FILE_EXTENSION = ".jpg" ;
4744 private static final String NT_SUFFIX = "SaveImgCmd" ;
4845
49- DateFormat df = new SimpleDateFormat ("yyyy-MM-dd" );
50- DateFormat tf = new SimpleDateFormat ("hhmmssSS" );
46+ static final DateFormat df = new SimpleDateFormat ("yyyy-MM-dd" );
47+ static final DateFormat tf = new SimpleDateFormat ("hhmmssSS" );
5148
5249 private final NetworkTable rootTable ;
5350 private NetworkTable subTable ;
5451 private final String ntEntryName ;
55- private IntegerEntry saveFrameEntry ;
52+ IntegerEntry saveFrameEntry ;
5653
5754 private StringSubscriber ntEventName ;
5855 private IntegerSubscriber ntMatchNum ;
@@ -74,21 +71,27 @@ public FileSaveFrameConsumer(String camNickname, String cameraUniqueName, String
7471
7572 NetworkTable fmsTable = NetworkTablesManager .getInstance ().getNTInst ().getTable ("FMSInfo" );
7673 this .ntEventName = fmsTable .getStringTopic ("EventName" ).subscribe ("UNKNOWN" );
77- this .ntMatchNum = fmsTable .getIntegerTopic ("MatchNumber" ).subscribe (- 1 );
74+ this .ntMatchNum = fmsTable .getIntegerTopic ("MatchNumber" ).subscribe (0 );
7875 this .ntMatchType = fmsTable .getIntegerTopic ("MatchType" ).subscribe (0 );
7976
8077 updateCameraNickname (camNickname );
8178 }
8279
8380 public void accept (CVMat image ) {
81+ accept (image , new Date ());
82+ }
83+
84+ public void accept (CVMat image , Date now ) {
8485 long currentCount = saveFrameEntry .get ();
8586
87+ System .out .println ("currentCount: " + currentCount + " savedImagesCount: " + savedImagesCount );
88+
8689 // Await save request
8790 if (currentCount == -1 ) return ;
8891
8992 // The requested count is greater than the actual count
9093 if (savedImagesCount < currentCount ) {
91- Date now = new Date ();
94+ String matchData = getMatchData ();
9295
9396 String fileName =
9497 cameraNickname
@@ -99,7 +102,7 @@ public void accept(CVMat image) {
99102 + "T"
100103 + tf .format (now )
101104 + "_"
102- + getMatchData () ;
105+ + matchData ;
103106
104107 // Check if the Unique Camera directory exists and create it if it doesn't
105108 String cameraPath = FILE_PATH + File .separator + this .cameraUniqueName ;
@@ -110,6 +113,7 @@ public void accept(CVMat image) {
110113
111114 String saveFilePath = cameraPath + File .separator + fileName + FILE_EXTENSION ;
112115
116+ logger .info ("Saving image to: " + saveFilePath );
113117 if (image == null || image .getMat () == null || image .getMat ().empty ()) {
114118 Imgcodecs .imwrite (saveFilePath , StaticFrames .LOST_MAT );
115119 } else {
@@ -146,28 +150,33 @@ public void overrideTakeSnapshot() {
146150
147151 /**
148152 * Returns the match Data collected from the NT. eg : Q58 for qualification match 58. If not in
149- * event, returns N/A -0-EVENTNAME
153+ * event, returns None -0-Unknown
150154 */
151155 private String getMatchData () {
152156 var matchType = ntMatchType .getAtomic ();
153157 if (matchType .timestamp == 0 ) {
154158 // no NT info yet
155- logger .warn ("Did not receive match type, defaulting to 0 " );
159+ logger .warn ("Did not receive match type, defaulting to None " );
156160 }
157161
158162 var matchNum = ntMatchNum .getAtomic ();
159163 if (matchNum .timestamp == 0 ) {
160- logger .warn ("Did not receive match num, defaulting to -1 " );
164+ logger .warn ("Did not receive match num, defaulting to 0 " );
161165 }
162166
163167 var eventName = ntEventName .getAtomic ();
164168 if (eventName .timestamp == 0 ) {
165169 logger .warn ("Did not receive event name, defaulting to 'UNKNOWN'" );
166170 }
167171
168- String matchTypeStr =
169- matchTypes [MathUtil .clamp ((int ) matchType .value , 0 , matchTypes .length - 1 )];
170- return matchTypeStr + "-" + matchNum .value + "-" + eventName .value ;
172+ MatchType wpiMatchType = MatchType .None ; // Default is to be unknown
173+ if (matchType .value < 0 || matchType .value >= MatchType .values ().length ) {
174+ logger .error ("Invalid match type from FMS: " + matchType .value );
175+ } else {
176+ wpiMatchType = MatchType .values ()[(int ) matchType .value ];
177+ }
178+
179+ return wpiMatchType .name () + "-" + matchNum .value + "-" + eventName .value ;
171180 }
172181
173182 public void close () {
0 commit comments