Search in sources :

Example 51 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class JsonSignalMastSocketServiceTest method testOnMessageChange.

@Test
@Ignore("Needs setup completed")
public void testOnMessageChange() {
    JsonNode message;
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
    //create a signalmast for testing
    String sysName = "IF$shsm:basic:one-searchlight:SM2";
    String userName = "SM2";
    SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
    SignalMast s = manager.provideSignalMast(sysName);
    s.setUserName(userName);
    try {
        // SignalMast Stop
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, "Stop");
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        //aspect should be Stop
        Assert.assertEquals("Stop", s.getAspect());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // Try to set SignalMast to Unknown, should throw error, remain at Stop
    Exception exception = null;
    try {
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        Assert.assertEquals("Stop", s.getAspect());
    } catch (IOException | JmriException | JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    // set SignalMast no value, should throw error, remain at Stop
    message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
    exception = null;
    try {
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
    } catch (JsonException ex) {
        exception = ex;
    } catch (IOException | JmriException ex) {
        Assert.fail(ex.getMessage());
    }
    Assert.assertNull(exception);
    Assert.assertEquals("Stop", s.getAspect());
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JmriException(jmri.JmriException) IOException(java.io.IOException) JsonException(jmri.server.json.JsonException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class SignalMastIconXml method load.

/**
     * Create a SignalMastIcon, then add
     *
     * @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 ed = (Editor) o;
    SignalMastIcon l = new SignalMastIcon(ed);
    String name;
    Attribute attr;
    /*
         * We need to set the rotation and scaling first, prior to setting the
         * signalmast, otherwise we end up in a situation where by the icons do
         * not get rotated or scaled correctly.
         **/
    try {
        int rotation = 0;
        double scale = 1.0;
        // former attribute name.
        attr = element.getAttribute("rotation");
        if (attr != null) {
            rotation = attr.getIntValue();
        }
        attr = element.getAttribute("degrees");
        if (attr != null) {
            rotation = attr.getIntValue();
        }
        l.rotate(rotation);
        attr = element.getAttribute("scale");
        String text = "Error attr null";
        if (attr != null) {
            scale = attr.getDoubleValue();
            text = attr.getValue();
        }
        l.setScale(scale);
        if (log.isDebugEnabled()) {
            log.debug("Load SignalMast rotation= " + rotation + " scale= " + scale + " attr text= " + text);
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert rotation or scale attribute");
    }
    attr = element.getAttribute("signalmast");
    if (attr == null) {
        log.error("incorrect information for signal mast; must use signalmast name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
        if (log.isDebugEnabled()) {
            log.debug("Load SignalMast " + name);
        }
    }
    SignalMast sh = jmri.InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
    if (sh != null) {
        l.setSignalMast(name);
    } else {
        log.error("SignalMast named '" + attr.getValue() + "' not found.");
        ed.loadFailed();
    //    return;
    }
    attr = element.getAttribute("imageset");
    if (attr != null) {
        l.useIconSet(attr.getValue());
    }
    attr = element.getAttribute("imageset");
    if (attr != null) {
        l.useIconSet(attr.getValue());
    }
    try {
        attr = element.getAttribute("clickmode");
        if (attr != null) {
            l.setClickMode(attr.getIntValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on clickmode attribute: " + e);
    }
    try {
        attr = element.getAttribute("litmode");
        if (attr != null) {
            l.setLitMode(attr.getBooleanValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on litmode attribute: " + e);
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SIGNALS, element);
}
Also used : Attribute(org.jdom2.Attribute) SignalMastIcon(jmri.jmrit.display.SignalMastIcon) SignalMast(jmri.SignalMast) Editor(jmri.jmrit.display.Editor)

Example 53 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class Warrant method readStoppingSignal.

//end propertyChange
private boolean readStoppingSignal() {
    String speed;
    if (_protectSignal instanceof SignalHead) {
        SignalHead head = (SignalHead) _protectSignal;
        int appearance = head.getAppearance();
        speed = jmri.InstanceManager.getDefault(SignalSpeedMap.class).getAppearanceSpeed(head.getAppearanceName(appearance));
        if (log.isDebugEnabled())
            log.debug("SignalHead {} sets appearance to {} - warrant= {}", _protectSignal.getDisplayName(), speed, getDisplayName());
    } else {
        SignalMast mast = (SignalMast) _protectSignal;
        String aspect = mast.getAspect();
        speed = jmri.InstanceManager.getDefault(SignalSpeedMap.class).getAspectSpeed(aspect, mast.getSignalSystem());
        if (log.isDebugEnabled())
            log.debug("SignalMast {} sets aspect to {} - warrant= {}", _protectSignal.getDisplayName(), speed, getDisplayName());
    }
    if (speed == null) {
        // dark or no specified speed
        return false;
    } else if (speed.equals(Stop)) {
        _waitForSignal = true;
    } else {
        if (!_waitForBlock && _engineer != null) {
            _engineer.setWaitforClear(false);
            firePropertyChange("SpeedChange", _idxCurrentOrder, _idxCurrentOrder);
        }
        _waitForSignal = false;
    }
    return true;
}
Also used : SignalHead(jmri.SignalHead) SignalMast(jmri.SignalMast)

Example 54 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class AbstractSignalMastServer method setSignalMastAspect.

protected void setSignalMastAspect(String signalMastName, String signalMastState) {
    SignalMast signalMast;
    try {
        addSignalMastToList(signalMastName);
        signalMast = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(signalMastName);
        if (signalMast == null) {
            log.error("SignalMast {} is not available.", signalMastName);
        } else {
            if (signalMast.getAspect() == null || !signalMast.getAspect().equals(signalMastState) || signalMast.getHeld()) {
                if (signalMastState.equals("Held")) {
                    signalMast.setHeld(true);
                } else {
                    if (signalMast.getHeld())
                        signalMast.setHeld(false);
                    signalMast.setAspect(signalMastState);
                }
            } else {
                try {
                    sendStatus(signalMastName, signalMastState);
                } catch (IOException ex) {
                    log.error("Error sending aspect ", ex);
                }
            }
        }
    } catch (Exception ex) {
        log.error("Exception setting signalMast {} aspect:", signalMastName, ex);
    }
}
Also used : SignalMast(jmri.SignalMast) IOException(java.io.IOException) JmriException(jmri.JmriException) IOException(java.io.IOException) JsonException(jmri.server.json.JsonException)

Example 55 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class SignalMastLogicTableAction method createModel.

@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int SOURCECOL = 0;

        public static final int SOURCEAPPCOL = 1;

        public static final int DESTCOL = 2;

        public static final int DESTAPPCOL = 3;

        public static final int COMCOL = 4;

        public static final int DELCOL = 5;

        public static final int ENABLECOL = 6;

        public static final int EDITLOGICCOL = 7;

        //We have to set a manager first off, but this gets replaced.
        @Override
        protected SignalMastLogicManager getManager() {
            return InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
        }

        /*public EcosLocoAddress getByDccAddress(int address) {return getManager().getByDccAddress(address);}*/
        @Override
        public String getValue(String s) {
            return "Set";
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(jmri.NamedBean t) {
        }

        @Override
        protected synchronized void updateNameList() {
            // first, remove listeners from the individual objects
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    // if object has been deleted, it's not here; ignore it
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        SignalMast dest = b.get(sm);
                        sm.removePropertyChangeListener(this);
                        sm.getSourceMast().removePropertyChangeListener(this);
                        dest.removePropertyChangeListener(this);
                    }
                }
            }
            ArrayList<SignalMastLogic> source = getManager().getSignalMastLogicList();
            signalMastLogicList = new ArrayList<Hashtable<SignalMastLogic, SignalMast>>();
            for (int i = 0; i < source.size(); i++) {
                ArrayList<SignalMast> destList = source.get(i).getDestinationList();
                source.get(i).addPropertyChangeListener(this);
                source.get(i).getSourceMast().addPropertyChangeListener(this);
                for (int j = 0; j < destList.size(); j++) {
                    Hashtable<SignalMastLogic, SignalMast> hash = new Hashtable<SignalMastLogic, SignalMast>(1);
                    hash.put(source.get(i), destList.get(j));
                    destList.get(j).addPropertyChangeListener(this);
                    signalMastLogicList.add(hash);
                }
            }
        }

        //Will need to redo this so that we work out the row number from looking in the signalmastlogiclist.
        @Override
        public void propertyChange(java.beans.PropertyChangeEvent e) {
            if (suppressUpdate) {
                return;
            }
            // updateNameList();
            if (e.getPropertyName().equals("length") || e.getPropertyName().equals("updatedDestination") || e.getPropertyName().equals("updatedSource")) {
                updateNameList();
                log.debug("Table changed length to " + signalMastLogicList.size());
                fireTableDataChanged();
            } else if (e.getSource() instanceof SignalMastLogic) {
                SignalMastLogic logic = (SignalMastLogic) e.getSource();
                if (matchPropertyName(e)) {
                    for (int i = 0; i < signalMastLogicList.size(); i++) {
                        Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                        Enumeration<SignalMastLogic> en = b.keys();
                        while (en.hasMoreElements()) {
                            SignalMastLogic sm = en.nextElement();
                            if (sm == logic) {
                                fireTableRowsUpdated(i, i);
                            }
                        }
                    }
                }
            } else if (e.getSource() instanceof jmri.SignalMast) {
                jmri.SignalMast sigMast = (jmri.SignalMast) e.getSource();
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        //SignalMast dest = b.get(sm);
                        if (sm.getSourceMast() == sigMast) {
                            fireTableRowsUpdated(i, i);
                        }
                    }
                }
            }
        }

        //}
        /**
             * Is this property event announcing a change this table should
             * display?
             * <P>
             * Note that events will come both from the NamedBeans and also from
             * the manager
             */
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return ((e.getPropertyName().indexOf("Comment") >= 0) || (e.getPropertyName().indexOf("Enable") >= 0));
        }

        @Override
        public int getColumnCount() {
            return EDITLOGICCOL + 1;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if (col == COMCOL) {
                getLogicFromRow(row).setComment((String) value, getDestMastFromRow(row));
            } else if (col == EDITLOGICCOL) {
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        editLogic(row, 0);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == DELCOL) {
                // button fired, delete Bean
                deleteLogic(row, col);
            } else if (col == ENABLECOL) {
                boolean enable = ((Boolean) value).booleanValue();
                if (enable) {
                    getLogicFromRow(row).setEnabled(getDestMastFromRow(row));
                } else {
                    getLogicFromRow(row).setDisabled(getDestMastFromRow(row));
                }
            }
        }

        @Override
        public String getColumnName(int col) {
            switch(col) {
                case SOURCECOL:
                    return Bundle.getMessage("Source");
                case DESTCOL:
                    return Bundle.getMessage("Destination");
                case SOURCEAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case DESTAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case COMCOL:
                    return Bundle.getMessage("Comment");
                case DELCOL:
                    // override default, no title for Delete column
                    return "";
                case EDITLOGICCOL:
                    // override default, no title for Edit column
                    return "";
                case ENABLECOL:
                    return Bundle.getMessage("ColumnHeadEnabled");
                default:
                    return "unknown";
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            switch(col) {
                case SOURCECOL:
                case DESTCOL:
                case SOURCEAPPCOL:
                case COMCOL:
                case DESTAPPCOL:
                    return String.class;
                case ENABLECOL:
                    return Boolean.class;
                case EDITLOGICCOL:
                case DELCOL:
                    return JButton.class;
                default:
                    return null;
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            switch(col) {
                case COMCOL:
                case EDITLOGICCOL:
                case DELCOL:
                case ENABLECOL:
                    return true;
                default:
                    return false;
            }
        }

        void editLogic(int row, int col) {
            sigLog.setMast(getLogicFromRow(row).getSourceMast(), getDestMastFromRow(row));
            sigLog.actionPerformed(null);
        }

        void deleteLogic(int row, int col) {
            //This needs to be looked at
            InstanceManager.getDefault(jmri.SignalMastLogicManager.class).removeSignalMastLogic(getLogicFromRow(row), getDestMastFromRow(row));
        }

        public SignalMast getDestMastFromRow(int row) {
            // if object has been deleted, it's not here; ignore it
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return b.get(en.nextElement());
            }
            return null;
        }

        public SignalMastLogic getLogicFromRow(int row) {
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return en.nextElement();
            }
            return null;
        }

        @Override
        public int getPreferredWidth(int col) {
            switch(col) {
                case SOURCECOL:
                    return new JTextField(10).getPreferredSize().width;
                case COMCOL:
                    return 75;
                case DESTCOL:
                    return new JTextField(10).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                EDITLOGICCOL:
                    return new JTextField(6).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                DELCOL:
                    return new JTextField(5).getPreferredSize().width;
                case DESTAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case SOURCEAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case ENABLECOL:
                    return new JTextField(5).getPreferredSize().width;
                default:
                    //log.warn("Unexpected column in getPreferredWidth: "+col);
                    return new JTextField(8).getPreferredSize().width;
            }
        }

        @Override
        public void configureTable(JTable table) {
            setColumnToHoldButton(table, EDITLOGICCOL, new JButton(Bundle.getMessage("ButtonEdit")));
            table.getTableHeader().setReorderingAllowed(true);
            // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            // resize columns as requested
            for (int i = 0; i < table.getColumnCount(); i++) {
                int width = getPreferredWidth(i);
                table.getColumnModel().getColumn(i).setPreferredWidth(width);
            }
            table.sizeColumnsToFit(-1);
            // configValueColumn(table);
            configDeleteColumn(table);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return null;
        }

        @Override
        public NamedBean getByUserName(String name) {
            return null;
        }

        @Override
        public synchronized void dispose() {
            getManager().removePropertyChangeListener(this);
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    SignalMastLogic b = getLogicFromRow(i);
                    if (b != null) {
                        b.removePropertyChangeListener(this);
                    }
                }
            }
        }

        @Override
        public int getRowCount() {
            return signalMastLogicList.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= signalMastLogicList.size()) {
                log.debug("row index is greater than signalMastLogicList size");
                return null;
            }
            SignalMastLogic b = getLogicFromRow(row);
            switch(col) {
                case SOURCECOL:
                    return getLogicFromRow(row).getSourceMast().getDisplayName();
                case // return user name
                DESTCOL:
                    // sometimes, the TableSorter invokes this on rows that no longer exist, so we check
                    return (b != null) ? getDestMastFromRow(row).getDisplayName() : null;
                case //
                SOURCEAPPCOL:
                    return (b != null) ? b.getSourceMast().getAspect() : null;
                case //
                DESTAPPCOL:
                    return (b != null) ? getDestMastFromRow(row).getAspect() : null;
                case COMCOL:
                    return (b != null) ? b.getComment(getDestMastFromRow(row)) : null;
                case DELCOL:
                    return Bundle.getMessage("ButtonDelete");
                case EDITLOGICCOL:
                    return Bundle.getMessage("ButtonEdit");
                case ENABLECOL:
                    return (b != null) ? b.isEnabled(getDestMastFromRow(row)) : null;
                default:
                    //log.error("internal state inconsistent with table requst for "+row+" "+col);
                    return null;
            }
        }

        @Override
        protected void configDeleteColumn(JTable table) {
            // have the delete column hold a button
            setColumnToHoldButton(table, DELCOL, new JButton(Bundle.getMessage("ButtonDelete")));
        }

        @Override
        protected String getBeanType() {
            return "Signal Mast Logic";
        }

        @Override
        protected void showPopup(MouseEvent e) {
        }
    };
}
Also used : NamedBean(jmri.NamedBean) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) SignalMast(jmri.SignalMast) JTextField(javax.swing.JTextField) NamedBean(jmri.NamedBean) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) PropertyChangeEvent(java.beans.PropertyChangeEvent) Enumeration(java.util.Enumeration) MouseEvent(java.awt.event.MouseEvent) Hashtable(java.util.Hashtable) SignalMastLogic(jmri.SignalMastLogic) JTable(javax.swing.JTable)

Aggregations

SignalMast (jmri.SignalMast)80 Test (org.junit.Test)19 NamedBean (jmri.NamedBean)11 Sensor (jmri.Sensor)11 SignalHead (jmri.SignalHead)11 Turnout (jmri.Turnout)10 SignalMastManager (jmri.SignalMastManager)9 JsonException (jmri.server.json.JsonException)9 ArrayList (java.util.ArrayList)8 JmriException (jmri.JmriException)8 Block (jmri.Block)7 SignalMastLogic (jmri.SignalMastLogic)7 Hashtable (java.util.Hashtable)6 Element (org.jdom2.Element)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 IOException (java.io.IOException)4 NamedBeanHandle (jmri.NamedBeanHandle)4 Section (jmri.Section)4 LayoutTurnout (jmri.jmrit.display.layoutEditor.LayoutTurnout)4 PropertyChangeEvent (java.beans.PropertyChangeEvent)3