Search in sources :

Example 76 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class PointDetails method getSensor.

Sensor getSensor() {
    if (getRefObject() == null) {
        return null;
    }
    if ((getPanel() != null) && (!getPanel().isEditable()) && (sensor != null)) {
        return sensor;
    }
    if (getRefObject() instanceof Sensor) {
        setSensor((Sensor) getRefObject());
        return (Sensor) getRefObject();
    }
    Object objLoc = getRefLocation();
    Object objRef = getRefObject();
    SignalHead head = null;
    SignalMast mast = null;
    String username = "";
    String systemname = "";
    Sensor foundSensor = null;
    if (objRef instanceof SignalMast) {
        mast = (SignalMast) objRef;
    }
    if (objRef instanceof SignalHead) {
        head = (SignalHead) objRef;
        username = head.getUserName();
        systemname = head.getSystemName();
    }
    jmri.SensorManager sm = InstanceManager.sensorManagerInstance();
    if (objLoc instanceof PositionablePoint) {
        PositionablePoint p = (PositionablePoint) objLoc;
        if (mast != null) {
            if (p.getEastBoundSignalMast() == objRef) {
                foundSensor = p.getEastBoundSensor();
            } else if (p.getWestBoundSignalMast() == objRef) {
                foundSensor = p.getWestBoundSensor();
            }
        } else if (head != null) {
            if ((p.getEastBoundSignal().equals(username)) || p.getEastBoundSignal().equals(systemname)) {
                foundSensor = p.getEastBoundSensor();
            } else if ((p.getWestBoundSignal().equals(username)) || p.getWestBoundSignal().equals(systemname)) {
                foundSensor = p.getWestBoundSensor();
            }
        }
    } else if (objLoc instanceof LayoutTurnout) {
        LayoutTurnout t = (LayoutTurnout) objLoc;
        if (mast != null) {
            if (t.getSignalAMast() == objRef) {
                foundSensor = t.getSensorA();
            } else if (t.getSignalBMast() == objRef) {
                foundSensor = t.getSensorB();
            } else if (t.getSignalCMast() == objRef) {
                foundSensor = t.getSensorC();
            } else if (t.getSignalDMast() == objRef) {
                foundSensor = t.getSensorD();
            }
        }
        if (head != null) {
            if ((t.getSignalA1Name().equals(username)) || (t.getSignalA1Name().equals(systemname))) {
                foundSensor = t.getSensorA();
            } else if ((t.getSignalA2Name().equals(username)) || (t.getSignalA2Name().equals(systemname))) {
                foundSensor = t.getSensorA();
            } else if ((t.getSignalA3Name().equals(username)) || (t.getSignalA3Name().equals(systemname))) {
                foundSensor = t.getSensorA();
            } else if ((t.getSignalB1Name().equals(username)) || (t.getSignalB1Name().equals(systemname))) {
                foundSensor = t.getSensorB();
            } else if ((t.getSignalB2Name().equals(username)) || (t.getSignalB2Name().equals(systemname))) {
                foundSensor = t.getSensorB();
            } else if ((t.getSignalC1Name().equals(username)) || (t.getSignalC1Name().equals(systemname))) {
                foundSensor = t.getSensorC();
            } else if ((t.getSignalC2Name().equals(username)) || (t.getSignalC2Name().equals(systemname))) {
                foundSensor = t.getSensorC();
            } else if ((t.getSignalD1Name().equals(username)) || (t.getSignalD1Name().equals(systemname))) {
                foundSensor = t.getSensorD();
            } else if ((t.getSignalD2Name().equals(username)) || (t.getSignalD2Name().equals(systemname))) {
                foundSensor = t.getSensorD();
            }
        }
    } else if (objLoc instanceof LevelXing) {
        LevelXing x = (LevelXing) objLoc;
        if (mast != null) {
            if (x.getSignalAMast() == objRef) {
                foundSensor = x.getSensorA();
            } else if (x.getSignalBMast() == objRef) {
                foundSensor = x.getSensorB();
            } else if (x.getSignalCMast() == objRef) {
                foundSensor = x.getSensorC();
            } else if (x.getSignalDMast() == objRef) {
                foundSensor = x.getSensorD();
            }
        }
        if (head != null) {
            if ((x.getSignalAName().equals(username)) || (x.getSignalAName().equals(systemname))) {
                foundSensor = x.getSensorA();
            } else if ((x.getSignalBName().equals(username)) || (x.getSignalBName().equals(systemname))) {
                foundSensor = x.getSensorB();
            } else if ((x.getSignalCName().equals(username)) || (x.getSignalCName().equals(systemname))) {
                foundSensor = x.getSensorC();
            } else if ((x.getSignalDName().equals(username)) || (x.getSignalDName().equals(systemname))) {
                foundSensor = x.getSensorD();
            }
        }
    } else if (objLoc instanceof LayoutSlip) {
        LayoutSlip sl = (LayoutSlip) objLoc;
        if (mast != null) {
            if (sl.getSignalAMast() == objRef) {
                foundSensor = sl.getSensorA();
            } else if (sl.getSignalBMast() == objRef) {
                foundSensor = sl.getSensorB();
            } else if (sl.getSignalCMast() == objRef) {
                foundSensor = sl.getSensorC();
            } else if (sl.getSignalDMast() == objRef) {
                foundSensor = sl.getSensorD();
            }
        }
        if (head != null) {
            if ((sl.getSignalA1Name().equals(username)) || (sl.getSignalA1Name().equals(systemname))) {
                foundSensor = sm.getSensor(sl.getSensorAName());
            } else if ((sl.getSignalB1Name().equals(username)) || (sl.getSignalB1Name().equals(systemname))) {
                foundSensor = sm.getSensor(sl.getSensorBName());
            } else if ((sl.getSignalC1Name().equals(username)) || (sl.getSignalC1Name().equals(systemname))) {
                foundSensor = sm.getSensor(sl.getSensorCName());
            } else if ((sl.getSignalD1Name().equals(username)) || (sl.getSignalD1Name().equals(systemname))) {
                foundSensor = sm.getSensor(sl.getSensorDName());
            }
        }
    }
    setSensor(foundSensor);
    return foundSensor;
}
Also used : LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) LevelXing(jmri.jmrit.display.layoutEditor.LevelXing) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) SignalHead(jmri.SignalHead) SignalMast(jmri.SignalMast) PositionablePoint(jmri.jmrit.display.layoutEditor.PositionablePoint) Sensor(jmri.Sensor)

