Search in sources :

Example 1 with Warrant

use of jmri.jmrit.logix.Warrant in project JMRI by JMRI.

the class WarrantManagerXml method load.

@Override
public boolean load(Element shared, Element perNode) {
    WarrantManager manager = InstanceManager.getDefault(WarrantManager.class);
    if (shared.getChildren().isEmpty()) {
        return true;
    }
    List<Element> warrantList = shared.getChildren("warrant");
    if (log.isDebugEnabled())
        log.debug("Found {} Warrant objects", warrantList.size());
    for (int i = 0; i < warrantList.size(); i++) {
        Element elem = warrantList.get(i);
        if (elem.getAttribute("systemName") == null) {
            log.warn("unexpected null for systemName in elem {}", elem);
            break;
        }
        String sysName = null;
        if (elem.getAttribute("systemName") != null)
            sysName = elem.getAttribute("systemName").getValue();
        String userName = null;
        if (elem.getAttribute("userName") != null)
            userName = elem.getAttribute("userName").getValue();
        boolean SCWa = true;
        log.debug("loading warrant {}", sysName);
        Attribute wType = elem.getAttribute("wtype");
        if (wType == null) {
            log.debug("wtype is null for {}", sysName);
            SCWa = false;
        } else if (!wType.getValue().equals("SC")) {
            log.debug("wtype is {} for {}", wType.getValue(), sysName);
            SCWa = false;
        }
        long timeToPlatform = 500;
        Attribute TTP = elem.getAttribute("timeToPlatform");
        if (TTP != null) {
            try {
                timeToPlatform = TTP.getLongValue();
            } catch (DataConversionException e) {
                log.debug("ignoring DataConversionException (and reverting to default value): " + e.toString());
            }
        }
        Warrant warrant = manager.createNewWarrant(sysName, userName, SCWa, timeToPlatform);
        if (warrant == null) {
            log.info("Warrant \"{}\" (userName={}) previously loaded. This version not loaded.", sysName, userName);
            continue;
        }
        if (SCWa) {
            if (elem.getAttribute("forward") != null) {
                ((SCWarrant) warrant).setForward(elem.getAttribute("forward").getValue().equals("true"));
            }
            warrant.setNoRamp(SCWa);
            warrant.setShareRoute(SCWa);
        }
        List<Element> orders = elem.getChildren("blockOrder");
        for (int k = 0; k < orders.size(); k++) {
            BlockOrder bo = loadBlockOrder(orders.get(k));
            if (bo == null) {
                break;
            }
            warrant.addBlockOrder(bo);
        }
        String c = elem.getChildText("comment");
        if (c != null) {
            warrant.setComment(c);
        }
        Element order = elem.getChild("viaOrder");
        if (order != null) {
            warrant.setViaOrder(loadBlockOrder(order));
        }
        order = elem.getChild("avoidOrder");
        if (order != null) {
            warrant.setAvoidOrder(loadBlockOrder(order));
        }
        boolean forward = true;
        List<Element> throttleCmds = elem.getChildren("throttleCommand");
        if (throttleCmds != null) {
            for (int k = 0; k < throttleCmds.size(); k++) {
                ThrottleSetting ts = loadThrottleCommand(throttleCmds.get(k));
                warrant.addThrottleCommand(ts);
                if (ts.getCommand().toUpperCase().equals("FORWARD")) {
                    forward = ts.getValue().toUpperCase().equals("TRUE");
                }
            }
        }
        if (SCWa) {
            if (elem.getAttribute("forward") != null) {
                forward = elem.getAttribute("forward").getValue().equals("true");
            }
            ((SCWarrant) warrant).setForward(forward);
            warrant.setNoRamp(SCWa);
            warrant.setShareRoute(SCWa);
        }
        Element train = elem.getChild("train");
        if (train != null) {
            loadTrain(train, warrant);
        }
    }
    return true;
}
Also used : SCWarrant(jmri.jmrit.logix.SCWarrant) Warrant(jmri.jmrit.logix.Warrant) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ThrottleSetting(jmri.jmrit.logix.ThrottleSetting) WarrantManager(jmri.jmrit.logix.WarrantManager) SCWarrant(jmri.jmrit.logix.SCWarrant) DataConversionException(org.jdom2.DataConversionException) BlockOrder(jmri.jmrit.logix.BlockOrder)

