18
18
package org .photonvision .vision .processes ;
19
19
20
20
import java .util .ArrayList ;
21
+ import java .util .List ;
21
22
import java .util .Map ;
23
+ import java .util .concurrent .locks .ReentrantLock ;
22
24
import org .apache .commons .lang3 .tuple .Pair ;
23
25
import org .opencv .core .Point ;
24
26
import org .photonvision .common .dataflow .DataChangeSubscriber ;
39
41
public class VisionModuleChangeSubscriber extends DataChangeSubscriber {
40
42
private final VisionModule parentModule ;
41
43
private final Logger logger ;
44
+ private List <VisionModuleChange <?>> settingChanges = new ArrayList <>();
45
+ private final ReentrantLock changeListLock = new ReentrantLock ();
42
46
43
47
public VisionModuleChangeSubscriber (VisionModule parentModule ) {
44
48
this .parentModule = parentModule ;
@@ -54,28 +58,47 @@ public void onDataChangeEvent(DataChangeEvent<?> event) {
54
58
if (event instanceof IncomingWebSocketEvent ) {
55
59
var wsEvent = (IncomingWebSocketEvent <?>) event ;
56
60
57
- // Camera index -1 means a "multicast event" (i.e. the event is received by all cameras)
61
+ // Camera index -1 means a "multicast event" (i.e. the event is received by all
62
+ // cameras)
58
63
if (wsEvent .cameraIndex != null
59
64
&& (wsEvent .cameraIndex == parentModule .moduleIndex || wsEvent .cameraIndex == -1 )) {
60
65
logger .trace ("Got PSC event - propName: " + wsEvent .propertyName );
66
+ changeListLock .lock ();
67
+ try {
68
+ getSettingChanges ()
69
+ .add (
70
+ new VisionModuleChange (
71
+ wsEvent .propertyName ,
72
+ wsEvent .data ,
73
+ parentModule .pipelineManager .getCurrentPipeline ().getSettings (),
74
+ wsEvent .originContext ));
75
+ } finally {
76
+ changeListLock .unlock ();
77
+ }
78
+ }
79
+ }
80
+ }
61
81
62
- var propName = wsEvent . propertyName ;
63
- var newPropValue = wsEvent . data ;
64
- var currentSettings = parentModule . pipelineManager . getCurrentPipeline (). getSettings ();
82
+ public List < VisionModuleChange <?>> getSettingChanges () {
83
+ return settingChanges ;
84
+ }
65
85
66
- // special case for non-PipelineSetting changes
86
+ public void processSettingChanges () {
87
+ // special case for non-PipelineSetting changes
88
+ changeListLock .lock ();
89
+ try {
90
+ for (var change : settingChanges ) {
91
+ var propName = change .getPropName ();
92
+ var newPropValue = change .getNewPropValue ();
93
+ var currentSettings = change .getCurrentSettings ();
94
+ var originContext = change .getOriginContext ();
67
95
switch (propName ) {
68
- // case "cameraNickname": // rename camera
69
- // var newNickname = (String) newPropValue;
70
- // logger.info("Changing nickname to " + newNickname);
71
- // parentModule.setCameraNickname(newNickname);
72
- // return;
73
96
case "pipelineName" : // rename current pipeline
74
97
logger .info ("Changing nick to " + newPropValue );
75
98
parentModule .pipelineManager .getCurrentPipelineSettings ().pipelineNickname =
76
99
(String ) newPropValue ;
77
100
parentModule .saveAndBroadcastAll ();
78
- return ;
101
+ continue ;
79
102
case "newPipelineInfo" : // add new pipeline
80
103
var typeName = (Pair <String , PipelineType >) newPropValue ;
81
104
var type = typeName .getRight ();
@@ -86,23 +109,23 @@ public void onDataChangeEvent(DataChangeEvent<?> event) {
86
109
var addedSettings = parentModule .pipelineManager .addPipeline (type );
87
110
addedSettings .pipelineNickname = name ;
88
111
parentModule .saveAndBroadcastAll ();
89
- return ;
112
+ continue ;
90
113
case "deleteCurrPipeline" :
91
114
var indexToDelete = parentModule .pipelineManager .getRequestedIndex ();
92
115
logger .info ("Deleting current pipe at index " + indexToDelete );
93
116
int newIndex = parentModule .pipelineManager .removePipeline (indexToDelete );
94
117
parentModule .setPipeline (newIndex );
95
118
parentModule .saveAndBroadcastAll ();
96
- return ;
119
+ continue ;
97
120
case "changePipeline" : // change active pipeline
98
121
var index = (Integer ) newPropValue ;
99
122
if (index == parentModule .pipelineManager .getRequestedIndex ()) {
100
123
logger .debug ("Skipping pipeline change, index " + index + " already active" );
101
- return ;
124
+ continue ;
102
125
}
103
126
parentModule .setPipeline (index );
104
127
parentModule .saveAndBroadcastAll ();
105
- return ;
128
+ continue ;
106
129
case "startCalibration" :
107
130
try {
108
131
var data =
@@ -113,25 +136,25 @@ public void onDataChangeEvent(DataChangeEvent<?> event) {
113
136
} catch (Exception e ) {
114
137
logger .error ("Error deserailizing start-cal request" , e );
115
138
}
116
- return ;
139
+ continue ;
117
140
case "saveInputSnapshot" :
118
141
parentModule .saveInputSnapshot ();
119
- return ;
142
+ continue ;
120
143
case "saveOutputSnapshot" :
121
144
parentModule .saveOutputSnapshot ();
122
- return ;
145
+ continue ;
123
146
case "takeCalSnapshot" :
124
147
parentModule .takeCalibrationSnapshot ();
125
- return ;
148
+ continue ;
126
149
case "duplicatePipeline" :
127
150
int idx = parentModule .pipelineManager .duplicatePipeline ((Integer ) newPropValue );
128
151
parentModule .setPipeline (idx );
129
152
parentModule .saveAndBroadcastAll ();
130
- return ;
153
+ continue ;
131
154
case "calibrationUploaded" :
132
155
if (newPropValue instanceof CameraCalibrationCoefficients )
133
156
parentModule .addCalibrationToConfig ((CameraCalibrationCoefficients ) newPropValue );
134
- return ;
157
+ continue ;
135
158
case "robotOffsetPoint" :
136
159
if (currentSettings instanceof AdvancedPipelineSettings ) {
137
160
var curAdvSettings = (AdvancedPipelineSettings ) currentSettings ;
@@ -176,14 +199,14 @@ public void onDataChangeEvent(DataChangeEvent<?> event) {
176
199
}
177
200
}
178
201
}
179
- return ;
202
+ continue ;
180
203
case "changePipelineType" :
181
204
parentModule .changePipelineType ((Integer ) newPropValue );
182
205
parentModule .saveAndBroadcastAll ();
183
- return ;
206
+ continue ;
184
207
case "isDriverMode" :
185
208
parentModule .setDriverMode ((Boolean ) newPropValue );
186
- return ;
209
+ continue ;
187
210
}
188
211
189
212
// special case for camera settables
@@ -218,8 +241,11 @@ public void onDataChangeEvent(DataChangeEvent<?> event) {
218
241
logger .error ("Unknown exception when setting PSC prop!" , e );
219
242
}
220
243
221
- parentModule .saveAndBroadcastSelective (wsEvent . originContext , propName , newPropValue );
244
+ parentModule .saveAndBroadcastSelective (originContext , propName , newPropValue );
222
245
}
246
+ getSettingChanges ().clear ();
247
+ } finally {
248
+ changeListLock .unlock ();
223
249
}
224
250
}
225
251
0 commit comments