Example 77 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class DefaultSignalMastManagerXml method store.

/**
     * Default implementation for storing the contents of a
     * DefaultSignalMastManager
     *
     * @param o Object to store
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    DefaultSignalMastManager m = (DefaultSignalMastManager) o;
    Element element = new Element("signalmasts");
    element.setAttribute("class", this.getClass().getName());
    if (m != null) {
        // include contents
        List<String> names = m.getSystemNameList();
        for (int i = 0; i < names.size(); i++) {
            //Element e = new Element("signalmast");
            SignalMast p = m.getSignalMast(names.get(i));
            try {
                Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(p);
                if (e != null) {
                    element.addContent(e);
                }
            } catch (Exception ex) {
                log.error("Error storing signalmast: {}", ex);
            }
        }
        List<SignalMastRepeater> repeaterList = m.getRepeaterList();
        if (repeaterList.size() > 0) {
            //Element repeatElem= new Element("signalmastrepeaters");
            for (SignalMastRepeater smr : repeaterList) {
                if (smr.getMasterMast() != null && smr.getSlaveMast() != null) {
                    Element e = new Element("signalmastrepeater");
                    e.addContent(new Element("masterMast").addContent(smr.getMasterMastName()));
                    e.addContent(new Element("slaveMast").addContent(smr.getSlaveMastName()));
                    e.addContent(new Element("enabled").addContent(smr.getEnabled() ? "true" : "false"));
                    switch(smr.getDirection()) {
                        case 1:
                            e.addContent(new Element("update").addContent("MasterToSlave"));
                            break;
                        case 2:
                            e.addContent(new Element("update").addContent("SlaveToMaster"));
                            break;
                        default:
                            e.addContent(new Element("update").addContent("BothWay"));
                            break;
                    }
                    element.addContent(e);
                }
            }
        //element.add(repeatElem);
        }
    }
    return element;
}
Also used : Element(org.jdom2.Element) SignalMast(jmri.SignalMast) DefaultSignalMastManager(jmri.managers.DefaultSignalMastManager) SignalMastRepeater(jmri.implementation.SignalMastRepeater)

Example 78 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class DefaultSignalAppearanceMapTest method testTwoHead.

@Test
public void testTwoHead() {
    SignalMast s = new SignalHeadSignalMast("IF$shsm:basic:two-searchlight:h1:h2") {

        @Override
        void configureAspectTable(String signalSystemName, String aspectMapName) {
            map = new DefaultSignalAppearanceMap("sys", "user");
        }
    };
    DefaultSignalAppearanceMap t = (DefaultSignalAppearanceMap) s.getAppearanceMap();
    t.addAspect("meh", new int[] { SignalHead.LUNAR, SignalHead.DARK });
    t.addAspect("biff", new int[] { SignalHead.GREEN, SignalHead.GREEN });
    s.setAspect("meh");
    Assert.assertEquals("meh 1 is LUNAR", SignalHead.LUNAR, h1.getAppearance());
    Assert.assertEquals("meh 2 is LUNAR", SignalHead.DARK, h2.getAppearance());
    s.setAspect("biff");
    Assert.assertEquals("biff 1 is GREEN", SignalHead.GREEN, h1.getAppearance());
    Assert.assertEquals("biff 2 is GREEN", SignalHead.GREEN, h2.getAppearance());
    InstanceManager.getDefault(jmri.SignalMastManager.class).deregister(s);
    s.dispose();
}
Also used : SignalMast(jmri.SignalMast) Test(org.junit.Test)

Example 79 with SignalMast

use of jmri.SignalMast in project JMRI by JMRI.

the class DefaultSignalAppearanceMapTest method testDefaultMap.

@Test
public void testDefaultMap() {
    SignalMast s = InstanceManager.getDefault(jmri.SignalMastManager.class).provideSignalMast("IF$shsm:basic:one-searchlight:h1");
    DefaultSignalAppearanceMap t = (DefaultSignalAppearanceMap) s.getAppearanceMap();
    t.loadDefaults();
    s.setAspect("Stop");
    Assert.assertEquals("Stop is RED", SignalHead.RED, h1.getAppearance());
    s.setAspect("Approach");
    Assert.assertEquals("Approach is YELLOW", SignalHead.YELLOW, h1.getAppearance());
    s.setAspect("Clear");
    Assert.assertEquals("Clear is GREEN", SignalHead.GREEN, h1.getAppearance());
    InstanceManager.getDefault(jmri.SignalMastManager.class).deregister(s);
    s.dispose();
}
Also used : SignalMast(jmri.SignalMast) Test(org.junit.Test)

Example 80 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)

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