Example 2 with Warrant

use of jmri.jmrit.logix.Warrant 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 3 with Warrant

use of jmri.jmrit.logix.Warrant in project JMRI by JMRI.

the class ConditionalVariable method evaluate.

/**
     * Evaluates this State Variable.
     *
     * @return true if variable evaluates true, otherwise false.
     */
// Date.getMinutes, Date.getHours
@SuppressWarnings("deprecation")
public boolean evaluate() {
    boolean result = true;
    // evaluate according to state variable type
    int itemType = Conditional.TEST_TO_ITEM[_type];
    log.debug("evaluate: \"{}\" type= {} itemType= {}", getName(), _type, itemType);
    switch(itemType) {
        case Conditional.ITEM_TYPE_SENSOR:
            //Sensor sn = InstanceManager.sensorManagerInstance().provideSensor(getName());
            Sensor sn = (Sensor) getBean();
            if (sn == null) {
                log.error("invalid sensor name= \"" + getName() + "\" in state variable");
                return false;
            }
            if (_type == Conditional.TYPE_SENSOR_ACTIVE) {
                result = sn.getState() == Sensor.ACTIVE;
            } else {
                result = sn.getState() == Sensor.INACTIVE;
            }
            break;
        case Conditional.ITEM_TYPE_TURNOUT:
            Turnout t = (Turnout) getBean();
            if (t == null) {
                log.error("invalid turnout name= \"" + getName() + "\" in state variable");
                return false;
            }
            if (_type == Conditional.TYPE_TURNOUT_THROWN) {
                result = t.getKnownState() == Turnout.THROWN;
            } else {
                result = t.getKnownState() == Turnout.CLOSED;
            }
            break;
        case Conditional.ITEM_TYPE_LIGHT:
            Light lgt = (Light) getBean();
            if (lgt == null) {
                log.error("invalid light name= \"" + getName() + "\" in state variable");
                return false;
            }
            if (_type == Conditional.TYPE_LIGHT_ON) {
                result = lgt.getState() == Light.ON;
            } else {
                result = lgt.getState() == Light.OFF;
            }
            break;
        case Conditional.ITEM_TYPE_SIGNALMAST:
            SignalMast f = (SignalMast) getBean();
            if (f == null) {
                log.error("invalid signal mast name= \"" + getName() + "\" in state variable");
                return false;
            }
            switch(_type) {
                case Conditional.TYPE_SIGNAL_MAST_LIT:
                    result = f.getLit();
                    break;
                case Conditional.TYPE_SIGNAL_MAST_HELD:
                    result = f.getHeld();
                    break;
                case Conditional.TYPE_SIGNAL_MAST_ASPECT_EQUALS:
                    if (f.getAspect() == null) {
                        result = false;
                    } else {
                        result = f.getAspect().equals(_dataString);
                    }
                    break;
                default:
                    log.warn("unexpected type {} in ITEM_TYPE_SIGNALMAST", _type);
            }
            break;
        case Conditional.ITEM_TYPE_SIGNALHEAD:
            SignalHead h = (SignalHead) getBean();
            if (h == null) {
                log.error("invalid signal head name= \"" + getName() + "\" in state variable");
                return false;
            }
            switch(_type) {
                case Conditional.TYPE_SIGNAL_HEAD_RED:
                    result = h.getAppearance() == SignalHead.RED;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_YELLOW:
                    result = h.getAppearance() == SignalHead.YELLOW;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_GREEN:
                    result = h.getAppearance() == SignalHead.GREEN;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_DARK:
                    result = h.getAppearance() == SignalHead.DARK;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_FLASHRED:
                    result = h.getAppearance() == SignalHead.FLASHRED;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW:
                    result = h.getAppearance() == SignalHead.FLASHYELLOW;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN:
                    result = h.getAppearance() == SignalHead.FLASHGREEN;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_LUNAR:
                    result = h.getAppearance() == SignalHead.LUNAR;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_FLASHLUNAR:
                    result = h.getAppearance() == SignalHead.FLASHLUNAR;
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_LIT:
                    result = h.getLit();
                    break;
                case Conditional.TYPE_SIGNAL_HEAD_HELD:
                    result = h.getHeld();
                    break;
                default:
                    result = false;
            }
            break;
        case Conditional.ITEM_TYPE_MEMORY:
            Memory m = (Memory) getBean();
            if (m == null) {
                log.error("invalid memory name= \"" + getName() + "\" in state variable");
                return false;
            }
            String value1 = null;
            String value2 = null;
            if (m.getValue() != null) {
                value1 = m.getValue().toString();
            }
            boolean caseInsensitive = ((_type == Conditional.TYPE_MEMORY_EQUALS_INSENSITIVE) || (_type == Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE));
            if ((_type == Conditional.TYPE_MEMORY_COMPARE) || (_type == Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE)) {
                Memory m2;
                if (_namedBeanData != null) {
                    m2 = (Memory) _namedBeanData.getBean();
                } else {
                    try {
                        m2 = InstanceManager.memoryManagerInstance().provideMemory(_dataString);
                    } catch (IllegalArgumentException ex) {
                        log.error("invalid data memory name= \"" + _dataString + "\" in state variable");
                        return false;
                    }
                }
                if (m2.getValue() != null) {
                    value2 = m2.getValue().toString();
                }
            } else {
                value2 = _dataString;
            }
            result = compare(value1, value2, caseInsensitive);
            break;
        case Conditional.ITEM_TYPE_CONDITIONAL:
            Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(getName());
            if (c == null) {
                c = InstanceManager.getDefault(jmri.ConditionalManager.class).getByUserName(getName());
                if (c == null) {
                    log.error("invalid conditional name= \"" + getName() + "\" in state variable");
                    return false;
                }
            }
            if (_type == Conditional.TYPE_CONDITIONAL_TRUE) {
                result = c.getState() == Conditional.TRUE;
            } else {
                result = c.getState() == Conditional.FALSE;
            }
            break;
        case Conditional.ITEM_TYPE_WARRANT:
            Warrant w = InstanceManager.getDefault(WarrantManager.class).getWarrant(getName());
            if (w == null) {
                log.error("invalid Warrant name= \"" + getName() + "\" in state variable");
                return false;
            }
            switch(_type) {
                case Conditional.TYPE_ROUTE_FREE:
                    result = w.routeIsFree();
                    break;
                case Conditional.TYPE_ROUTE_OCCUPIED:
                    result = w.routeIsOccupied();
                    break;
                case Conditional.TYPE_ROUTE_ALLOCATED:
                    result = w.isAllocated();
                    break;
                case Conditional.TYPE_ROUTE_SET:
                    result = w.hasRouteSet();
                    break;
                case Conditional.TYPE_TRAIN_RUNNING:
                    // not in either RUN or LEARN state
                    result = !(w.getRunMode() == Warrant.MODE_NONE);
                    break;
                default:
                    result = false;
            }
            break;
        case Conditional.ITEM_TYPE_CLOCK:
            Timebase fastClock = InstanceManager.getDefault(jmri.Timebase.class);
            Date currentTime = fastClock.getTime();
            int currentMinutes = (currentTime.getHours() * 60) + currentTime.getMinutes();
            int beginTime = fixMidnight(_num1);
            int endTime = fixMidnight(_num2);
            // check if current time is within range specified
            if (beginTime <= endTime) {
                // range is entirely within one day
                result = (beginTime <= currentMinutes) && (currentMinutes <= endTime);
            } else {
                // range includes midnight
                result = beginTime <= currentMinutes || currentMinutes <= endTime;
            }
            break;
        case Conditional.ITEM_TYPE_OBLOCK:
            OBlock b = InstanceManager.getDefault(jmri.jmrit.logix.OBlockManager.class).getOBlock(getName());
            if (b == null) {
                log.error("invalid OBlock name= \"" + getName() + "\" in state variable");
                return false;
            }
            result = b.statusIs(_dataString);
            break;
        case Conditional.ITEM_TYPE_ENTRYEXIT:
            NamedBean e = getBean();
            if (_type == Conditional.TYPE_ENTRYEXIT_ACTIVE) {
                result = e.getState() == 0x02;
            } else {
                result = e.getState() == 0x04;
            }
            break;
        default:
            break;
    }
    // apply NOT if specified
    if (_not) {
        result = !result;
    }
    if (result) {
        setState(Conditional.TRUE);
    } else {
        setState(Conditional.FALSE);
    }
    return (result);
}
Also used : Warrant(jmri.jmrit.logix.Warrant) WarrantManager(jmri.jmrit.logix.WarrantManager) Date(java.util.Date) OBlock(jmri.jmrit.logix.OBlock)

