Search in sources :

Example 66 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonClientHandler method onMessage.

/**
     * Process a JSON node and handle appropriately.
     *
     * See {@link #onMessage(java.lang.String)} for expected JSON objects.
     *
     * @param root the JSON node.
     * @throws java.io.IOException if communications is broken with the client.
     * @see #onMessage(java.lang.String)
     */
public void onMessage(JsonNode root) throws IOException {
    try {
        String type = root.path(TYPE).asText();
        if (root.path(TYPE).isMissingNode() && root.path(LIST).isValueNode()) {
            type = LIST;
        }
        JsonNode data = root.path(DATA);
        if ((type.equals(HELLO) || type.equals(PING) || type.equals(GOODBYE) || type.equals(LIST)) && data.isMissingNode()) {
            // these messages are not required to have a data payload,
            // so create one if the message did not contain one to avoid
            // special casing later
            data = this.connection.getObjectMapper().createObjectNode();
        }
        if (root.path(METHOD).isValueNode() && data.path(METHOD).isMissingNode()) {
            ((ObjectNode) data).put(METHOD, root.path(METHOD).asText());
        }
        log.debug("Processing {} with {}", type, data);
        if (type.equals(LIST)) {
            String list = root.path(LIST).asText();
            if (this.services.get(list) != null) {
                for (JsonSocketService service : this.services.get(list)) {
                    service.onList(list, data, this.connection.getLocale());
                }
                return;
            } else {
                log.warn("Requested list type '{}' unknown.", list);
                this.sendErrorMessage(404, Bundle.getMessage(this.connection.getLocale(), "ErrorUnknownType", list));
                return;
            }
        } else if (!data.isMissingNode()) {
            switch(type) {
                case HELLO:
                case LOCALE:
                    if (!data.path(LOCALE).isMissingNode()) {
                        String locale = data.path(LOCALE).asText();
                        if (!locale.isEmpty()) {
                            this.connection.setLocale(Locale.forLanguageTag(locale));
                        }
                    }
                //$FALL-THROUGH$ to default action
                default:
                    if (this.services.get(type) != null) {
                        for (JsonSocketService service : this.services.get(type)) {
                            service.onMessage(type, data, this.connection.getLocale());
                        }
                    } else {
                        log.warn("Requested type '{}' unknown.", type);
                        this.sendErrorMessage(404, Bundle.getMessage(this.connection.getLocale(), "ErrorUnknownType", type));
                    }
                    break;
            }
        } else {
            this.sendErrorMessage(400, Bundle.getMessage(this.connection.getLocale(), "ErrorMissingData"));
        }
        if (type.equals(GOODBYE)) {
            // close the connection if GOODBYE is received.
            this.connection.close();
        }
    } catch (JmriException je) {
        this.sendErrorMessage(500, Bundle.getMessage(this.connection.getLocale(), "ErrorUnsupportedOperation", je.getLocalizedMessage()));
    } catch (JsonException je) {
        this.sendErrorMessage(je);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 67 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class AbstractTurnout method setDivergingSpeed.

@Override
public void setDivergingSpeed(String s) throws JmriException {
    if (s == null) {
        throw new JmriException("Value of requested turnout thrown speed can not be null");
    }
    if (_divergeSpeed.equals(s)) {
        return;
    }
    if (s.contains("Global")) {
        s = "Global";
    } else if (s.contains("Block")) {
        s = "Block";
    } else {
        try {
            Float.parseFloat(s);
        } catch (NumberFormatException nx) {
            try {
                jmri.InstanceManager.getDefault(SignalSpeedMap.class).getSpeed(s);
            } catch (Exception ex) {
                throw new JmriException("Value of requested block speed is not valid");
            }
        }
    }
    String oldSpeed = _divergeSpeed;
    _divergeSpeed = s;
    firePropertyChange("TurnoutDivergingSpeedChange", oldSpeed, s);
}
Also used : JmriException(jmri.JmriException) JmriException(jmri.JmriException)

Example 68 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonThrottle method getThrottle.

/**
     * Creates a new JsonThrottle or returns an existing one if the request is
     * for an existing throttle.
     *
     * data can contain either a string {@link JSON#ID} node containing the ID
     * of a {@link jmri.jmrit.roster.RosterEntry} or an integer
     * {@link JSON#ADDRESS} node. If data contains an ADDRESS, the ID node is
     * ignored. The ADDRESS may be accompanied by a boolean
     * {@link JSON#IS_LONG_ADDRESS} node specifying the type of address, if
     * IS_LONG_ADDRESS is not specified, the inverse of {@link jmri.ThrottleManager#canBeShortAddress(int)
     * } is used as the "best guess" of the address length.
     *
     * @param throttleId The client's identity token for this throttle
     * @param data       JSON object containing either an ADDRESS or an ID
     * @param server     The server requesting this throttle on behalf of a
     *                   client
     * @return The throttle
     * @throws jmri.JmriException if unable to get the requested throttle
     * @throws java.io.IOException if unable to communicate with client
     */
public static JsonThrottle getThrottle(String throttleId, JsonNode data, JsonThrottleServer server) throws JmriException, IOException {
    DccLocoAddress address = null;
    if (!data.path(ADDRESS).isMissingNode()) {
        if (InstanceManager.throttleManagerInstance().canBeLongAddress(data.path(ADDRESS).asInt()) || InstanceManager.throttleManagerInstance().canBeShortAddress(data.path(ADDRESS).asInt())) {
            address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(!InstanceManager.throttleManagerInstance().canBeShortAddress(data.path(ADDRESS).asInt())));
        } else {
            server.sendErrorMessage(-103, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleInvalidAddress", data.path(ADDRESS).asInt()));
            // NOI18N
            throw new JmriException("Address " + data.path(ADDRESS).asInt() + " is not a valid address.");
        }
    } else if (!data.path(ID).isMissingNode()) {
        try {
            address = Roster.getDefault().getEntryForId(data.path(ID).asText()).getDccLocoAddress();
        } catch (NullPointerException ex) {
            server.sendErrorMessage(-100, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleRosterEntry", data.path(ID).asText()));
            // NOI18N
            throw new JmriException("Roster entry " + data.path(ID).asText() + " does not exist.");
        }
    } else {
        server.sendErrorMessage(-101, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleNoAddress"));
        // NOI18N
        throw new JmriException("No address specified.");
    }
    if (JsonThrottle.throttles == null) {
        JsonThrottle.throttles = new HashMap<>();
    }
    if (JsonThrottle.throttles.containsKey(address)) {
        JsonThrottle throttle = JsonThrottle.throttles.get(address);
        throttle.servers.add(server);
        throttle.sendMessage(throttle.mapper.createObjectNode().put(CLIENTS, throttle.servers.size()));
        return throttle;
    } else {
        JsonThrottle throttle = new JsonThrottle(address, server);
        if (!InstanceManager.throttleManagerInstance().requestThrottle(address, throttle)) {
            // NOI18N
            throw new JmriException("Error getting throttle for " + address);
        }
        JsonThrottle.throttles.put(address, throttle);
        return throttle;
    }
}
Also used : JmriException(jmri.JmriException) DccLocoAddress(jmri.DccLocoAddress)

Example 69 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class ThrottleWindow method initializeMenu.

/**
     * Set up View, Edit and Power Menus
     */
private void initializeMenu() {
    JMenu fileMenu = new JMenu(Bundle.getMessage("MenuFile"));
    JMenuItem fileMenuLoad = new JMenuItem(Bundle.getMessage("ThrottleFileMenuLoadThrottle"));
    fileMenuLoad.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getCurrentThrottleFrame().loadThrottle(null);
        }
    });
    fileMenuSave = new JMenuItem(Bundle.getMessage("ThrottleFileMenuSaveThrottle"));
    fileMenuSave.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getCurrentThrottleFrame().saveThrottle();
        }
    });
    JMenuItem fileMenuSaveAs = new JMenuItem(Bundle.getMessage("ThrottleFileMenuSaveAsThrottle"));
    fileMenuSaveAs.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getCurrentThrottleFrame().saveThrottleAs();
        }
    });
    fileMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction(Bundle.getMessage("MenuItemNewThrottle")));
    fileMenu.add(fileMenuLoad);
    fileMenu.add(fileMenuSave);
    fileMenu.add(fileMenuSaveAs);
    fileMenu.addSeparator();
    fileMenu.add(new jmri.jmrit.throttle.LoadXmlThrottlesLayoutAction(Bundle.getMessage("MenuItemLoadThrottleLayout")));
    fileMenu.add(new jmri.jmrit.throttle.StoreXmlThrottlesLayoutAction(Bundle.getMessage("MenuItemSaveThrottleLayout")));
    fileMenu.addSeparator();
    fileMenu.add(new jmri.jmrit.throttle.LoadDefaultXmlThrottlesLayoutAction(Bundle.getMessage("MenuItemLoadDefaultThrottleLayout")));
    fileMenu.add(new jmri.jmrit.throttle.StoreDefaultXmlThrottlesLayoutAction(Bundle.getMessage("MenuItemSaveAsDefaultThrottleLayout")));
    fileMenu.addSeparator();
    fileMenu.add(new jmri.jmrit.withrottle.WiThrottleCreationAction(Bundle.getMessage("MenuItemStartWiThrottle")));
    JMenu viewMenu = new JMenu(Bundle.getMessage("ThrottleMenuView"));
    viewAddressPanel = new JCheckBoxMenuItem(Bundle.getMessage("ThrottleMenuViewAddressPanel"));
    viewAddressPanel.setSelected(true);
    viewAddressPanel.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            getCurrentThrottleFrame().getAddressPanel().setVisible(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    viewControlPanel = new JCheckBoxMenuItem(Bundle.getMessage("ThrottleMenuViewControlPanel"));
    viewControlPanel.setSelected(true);
    viewControlPanel.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            getCurrentThrottleFrame().getControlPanel().setVisible(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    viewFunctionPanel = new JCheckBoxMenuItem(Bundle.getMessage("ThrottleMenuViewFunctionPanel"));
    viewFunctionPanel.setSelected(true);
    viewFunctionPanel.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            getCurrentThrottleFrame().getFunctionPanel().setVisible(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    viewAllButtons = new JMenuItem(Bundle.getMessage("ThrottleMenuViewAllFunctionButtons"));
    viewAllButtons.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            getCurrentThrottleFrame().getFunctionPanel().resetFnButtons();
            getCurrentThrottleFrame().getFunctionPanel().setEnabled();
        }
    });
    JMenuItem makeAllComponentsInBounds = new JMenuItem(Bundle.getMessage("ThrottleMenuViewMakeAllComponentsInBounds"));
    makeAllComponentsInBounds.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            getCurrentThrottleFrame().makeAllComponentsInBounds();
        }
    });
    JMenuItem switchViewMode = new JMenuItem(Bundle.getMessage("ThrottleMenuViewSwitchMode"));
    switchViewMode.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            switchMode();
        }
    });
    JMenuItem viewThrottlesList = new JMenuItem(Bundle.getMessage("ThrottleMenuViewViewThrottleList"));
    viewThrottlesList.addActionListener(new ThrottlesListAction());
    viewMenu.add(viewAddressPanel);
    viewMenu.add(viewControlPanel);
    viewMenu.add(viewFunctionPanel);
    viewMenu.addSeparator();
    viewMenu.add(viewAllButtons);
    viewMenu.add(makeAllComponentsInBounds);
    viewMenu.addSeparator();
    viewMenu.add(switchViewMode);
    viewMenu.add(viewThrottlesList);
    JMenu editMenu = new JMenu(Bundle.getMessage("MenuEdit"));
    JMenuItem preferencesItem = new JMenuItem(Bundle.getMessage("ThrottleMenuEditFrameProperties"));
    editMenu.add(preferencesItem);
    preferencesItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editPreferences();
        }
    });
    editMenuExportRoster = new JMenuItem(Bundle.getMessage("ThrottleMenuEditSaveCustoms"));
    editMenu.add(editMenuExportRoster);
    editMenuExportRoster.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getCurrentThrottleFrame().saveRosterChanges();
        }
    });
    editMenu.addSeparator();
    // now in tabbed preferences
    editMenu.add(new jmri.jmrit.throttle.ThrottlesPreferencesAction(Bundle.getMessage("MenuItemThrottlesPreferences")));
    this.setJMenuBar(new JMenuBar());
    this.getJMenuBar().add(fileMenu);
    this.getJMenuBar().add(editMenu);
    this.getJMenuBar().add(viewMenu);
    if (powerMgr != null) {
        JMenu powerMenu = new JMenu(Bundle.getMessage("ThrottleMenuPower"));
        JMenuItem powerOn = new JMenuItem(Bundle.getMessage("ThrottleMenuPowerOn"));
        powerMenu.add(powerOn);
        powerOn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    powerMgr.setPower(PowerManager.ON);
                } catch (JmriException e1) {
                    log.error("Error when setting power " + e1);
                }
            }
        });
        JMenuItem powerOff = new JMenuItem(Bundle.getMessage("ThrottleMenuPowerOff"));
        powerMenu.add(powerOff);
        powerOff.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    powerMgr.setPower(PowerManager.OFF);
                } catch (JmriException e1) {
                    log.error("Error when setting power " + e1);
                }
            }
        });
        this.getJMenuBar().add(powerMenu);
        if ((!jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle()) || (!jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingToolBar())) {
            this.getJMenuBar().add(new SmallPowerManagerButton());
        }
    }
    // add help selection
    addHelpMenu("package.jmri.jmrit.throttle.ThrottleFrame", true);
}
Also used : ItemEvent(java.awt.event.ItemEvent) ActionEvent(java.awt.event.ActionEvent) JmriException(jmri.JmriException) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionListener(java.awt.event.ActionListener) ItemListener(java.awt.event.ItemListener) JMenuItem(javax.swing.JMenuItem) AbstractAction(javax.swing.AbstractAction) JMenu(javax.swing.JMenu) JMenuBar(javax.swing.JMenuBar)

