Search in sources :

Example 31 with Block

use of jmri.Block in project JMRI by JMRI.

the class DefaultSignalMastLogicManagerXml method store.

@Override
public Element store(Object o) {
    Element signalMastLogic = new Element("signalmastlogics");
    setStoreElementClass(signalMastLogic);
    SignalMastLogicManager smlm = (SignalMastLogicManager) o;
    signalMastLogic.addContent(new Element("logicDelay").addContent(Long.toString(smlm.getSignalLogicDelay())));
    ArrayList<SignalMastLogic> sml = smlm.getSignalMastLogicList();
    for (int i = 0; i < sml.size(); i++) {
        SignalMastLogic sm = sml.get(i);
        Element source = new Element("signalmastlogic");
        // added purely to make human reading of the xml easier
        source.setAttribute("source", sm.getSourceMast().getDisplayName());
        source.addContent(new Element("sourceSignalMast").addContent(sm.getSourceMast().getDisplayName()));
        ArrayList<SignalMast> destination = sm.getDestinationList();
        if (destination.size() != 0) {
            for (int k = 0; k < destination.size(); k++) {
                SignalMast dest = destination.get(k);
                if (sml.get(i).getStoreState(dest) != SignalMastLogic.STORENONE) {
                    Element elem = new Element("destinationMast");
                    // added purely to make human reading of the xml easier
                    elem.setAttribute("destination", dest.getDisplayName());
                    elem.addContent(new Element("destinationSignalMast").addContent(dest.getDisplayName()));
                    elem.addContent(new Element("comment").addContent(sm.getComment(dest)));
                    if (sm.isEnabled(dest)) {
                        elem.addContent(new Element("enabled").addContent("yes"));
                    } else {
                        elem.addContent(new Element("enabled").addContent("no"));
                    }
                    if (sm.allowAutoMaticSignalMastGeneration(dest)) {
                        elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("yes"));
                    } else {
                        elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("no"));
                    }
                    if (sm.useLayoutEditor(dest)) {
                        elem.addContent(new Element("useLayoutEditor").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditor").addContent("no"));
                    }
                    if (sm.useLayoutEditorTurnouts(dest)) {
                        elem.addContent(new Element("useLayoutEditorTurnouts").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditorTurnouts").addContent("no"));
                    }
                    if (sm.useLayoutEditorBlocks(dest)) {
                        elem.addContent(new Element("useLayoutEditorBlocks").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditorBlocks").addContent("no"));
                    }
                    if (sm.getAssociatedSection(dest) != null) {
                        elem.addContent(new Element("associatedSection").addContent(sm.getAssociatedSection(dest).getDisplayName()));
                    }
                    if (sm.isTurnoutLockAllowed(dest)) {
                        elem.addContent(new Element("lockTurnouts").addContent("yes"));
                    } else {
                        elem.addContent(new Element("lockTurnouts").addContent("no"));
                    }
                    if (sml.get(i).getStoreState(dest) == SignalMastLogic.STOREALL) {
                        ArrayList<Block> blocks = sm.getBlocks(dest);
                        if (blocks.size() > 0) {
                            Element blockElement = new Element("blocks");
                            for (int j = 0; j < blocks.size(); j++) {
                                Element bloc = new Element("block");
                                bloc.addContent(new Element("blockName").addContent(blocks.get(j).getDisplayName()));
                                String blkState = "anyState";
                                if (sm.getBlockState(blocks.get(j), dest) == Block.OCCUPIED) {
                                    blkState = "occupied";
                                } else if (sm.getBlockState(blocks.get(j), dest) == Block.UNOCCUPIED) {
                                    blkState = "unoccupied";
                                }
                                bloc.addContent(new Element("blockState").addContent(blkState));
                                blockElement.addContent(bloc);
                            }
                            elem.addContent(blockElement);
                        }
                        ArrayList<NamedBeanHandle<Turnout>> turnouts = sm.getNamedTurnouts(dest);
                        if (turnouts.size() > 0) {
                            Element turnoutElement = new Element("turnouts");
                            for (int j = 0; j < turnouts.size(); j++) {
                                Element turn = new Element("turnout");
                                turn.addContent(new Element("turnoutName").addContent(turnouts.get(j).getName()));
                                String turnState = "thrown";
                                if (sm.getTurnoutState(turnouts.get(j).getBean(), dest) == Turnout.CLOSED) {
                                    turnState = "closed";
                                }
                                turn.addContent(new Element("turnoutState").addContent(turnState));
                                turnoutElement.addContent(turn);
                            }
                            elem.addContent(turnoutElement);
                        }
                        ArrayList<NamedBeanHandle<Sensor>> sensors = sm.getNamedSensors(dest);
                        if (sensors.size() > 0) {
                            Element sensorElement = new Element("sensors");
                            for (int j = 0; j < sensors.size(); j++) {
                                Element sensor = new Element("sensor");
                                sensor.addContent(new Element("sensorName").addContent(sensors.get(j).getName()));
                                String sensorState = "inActive";
                                if (sm.getSensorState(sensors.get(j).getBean(), dest) == Sensor.ACTIVE) {
                                    sensorState = "active";
                                }
                                sensor.addContent(new Element("sensorState").addContent(sensorState));
                                sensorElement.addContent(sensor);
                            }
                            elem.addContent(sensorElement);
                        }
                        ArrayList<SignalMast> masts = sm.getSignalMasts(dest);
                        if (masts.size() > 0) {
                            Element mastElement = new Element("masts");
                            for (int j = 0; j < masts.size(); j++) {
                                Element mast = new Element("mast");
                                mast.addContent(new Element("mastName").addContent(masts.get(j).getDisplayName()));
                                mast.addContent(new Element("mastState").addContent(sm.getSignalMastState(masts.get(j), dest)));
                                mastElement.addContent(mast);
                            }
                            elem.addContent(mastElement);
                        }
                    }
                    source.addContent(elem);
                }
            }
            signalMastLogic.addContent(source);
        }
    }
    return signalMastLogic;
}
Also used : Element(org.jdom2.Element) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMastLogic(jmri.SignalMastLogic) SignalMast(jmri.SignalMast) Block(jmri.Block) NamedBeanHandle(jmri.NamedBeanHandle)

Example 32 with Block

use of jmri.Block in project JMRI by JMRI.

the class OPathTest method testEquals.

@Test
public void testEquals() {
    Block b1 = new Block("IB1");
    OPath op1 = new OPath(b1, "name");
    op1.setBlock(null);
    OPath op2 = new OPath(b1, "name");
    op2.setBlock(null);
    Assert.assertFalse("not equals null", op1.equals(null));
    Assert.assertFalse("not equals string", op1.equals(""));
    Assert.assertTrue("equals self", op1.equals(op1));
    Assert.assertTrue("on contents", op1.equals(op2));
}
Also used : Block(jmri.Block) Test(org.junit.Test)

Example 33 with Block

use of jmri.Block in project JMRI by JMRI.

the class OPathTest method testSetBlockWasNull.

@Test
public void testSetBlockWasNull() {
    Block b = new Block("IB1");
    OPath op = new OPath(null, "name");
    op.setBlock(b);
    Assert.assertEquals("block", b, op.getBlock());
}
Also used : Block(jmri.Block) Test(org.junit.Test)

Example 34 with Block

use of jmri.Block in project JMRI by JMRI.

the class OBlockManagerTest method testProvideWorksTwice.

@Test
public void testProvideWorksTwice() {
    Block b1 = l.provideOBlock("OB102");
    Block b2 = l.provideOBlock("OB102");
    Assert.assertNotNull(b1);
    Assert.assertNotNull(b2);
    Assert.assertEquals(b1, b2);
}
Also used : Block(jmri.Block) Test(org.junit.Test)

Example 35 with Block

use of jmri.Block in project JMRI by JMRI.

the class DestinationPoints method setRoute.

//For a clear down we need to add a message, if it is a cancel, manual clear down or I didn't mean it.
void setRoute(boolean state) {
    if (log.isDebugEnabled()) {
        log.debug("Set route " + src.getPoint().getDisplayName());
    }
    if (disposed) {
        log.error("Set route called even though interlock has been disposed of");
        return;
    }
    if (routeDetails == null) {
        log.error("No route to set or clear down");
        setActiveEntryExit(false);
        setRouteTo(false);
        setRouteFrom(false);
        if ((getSignal() instanceof SignalMast) && (getEntryExitType() != EntryExitPairs.FULLINTERLOCK)) {
            SignalMast mast = (SignalMast) getSignal();
            mast.setHeld(false);
        }
        synchronized (this) {
            destination = null;
        }
        return;
    }
    if (!state) {
        switch(manager.getClearDownOption()) {
            case EntryExitPairs.PROMPTUSER:
                cancelClearOptionBox();
                break;
            case EntryExitPairs.AUTOCANCEL:
                cancelClearInterlock(EntryExitPairs.CANCELROUTE);
                break;
            case EntryExitPairs.AUTOCLEAR:
                cancelClearInterlock(EntryExitPairs.CLEARROUTE);
                break;
            default:
                cancelClearOptionBox();
                break;
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit " + src.getPoint().getDisplayName());
        }
        return;
    }
    if (manager.isRouteStacked(this, false)) {
        manager.cancelStackedRoute(this, false);
    }
    /* We put the setting of the route into a seperate thread and put a glass pane in front of the layout editor.
         The swing thread for flashing the icons will carry on without interuption. */
    final ArrayList<Color> realColorStd = new ArrayList<Color>();
    final ArrayList<Color> realColorXtra = new ArrayList<Color>();
    final ArrayList<LayoutBlock> routeBlocks = new ArrayList<LayoutBlock>();
    if (manager.useDifferentColorWhenSetting()) {
        for (LayoutBlock lbk : routeDetails) {
            routeBlocks.add(lbk);
            realColorXtra.add(lbk.getBlockExtraColor());
            realColorStd.add(lbk.getBlockTrackColor());
            lbk.setBlockExtraColor(manager.getSettingRouteColor());
            lbk.setBlockTrackColor(manager.getSettingRouteColor());
        }
        //Force a redraw, to reflect color change
        src.getPoint().getPanel().redrawPanel();
    }
    ActiveTrain tmpat = null;
    if (manager.getDispatcherIntegration() && jmri.InstanceManager.getNullableDefault(jmri.jmrit.dispatcher.DispatcherFrame.class) != null) {
        jmri.jmrit.dispatcher.DispatcherFrame df = jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class);
        for (ActiveTrain atl : df.getActiveTrainsList()) {
            if (atl.getEndBlock() == src.getStart().getBlock()) {
                if (atl.getLastAllocatedSection() == atl.getEndBlockSection()) {
                    if (!atl.getReverseAtEnd() && !atl.getResetWhenDone()) {
                        tmpat = atl;
                        break;
                    }
                    log.warn("Interlock will not be added to existing Active Train as it is set for back and forth operation");
                }
            }
        }
    }
    final ActiveTrain at = tmpat;
    Runnable setRouteRun = new Runnable() {

        @Override
        public void run() {
            src.getPoint().getPanel().getGlassPane().setVisible(true);
            try {
                Hashtable<Turnout, Integer> turnoutSettings = new Hashtable<Turnout, Integer>();
                ConnectivityUtil connection = new ConnectivityUtil(point.getPanel());
                // Last block in the route is the one that we are protecting at the last sensor/signalmast
                for (int i = 0; i < routeDetails.size(); i++) {
                    //if we are not using the dispatcher and the signal logic is dynamic, then set the turnouts
                    if (at == null && isSignalLogicDynamic()) {
                        if (i > 0) {
                            ArrayList<LayoutTurnout> turnoutlist;
                            int nxtBlk = i + 1;
                            int preBlk = i - 1;
                            if (i < routeDetails.size() - 1) {
                                turnoutlist = connection.getTurnoutList(routeDetails.get(i).getBlock(), routeDetails.get(preBlk).getBlock(), routeDetails.get(nxtBlk).getBlock());
                                ArrayList<Integer> throwlist = connection.getTurnoutSettingList();
                                for (int x = 0; x < turnoutlist.size(); x++) {
                                    if (turnoutlist.get(x) instanceof LayoutSlip) {
                                        int slipState = throwlist.get(x);
                                        LayoutSlip ls = (LayoutSlip) turnoutlist.get(x);
                                        int taState = ls.getTurnoutState(slipState);
                                        turnoutSettings.put(ls.getTurnout(), taState);
                                        int tbState = ls.getTurnoutBState(slipState);
                                        ls.getTurnoutB().setCommandedState(tbState);
                                        turnoutSettings.put(ls.getTurnoutB(), tbState);
                                    } else {
                                        String t = turnoutlist.get(x).getTurnoutName();
                                        Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(t);
                                        turnoutSettings.put(turnout, throwlist.get(x));
                                        if (turnoutlist.get(x).getSecondTurnout() != null) {
                                            turnoutSettings.put(turnoutlist.get(x).getSecondTurnout(), throwlist.get(x));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ((getEntryExitType() == EntryExitPairs.FULLINTERLOCK)) {
                        // was set against occupancy sensor
                        routeDetails.get(i).getBlock().addPropertyChangeListener(propertyBlockListener);
                        if (i > 0) {
                            routeDetails.get(i).setUseExtraColor(true);
                        }
                    } else {
                        // was set against occupancy sensor
                        routeDetails.get(i).getBlock().removePropertyChangeListener(propertyBlockListener);
                    }
                }
                if (at == null) {
                    if (!isSignalLogicDynamic()) {
                        jmri.SignalMastLogic tmSml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).getSignalMastLogic((SignalMast) src.sourceSignal);
                        for (Turnout t : tmSml.getAutoTurnouts((SignalMast) getSignal())) {
                            turnoutSettings.put(t, tmSml.getAutoTurnoutState(t, (SignalMast) getSignal()));
                        }
                    }
                    for (Map.Entry<Turnout, Integer> entry : turnoutSettings.entrySet()) {
                        entry.getKey().setCommandedState(entry.getValue());
                        Runnable r = new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(250 + manager.turnoutSetDelay);
                                } catch (InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                            }
                        };
                        Thread thr = new Thread(r, "Entry Exit Route, turnout setting");
                        thr.start();
                        try {
                            thr.join();
                        } catch (InterruptedException ex) {
                        //            log.info("interrupted at join " + ex);
                        }
                    }
                }
                src.getPoint().getPanel().redrawPanel();
                if (getEntryExitType() != EntryExitPairs.SETUPTURNOUTSONLY) {
                    if (getEntryExitType() == EntryExitPairs.FULLINTERLOCK) {
                        //If our start block is already active we will set it as our lastSeenActiveBlock.
                        if (src.getStart().getState() == Block.OCCUPIED) {
                            src.getStart().removePropertyChangeListener(propertyBlockListener);
                            lastSeenActiveBlockObject = src.getStart().getBlock().getValue();
                            log.debug("Last seen value " + lastSeenActiveBlockObject);
                        }
                    }
                    if ((src.sourceSignal instanceof SignalMast) && (getSignal() instanceof SignalMast)) {
                        SignalMast smSource = (SignalMast) src.sourceSignal;
                        SignalMast smDest = (SignalMast) getSignal();
                        synchronized (this) {
                            sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).newSignalMastLogic(smSource);
                            if (!sml.isDestinationValid(smDest)) {
                                //if no signalmastlogic existed then created it, but set it not to be stored.
                                sml.setDestinationMast(smDest);
                                sml.setStore(jmri.SignalMastLogic.STORENONE, smDest);
                            }
                        }
                        //Remove the first block as it is our start block
                        routeDetails.remove(0);
                        synchronized (this) {
                            smSource.setHeld(false);
                            //Only change the block and turnout details if this a temp signalmast logic
                            if (sml.getStoreState(smDest) == jmri.SignalMastLogic.STORENONE) {
                                LinkedHashMap<Block, Integer> blks = new LinkedHashMap<Block, Integer>();
                                for (int i = 0; i < routeDetails.size(); i++) {
                                    if (routeDetails.get(i).getBlock().getState() == Block.UNKNOWN) {
                                        routeDetails.get(i).getBlock().setState(Block.UNOCCUPIED);
                                    }
                                    blks.put(routeDetails.get(i).getBlock(), Block.UNOCCUPIED);
                                }
                                sml.setAutoBlocks(blks, smDest);
                                sml.setAutoTurnouts(turnoutSettings, smDest);
                                sml.initialise(smDest);
                            }
                        }
                        smSource.addPropertyChangeListener(new PropertyChangeListener() {

                            @Override
                            public void propertyChange(PropertyChangeEvent e) {
                                SignalMast source = (SignalMast) e.getSource();
                                source.removePropertyChangeListener(this);
                                setRouteFrom(true);
                                setRouteTo(true);
                            }
                        });
                        src.pd.extendedtime = true;
                        point.extendedtime = true;
                    } else {
                        if (src.sourceSignal instanceof SignalMast) {
                            SignalMast mast = (SignalMast) src.sourceSignal;
                            mast.setHeld(false);
                        } else if (src.sourceSignal instanceof SignalHead) {
                            SignalHead head = (SignalHead) src.sourceSignal;
                            head.setHeld(false);
                        }
                        setRouteFrom(true);
                        setRouteTo(true);
                    }
                }
                if (manager.useDifferentColorWhenSetting()) {
                    //final ArrayList<Color> realColorXtra = realColorXtra;
                    javax.swing.Timer resetColorBack = new javax.swing.Timer(manager.getSettingTimer(), new java.awt.event.ActionListener() {

                        @Override
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                            for (int i = 0; i < routeBlocks.size(); i++) {
                                LayoutBlock lbk = routeBlocks.get(i);
                                lbk.setBlockExtraColor(realColorXtra.get(i));
                                lbk.setBlockTrackColor(realColorStd.get(i));
                            }
                            src.getPoint().getPanel().redrawPanel();
                        }
                    });
                    resetColorBack.setRepeats(false);
                    resetColorBack.start();
                }
                if (at != null) {
                    jmri.Section sec = null;
                    if (sml != null && sml.getAssociatedSection((SignalMast) getSignal()) != null) {
                        sec = sml.getAssociatedSection((SignalMast) getSignal());
                    } else {
                        sec = InstanceManager.getDefault(jmri.SectionManager.class).createNewSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
                        if (sec == null) {
                            //A Section already exists, lets grab it and check that it is one used with the Interlocking, if so carry on using that.
                            sec = InstanceManager.getDefault(jmri.SectionManager.class).getSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
                        } else {
                            sec.setSectionType(jmri.Section.DYNAMICADHOC);
                        }
                        if (sec.getSectionType() == jmri.Section.DYNAMICADHOC) {
                            sec.removeAllBlocksFromSection();
                            for (LayoutBlock key : routeDetails) {
                                if (key != src.getStart()) {
                                    sec.addBlock(key.getBlock());
                                }
                            }
                            String dir = jmri.Path.decodeDirection(src.getStart().getNeighbourDirection(routeDetails.get(0).getBlock()));
                            jmri.EntryPoint ep = new jmri.EntryPoint(routeDetails.get(0).getBlock(), src.getStart().getBlock(), dir);
                            ep.setTypeForward();
                            sec.addToForwardList(ep);
                            LayoutBlock proDestLBlock = point.getProtecting().get(0);
                            if (proDestLBlock != null) {
                                dir = jmri.Path.decodeDirection(proDestLBlock.getNeighbourDirection(point.getFacing()));
                                ep = new jmri.EntryPoint(point.getFacing().getBlock(), proDestLBlock.getBlock(), dir);
                                ep.setTypeReverse();
                                sec.addToReverseList(ep);
                            }
                        }
                    }
                    jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class).extendActiveTrainsPath(sec, at, src.getPoint().getPanel());
                }
                src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
            } catch (RuntimeException ex) {
                log.error("An error occured while setting the route");
                ex.printStackTrace();
                src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
                if (manager.useDifferentColorWhenSetting()) {
                    for (int i = 0; i < routeBlocks.size(); i++) {
                        LayoutBlock lbk = routeBlocks.get(i);
                        lbk.setBlockExtraColor(realColorXtra.get(i));
                        lbk.setBlockTrackColor(realColorStd.get(i));
                    }
                }
                src.getPoint().getPanel().redrawPanel();
            }
            src.getPoint().getPanel().getGlassPane().setVisible(false);
        //src.setMenuEnabled(true);
        }
    };
    Thread thrMain = new Thread(setRouteRun, "Entry Exit Set Route");
    thrMain.start();
    try {
        thrMain.join();
    } catch (InterruptedException e) {
        log.error("Interuption exception " + e.toString());
    }
    if (log.isDebugEnabled()) {
        log.debug("finish route " + src.getPoint().getDisplayName());
    }
}
Also used : LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ActiveTrain(jmri.jmrit.dispatcher.ActiveTrain) PropertyChangeEvent(java.beans.PropertyChangeEvent) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Color(java.awt.Color) Block(jmri.Block) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Turnout(jmri.Turnout) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConnectivityUtil(jmri.jmrit.display.layoutEditor.ConnectivityUtil) ActionListener(java.awt.event.ActionListener) PropertyChangeListener(java.beans.PropertyChangeListener) SignalHead(jmri.SignalHead) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) SignalMast(jmri.SignalMast) Hashtable(java.util.Hashtable) ActionEvent(java.awt.event.ActionEvent)

Aggregations

Block (jmri.Block)84 ArrayList (java.util.ArrayList)19 EntryPoint (jmri.EntryPoint)16 Sensor (jmri.Sensor)10 Element (org.jdom2.Element)9 BlockManager (jmri.BlockManager)8 SignalMast (jmri.SignalMast)8 Turnout (jmri.Turnout)8 Test (org.junit.Test)7 Path (jmri.Path)6 Reporter (jmri.Reporter)6 ActionEvent (java.awt.event.ActionEvent)5 ActionListener (java.awt.event.ActionListener)5 Section (jmri.Section)5 Hashtable (java.util.Hashtable)4 NamedBean (jmri.NamedBean)4 SignalHead (jmri.SignalHead)4 LayoutBlockManager (jmri.jmrit.display.layoutEditor.LayoutBlockManager)4 PropertyChangeEvent (java.beans.PropertyChangeEvent)3 PropertyChangeListener (java.beans.PropertyChangeListener)3