Example 4 with Warrant

use of jmri.jmrit.logix.Warrant in project JMRI by JMRI.

the class WarrantManagerXml method store.

/**
     * Store the contents of a WarrantManager.
     *
     * @param o Object to store, of type warrantManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element warrants = new Element("warrants");
    warrants.setAttribute("class", "jmri.jmrit.logix.configurexml.WarrantManagerXml");
    WarrantManager manager = (WarrantManager) o;
    Iterator<String> iter = manager.getSystemNameList().iterator();
    while (iter.hasNext()) {
        String sname = iter.next();
        Warrant warrant = manager.getBySystemName(sname);
        String uname = warrant.getUserName();
        if (log.isDebugEnabled())
            log.debug("Warrant: sysName= {}, userName= {}", sname, uname);
        Element elem = new Element("warrant");
        elem.setAttribute("systemName", sname);
        if (uname == null)
            uname = "";
        if (uname.length() > 0) {
            elem.setAttribute("userName", uname);
        }
        if (warrant instanceof SCWarrant) {
            elem.setAttribute("wtype", "SC");
            elem.setAttribute("timeToPlatform", "" + ((SCWarrant) warrant).getTimeToPlatform());
            elem.setAttribute("forward", ((SCWarrant) warrant).getForward() ? "true" : "false");
        } else {
            elem.setAttribute("wtype", "normal");
        }
        String comment = warrant.getComment();
        if (comment != null) {
            Element c = new Element("comment");
            c.addContent(comment);
            elem.addContent(c);
        }
        List<BlockOrder> orders = warrant.getSavedOrders();
        for (int j = 0; j < orders.size(); j++) {
            elem.addContent(storeOrder(orders.get(j), "blockOrder"));
        }
        BlockOrder viaOrder = warrant.getViaOrder();
        if (viaOrder != null) {
            elem.addContent(storeOrder(viaOrder, "viaOrder"));
        }
        BlockOrder avoidOrder = warrant.getAvoidOrder();
        if (avoidOrder != null) {
            elem.addContent(storeOrder(avoidOrder, "avoidOrder"));
        }
        List<ThrottleSetting> throttleCmds = warrant.getThrottleCommands();
        for (int j = 0; j < throttleCmds.size(); j++) {
            elem.addContent(storeCommand(throttleCmds.get(j), "throttleCommand"));
        }
        elem.addContent(storeTrain(warrant, "train"));
        // and put this element out
        warrants.addContent(elem);
    }
    return warrants;
}
Also used : SCWarrant(jmri.jmrit.logix.SCWarrant) Warrant(jmri.jmrit.logix.Warrant) SCWarrant(jmri.jmrit.logix.SCWarrant) Element(org.jdom2.Element) ThrottleSetting(jmri.jmrit.logix.ThrottleSetting) WarrantManager(jmri.jmrit.logix.WarrantManager) BlockOrder(jmri.jmrit.logix.BlockOrder)

Aggregations

Warrant (jmri.jmrit.logix.Warrant)4 WarrantManager (jmri.jmrit.logix.WarrantManager)3 Date (java.util.Date)2 BlockOrder (jmri.jmrit.logix.BlockOrder)2 OBlock (jmri.jmrit.logix.OBlock)2 SCWarrant (jmri.jmrit.logix.SCWarrant)2 ThrottleSetting (jmri.jmrit.logix.ThrottleSetting)2 Element (org.jdom2.Element)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ScriptException (javax.script.ScriptException)1 Timer (javax.swing.Timer)1 Audio (jmri.Audio)1 Conditional (jmri.Conditional)1 ConditionalAction (jmri.ConditionalAction)1 ConditionalManager (jmri.ConditionalManager)1 JmriException (jmri.JmriException)1 Light (jmri.Light)1 Logix (jmri.Logix)1