Example 70 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class Dcc4PcSensorManager method getNextValidAddress.

@Override
public String getNextValidAddress(String curAddress, String prefix) {
    String tmpSName = "";
    try {
        tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
        jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showErrorMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
        return null;
    }
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    Sensor s = getBySystemName(tmpSName);
    if (s != null) {
        for (int x = 1; x < 10; x++) {
            if (channel < 16) {
                channel++;
            } else {
                board++;
                channel = 1;
            }
            s = getBySystemName(prefix + typeLetter() + board + ":" + channel);
            if (s == null) {
                return board + ":" + channel;
            }
        }
        return null;
    } else {
        return curAddress;
    }
}
Also used : JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Aggregations

JmriException (jmri.JmriException)80 Sensor (jmri.Sensor)22 JsonException (jmri.server.json.JsonException)22 IOException (java.io.IOException)20 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 JsonMockConnection (jmri.server.json.JsonMockConnection)18 ArrayList (java.util.ArrayList)10 NamedBean (jmri.NamedBean)10 Turnout (jmri.Turnout)10 Test (org.junit.Test)7 SignalMast (jmri.SignalMast)6 ConfigureManager (jmri.ConfigureManager)5 SensorManager (jmri.SensorManager)5 File (java.io.File)4 PowerManager (jmri.PowerManager)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Light (jmri.Light)3 SignalHead (jmri.SignalHead)3 FileNotFoundException (java.io.FileNotFoundException)2 Hashtable (java.util.Hashtable)2