001package jmri.implementation;
002
003import java.io.File;
004import java.util.Date;
005import java.util.List;
006
007import javax.annotation.Nonnull;
008import javax.script.ScriptException;
009import javax.swing.Timer;
010
011import jmri.*;
012import jmri.implementation.DefaultConditional.TimeSensor;
013import jmri.implementation.DefaultConditional.TimeTurnout;
014import jmri.jmrit.Sound;
015import jmri.jmrit.audio.AudioListener;
016import jmri.jmrit.audio.AudioSource;
017import jmri.jmrit.entryexit.DestinationPoints;
018import jmri.jmrit.logix.OBlock;
019import jmri.jmrit.logix.Warrant;
020import jmri.script.JmriScriptEngineManager;
021import jmri.script.swing.ScriptOutput;
022
023/**
024 * Helper class for DefaultConditional that executes the  actions of a
025 * DefaultConditional.
026 * @author Daniel Bergqvist (C) 2021
027 */
028public class DefaultConditionalExecute {
029
030    private final DefaultConditional conditional;
031
032    DefaultConditionalExecute(@Nonnull DefaultConditional conditional) {
033        this.conditional = conditional;
034    }
035
036    void setTurnout(@Nonnull ConditionalAction action, Turnout t, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
037        if (t == null) {
038            errorList.add("invalid turnout name in action - " + action.getDeviceName());  // NOI18N
039        } else {
040            int act = action.getActionData();
041            if (act == Route.TOGGLE) {
042                int state = t.getKnownState();
043                if (state == Turnout.CLOSED) {
044                    act = Turnout.THROWN;
045                } else {
046                    act = Turnout.CLOSED;
047                }
048            }
049            t.setCommandedState(act);
050            increaseCounter(actionCount);
051        }
052    }
053
054    void delayedTurnout(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull TimeTurnout timeTurnout, boolean reset, String devName) {
055        if (reset) action.stopTimer();
056        if (!action.isTimerActive()) {
057            // Create a timer if one does not exist
058            Timer timer = action.getTimer();
059            if (timer == null) {
060                action.setListener(timeTurnout);
061                timer = new Timer(2000, action.getListener());
062                timer.setRepeats(true);
063            }
064            // Start the Timer to set the turnout
065            int value = conditional.getMillisecondValue(action);
066            if (value < 0) {
067                return;
068            }
069            timer.setInitialDelay(value);
070            action.setTimer(timer);
071            action.startTimer();
072            increaseCounter(actionCount);
073        } else {
074            log.warn("timer already active on request to start delayed turnout action - {}", devName);
075        }
076    }
077
078    void cancelTurnoutTimers(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
079        ConditionalManager cmg = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
080        java.util.Iterator<Conditional> iter = cmg.getNamedBeanSet().iterator();
081        while (iter.hasNext()) {
082            String sname = iter.next().getSystemName();
083
084            Conditional c = cmg.getBySystemName(sname);
085            if (c == null) {
086                errorList.add("Conditional null during cancel turnout timers for "  // NOI18N
087                        + action.getDeviceName());
088                continue; // no more processing of this one
089            }
090
091            c.cancelTurnoutTimer(devName);
092            increaseCounter(actionCount);
093        }
094    }
095
096    void lockTurnout(@Nonnull ConditionalAction action, Turnout tl, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
097        if (tl == null) {
098            errorList.add("invalid turnout name in action - " + action.getDeviceName());  // NOI18N
099        } else {
100            int act = action.getActionData();
101            if (act == Route.TOGGLE) {
102                if (tl.getLocked(Turnout.CABLOCKOUT)) {
103                    act = Turnout.UNLOCKED;
104                } else {
105                    act = Turnout.LOCKED;
106                }
107            }
108            if (act == Turnout.LOCKED) {
109                tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
110            } else if (act == Turnout.UNLOCKED) {
111                tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, false);
112            }
113            increaseCounter(actionCount);
114        }
115    }
116
117    void setSignalAppearance(@Nonnull ConditionalAction action, SignalHead h, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
118        if (h == null) {
119            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
120        } else {
121            h.setAppearance(action.getActionData());
122            increaseCounter(actionCount);
123        }
124    }
125
126    void setSignalHeld(@Nonnull ConditionalAction action, SignalHead h, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
127        if (h == null) {
128            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
129        } else {
130            h.setHeld(true);
131            increaseCounter(actionCount);
132        }
133    }
134
135    void clearSignalHeld(@Nonnull ConditionalAction action, SignalHead h, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
136        if (h == null) {
137            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
138        } else {
139            h.setHeld(false);
140            increaseCounter(actionCount);
141        }
142    }
143
144    void setSignalDark(@Nonnull ConditionalAction action, SignalHead h, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
145        if (h == null) {
146            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
147        } else {
148            h.setLit(false);
149            increaseCounter(actionCount);
150        }
151    }
152
153    void setSignalLit(@Nonnull ConditionalAction action, SignalHead h, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
154        if (h == null) {
155            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
156        } else {
157            h.setLit(true);
158            increaseCounter(actionCount);
159        }
160    }
161
162    void triggerRoute(@Nonnull ConditionalAction action, Route r, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
163        if (r == null) {
164            errorList.add("invalid Route name in action - " + action.getDeviceName());  // NOI18N
165        } else {
166            r.setRoute();
167            increaseCounter(actionCount);
168        }
169    }
170
171    void setSensor(@Nonnull ConditionalAction action, Sensor sn, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
172        if (sn == null) {
173            errorList.add("invalid Sensor name in action - " + action.getDeviceName());  // NOI18N
174        } else {
175            int act = action.getActionData();
176            if (act == Route.TOGGLE) {
177                int state = sn.getState();
178                if (state == Sensor.ACTIVE) {
179                    act = Sensor.INACTIVE;
180                } else {
181                    act = Sensor.ACTIVE;
182                }
183            }
184            try {
185                sn.setKnownState(act);
186                increaseCounter(actionCount);
187            } catch (JmriException e) {
188                log.warn("Exception setting Sensor {} in action", devName);  // NOI18N
189            }
190        }
191    }
192
193    void delayedSensor(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull TimeSensor timeSensor, int delay, boolean reset, String devName) {
194        if (reset) action.stopTimer();
195        if (!action.isTimerActive()) {
196            // Create a timer if one does not exist
197            Timer timer = action.getTimer();
198            if (timer == null) {
199                action.setListener(timeSensor);
200                timer = new Timer(2000, action.getListener());
201                timer.setRepeats(true);
202            }
203            // Start the Timer to set the sensor
204            if (delay < 0) {
205                return;
206            }
207            timer.setInitialDelay(delay);
208            action.setTimer(timer);
209            action.startTimer();
210            increaseCounter(actionCount);
211        } else {
212            log.warn("timer already active on request to start delayed sensor action - {}", devName);
213        }
214    }
215
216    void cancelSensorTimers(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
217        ConditionalManager cm = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
218        java.util.Iterator<Conditional> itr = cm.getNamedBeanSet().iterator();
219        while (itr.hasNext()) {
220            String sname = itr.next().getSystemName();
221            Conditional c = cm.getBySystemName(sname);
222            if (c == null) {
223                errorList.add("Conditional null during cancel sensor timers for "  // NOI18N
224                        + action.getDeviceName());
225                continue; // no more processing of this one
226            }
227
228            c.cancelSensorTimer(devName);
229            increaseCounter(actionCount);
230        }
231    }
232
233    void setLight(@Nonnull ConditionalAction action, Light lgt, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
234        if (lgt == null) {
235            errorList.add("invalid light name in action - " + action.getDeviceName());  // NOI18N
236        } else {
237            int act = action.getActionData();
238            if (act == Route.TOGGLE) {
239                int state = lgt.getState();
240                if (state == Light.ON) {
241                    act = Light.OFF;
242                } else {
243                    act = Light.ON;
244                }
245            }
246            lgt.setState(act);
247            increaseCounter(actionCount);
248        }
249    }
250
251    void setLightIntensity(@Nonnull ConditionalAction action, Light lgt, int intensity, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
252        if (lgt == null) {
253            errorList.add("invalid light name in action - " + action.getDeviceName());  // NOI18N
254        } else {
255            try {
256                if (intensity < 0) {
257                    return;
258                }
259                if (lgt instanceof VariableLight) {
260                    ((VariableLight)lgt).setTargetIntensity((intensity) / 100.0);
261                } else {
262                    lgt.setState(intensity > 0.5 ? Light.ON : Light.OFF);
263                }
264                increaseCounter(actionCount);
265            } catch (IllegalArgumentException e) {
266                errorList.add("Exception in set light intensity action - " + action.getDeviceName());  // NOI18N
267            }
268        }
269    }
270
271    void setLightTransitionTime(@Nonnull ConditionalAction action, Light lgt, int time, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
272        if (lgt == null) {
273            errorList.add("invalid light name in action - " + action.getDeviceName());  // NOI18N
274        } else {
275            try {
276                if (time  < 0) {
277                    return;
278                }
279                if (lgt instanceof VariableLight) {
280                    ((VariableLight)lgt).setTransitionTime(time );
281                }
282                increaseCounter(actionCount);
283            } catch (IllegalArgumentException e) {
284                errorList.add("Exception in set light transition time action - " + action.getDeviceName());  // NOI18N
285            }
286        }
287    }
288
289    void setMemory(@Nonnull ConditionalAction action, Memory m, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
290        if (m == null) {
291            errorList.add("invalid memory name in action - " + action.getDeviceName());  // NOI18N
292        } else {
293            m.setValue(action.getActionString());
294            increaseCounter(actionCount);
295        }
296    }
297
298    void copyMemory(@Nonnull ConditionalAction action, Memory mFrom, Memory mTo, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
299        if (mFrom == null) {
300            errorList.add("invalid memory name in action - " + action.getDeviceName());  // NOI18N
301        } else {
302            if (mTo == null) {
303                errorList.add("invalid memory name in action - " + action.getActionString());  // NOI18N
304            } else {
305                mTo.setValue(mFrom.getValue());
306                increaseCounter(actionCount);
307            }
308        }
309    }
310
311    void enableLogix(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
312        Logix x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
313        if (x == null) {
314            errorList.add("invalid logix name in action - " + action.getDeviceName());  // NOI18N
315        } else {
316            x.setEnabled(true);
317            increaseCounter(actionCount);
318        }
319    }
320
321    void disableLogix(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
322        Logix x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
323        if (x == null) {
324            errorList.add("invalid logix name in action - " + action.getDeviceName());  // NOI18N
325        } else {
326            x.setEnabled(false);
327            increaseCounter(actionCount);
328        }
329    }
330
331    void playSound(@Nonnull ConditionalAction action, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
332        String path = actionStr;
333        if (!path.equals("")) {
334            Sound sound = action.getSound();
335            if (sound == null) {
336                try {
337                    sound = new Sound(path);
338                } catch (NullPointerException ex) {
339                    errorList.add("invalid path to sound: " + path);  // NOI18N
340                }
341            }
342            if (sound != null) {
343                sound.play();
344            }
345            increaseCounter(actionCount);
346        }
347    }
348
349    void runScript(@Nonnull ConditionalAction action, String actionStr, @Nonnull Reference<Integer> actionCount) {
350        if (!(actionStr.equals(""))) {
351            JmriScriptEngineManager.getDefault().runScript(new File(jmri.util.FileUtil.getExternalFilename(actionStr)));
352            increaseCounter(actionCount);
353        }
354    }
355
356    @SuppressWarnings({"deprecation"})  // date.setHours, date.setMinutes, date.setSeconds
357    void setFastClockTime(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount) {
358        Date date = InstanceManager.getDefault(jmri.Timebase.class).getTime();
359        date.setHours(action.getActionData() / 60);
360        date.setMinutes(action.getActionData() - ((action.getActionData() / 60) * 60));
361        date.setSeconds(0);
362        InstanceManager.getDefault(jmri.Timebase.class).userSetTime(date);
363        increaseCounter(actionCount);
364    }
365
366    void startFastClock(@Nonnull Reference<Integer> actionCount) {
367        InstanceManager.getDefault(jmri.Timebase.class).setRun(true);
368        increaseCounter(actionCount);
369    }
370
371    void stopFastClock(@Nonnull Reference<Integer> actionCount) {
372        InstanceManager.getDefault(jmri.Timebase.class).setRun(false);
373        increaseCounter(actionCount);
374    }
375
376    void controlAudio(@Nonnull ConditionalAction action, String devName) {
377        Audio audio = InstanceManager.getDefault(jmri.AudioManager.class).getAudio(devName);
378        if (audio == null) {
379            return;
380        }
381        if (audio.getSubType() == Audio.SOURCE) {
382            AudioSource audioSource = (AudioSource) audio;
383            switch (action.getActionData()) {
384                case Audio.CMD_PLAY:
385                    audioSource.play();
386                    break;
387                case Audio.CMD_STOP:
388                    audioSource.stop();
389                    break;
390                case Audio.CMD_PLAY_TOGGLE:
391                    audioSource.togglePlay();
392                    break;
393                case Audio.CMD_PAUSE:
394                    audioSource.pause();
395                    break;
396                case Audio.CMD_RESUME:
397                    audioSource.resume();
398                    break;
399                case Audio.CMD_PAUSE_TOGGLE:
400                    audioSource.togglePause();
401                    break;
402                case Audio.CMD_REWIND:
403                    audioSource.rewind();
404                    break;
405                case Audio.CMD_FADE_IN:
406                    audioSource.fadeIn();
407                    break;
408                case Audio.CMD_FADE_OUT:
409                    audioSource.fadeOut();
410                    break;
411                case Audio.CMD_RESET_POSITION:
412                    audioSource.resetCurrentPosition();
413                    break;
414                default:
415                    break;
416            }
417        } else if (audio.getSubType() == Audio.LISTENER) {
418            AudioListener audioListener = (AudioListener) audio;
419            switch (action.getActionData()) {
420                case Audio.CMD_RESET_POSITION:
421                    audioListener.resetCurrentPosition();
422                    break;
423                default:
424                    break; // nothing needed for others
425            }
426        }
427    }
428
429    void jythonCommand(@Nonnull ConditionalAction action, String actionStr, @Nonnull Reference<Integer> actionCount) {
430        if (!(actionStr.isEmpty())) {
431            // add the text to the output frame
432            ScriptOutput.writeScript(actionStr);
433            // and execute
434
435            javax.script.ScriptEngine se =  JmriScriptEngineManager.getDefault().getEngine(JmriScriptEngineManager.JYTHON);
436            if (se!=null) {
437                try {
438                    JmriScriptEngineManager.getDefault().eval(actionStr, se);
439                } catch (ScriptException ex) {
440                    log.error("Error executing script:", ex);  // NOI18N
441                }
442            } else {
443                log.error("Error getting default ScriptEngine");
444            }
445            increaseCounter(actionCount);
446        }
447    }
448
449    void allocateWarrantRoute(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
450        if (w == null) {
451            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
452        } else {
453            String msg = w.allocateRoute(false, null);
454            if (msg != null) {
455                log.info("Warrant {} - {}", action.getDeviceName(), msg);  // NOI18N
456            }
457            increaseCounter(actionCount);
458        }
459    }
460
461    void deallocateWarrantRoute(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
462        if (w == null) {
463            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
464        } else {
465            w.deAllocate();
466            increaseCounter(actionCount);
467        }
468    }
469
470    void setRouteTurnouts(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
471        if (w == null) {
472            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
473        } else {
474            String msg = w.setRoute(false, null);
475            if (msg != null) {
476                log.info("Warrant {} unable to Set Route - {}", action.getDeviceName(), msg);  // NOI18N
477            }
478            increaseCounter(actionCount);
479        }
480    }
481
482    void setTrainId(@Nonnull ConditionalAction action, Warrant w, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
483        if (w == null) {
484            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
485        } else {
486            if (w.getRunMode() != Warrant.MODE_NONE) {
487                errorList.add("Cannot set when Warrant is running - " + action.getActionString());  // NOI18N
488            } else if(!w.getSpeedUtil().setAddress(actionStr)) {
489                errorList.add("invalid train ID in action - " + action.getDeviceName());  // NOI18N
490            }
491            increaseCounter(actionCount);
492        }
493    }
494
495    void setTrainName(@Nonnull ConditionalAction action, Warrant w, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
496        if (w == null) {
497            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
498        } else {
499            w.setTrainName(actionStr);
500            increaseCounter(actionCount);
501        }
502    }
503
504    void getTrainLocation(@Nonnull ConditionalAction action, Warrant w, Memory mTo, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
505        if (w == null) {
506            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
507        } else {
508            if (mTo == null) {
509                errorList.add("invalid memory name in action - " + action.getActionString());  // NOI18N
510            } else {
511                mTo.setValue(w.getCurrentBlockName());
512                increaseCounter(actionCount);
513            }
514        }
515    }
516
517    void autoRunWarrant(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
518        if (w == null) {
519            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
520        } else {
521            jmri.jmrit.logix.WarrantTableFrame frame = jmri.jmrit.logix.WarrantTableFrame.getDefault();
522            String err = frame.runTrain(w, Warrant.MODE_RUN);
523            if (err != null) {
524                errorList.add("runAutoTrain error - " + err);  // NOI18N
525                w.stopWarrant(true, true);
526            }
527            increaseCounter(actionCount);
528        }
529    }
530
531    void manualRunWarrant(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
532        if (w == null) {
533            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
534        } else {
535            if (w.getRunMode() != Warrant.MODE_NONE) {
536                errorList.add("Cannot set when Warrant is running - " + action.getActionString());  // NOI18N
537            } else {
538                String err = w.setRoute(false, null);
539                if (err == null) {
540                    err = w.setRunMode(Warrant.MODE_MANUAL, null, null, null, false);
541                }
542                if (err != null) {
543                    errorList.add("runManualTrain error - " + err);  // NOI18N
544                }
545            }
546            increaseCounter(actionCount);
547        }
548    }
549
550    void controlTrain(@Nonnull ConditionalAction action, Warrant w, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
551        if (w == null) {
552            errorList.add("invalid Warrant name in action - " + action.getDeviceName());  // NOI18N
553        } else {
554            if (!w.controlRunTrain(action.getActionData())) {
555                log.info("Train {} not running  - {}", w.getSpeedUtil().getRosterId(), devName);  // NOI18N
556            }
557            increaseCounter(actionCount);
558        }
559    }
560
561    void setSignalMastAspect(@Nonnull ConditionalAction action, SignalMast f, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
562        if (f == null) {
563            errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());  // NOI18N
564        } else {
565            f.setAspect(actionStr);
566            increaseCounter(actionCount);
567        }
568    }
569
570    void setSignalMastHeld(@Nonnull ConditionalAction action, SignalMast f, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
571        if (f == null) {
572            errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());  // NOI18N
573        } else {
574            f.setHeld(true);
575            increaseCounter(actionCount);
576        }
577    }
578
579    void clearSignalMastHeld(@Nonnull ConditionalAction action, SignalMast f, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
580        if (f == null) {
581            errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());  // NOI18N
582        } else {
583            f.setHeld(false);
584            increaseCounter(actionCount);
585        }
586    }
587
588    void setSignalMastDark(@Nonnull ConditionalAction action, SignalMast f, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
589        if (f == null) {
590            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
591        } else {
592            f.setLit(false);
593            increaseCounter(actionCount);
594        }
595    }
596
597    void setSignalMastLit(@Nonnull ConditionalAction action, SignalMast f, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
598        if (f == null) {
599            errorList.add("invalid Signal Head name in action - " + action.getDeviceName());  // NOI18N
600        } else {
601            f.setLit(true);
602            increaseCounter(actionCount);
603        }
604    }
605
606    void setBlockValue(@Nonnull ConditionalAction action, OBlock b, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
607        if (b == null) {
608            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
609        } else {
610            b.setValue(actionStr);
611            increaseCounter(actionCount);
612        }
613    }
614
615    void setBlockError(@Nonnull ConditionalAction action, OBlock b, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
616        if (b == null) {
617            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
618        } else {
619            b.setError(true);
620            increaseCounter(actionCount);
621        }
622    }
623
624    void clearBlockError(@Nonnull ConditionalAction action, OBlock b, @Nonnull List<String> errorList) {
625        if (b == null) {
626            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
627        } else {
628            b.setError(false);
629        }
630    }
631
632    void deallocateBlock(@Nonnull ConditionalAction action, OBlock b, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
633        if (b == null) {
634            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
635        } else {
636            b.deAllocate(null);
637            increaseCounter(actionCount);
638        }
639    }
640
641    void setBlockOutOfService(@Nonnull ConditionalAction action, OBlock b, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
642        if (b == null) {
643            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
644        } else {
645            b.setOutOfService(true);
646            increaseCounter(actionCount);
647        }
648    }
649
650    void setBlockInService(@Nonnull ConditionalAction action, OBlock b, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
651        if (b == null) {
652            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
653        } else {
654            b.setOutOfService(false);
655            increaseCounter(actionCount);
656        }
657    }
658
659    void getBlockTrainName(@Nonnull ConditionalAction action, OBlock b, Memory mTo, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
660        if (b == null) {
661            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
662        } else {
663            if (mTo == null) {
664                errorList.add("invalid memory name in action - " + action.getActionString());  // NOI18N
665            } else {
666                String name = (String)b.getValue();
667                if (name == null) {
668                    name = " ";
669                }
670                mTo.setValue(name);
671                increaseCounter(actionCount);
672            }
673        }
674    }
675
676    void getBlockWarrant(@Nonnull ConditionalAction action, OBlock b, Memory mTo, String actionStr, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList) {
677        if (b == null) {
678            errorList.add("invalid Block name in action - " + action.getDeviceName());  // NOI18N
679        } else {
680            if (mTo == null) {
681                errorList.add("invalid memory name in action - " + action.getActionString());  // NOI18N
682            } else {
683                Warrant w = b.getWarrant();
684                String name;
685                if (w != null) {
686                    name = w.getDisplayName();
687                } else {
688                    name = " ";
689                }
690                mTo.setValue(name);
691                increaseCounter(actionCount);
692            }
693        }
694    }
695
696    void setNXPairEnabled(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
697        DestinationPoints dp = jmri.InstanceManager.getDefault(jmri.jmrit.entryexit.EntryExitPairs.class).getNamedBean(devName);
698        if (dp == null) {
699            errorList.add("Invalid NX Pair name in action - " + action.getDeviceName());  // NOI18N
700        } else {
701            dp.setEnabled(true);
702            increaseCounter(actionCount);
703        }
704    }
705
706    void setNXPairDisabled(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
707        DestinationPoints dp = jmri.InstanceManager.getDefault(jmri.jmrit.entryexit.EntryExitPairs.class).getNamedBean(devName);
708        if (dp == null) {
709            errorList.add("Invalid NX Pair name in action - " + action.getDeviceName());  // NOI18N
710        } else {
711            dp.setEnabled(false);
712            increaseCounter(actionCount);
713        }
714    }
715
716    void setNXPairSegment(@Nonnull ConditionalAction action, @Nonnull Reference<Integer> actionCount, @Nonnull List<String> errorList, String devName) {
717        DestinationPoints dp = jmri.InstanceManager.getDefault(jmri.jmrit.entryexit.EntryExitPairs.class).getNamedBean(devName);
718        if (dp == null) {
719            errorList.add("Invalid NX Pair name in action - " + action.getDeviceName());  // NOI18N
720        } else {
721            jmri.InstanceManager.getDefault(jmri.jmrit.entryexit.EntryExitPairs.class).
722                    setSingleSegmentRoute(devName);
723            increaseCounter(actionCount);
724        }
725    }
726
727    private void increaseCounter(@Nonnull Reference<Integer> actionCount) {
728        // actionCount.get() is never null, but Spotbugs doesn't know that
729        Integer value = actionCount.get();
730        actionCount.set(value != null ? value+1 : 0);
731    }
732
733    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultConditionalExecute.class);
734}