Search in sources :

Example 1 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultConditional method takeActionIfNeeded.

/**
     * Compares action options, and takes action if appropriate
     * <P>
     * Only get here if a change in state has occurred when calculating this
     * Conditional
     */
@SuppressWarnings({ "deprecation", "fallthrough" })
@SuppressFBWarnings(value = "SF_SWITCH_FALLTHROUGH")
private // have to apply to more than 500 lines of code - jake
void takeActionIfNeeded() {
    if (log.isTraceEnabled()) {
        log.trace("takeActionIfNeeded starts for " + getSystemName());
    }
    int actionCount = 0;
    int actionNeeded = 0;
    int act = 0;
    int state = 0;
    ArrayList<String> errorList = new ArrayList<>();
    // Use a local copy of state to guarantee the entire list of actions will be fired off
    // before a state change occurs that may block their completion.
    int currentState = _currentState;
    for (int i = 0; i < _actionList.size(); i++) {
        ConditionalAction action = _actionList.get(i);
        int neededAction = actionNeeded;
        int option = action.getOption();
        if (log.isTraceEnabled()) {
            log.trace(" takeActionIfNeeded considers action " + i + " with currentState: " + currentState + " and option: " + option);
        }
        if (((currentState == TRUE) && (option == ACTION_OPTION_ON_CHANGE_TO_TRUE)) || ((currentState == FALSE) && (option == ACTION_OPTION_ON_CHANGE_TO_FALSE)) || (option == ACTION_OPTION_ON_CHANGE)) {
            // need to take this action
            actionNeeded++;
            SignalHead h = null;
            SignalMast f = null;
            Logix x = null;
            Light lgt = null;
            Warrant w = null;
            NamedBean nb = null;
            if (action.getNamedBean() != null) {
                nb = action.getNamedBean().getBean();
            }
            int value = 0;
            Timer timer = null;
            int type = action.getType();
            String devName = getDeviceName(action);
            if (devName == null) {
                errorList.add("invalid memory name in action - " + action);
                continue;
            }
            if (log.isDebugEnabled()) {
                log.debug("getDeviceName()=" + action.getDeviceName() + " devName= " + devName);
            }
            switch(type) {
                case Conditional.ACTION_NONE:
                    break;
                case Conditional.ACTION_SET_TURNOUT:
                    Turnout t = (Turnout) nb;
                    if (t == null) {
                        errorList.add("invalid turnout name in action - " + action.getDeviceName());
                    } else {
                        act = action.getActionData();
                        if (act == Route.TOGGLE) {
                            state = t.getKnownState();
                            if (state == Turnout.CLOSED) {
                                act = Turnout.THROWN;
                            } else {
                                act = Turnout.CLOSED;
                            }
                        }
                        t.setCommandedState(act);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_RESET_DELAYED_TURNOUT:
                    action.stopTimer();
                // fall through
                case Conditional.ACTION_DELAYED_TURNOUT:
                    if (!action.isTimerActive()) {
                        // Create a timer if one does not exist
                        timer = action.getTimer();
                        if (timer == null) {
                            action.setListener(new TimeTurnout(i));
                            timer = new Timer(2000, action.getListener());
                            timer.setRepeats(true);
                        }
                        // Start the Timer to set the turnout
                        value = getMillisecondValue(action);
                        if (value < 0) {
                            break;
                        }
                        timer.setInitialDelay(value);
                        action.setTimer(timer);
                        action.startTimer();
                        actionCount++;
                    } else {
                        log.warn("timer already active on request to start delayed turnout action - " + devName);
                    }
                    break;
                case Conditional.ACTION_CANCEL_TURNOUT_TIMERS:
                    ConditionalManager cmg = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
                    java.util.Iterator<String> iter = cmg.getSystemNameList().iterator();
                    while (iter.hasNext()) {
                        String sname = iter.next();
                        if (sname == null) {
                            errorList.add("Conditional system name null during cancel turnout timers for " + action.getDeviceName());
                        }
                        Conditional c = cmg.getBySystemName(sname);
                        if (c == null) {
                            errorList.add("Conditional null during cancel turnout timers for " + action.getDeviceName());
                        } else {
                            c.cancelTurnoutTimer(devName);
                            actionCount++;
                        }
                    }
                    break;
                case Conditional.ACTION_LOCK_TURNOUT:
                    Turnout tl = (Turnout) nb;
                    if (tl == null) {
                        errorList.add("invalid turnout name in action - " + action.getDeviceName());
                    } else {
                        act = action.getActionData();
                        if (act == Route.TOGGLE) {
                            if (tl.getLocked(Turnout.CABLOCKOUT)) {
                                act = Turnout.UNLOCKED;
                            } else {
                                act = Turnout.LOCKED;
                            }
                        }
                        if (act == Turnout.LOCKED) {
                            tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
                        } else if (act == Turnout.UNLOCKED) {
                            tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, false);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNAL_APPEARANCE:
                    h = (SignalHead) nb;
                    if (h == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        h.setAppearance(action.getActionData());
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNAL_HELD:
                    h = (SignalHead) nb;
                    if (h == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        h.setHeld(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_CLEAR_SIGNAL_HELD:
                    h = (SignalHead) nb;
                    if (h == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        h.setHeld(false);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNAL_DARK:
                    h = (SignalHead) nb;
                    if (h == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        h.setLit(false);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNAL_LIT:
                    h = (SignalHead) nb;
                    if (h == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        h.setLit(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_TRIGGER_ROUTE:
                    Route r = (Route) nb;
                    if (r == null) {
                        errorList.add("invalid Route name in action - " + action.getDeviceName());
                    } else {
                        r.setRoute();
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SENSOR:
                    Sensor sn = (Sensor) nb;
                    if (sn == null) {
                        errorList.add("invalid Sensor name in action - " + action.getDeviceName());
                    } else {
                        act = action.getActionData();
                        if (act == Route.TOGGLE) {
                            state = sn.getState();
                            if (state == Sensor.ACTIVE) {
                                act = Sensor.INACTIVE;
                            } else {
                                act = Sensor.ACTIVE;
                            }
                        }
                        try {
                            sn.setKnownState(act);
                            actionCount++;
                        } catch (JmriException e) {
                            log.warn("Exception setting Sensor " + devName + " in action");
                        }
                    }
                    break;
                case Conditional.ACTION_RESET_DELAYED_SENSOR:
                    action.stopTimer();
                // fall through
                case Conditional.ACTION_DELAYED_SENSOR:
                    if (!action.isTimerActive()) {
                        // Create a timer if one does not exist
                        timer = action.getTimer();
                        if (timer == null) {
                            action.setListener(new TimeSensor(i));
                            timer = new Timer(2000, action.getListener());
                            timer.setRepeats(true);
                        }
                        // Start the Timer to set the turnout
                        value = getMillisecondValue(action);
                        if (value < 0) {
                            break;
                        }
                        timer.setInitialDelay(value);
                        action.setTimer(timer);
                        action.startTimer();
                        actionCount++;
                    } else {
                        log.warn("timer already active on request to start delayed sensor action - " + devName);
                    }
                    break;
                case Conditional.ACTION_CANCEL_SENSOR_TIMERS:
                    ConditionalManager cm = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
                    java.util.Iterator<String> itr = cm.getSystemNameList().iterator();
                    while (itr.hasNext()) {
                        String sname = itr.next();
                        if (sname == null) {
                            errorList.add("Conditional system name null during cancel sensor timers for " + action.getDeviceName());
                        }
                        Conditional c = cm.getBySystemName(sname);
                        if (c == null) {
                            errorList.add("Conditional null during cancel sensor timers for " + action.getDeviceName());
                        } else {
                            c.cancelSensorTimer(devName);
                            actionCount++;
                        }
                    }
                    break;
                case Conditional.ACTION_SET_LIGHT:
                    lgt = (Light) nb;
                    if (lgt == null) {
                        errorList.add("invalid light name in action - " + action.getDeviceName());
                    } else {
                        act = action.getActionData();
                        if (act == Route.TOGGLE) {
                            state = lgt.getState();
                            if (state == Light.ON) {
                                act = Light.OFF;
                            } else {
                                act = Light.ON;
                            }
                        }
                        lgt.setState(act);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_LIGHT_INTENSITY:
                    lgt = (Light) nb;
                    if (lgt == null) {
                        errorList.add("invalid light name in action - " + action.getDeviceName());
                    } else {
                        try {
                            value = getIntegerValue(action);
                            if (value < 0) {
                                break;
                            }
                            lgt.setTargetIntensity((value) / 100.0);
                            actionCount++;
                        } catch (IllegalArgumentException e) {
                            errorList.add("Exception in set light intensity action - " + action.getDeviceName());
                        }
                    }
                    break;
                case Conditional.ACTION_SET_LIGHT_TRANSITION_TIME:
                    lgt = (Light) nb;
                    if (lgt == null) {
                        errorList.add("invalid light name in action - " + action.getDeviceName());
                    } else {
                        try {
                            value = getIntegerValue(action);
                            if (value < 0) {
                                break;
                            }
                            lgt.setTransitionTime(value);
                            actionCount++;
                        } catch (IllegalArgumentException e) {
                            errorList.add("Exception in set light transition time action - " + action.getDeviceName());
                        }
                    }
                    break;
                case Conditional.ACTION_SET_MEMORY:
                    Memory m = (Memory) nb;
                    if (m == null) {
                        errorList.add("invalid memory name in action - " + action.getDeviceName());
                    } else {
                        m.setValue(action.getActionString());
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_COPY_MEMORY:
                    Memory mFrom = (Memory) nb;
                    if (mFrom == null) {
                        errorList.add("invalid memory name in action - " + action.getDeviceName());
                    } else {
                        Memory mTo = getMemory(action.getActionString());
                        if (mTo == null) {
                            errorList.add("invalid memory name in action - " + action.getActionString());
                        } else {
                            mTo.setValue(mFrom.getValue());
                            actionCount++;
                        }
                    }
                    break;
                case Conditional.ACTION_ENABLE_LOGIX:
                    x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
                    if (x == null) {
                        errorList.add("invalid logix name in action - " + action.getDeviceName());
                    } else {
                        x.setEnabled(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_DISABLE_LOGIX:
                    x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
                    if (x == null) {
                        errorList.add("invalid logix name in action - " + action.getDeviceName());
                    } else {
                        x.setEnabled(false);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_PLAY_SOUND:
                    String path = getActionString(action);
                    if (!path.equals("")) {
                        Sound sound = action.getSound();
                        if (sound == null) {
                            try {
                                sound = new Sound(path);
                            } catch (NullPointerException ex) {
                                errorList.add("invalid path to sound: " + path);
                            }
                        }
                        if (sound != null) {
                            sound.play();
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_RUN_SCRIPT:
                    if (!(getActionString(action).equals(""))) {
                        JmriScriptEngineManager.getDefault().runScript(new File(jmri.util.FileUtil.getExternalFilename(getActionString(action))));
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_FAST_CLOCK_TIME:
                    Date date = InstanceManager.getDefault(jmri.Timebase.class).getTime();
                    date.setHours(action.getActionData() / 60);
                    date.setMinutes(action.getActionData() - ((action.getActionData() / 60) * 60));
                    date.setSeconds(0);
                    InstanceManager.getDefault(jmri.Timebase.class).userSetTime(date);
                    actionCount++;
                    break;
                case Conditional.ACTION_START_FAST_CLOCK:
                    InstanceManager.getDefault(jmri.Timebase.class).setRun(true);
                    actionCount++;
                    break;
                case Conditional.ACTION_STOP_FAST_CLOCK:
                    InstanceManager.getDefault(jmri.Timebase.class).setRun(false);
                    actionCount++;
                    break;
                case Conditional.ACTION_CONTROL_AUDIO:
                    Audio audio = InstanceManager.getDefault(jmri.AudioManager.class).getAudio(devName);
                    if (audio.getSubType() == Audio.SOURCE) {
                        AudioSource audioSource = (AudioSource) audio;
                        switch(action.getActionData()) {
                            case Audio.CMD_PLAY:
                                audioSource.play();
                                break;
                            case Audio.CMD_STOP:
                                audioSource.stop();
                                break;
                            case Audio.CMD_PLAY_TOGGLE:
                                audioSource.togglePlay();
                                break;
                            case Audio.CMD_PAUSE:
                                audioSource.pause();
                                break;
                            case Audio.CMD_RESUME:
                                audioSource.resume();
                                break;
                            case Audio.CMD_PAUSE_TOGGLE:
                                audioSource.togglePause();
                                break;
                            case Audio.CMD_REWIND:
                                audioSource.rewind();
                                break;
                            case Audio.CMD_FADE_IN:
                                audioSource.fadeIn();
                                break;
                            case Audio.CMD_FADE_OUT:
                                audioSource.fadeOut();
                                break;
                            case Audio.CMD_RESET_POSITION:
                                audioSource.resetCurrentPosition();
                                break;
                            default:
                                break;
                        }
                    } else if (audio.getSubType() == Audio.LISTENER) {
                        AudioListener audioListener = (AudioListener) audio;
                        switch(action.getActionData()) {
                            case Audio.CMD_RESET_POSITION:
                                audioListener.resetCurrentPosition();
                                break;
                            default:
                                // nothing needed for others
                                break;
                        }
                    }
                    break;
                case Conditional.ACTION_JYTHON_COMMAND:
                    if (!(getActionString(action).isEmpty())) {
                        // add the text to the output frame
                        ScriptOutput.writeScript(getActionString(action));
                        // and execute
                        try {
                            JmriScriptEngineManager.getDefault().eval(getActionString(action), JmriScriptEngineManager.getDefault().getEngine(JmriScriptEngineManager.PYTHON));
                        } catch (ScriptException ex) {
                            log.error("Error executing script:", ex);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_ALLOCATE_WARRANT_ROUTE:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        String msg = w.allocateRoute(null);
                        if (msg != null) {
                            log.info("Warrant " + action.getDeviceName() + " - " + msg);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_DEALLOCATE_WARRANT_ROUTE:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        w.deAllocate();
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_ROUTE_TURNOUTS:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        String msg = w.setRoute(0, null);
                        if (msg != null) {
                            log.info("Warrant " + action.getDeviceName() + " unable to Set Route - " + msg);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_THROTTLE_FACTOR:
                    log.info("Set warrant Throttle Factor deprecated - Use Warrrant Preferences");
                    break;
                case Conditional.ACTION_SET_TRAIN_ID:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        if (!w.setTrainId(getActionString(action))) {
                            String s = "Unable to find train Id " + getActionString(action) + " in Roster  - " + action.getDeviceName();
                            log.info(s);
                            // could be serious - display error to UI
                            errorList.add(s);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_TRAIN_NAME:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        w.setTrainName(getActionString(action));
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_AUTO_RUN_WARRANT:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        jmri.jmrit.logix.WarrantTableFrame frame = jmri.jmrit.logix.WarrantTableFrame.getDefault();
                        String err = frame.runTrain(w, Warrant.MODE_RUN);
                        if (err != null) {
                            w.stopWarrant(true);
                        }
                        if (err != null) {
                            errorList.add("runAutoTrain error - " + err);
                            w.stopWarrant(true);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_MANUAL_RUN_WARRANT:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        String err = w.setRoute(0, null);
                        if (err == null) {
                            err = w.setRunMode(Warrant.MODE_MANUAL, null, null, null, false);
                        }
                        if (err != null) {
                            errorList.add("runManualTrain error - " + err);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_CONTROL_TRAIN:
                    w = (Warrant) nb;
                    if (w == null) {
                        errorList.add("invalid Warrant name in action - " + action.getDeviceName());
                    } else {
                        if (!w.controlRunTrain(action.getActionData())) {
                            log.info("Train " + w.getTrainId() + " not running  - " + devName);
                        }
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNALMAST_ASPECT:
                    f = (SignalMast) nb;
                    if (f == null) {
                        errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
                    } else {
                        f.setAspect(getActionString(action));
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNALMAST_HELD:
                    f = (SignalMast) nb;
                    if (f == null) {
                        errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
                    } else {
                        f.setHeld(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_CLEAR_SIGNALMAST_HELD:
                    f = (SignalMast) nb;
                    if (f == null) {
                        errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
                    } else {
                        f.setHeld(false);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNALMAST_DARK:
                    f = (SignalMast) nb;
                    if (f == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        f.setLit(false);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_SIGNALMAST_LIT:
                    f = (SignalMast) nb;
                    if (f == null) {
                        errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
                    } else {
                        f.setLit(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_BLOCK_VALUE:
                    OBlock b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.setValue(getActionString(action));
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_SET_BLOCK_ERROR:
                    b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.setError(true);
                        actionCount++;
                    }
                    break;
                case Conditional.ACTION_CLEAR_BLOCK_ERROR:
                    b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.setError(false);
                    }
                    break;
                case ACTION_DEALLOCATE_BLOCK:
                    b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.deAllocate(null);
                        actionCount++;
                    }
                    break;
                case ACTION_SET_BLOCK_OUT_OF_SERVICE:
                    b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.setOutOfService(true);
                        actionCount++;
                    }
                    break;
                case ACTION_SET_BLOCK_IN_SERVICE:
                    b = (OBlock) nb;
                    if (b == null) {
                        errorList.add("invalid Block name in action - " + action.getDeviceName());
                    } else {
                        b.setOutOfService(false);
                        actionCount++;
                    }
                    break;
                default:
                    log.warn("takeActionIfNeeded drops through switch statement for action " + i + " of " + getSystemName());
                    break;
            }
        }
        if (PARKS_DEBUG) {
            System.out.println("Global state= " + _currentState + " Local state= " + currentState + " - Action " + (actionNeeded > neededAction ? "WAS" : "NOT") + " taken for action = " + action.getTypeString() + " " + action.getActionString() + " for device " + action.getDeviceName());
        }
    }
    if (errorList.size() > 0) {
        for (int i = 0; i < errorList.size(); i++) {
            log.error(getDisplayName() + " - " + errorList.get(i));
        }
        if (!GraphicsEnvironment.isHeadless()) {
            java.awt.Toolkit.getDefaultToolkit().beep();
            if (!_skipErrorDialog) {
                new ErrorDialog(errorList, this);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Conditional \"" + getUserName() + "\" (" + getSystemName() + " has " + _actionList.size() + " actions and has executed " + actionCount + " actions of " + actionNeeded + " actions needed on state change to " + currentState);
    }
    if (PARKS_DEBUG) {
        System.out.println("Conditional \"" + getUserName() + "\" (" + getSystemName() + " has " + _actionList.size() + " actions and has executed " + actionCount + " actions of " + actionNeeded + " actions needed on state change to " + currentState);
    }
}
Also used : Warrant(jmri.jmrit.logix.Warrant) NamedBean(jmri.NamedBean) Memory(jmri.Memory) ArrayList(java.util.ArrayList) SignalHead(jmri.SignalHead) Conditional(jmri.Conditional) ScriptException(javax.script.ScriptException) ConditionalAction(jmri.ConditionalAction) Light(jmri.Light) SignalMast(jmri.SignalMast) OBlock(jmri.jmrit.logix.OBlock) Route(jmri.Route) AudioListener(jmri.jmrit.audio.AudioListener) JmriException(jmri.JmriException) Sound(jmri.jmrit.Sound) Date(java.util.Date) AudioSource(jmri.jmrit.audio.AudioSource) Logix(jmri.Logix) Timer(javax.swing.Timer) ConditionalManager(jmri.ConditionalManager) Audio(jmri.Audio) Turnout(jmri.Turnout) File(java.io.File) Sensor(jmri.Sensor) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultLogix method deleteConditional.

/**
     * Delete a Conditional and remove it from this Logix
     * <P>
     * Note: Since each Logix must have at least one Conditional to do anything,
     * the user is warned in Logix Table Action when the last Conditional is
     * deleted.
     * <P>
     * Returns true if Conditional was successfully deleted, otherwise returns
     * false.
     *
     * @param systemName The Conditional system name
     */
@Override
public String[] deleteConditional(String systemName) {
    if (_conditionalSystemNames.size() <= 0) {
        return (null);
    }
    // check other Logix(es) for use of this conditional (systemName) for use as a
    // variable in one of their conditionals
    ArrayList<String> checkReferences = InstanceManager.getDefault(jmri.ConditionalManager.class).getWhereUsed(systemName);
    if (checkReferences != null) {
        Conditional c = getConditional(systemName);
        String refName = checkReferences.get(0);
        Logix x = InstanceManager.getDefault(jmri.ConditionalManager.class).getParentLogix(refName);
        Conditional cRef = x.getConditional(refName);
        String[] result = new String[] { c.getUserName(), c.getSystemName(), cRef.getUserName(), cRef.getSystemName(), x.getUserName(), x.getSystemName() };
        return result;
    }
    // Remove Conditional from this logix
    if (!_conditionalSystemNames.remove(systemName)) {
        log.error("attempt to delete Conditional not in Logix: " + systemName);
        return null;
    }
    // delete the Conditional object
    Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(systemName);
    if (c == null) {
        log.error("attempt to delete non-existant Conditional - " + systemName);
        return null;
    }
    _conditionalMap.remove(systemName);
    return (null);
}
Also used : Logix(jmri.Logix) ConditionalManager(jmri.ConditionalManager) Conditional(jmri.Conditional)

Example 3 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class LRouteTableAction method updatePressed.

/**
     * Update the Route Table.
     */
void updatePressed() {
    Logix logix = checkNamesOK();
    if (logix == null) {
        log.error("No Logix found!");
        return;
    }
    String sName = logix.getSystemName();
    // Check if the User Name has been changed
    String uName = _userName.getText();
    logix.setUserName(uName);
    initializeIncludedInputList();
    initializeIncludedOutputList();
    initializeIncludedAlignList();
    if (log.isDebugEnabled()) {
        log.debug("updatePressed: _includedInputList.size()= " + _includedInputList.size() + ", _includedOutputList.size()= " + _includedOutputList.size() + ", _includedAlignList.size()= " + _includedAlignList.size());
    }
    ////// Construct output actions for trigger conditionals ///////////
    ArrayList<ConditionalAction> actionList = new ArrayList<>();
    for (int i = 0; i < _includedOutputList.size(); i++) {
        RouteOutputElement elt = _includedOutputList.get(i);
        String name = elt.getUserName();
        if (name == null || name.length() == 0) {
            name = elt.getSysName();
        }
        // actionData
        int state = elt.getState();
        int actionType = 0;
        String params = "";
        switch(elt.getType()) {
            case SENSOR_TYPE:
                actionType = Conditional.ACTION_SET_SENSOR;
                break;
            case TURNOUT_TYPE:
                actionType = Conditional.ACTION_SET_TURNOUT;
                break;
            case LIGHT_TYPE:
                actionType = Conditional.ACTION_SET_LIGHT;
                break;
            case SIGNAL_TYPE:
                actionType = Conditional.ACTION_SET_SIGNAL_APPEARANCE;
                if (state > OFFSET) {
                    actionType = state & ~OFFSET;
                }
                break;
            default:
                log.debug("updatePressed: Unknown action type " + elt.getType());
        }
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, actionType, name, state, params));
    }
    String file = scriptFile.getText();
    if (file.length() > 0) {
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_RUN_SCRIPT, "", -1, file));
    }
    file = soundFile.getText();
    if (file.length() > 0) {
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_PLAY_SOUND, "", -1, file));
    }
    ArrayList<ConditionalAction> onChangeList = cloneActionList(actionList, Conditional.ACTION_OPTION_ON_CHANGE);
    /////// Construct 'AND' clause from 'VETO' controls ////////
    ArrayList<ConditionalVariable> vetoList = new ArrayList<>();
    if (!_initialize) {
        for (int i = 0; i < _includedInputList.size(); i++) {
            RouteInputElement elt = _includedInputList.get(i);
            String name = elt.getUserName();
            if (name == null || name.length() == 0) {
                name = elt.getSysName();
            }
            //int opern = newRouteType ? Conditional.OPERATOR_AND : Conditional.OPERATOR_OR;
            int opern = Conditional.OPERATOR_AND;
            if (i == 0) {
                opern = Conditional.OPERATOR_NONE;
            }
            int state = elt.getState();
            if (VETO < state) {
                vetoList.add(new ConditionalVariable(true, opern, (state & ~VETO), name, _newRouteType));
            }
        }
    }
    ///////////////// Make Trigger Conditional Controls /////////////////
    ArrayList<ConditionalVariable> oneTriggerList = new ArrayList<>();
    ArrayList<ConditionalVariable> twoTriggerList = new ArrayList<>();
    if (!_initialize) {
        for (int i = 0; i < _includedInputList.size(); i++) {
            RouteInputElement elt = _includedInputList.get(i);
            String name = elt.getUserName();
            if (name == null || name.length() == 0) {
                name = elt.getSysName();
            }
            int opern = _newRouteType ? Conditional.OPERATOR_OR : Conditional.OPERATOR_AND;
            if (i == 0) {
                opern = Conditional.OPERATOR_NONE;
            }
            int type = elt.getState();
            if (VETO > type) {
                if (Route.ONCHANGE == type) {
                    switch(elt.getType()) {
                        case SENSOR_TYPE:
                            type = Conditional.TYPE_SENSOR_ACTIVE;
                            break;
                        case TURNOUT_TYPE:
                            type = Conditional.TYPE_TURNOUT_CLOSED;
                            break;
                        case LIGHT_TYPE:
                            type = Conditional.TYPE_LIGHT_ON;
                            break;
                        case SIGNAL_TYPE:
                            type = Conditional.TYPE_SIGNAL_HEAD_LIT;
                            break;
                        default:
                            log.debug("updatePressed: Unknown state variable type " + elt.getType());
                    }
                    twoTriggerList.add(new ConditionalVariable(false, opern, type, name, true));
                } else {
                    oneTriggerList.add(new ConditionalVariable(false, opern, type, name, true));
                }
            }
        }
        if (actionList.isEmpty()) {
            javax.swing.JOptionPane.showMessageDialog(_addFrame, rbx.getString("noAction"), rbx.getString("addErr"), javax.swing.JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        oneTriggerList.add(new ConditionalVariable(false, Conditional.OPERATOR_NONE, Conditional.TYPE_NONE, LOGIX_INITIALIZER, true));
    }
    if (log.isDebugEnabled()) {
        log.debug("actionList.size()= " + actionList.size() + ", oneTriggerList.size()= " + oneTriggerList.size() + ", twoTriggerList.size()= " + twoTriggerList.size() + ", onChangeList.size()= " + onChangeList.size() + ", vetoList.size()= " + vetoList.size());
    }
    logix.deActivateLogix();
    // remove old Conditionals for actions (ver 2.5.2 only -remove a bad idea)
    char[] ch = sName.toCharArray();
    int hash = 0;
    for (int i = 0; i < ch.length; i++) {
        hash += ch[i];
    }
    String cSystemName = CONDITIONAL_SYS_PREFIX + "T" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "F" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "A" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "L" + hash;
    removeConditionals(cSystemName, logix);
    int n = 0;
    do {
        n++;
        cSystemName = sName + n + "A";
    } while (removeConditionals(cSystemName, logix));
    n = 0;
    do {
        n++;
        cSystemName = sName + n + "T";
    } while (removeConditionals(cSystemName, logix));
    cSystemName = sName + "L";
    removeConditionals(cSystemName, logix);
    //String cUserName = null;
    int numConds = 1;
    if (_newRouteType) {
        numConds = makeRouteConditional(numConds, /*false,*/
        actionList, oneTriggerList, vetoList, logix, sName, uName, "T");
        if (!_initialize && twoTriggerList.size() > 0) {
            numConds = makeRouteConditional(numConds, /*true, actionList,*/
            onChangeList, twoTriggerList, null, logix, sName, uName, "T");
        }
    } else {
        for (int i = 0; i < oneTriggerList.size(); i++) {
            ArrayList<ConditionalVariable> vList = new ArrayList<>();
            vList.add(oneTriggerList.get(i));
            numConds = makeRouteConditional(numConds, /*false,*/
            actionList, vList, vetoList, logix, sName, uName, "T");
        }
        for (int i = 0; i < twoTriggerList.size(); i++) {
            ArrayList<ConditionalVariable> vList = new ArrayList<>();
            vList.add(twoTriggerList.get(i));
            numConds = makeRouteConditional(numConds, /*true, actionList,*/
            onChangeList, vList, vetoList, logix, sName, uName, "T");
        }
    }
    if (numConds == 1) {
        javax.swing.JOptionPane.showMessageDialog(_addFrame, rbx.getString("noVars"), rbx.getString("addErr"), javax.swing.JOptionPane.ERROR_MESSAGE);
        return;
    }
    ///////////////// Make Alignment Conditionals //////////////////////////
    numConds = 1;
    for (int i = 0; i < _includedAlignList.size(); i++) {
        ArrayList<ConditionalVariable> vList = new ArrayList<>();
        ArrayList<ConditionalAction> aList = new ArrayList<>();
        AlignElement sensor = _includedAlignList.get(i);
        String name = sensor.getUserName();
        if (name == null || name.length() == 0) {
            name = sensor.getSysName();
        }
        aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_SET_SENSOR, name, Sensor.ACTIVE, ""));
        aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE, Conditional.ACTION_SET_SENSOR, name, Sensor.INACTIVE, ""));
        int alignType = sensor.getState();
        for (int k = 0; k < _includedOutputList.size(); k++) {
            RouteOutputElement elt = _includedOutputList.get(k);
            int varType = 0;
            boolean add = (ALL_TYPE == alignType);
            switch(elt.getType()) {
                case SENSOR_TYPE:
                    if (alignType == SENSOR_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Sensor.INACTIVE:
                            varType = Conditional.TYPE_SENSOR_INACTIVE;
                            break;
                        case Sensor.ACTIVE:
                            varType = Conditional.TYPE_SENSOR_ACTIVE;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in SENSOR_TYPE", elt.getState());
                            break;
                    }
                    break;
                case TURNOUT_TYPE:
                    if (alignType == TURNOUT_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Turnout.CLOSED:
                            varType = Conditional.TYPE_TURNOUT_CLOSED;
                            break;
                        case Turnout.THROWN:
                            varType = Conditional.TYPE_TURNOUT_THROWN;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in TURNOUT_TYPE", elt.getState());
                            break;
                    }
                    break;
                case LIGHT_TYPE:
                    if (alignType == LIGHT_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Light.ON:
                            varType = Conditional.TYPE_LIGHT_ON;
                            break;
                        case Light.OFF:
                            varType = Conditional.TYPE_LIGHT_OFF;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in LIGHT_TYPE", elt.getState());
                            break;
                    }
                    break;
                case SIGNAL_TYPE:
                    if (alignType == SIGNAL_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case SignalHead.DARK:
                            varType = Conditional.TYPE_SIGNAL_HEAD_DARK;
                            break;
                        case SignalHead.RED:
                            varType = Conditional.TYPE_SIGNAL_HEAD_RED;
                            break;
                        case SignalHead.FLASHRED:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHRED;
                            break;
                        case SignalHead.YELLOW:
                            varType = Conditional.TYPE_SIGNAL_HEAD_YELLOW;
                            break;
                        case SignalHead.FLASHYELLOW:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW;
                            break;
                        case SignalHead.GREEN:
                            varType = Conditional.TYPE_SIGNAL_HEAD_GREEN;
                            break;
                        case SignalHead.FLASHGREEN:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN;
                            break;
                        case SET_SIGNAL_HELD:
                            varType = Conditional.TYPE_SIGNAL_HEAD_HELD;
                            break;
                        case CLEAR_SIGNAL_HELD:
                            // don't know how to test for this
                            add = false;
                            break;
                        case SET_SIGNAL_DARK:
                            varType = Conditional.TYPE_SIGNAL_HEAD_DARK;
                            break;
                        case SET_SIGNAL_LIT:
                            varType = Conditional.TYPE_SIGNAL_HEAD_LIT;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in SIGNAL_TYPE", elt.getState());
                            break;
                    }
                    break;
                default:
                    log.debug("updatePressed: Unknown Alignment state variable type " + elt.getType());
            }
            if (add && !_initialize) {
                String eltName = elt.getUserName();
                if (eltName == null || eltName.length() == 0) {
                    eltName = elt.getSysName();
                }
                vList.add(new ConditionalVariable(false, Conditional.OPERATOR_AND, varType, eltName, true));
            }
        }
        if (vList.size() > 0) {
            numConds = makeAlignConditional(numConds, aList, vList, logix, sName, uName);
        } else {
            javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("NoAlign"), new Object[] { name, sensor.getAlignType() }), Bundle.getMessage("WarningTitle"), javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }
    ///////////////// Make Lock Conditional //////////////////////////
    numConds = 1;
    if (_lock) {
        ArrayList<ConditionalAction> aList = new ArrayList<>();
        for (int k = 0; k < _includedOutputList.size(); k++) {
            RouteOutputElement elt = _includedOutputList.get(k);
            if (elt.getType() != TURNOUT_TYPE) {
                continue;
            }
            if (elt.getState() == Route.TOGGLE) {
                continue;
            }
            String eltName = elt.getUserName();
            if (eltName == null || eltName.length() == 0) {
                eltName = elt.getSysName();
            }
            aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_LOCK_TURNOUT, eltName, Turnout.LOCKED, ""));
            aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE, Conditional.ACTION_LOCK_TURNOUT, eltName, Turnout.UNLOCKED, ""));
        }
        numConds = makeRouteConditional(numConds, /*false,*/
        aList, oneTriggerList, vetoList, logix, sName, uName, "L");
    }
    log.debug("Conditionals added= " + logix.getNumConditionals());
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        log.debug("Conditional SysName= \"" + logix.getConditionalByNumberOrder(i) + "\"");
    }
    logix.activateLogix();
    log.debug("Conditionals added= " + logix.getNumConditionals());
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        log.debug("Conditional SysName= \"" + logix.getConditionalByNumberOrder(i) + "\"");
    }
    finishUpdate();
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) ArrayList(java.util.ArrayList) ConditionalVariable(jmri.ConditionalVariable) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction)

Example 4 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class LRouteTableAction method checkNamesOK.

Logix checkNamesOK() {
    // Get system name and user name
    String sName = _systemName.getText();
    if (sName.length() == 0) {
        showMessage("EnterNames");
        return null;
    }
    Logix logix = _logixManager.getBySystemName(sName);
    if (!sName.startsWith(LOGIX_SYS_NAME)) {
        sName = LOGIX_SYS_NAME + sName;
    }
    if (logix != null) {
        return logix;
    }
    String uName = _userName.getText();
    if (uName.length() != 0) {
        logix = _logixManager.getByUserName(uName);
        if (logix != null) {
            return logix;
        }
    }
    logix = _logixManager.createNewLogix(sName, uName);
    if (logix == null) {
        // should never get here
        log.error("Unknown failure to create Route with System Name: " + sName);
    }
    return logix;
}
Also used : Logix(jmri.Logix)

Example 5 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class LRouteTableAction method deletePressed.

/**
     * Responds to the Delete button.
     *
     * @param e the action event
     */
void deletePressed(ActionEvent e) {
    Logix l = checkNamesOK();
    if (l != null) {
        l.deActivateLogix();
        // delete the Logix and all its Conditionals
        _logixManager.deleteLogix(l);
    }
    finishUpdate();
}
Also used : Logix(jmri.Logix)

Aggregations

Logix (jmri.Logix)39 Conditional (jmri.Conditional)15 ConditionalVariable (jmri.ConditionalVariable)8 ConditionalAction (jmri.ConditionalAction)7 ArrayList (java.util.ArrayList)6 LogixManager (jmri.LogixManager)6 ConditionalManager (jmri.ConditionalManager)5 DefaultConditionalAction (jmri.implementation.DefaultConditionalAction)5 SensorGroupConditional (jmri.implementation.SensorGroupConditional)5 DefaultListModel (javax.swing.DefaultListModel)4 DefaultConditional (jmri.implementation.DefaultConditional)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 JButton (javax.swing.JButton)3 Route (jmri.Route)3 Sensor (jmri.Sensor)3 UserPreferencesManager (jmri.UserPreferencesManager)3 Element (org.jdom2.Element)3 BoxLayout (javax.swing.BoxLayout)2 JLabel (javax.swing.JLabel)2