Search in sources :

Example 1 with Memory

use of jmri.Memory 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 Memory

use of jmri.Memory in project JMRI by JMRI.

the class DefaultLogix method assembleListenerList.

/**
     * Assembles a list of Listeners needed to activate this Logix
     */
private void assembleListenerList() {
    // initialize
    for (int i = _listeners.size() - 1; i >= 0; i--) {
        removeListener(_listeners.get(i));
    }
    _listeners = new ArrayList<JmriSimplePropertyListener>();
    // cycle thru Conditionals to find objects to listen to
    for (int i = 0; i < _conditionalSystemNames.size(); i++) {
        Conditional conditional = getConditional(_conditionalSystemNames.get(i));
        if (conditional != null) {
            ArrayList<ConditionalVariable> variableList = conditional.getCopyOfStateVariables();
            for (int k = 0; k < variableList.size(); k++) {
                ConditionalVariable variable = variableList.get(k);
                // check if listening for a change has been suppressed
                int varListenerType = 0;
                String varName = variable.getName();
                NamedBeanHandle<?> namedBean = variable.getNamedBean();
                int varType = variable.getType();
                int signalAspect = -1;
                // Get Listener type from variable type
                switch(varType) {
                    case Conditional.TYPE_SENSOR_ACTIVE:
                    case Conditional.TYPE_SENSOR_INACTIVE:
                        varListenerType = LISTENER_TYPE_SENSOR;
                        break;
                    case Conditional.TYPE_TURNOUT_THROWN:
                    case Conditional.TYPE_TURNOUT_CLOSED:
                        varListenerType = LISTENER_TYPE_TURNOUT;
                        break;
                    case Conditional.TYPE_CONDITIONAL_TRUE:
                    case Conditional.TYPE_CONDITIONAL_FALSE:
                        varListenerType = LISTENER_TYPE_CONDITIONAL;
                        break;
                    case Conditional.TYPE_LIGHT_ON:
                    case Conditional.TYPE_LIGHT_OFF:
                        varListenerType = LISTENER_TYPE_LIGHT;
                        break;
                    case Conditional.TYPE_MEMORY_EQUALS:
                    case Conditional.TYPE_MEMORY_COMPARE:
                    case Conditional.TYPE_MEMORY_EQUALS_INSENSITIVE:
                    case Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE:
                        varListenerType = LISTENER_TYPE_MEMORY;
                        break;
                    case Conditional.TYPE_ROUTE_FREE:
                    case Conditional.TYPE_ROUTE_OCCUPIED:
                    case Conditional.TYPE_ROUTE_ALLOCATED:
                    case Conditional.TYPE_ROUTE_SET:
                    case Conditional.TYPE_TRAIN_RUNNING:
                        varListenerType = LISTENER_TYPE_WARRANT;
                        break;
                    case Conditional.TYPE_FAST_CLOCK_RANGE:
                        varListenerType = LISTENER_TYPE_FASTCLOCK;
                        varName = "clock";
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_RED:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.RED;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_YELLOW:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.YELLOW;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_GREEN:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.GREEN;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_DARK:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.DARK;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHRED:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHRED;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHYELLOW;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHGREEN;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_LIT:
                    case Conditional.TYPE_SIGNAL_HEAD_HELD:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        break;
                    case Conditional.TYPE_SIGNAL_MAST_ASPECT_EQUALS:
                    case Conditional.TYPE_SIGNAL_MAST_LIT:
                    case Conditional.TYPE_SIGNAL_MAST_HELD:
                        varListenerType = LISTENER_TYPE_SIGNALMAST;
                        break;
                    case Conditional.TYPE_BLOCK_STATUS_EQUALS:
                        varListenerType = LISTENER_TYPE_OBLOCK;
                        break;
                    case Conditional.TYPE_ENTRYEXIT_ACTIVE:
                    case Conditional.TYPE_ENTRYEXIT_INACTIVE:
                        varListenerType = LISTENER_TYPE_ENTRYEXIT;
                        break;
                    default:
                        if (!LRouteTableAction.LOGIX_INITIALIZER.equals(varName)) {
                            log.warn("Unhandled conditional variable type: {}", varType);
                        }
                        break;
                }
                int positionOfListener = getPositionOfListener(varListenerType, varType, varName);
                // add to list if new
                JmriSimplePropertyListener listener = null;
                if (positionOfListener == -1) {
                    switch(varListenerType) {
                        case LISTENER_TYPE_SENSOR:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_SENSOR, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_TURNOUT:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_TURNOUT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_CONDITIONAL:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_CONDITIONAL, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_LIGHT:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_LIGHT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_MEMORY:
                            listener = new JmriTwoStatePropertyListener("value", LISTENER_TYPE_MEMORY, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_WARRANT:
                            listener = new JmriSimplePropertyListener(null, LISTENER_TYPE_WARRANT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_FASTCLOCK:
                            listener = new JmriClockPropertyListener("minutes", LISTENER_TYPE_FASTCLOCK, varName, varType, conditional, variable.getNum1(), variable.getNum2());
                            break;
                        case LISTENER_TYPE_SIGNALHEAD:
                            if (signalAspect < 0) {
                                if (varType == Conditional.TYPE_SIGNAL_HEAD_LIT) {
                                    listener = new JmriTwoStatePropertyListener("Lit", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional);
                                } else {
                                    // varType == Conditional.TYPE_SIGNAL_HEAD_HELD
                                    listener = new JmriTwoStatePropertyListener("Held", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional);
                                }
                            } else {
                                listener = new JmriMultiStatePropertyListener("Appearance", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional, signalAspect);
                            }
                            break;
                        case LISTENER_TYPE_SIGNALMAST:
                            listener = new JmriTwoStatePropertyListener("Aspect", LISTENER_TYPE_SIGNALMAST, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_OBLOCK:
                            listener = new JmriTwoStatePropertyListener("state", LISTENER_TYPE_OBLOCK, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_ENTRYEXIT:
                            listener = new JmriTwoStatePropertyListener("active", LISTENER_TYPE_ENTRYEXIT, namedBean, varType, conditional);
                            break;
                        default:
                            if (!LRouteTableAction.LOGIX_INITIALIZER.equals(varName)) {
                                log.error("Unknown (new) Variable Listener type= " + varListenerType + ", for varName= " + varName + ", varType= " + varType + " in Conditional, " + _conditionalSystemNames.get(i));
                            }
                            continue;
                    }
                    _listeners.add(listener);
                //log.debug("Add listener for "+varName);
                } else {
                    switch(varListenerType) {
                        case LISTENER_TYPE_SENSOR:
                        case LISTENER_TYPE_TURNOUT:
                        case LISTENER_TYPE_CONDITIONAL:
                        case LISTENER_TYPE_LIGHT:
                        case LISTENER_TYPE_MEMORY:
                        case LISTENER_TYPE_WARRANT:
                        case LISTENER_TYPE_SIGNALMAST:
                        case LISTENER_TYPE_OBLOCK:
                        case LISTENER_TYPE_ENTRYEXIT:
                            listener = _listeners.get(positionOfListener);
                            listener.addConditional(conditional);
                            break;
                        case LISTENER_TYPE_FASTCLOCK:
                            JmriClockPropertyListener cpl = (JmriClockPropertyListener) _listeners.get(positionOfListener);
                            cpl.setRange(variable.getNum1(), variable.getNum2());
                            cpl.addConditional(conditional);
                            break;
                        case LISTENER_TYPE_SIGNALHEAD:
                            if (signalAspect < 0) {
                                listener = _listeners.get(positionOfListener);
                                listener.addConditional(conditional);
                            } else {
                                JmriMultiStatePropertyListener mpl = (JmriMultiStatePropertyListener) _listeners.get(positionOfListener);
                                mpl.addConditional(conditional);
                                mpl.setState(signalAspect);
                            }
                            break;
                        default:
                            log.error("Unknown (old) Variable Listener type= " + varListenerType + ", for varName= " + varName + ", varType= " + varType + " in Conditional, " + _conditionalSystemNames.get(i));
                    }
                }
                // addition listeners needed for memory compare
                if (varType == Conditional.TYPE_MEMORY_COMPARE || varType == Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE) {
                    positionOfListener = getPositionOfListener(varListenerType, varType, variable.getDataString());
                    if (positionOfListener == -1) {
                        String name = variable.getDataString();
                        try {
                            Memory my = InstanceManager.memoryManagerInstance().provideMemory(name);
                            NamedBeanHandle<?> nb = jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, my);
                            listener = new JmriTwoStatePropertyListener("value", LISTENER_TYPE_MEMORY, nb, varType, conditional);
                            _listeners.add(listener);
                        } catch (IllegalArgumentException ex) {
                            log.error("invalid memory name= \"" + name + "\" in state variable");
                            break;
                        }
                    } else {
                        listener = _listeners.get(positionOfListener);
                        listener.addConditional(conditional);
                    }
                }
            }
        } else {
            log.error("invalid conditional system name in Logix \"" + getSystemName() + "\" assembleListenerList DELETING " + _conditionalSystemNames.get(i) + " from Conditional list.");
            _conditionalSystemNames.remove(i);
        }
    }
}
Also used : Memory(jmri.Memory) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable)

Example 3 with Memory

use of jmri.Memory in project JMRI by JMRI.

the class MemoryIcon method setMemory.

/**
     * Attached a named Memory to this display item
     *
     * @param pName Used as a system/user name to lookup the Memory object
     */
public void setMemory(String pName) {
    if (InstanceManager.getNullableDefault(jmri.MemoryManager.class) != null) {
        try {
            Memory memory = InstanceManager.memoryManagerInstance().provideMemory(pName);
            setMemory(jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(pName, memory));
        } catch (IllegalArgumentException e) {
            log.error("Memory '" + pName + "' not available, icon won't see changes");
        }
    } else {
        log.error("No MemoryManager for this protocol, icon won't see changes");
    }
    updateSize();
}
Also used : Memory(jmri.Memory)

Example 4 with Memory

use of jmri.Memory in project JMRI by JMRI.

the class MemorySpinnerIcon method setMemory.

/**
     * Attached a named Memory to this display item
     *
     * @param pName Used as a system/user name to lookup the Memory object
     */
public void setMemory(String pName) {
    log.debug("setMemory for memory= {}", pName);
    if (InstanceManager.getNullableDefault(jmri.MemoryManager.class) != null) {
        try {
            Memory memory = InstanceManager.memoryManagerInstance().provideMemory(pName);
            setMemory(jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(pName, memory));
        } catch (IllegalArgumentException e) {
            log.error("Memory '{}' not available, icon won't see changes", pName);
        }
    } else {
        log.error("No MemoryManager for this protocol, icon won't see changes");
    }
    updateSize();
}
Also used : Memory(jmri.Memory)

Example 5 with Memory

use of jmri.Memory in project JMRI by JMRI.

the class MemoryComboIconXml method load.

/**
     * Load, starting with the memoryComboIcon element, then all the value-icon
     * pairs
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    Element elem = element.getChild("itemList");
    List<Element> list = elem.getChildren("item");
    String[] items = new String[list.size()];
    for (int i = 0; i < list.size(); i++) {
        Element e = list.get(i);
        String item = e.getText();
        try {
            int idx = e.getAttribute("index").getIntValue();
            items[idx] = item;
        } catch (org.jdom2.DataConversionException ex) {
            log.error("failed to convert ComboBoxIcon index attribute");
            if (items[i] == null) {
                items[i] = item;
            }
        }
    }
    MemoryComboIcon l = new MemoryComboIcon(p, items);
    loadTextInfo(l, element);
    String name;
    Attribute attr = element.getAttribute("memory");
    if (attr == null) {
        log.error("incorrect information for a memory location; must use memory name");
        p.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    Memory m = jmri.InstanceManager.memoryManagerInstance().getMemory(name);
    if (m != null) {
        l.setMemory(name);
    } else {
        log.error("Memory named '" + attr.getValue() + "' not found.");
        p.loadFailed();
        return;
    }
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MEMORIES, element);
}
Also used : Attribute(org.jdom2.Attribute) Memory(jmri.Memory) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) MemoryComboIcon(jmri.jmrit.display.MemoryComboIcon)

Aggregations

Memory (jmri.Memory)31 MemoryManager (jmri.MemoryManager)7 Sensor (jmri.Sensor)5 JsonException (jmri.server.json.JsonException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Element (org.jdom2.Element)4 Conditional (jmri.Conditional)3 JmriException (jmri.JmriException)3 SignalHead (jmri.SignalHead)3 Turnout (jmri.Turnout)3 Editor (jmri.jmrit.display.Editor)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 Block (jmri.Block)2 ConfigureManager (jmri.ConfigureManager)2 Light (jmri.Light)2 Logix (jmri.Logix)2 NamedBean (jmri.NamedBean)2 Path (jmri.Path)2