35
35
import com .google .common .eventbus .Subscribe ;
36
36
import net .miginfocom .swing .MigLayout ;
37
37
import pt .lsts .imc .EntityState ;
38
+ import pt .lsts .imc .IMCMessage ;
38
39
import pt .lsts .imc .RemoteActions ;
39
40
import pt .lsts .imc .RemoteActionsRequest ;
40
41
import pt .lsts .imc .VehicleState ;
50
51
import pt .lsts .neptus .plugins .Popup ;
51
52
import pt .lsts .neptus .plugins .update .Periodic ;
52
53
import pt .lsts .neptus .util .MathMiscUtils ;
54
+ import pt .lsts .neptus .util .PropertiesLoader ;
55
+ import pt .lsts .neptus .util .conf .ConfigFetch ;
53
56
54
57
import javax .swing .JButton ;
55
58
import javax .swing .JLabel ;
56
59
import javax .swing .SwingConstants ;
57
60
import java .awt .event .KeyEvent ;
61
+ import java .io .File ;
62
+ import java .io .IOException ;
58
63
import java .util .ArrayList ;
59
64
import java .util .Arrays ;
60
65
import java .util .Collections ;
66
+ import java .util .Enumeration ;
61
67
import java .util .LinkedHashMap ;
62
68
import java .util .List ;
63
69
import java .util .Map ;
70
76
description = "This plugin listen for non motion related remote actions and displays its controls." )
71
77
@ Popup (name = "Remote Actions Extra" , width = 300 , height = 200 , pos = Popup .POSITION .BOTTOM , accelerator = KeyEvent .VK_3 )
72
78
public class RemoteActionsExtra extends ConsolePanel implements MainVehicleChangeListener , ConfigurationListener {
79
+ public static final String REMOTE_ACTIONS_EXTRA_PROPERTIES_FILE = ".cache/db/remote-actions-extra.properties" ;
73
80
74
81
static final boolean DEFAULT_AXIS_DECIMAL_VAL = false ;
75
82
private static final int DECIMAL_HOUSES_FOR_DECIMAL_AXIS = 6 ;
@@ -129,6 +136,44 @@ enum ActionTypeEnum {
129
136
130
137
private final TakeControlMonitor takeControlMonitor ;
131
138
139
+ private static PropertiesLoader properties = null ;
140
+ static {
141
+ String propertiesFile = ConfigFetch .resolvePathBasedOnConfigFile (REMOTE_ACTIONS_EXTRA_PROPERTIES_FILE );
142
+ if (!new File (propertiesFile ).exists ()) {
143
+ String testFile = ConfigFetch .resolvePathBasedOnConfigFile ("../" + REMOTE_ACTIONS_EXTRA_PROPERTIES_FILE );
144
+ if (new File (testFile ).exists ())
145
+ propertiesFile = testFile ;
146
+ }
147
+ new File (propertiesFile ).getParentFile ().mkdirs ();
148
+ properties = new PropertiesLoader (propertiesFile , PropertiesLoader .PROPERTIES );
149
+
150
+ Enumeration <Object > it = properties .keys ();
151
+ while (it .hasMoreElements ()) {
152
+ String key = it .nextElement ().toString ();
153
+ String value = properties .getProperty (key );
154
+ setRemoteActionsExtra (key , value , false );
155
+ }
156
+ }
157
+
158
+ private static void saveProperties () {
159
+ try {
160
+ properties .store ("RemoteActionsExtra properties" );
161
+ }
162
+ catch (IOException e ) {
163
+ NeptusLog .pub ().error ("saveProperties" , e );
164
+ }
165
+ }
166
+
167
+ private static void setRemoteActionsExtra (String system , String actionsStr , boolean save ) {
168
+ if (save ) {
169
+ String old = properties .getProperty (system );
170
+ if (old == null || !old .equals (actionsStr )) {
171
+ properties .setProperty (system , actionsStr );
172
+ saveProperties ();
173
+ }
174
+ }
175
+ }
176
+
132
177
@ NeptusProperty (name = "OBS Entity Name" , userLevel = NeptusProperty .LEVEL .ADVANCED ,
133
178
description = "Used to check the state of the OBS take control status." )
134
179
public String obsEntityName = "OBS Broker" ;
@@ -145,6 +190,7 @@ public RemoteActionsExtra(ConsoleLayout console, boolean usedInsideAnotherConsol
145
190
146
191
@ Override
147
192
public void initSubPanel () {
193
+ updateForMainSystems ();
148
194
resetUIWithActions ();
149
195
}
150
196
@@ -165,64 +211,66 @@ private synchronized void resetUIWithActions() {
165
211
removeAll ();
166
212
setLayout (new MigLayout ("insets 10px" ));
167
213
168
- if (extraActionsTypesMap .isEmpty ()) {
169
- add (new JLabel ("No actions available" , SwingConstants .CENTER ), "dock center" );
170
- invalidate ();
171
- validate ();
172
- repaint (100 );
173
- return ;
174
- }
175
-
176
- // Let us process the actions list
177
- List <List <String >> groupedActions = groupActionsBySimilarity (extraActionsTypesMap .keySet (), true );
178
- groupedActions = processActions (groupedActions , 2 , false );
179
-
180
- int grpIdx = 0 ;
181
- for (List <String > grp1 : groupedActions ) {
182
- grpIdx ++;
183
- String lastAct = grp1 .get (grp1 .size () - 1 );
184
- for (String action : grp1 ) {
185
- String wrapLay = "" ;
186
- if (lastAct .equals (action )) {
187
- wrapLay = "wrap" ;
188
- }
214
+ synchronized (extraActionsTypesMap ) {
215
+ if (extraActionsTypesMap .isEmpty ()) {
216
+ add (new JLabel ("No actions available" , SwingConstants .CENTER ), "dock center" );
217
+ invalidate ();
218
+ validate ();
219
+ repaint (100 );
220
+ return ;
221
+ }
189
222
190
- boolean provideLock = false ;
191
- if (curState .extraActionsLocksMap .containsKey (action )) {
192
- Boolean v = curState .extraActionsLocksMap .get (action );
193
- if (v != null && v ) {
194
- provideLock = true ;
223
+ // Let us process the actions list
224
+ List <List <String >> groupedActions = groupActionsBySimilarity (extraActionsTypesMap .keySet (), true );
225
+ groupedActions = processActions (groupedActions , 2 , false );
226
+
227
+ int grpIdx = 0 ;
228
+ for (List <String > grp1 : groupedActions ) {
229
+ grpIdx ++;
230
+ String lastAct = grp1 .get (grp1 .size () - 1 );
231
+ for (String action : grp1 ) {
232
+ String wrapLay = "" ;
233
+ if (lastAct .equals (action )) {
234
+ wrapLay = "wrap" ;
195
235
}
196
- }
197
236
198
- switch (extraActionsTypesMap .get (action )) {
199
- case BUTTON :
200
- boolean isToProvideLock = provideLock || isActionForLock (action );
201
- JButton button = isToProvideLock ? new HoldFillButton (action , 2000 ) : new JButton (action );
202
- button .addActionListener (e -> {
203
- curState .changeButtonActionValue (action , 1 );
204
- });
205
- String lay = "dock center, sg grp" + grpIdx ;
206
- lay += ", " + wrapLay ;
207
- add (button , lay );
208
- if (isToProvideLock ) {
209
- extraLockableButtons .add (button );
237
+ boolean provideLock = false ;
238
+ if (curState .extraActionsLocksMap .containsKey (action )) {
239
+ Boolean v = curState .extraActionsLocksMap .get (action );
240
+ if (v != null && v ) {
241
+ provideLock = true ;
210
242
}
211
- if ("Take Control" .equalsIgnoreCase (action )) {
212
- takeControlMonitor .setButton (button );
213
- takeControlMonitor .askedControl ();
214
- }
215
- break ;
216
- case AXIS :
217
- // TODO
218
- case SLIDER :
219
- // TODO
220
- case HALF_SLIDER :
221
- // TODO
222
- break ;
223
- }
224
- }
225
- }
243
+ }
244
+
245
+ switch (extraActionsTypesMap .get (action )) {
246
+ case BUTTON :
247
+ boolean isToProvideLock = provideLock || isActionForLock (action );
248
+ JButton button = isToProvideLock ? new HoldFillButton (action , 2000 ) : new JButton (action );
249
+ button .addActionListener (e -> {
250
+ curState .changeButtonActionValue (action , 1 );
251
+ });
252
+ String lay = "dock center, sg grp" + grpIdx ;
253
+ lay += ", " + wrapLay ;
254
+ add (button , lay );
255
+ if (isToProvideLock ) {
256
+ extraLockableButtons .add (button );
257
+ }
258
+ if ("Take Control" .equalsIgnoreCase (action )) {
259
+ takeControlMonitor .setButton (button );
260
+ takeControlMonitor .askedControl ();
261
+ }
262
+ break ;
263
+ case AXIS :
264
+ // TODO
265
+ case SLIDER :
266
+ // TODO
267
+ case HALF_SLIDER :
268
+ // TODO
269
+ break ;
270
+ }
271
+ }
272
+ }
273
+ }
226
274
227
275
invalidate ();
228
276
validate ();
@@ -232,9 +280,22 @@ private synchronized void resetUIWithActions() {
232
280
@ Subscribe
233
281
public void on (ConsoleEventMainSystemChange evt ) {
234
282
configureActions ("" , DEFAULT_AXIS_DECIMAL_VAL , false );
283
+ updateForMainSystems ();
235
284
takeControlMonitor .on (evt );
236
285
}
237
286
287
+ private void updateForMainSystems () {
288
+ String actionsString = "" ;
289
+ try {
290
+ if (properties .containsKey (getConsole ().getMainSystem ())) {
291
+ actionsString = (String ) properties .get (getConsole ().getMainSystem ());
292
+ }
293
+ } catch (Exception e ) {
294
+ NeptusLog .pub ().error (e .getMessage ());
295
+ }
296
+ configureActions (actionsString , DEFAULT_AXIS_DECIMAL_VAL , false );
297
+ }
298
+
238
299
@ Subscribe
239
300
public void on (RemoteActionsRequest msg ) {
240
301
if (!msg .getSourceName ().equals (getMainVehicleId ())) {
@@ -243,7 +304,8 @@ public void on(RemoteActionsRequest msg) {
243
304
244
305
if (msg .getOp () != RemoteActionsRequest .OP .REPORT ) return ;
245
306
246
- configureActions (msg .getAsString ("actions" ), DEFAULT_AXIS_DECIMAL_VAL , false );
307
+ setRemoteActionsExtra (msg .getSourceName (), IMCMessage .encodeTupleList (msg .getActions ()), true );
308
+ configureActions (msg .getActions (), DEFAULT_AXIS_DECIMAL_VAL , false );
247
309
}
248
310
249
311
@ Subscribe
0 commit comments