Search in sources :

Example 1 with Reporter

use of jmri.Reporter in project JMRI by JMRI.

the class BlockManagerXml method store.

/**
     * Store the contents of a BlockManager.
     *
     * @param o Object to store, of type BlockManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element blocks = new Element("blocks");
    setStoreElementClass(blocks);
    BlockManager tm = (BlockManager) o;
    if (tm != null) {
        java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
        // don't return an element if there are not blocks to include
        if (!iter.hasNext()) {
            return null;
        }
        blocks.addContent(new Element("defaultspeed").addContent(tm.getDefaultSpeed()));
        // write out first set of blocks without contents
        while (iter.hasNext()) {
            try {
                String sname = iter.next();
                if (sname == null) {
                    log.error("System name null during store");
                } else {
                    Block b = tm.getBySystemName(sname);
                    // the following null check is to catch a null pointer exception that sometimes was found to happen
                    if (b == null) {
                        log.error("Null block during store - sname = " + sname);
                    } else {
                        Element elem = new Element("block");
                        elem.addContent(new Element("systemName").addContent(sname));
                        // the following null check is to catch a null pointer exception that sometimes was found to happen
                        String uname = b.getUserName();
                        if ((uname != null) && (!uname.equals(""))) {
                            elem.addContent(new Element("userName").addContent(b.getUserName()));
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("initial store Block " + sname);
                        }
                        // and put this element out
                        blocks.addContent(elem);
                    }
                }
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
        // write out again with contents
        iter = tm.getSystemNameList().iterator();
        while (iter.hasNext()) {
            String sname = iter.next();
            if (sname == null) {
                log.error("System name null during store skipped for this block");
            } else {
                Block b = tm.getBySystemName(sname);
                // the following null check is to catch a null pointer exception that sometimes was found to happen
                if (b == null) {
                    log.error("Null Block during store - second store skipped for this block - " + sname);
                } else {
                    String uname = b.getUserName();
                    if (uname == null) {
                        uname = "";
                    }
                    Element elem = new Element("block");
                    elem.addContent(new Element("systemName").addContent(sname));
                    if (log.isDebugEnabled()) {
                        log.debug("second store Block " + sname + ":" + uname);
                    }
                    // store length and curvature attributes
                    elem.setAttribute("length", Float.toString(b.getLengthMm()));
                    elem.setAttribute("curve", Integer.toString(b.getCurvature()));
                    // store common parts
                    storeCommon(b, elem);
                    if ((!b.getBlockSpeed().equals("")) && !b.getBlockSpeed().contains("Global")) {
                        elem.addContent(new Element("speed").addContent(b.getBlockSpeed()));
                    }
                    String perm = "no";
                    if (b.getPermissiveWorking()) {
                        perm = "yes";
                    }
                    elem.addContent(new Element("permissive").addContent(perm));
                    // Add content. First, the sensor.
                    if (b.getNamedSensor() != null) {
                        elem.addContent(new Element("occupancysensor").addContent(b.getNamedSensor().getName()));
                    }
                    if (b.getDeniedBlocks().size() > 0) {
                        Element denied = new Element("deniedBlocks");
                        b.getDeniedBlocks().forEach((deniedBlock) -> {
                            denied.addContent(new Element("block").addContent(deniedBlock));
                        });
                        elem.addContent(denied);
                    }
                    // Now the Reporter
                    Reporter r = b.getReporter();
                    if (r != null) {
                        Element re = new Element("reporter");
                        re.setAttribute("systemName", r.getSystemName());
                        re.setAttribute("useCurrent", b.isReportingCurrent() ? "yes" : "no");
                        elem.addContent(re);
                    }
                    if (tm.isSavedPathInfo()) {
                        // then the paths
                        List<Path> paths = b.getPaths();
                        for (int i = 0; i < paths.size(); i++) {
                            addPath(elem, paths.get(i));
                        }
                    // and put this element out
                    }
                    blocks.addContent(elem);
                }
            }
        }
    }
    return blocks;
}
Also used : Path(jmri.Path) BlockManager(jmri.BlockManager) Element(org.jdom2.Element) Reporter(jmri.Reporter) Block(jmri.Block)

Example 2 with Reporter

use of jmri.Reporter in project JMRI by JMRI.

the class BlockManagerXml method loadBlock.

/**
     * Utility method to load the individual Block objects.
     *
     * @param element Element containing one block
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public void loadBlock(Element element) throws JmriConfigureXmlException {
    String sysName = getSystemName(element);
    String userName = getUserName(element);
    if (log.isDebugEnabled()) {
        log.debug("defined Block: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
    }
    Block block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    if (block == null) {
        // create it if doesn't exist
        InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sysName, userName);
        block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    }
    if (block == null) {
        log.error("Unable to load block with system name " + sysName + " and username of " + (userName == null ? "<null>" : userName));
        return;
    }
    if (userName != null) {
        block.setUserName(userName);
    }
    if (element.getAttribute("length") != null) {
        // load length in millimeters
        block.setLength(Float.parseFloat(element.getAttribute("length").getValue()));
    }
    if (element.getAttribute("curve") != null) {
        // load curve attribute
        block.setCurvature(Integer.parseInt((element.getAttribute("curve")).getValue()));
    }
    try {
        block.setBlockSpeed("Global");
        if (element.getChild("speed") != null) {
            String speed = element.getChild("speed").getText();
            if (speed != null && !speed.equals("") && !speed.contains("Global")) {
                block.setBlockSpeed(speed);
            }
        }
    } catch (jmri.JmriException ex) {
        log.error(ex.toString());
    }
    if (element.getChild("permissive") != null) {
        boolean permissive = false;
        if (element.getChild("permissive").getText().equals("yes")) {
            permissive = true;
        }
        block.setPermissiveWorking(permissive);
    }
    Element deniedBlocks = element.getChild("deniedBlocks");
    if (deniedBlocks != null) {
        List<Element> denyBlock = deniedBlocks.getChildren("block");
        for (Element deny : denyBlock) {
            block.addBlockDenyList(deny.getText());
        }
    }
    // load common parts
    loadCommon(block, element);
    // load sensor if present
    List<Element> sensors = element.getChildren("sensor");
    if (sensors.size() > 1) {
        log.error("More than one sensor present: " + sensors.size());
    }
    if (sensors.size() == 1) {
        //Old method of saving sensors
        if (sensors.get(0).getAttribute("systemName") != null) {
            String name = sensors.get(0).getAttribute("systemName").getValue();
            if (!name.equals("")) {
                block.setSensor(name);
            }
        }
    }
    if (element.getChild("occupancysensor") != null) {
        String name = element.getChild("occupancysensor").getText();
        if (!name.equals("")) {
            block.setSensor(name);
        }
    }
    // load Reporter if present
    List<Element> reporters = element.getChildren("reporter");
    if (reporters.size() > 1) {
        log.error("More than one reporter present: " + reporters.size());
    }
    if (reporters.size() == 1) {
        // Reporter
        String name = reporters.get(0).getAttribute("systemName").getValue();
        try {
            Reporter reporter = InstanceManager.getDefault(jmri.ReporterManager.class).provideReporter(name);
            block.setReporter(reporter);
            block.setReportingCurrent(reporters.get(0).getAttribute("useCurrent").getValue().equals("yes"));
        } catch (IllegalArgumentException ex) {
            log.warn("failed to create Reporter \"{}\" during Block load", name);
        }
    }
    // load paths if present
    List<Element> paths = element.getChildren("path");
    int startSize = block.getPaths().size();
    int loadCount = 0;
    for (int i = 0; i < paths.size(); i++) {
        Element path = paths.get(i);
        if (loadPath(block, path)) {
            loadCount++;
        }
    }
    if (startSize > 0 && loadCount > 0) {
        log.warn("Added " + loadCount++ + " paths to block " + sysName + " that already had " + startSize + " blocks.");
    }
    if (startSize + loadCount != block.getPaths().size()) {
        log.error("Started with " + startSize + " paths in block " + sysName + ", added " + loadCount + " but final count is " + block.getPaths().size() + "; something not right.");
    }
}
Also used : BlockManager(jmri.BlockManager) Element(org.jdom2.Element) Reporter(jmri.Reporter) Block(jmri.Block)

Example 3 with Reporter

use of jmri.Reporter in project JMRI by JMRI.

the class DefaultIdTag method load.

@Override
public void load(Element e) {
    if (e.getName().equals("idtag")) {
        //NOI18N
        if (log.isDebugEnabled()) {
            log.debug("Load IdTag element for " + this.getSystemName());
        }
        if (//NOI18N
        e.getChild("userName") != null) {
            //NOI18N
            this.setUserName(e.getChild("userName").getText());
        }
        if (//NOI18N
        e.getChild("comment") != null) {
            //NOI18N
            this.setComment(e.getChild("comment").getText());
        }
        if (e.getChild("whereLastSeen") != null) {
            //NOI18N
            try {
                Reporter r = InstanceManager.getDefault(jmri.ReporterManager.class).provideReporter(//NOI18N
                e.getChild("whereLastSeen").getText());
                this.setWhereLastSeen(r);
                this.whenLastSeen = null;
            } catch (IllegalArgumentException ex) {
                log.warn("Failed to provide Turnout \"{}\" in load", e.getChild("whereLastSeen").getText());
            }
        }
        if (e.getChild("whenLastSeen") != null) {
            //NOI18N
            log.debug("When Last Seen: " + e.getChild("whenLastSeen").getText());
            try {
                //NOI18N
                this.whenLastSeen = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).parse(e.getChild("whenLastSeen").getText());
            } catch (ParseException ex) {
                log.warn("Error parsing when last seen: " + ex);
            }
        }
    } else {
        log.error("Not an IdTag element: " + e.getName());
    }
}
Also used : Reporter(jmri.Reporter) ParseException(java.text.ParseException)

Example 4 with Reporter

use of jmri.Reporter in project JMRI by JMRI.

the class JsonUtil method getReporter.

public static JsonNode getReporter(Locale locale, String name) {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, REPORTER);
    ObjectNode data = root.putObject(DATA);
    Reporter reporter = InstanceManager.getDefault(jmri.ReporterManager.class).getReporter(name);
    data.put(NAME, reporter.getSystemName());
    data.put(USERNAME, reporter.getUserName());
    data.put(STATE, reporter.getState());
    data.put(COMMENT, reporter.getComment());
    data.put(REPORT, (reporter.getCurrentReport() != null) ? reporter.getCurrentReport().toString() : null);
    data.put(LAST_REPORT, (reporter.getLastReport() != null) ? reporter.getLastReport().toString() : null);
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Reporter(jmri.Reporter)

Example 5 with Reporter

use of jmri.Reporter in project JMRI by JMRI.

the class BlockEditAction method reporterDetails.

BeanItemPanel reporterDetails() {
    BeanItemPanel reporter = new BeanItemPanel();
    reporter.setName(Bundle.getMessage("BeanNameReporter"));
    reporterComboBox = new JmriBeanComboBox(InstanceManager.getDefault(jmri.ReporterManager.class), ((Block) bean).getReporter(), JmriBeanComboBox.DisplayOptions.DISPLAYNAME);
    reporterComboBox.setFirstItemBlank(true);
    reporter.addItem(new BeanEditItem(reporterComboBox, Bundle.getMessage("BeanNameReporter"), Bundle.getMessage("BlockReporterText")));
    reporterComboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (reporterComboBox.getSelectedBean() != null) {
                useCurrent.setEnabled(true);
            } else {
                useCurrent.setEnabled(false);
            }
        }
    });
    reporter.addItem(new BeanEditItem(useCurrent, Bundle.getMessage("BlockReporterCurrent"), Bundle.getMessage("BlockUseCurrentText")));
    if (reporterComboBox.getSelectedBean() == null) {
        useCurrent.setEnabled(false);
    }
    reporter.setResetItem(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            reporterComboBox.setSelectedBean(((Block) bean).getReporter());
            useCurrent.setSelected(((Block) bean).isReportingCurrent());
        }
    });
    reporter.setSaveItem(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Block blk = (Block) bean;
            blk.setReporter((Reporter) reporterComboBox.getSelectedBean());
            blk.setReportingCurrent(useCurrent.isSelected());
        }
    });
    bei.add(reporter);
    if (jmri.InstanceManager.getNullableDefault(jmri.ReporterManager.class) == null) {
        setEnabled(false);
    }
    return reporter;
}
Also used : JmriBeanComboBox(jmri.util.swing.JmriBeanComboBox) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) Reporter(jmri.Reporter) Block(jmri.Block) AbstractAction(javax.swing.AbstractAction)

Aggregations

Reporter (jmri.Reporter)55 Test (org.junit.Test)18 ReporterManager (jmri.ReporterManager)10 Block (jmri.Block)6 Element (org.jdom2.Element)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 IOException (java.io.IOException)4 BlockManager (jmri.BlockManager)4 JsonException (jmri.server.json.JsonException)4 Date (java.util.Date)3 JmriException (jmri.JmriException)3 Sensor (jmri.Sensor)3 OBlock (jmri.jmrit.logix.OBlock)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ParseException (java.text.ParseException)2 PhysicalLocationReporter (jmri.PhysicalLocationReporter)2 Location (jmri.jmrit.operations.locations.Location)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 GuiLafPreferencesManager (apps.gui.GuiLafPreferencesManager)1