Search in sources :

Example 1 with SignalMastManager

use of jmri.SignalMastManager in project JMRI by JMRI.

the class JsonSignalMastHttpServiceTest method testDoGetList.

@Test
@Ignore("Needs setup completed")
public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonSignalMastHttpService service = new JsonSignalMastHttpService(mapper);
        SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
        JsonNode result;
        result = service.doGetList(JsonSignalMast.SIGNAL_MAST, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideSignalMast("IF$shsm:basic:one-searchlight:SM1");
        manager.provideSignalMast("IF$shsm:basic:one-searchlight:SM2");
        result = service.doGetList(JsonSignalMast.SIGNAL_MAST, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalMastManager(jmri.SignalMastManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with SignalMastManager

use of jmri.SignalMastManager in project JMRI by JMRI.

the class DefaultSignalMastLogicManagerXml method loadSignalMastLogic.

public boolean loadSignalMastLogic(Element signalMastLogic) {
    List<Element> logicList = signalMastLogic.getChildren("signalmastlogic");
    if (log.isDebugEnabled()) {
        log.debug("Found " + logicList.size() + " signal mast logics");
    }
    SignalMastManager sm = InstanceManager.getDefault(jmri.SignalMastManager.class);
    SignalMastLogicManager sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
    try {
        String logicDelay = signalMastLogic.getChild("logicDelay").getText();
        sml.setSignalLogicDelay(Long.parseLong(logicDelay));
    } catch (java.lang.NullPointerException e) {
    //Considered normal if it doesn't exists
    }
    boolean loadOk = true;
    for (Element so : logicList) {
        String source = so.getChild("sourceSignalMast").getText();
        SignalMast sourceMast = sm.getSignalMast(source);
        if (sourceMast != null) {
            SignalMastLogic logic = sml.newSignalMastLogic(sourceMast);
            List<Element> destList = so.getChildren("destinationMast");
            for (Element s : destList) {
                String destination = s.getChild("destinationSignalMast").getText();
                SignalMast dest = sm.getSignalMast(destination);
                if (dest != null) {
                    logic.setDestinationMast(dest);
                    if (s.getChild("comment") != null) {
                        logic.setComment(s.getChild("comment").getText(), dest);
                    }
                    if (s.getChild("enabled") != null) {
                        if (s.getChild("enabled").getText().equals("yes")) {
                            logic.setEnabled(dest);
                        } else {
                            logic.setDisabled(dest);
                        }
                    }
                    if (s.getChild("allowAutoMaticSignalMastGeneration") != null) {
                        if (s.getChild("allowAutoMaticSignalMastGeneration").getText().equals("no")) {
                            logic.allowAutoMaticSignalMastGeneration(false, dest);
                        } else {
                            logic.allowAutoMaticSignalMastGeneration(true, dest);
                        }
                    }
                    boolean useLayoutEditorTurnout = true;
                    boolean useLayoutEditorBlock = true;
                    if (s.getChild("useLayoutEditorTurnouts") != null) {
                        if (s.getChild("useLayoutEditorTurnouts").getText().equals("no")) {
                            useLayoutEditorTurnout = false;
                        }
                    }
                    if (s.getChild("useLayoutEditorBlocks") != null) {
                        if (s.getChild("useLayoutEditorBlocks").getText().equals("no")) {
                            useLayoutEditorBlock = false;
                        }
                    }
                    try {
                        logic.useLayoutEditorDetails(useLayoutEditorTurnout, useLayoutEditorBlock, dest);
                    } catch (jmri.JmriException ex) {
                    }
                    if (s.getChild("useLayoutEditor") != null) {
                        try {
                            if (s.getChild("useLayoutEditor").getText().equals("yes")) {
                                logic.useLayoutEditor(true, dest);
                            } else {
                                logic.useLayoutEditor(false, dest);
                            }
                        } catch (jmri.JmriException e) {
                        //Considered normal if layout editor hasn't yet been set up.
                        }
                    }
                    if (s.getChild("associatedSection") != null) {
                        Section sect = InstanceManager.getDefault(jmri.SectionManager.class).getSection(s.getChild("associatedSection").getText());
                        logic.setAssociatedSection(sect, dest);
                    }
                    Element turnoutElem = s.getChild("turnouts");
                    if (turnoutElem != null) {
                        List<Element> turnoutList = turnoutElem.getChildren("turnout");
                        if (turnoutList.size() > 0) {
                            Hashtable<NamedBeanHandle<Turnout>, Integer> list = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
                            for (Element t : turnoutList) {
                                String turnout = t.getChild("turnoutName").getText();
                                String state = t.getChild("turnoutState").getText();
                                int value = Turnout.CLOSED;
                                if (state.equals("thrown")) {
                                    value = Turnout.THROWN;
                                }
                                Turnout turn = InstanceManager.turnoutManagerInstance().getTurnout(turnout);
                                if (turn != null) {
                                    NamedBeanHandle<Turnout> namedTurnout = nbhm.getNamedBeanHandle(turnout, turn);
                                    list.put(namedTurnout, value);
                                }
                                log.debug("Unable to add Turnout {} as it does not exist in the panel file", turnout);
                            }
                            logic.setTurnouts(list, dest);
                        }
                    }
                    Element sensorElem = s.getChild("sensors");
                    if (sensorElem != null) {
                        List<Element> sensorList = sensorElem.getChildren("sensor");
                        if (sensorList.size() > 0) {
                            Hashtable<NamedBeanHandle<Sensor>, Integer> list = new Hashtable<NamedBeanHandle<Sensor>, Integer>();
                            for (Element sl : sensorList) {
                                String sensorName = sl.getChild("sensorName").getText();
                                String state = sl.getChild("sensorState").getText();
                                int value = Sensor.INACTIVE;
                                if (state.equals("active")) {
                                    value = Sensor.ACTIVE;
                                }
                                Sensor sen = InstanceManager.sensorManagerInstance().getSensor(sensorName);
                                if (sen != null) {
                                    NamedBeanHandle<Sensor> namedSensor = nbhm.getNamedBeanHandle(sensorName, sen);
                                    list.put(namedSensor, value);
                                }
                                log.debug("Unable to add sensor {} as it does not exist in the panel file", sensorName);
                            }
                            logic.setSensors(list, dest);
                        }
                    }
                    Element blockElem = s.getChild("blocks");
                    if (blockElem != null) {
                        List<Element> blockList = blockElem.getChildren("block");
                        if (blockList.size() > 0) {
                            Hashtable<Block, Integer> list = new Hashtable<Block, Integer>();
                            for (Element b : blockList) {
                                String block = b.getChild("blockName").getText();
                                String state = b.getChild("blockState").getText();
                                int value = 0x03;
                                if (state.equals("occupied")) {
                                    value = Block.OCCUPIED;
                                } else if (state.equals("unoccupied")) {
                                    value = Block.UNOCCUPIED;
                                }
                                Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(block);
                                if (blk != null) {
                                    list.put(blk, value);
                                }
                                log.debug("Unable to add Block {} as it does not exist in the panel file", block);
                            }
                            logic.setBlocks(list, dest);
                        }
                    }
                    Element mastElem = s.getChild("masts");
                    if (mastElem != null) {
                        List<Element> mastList = mastElem.getChildren("mast");
                        if (mastList.size() > 0) {
                            Hashtable<SignalMast, String> list = new Hashtable<SignalMast, String>();
                            for (Element m : mastList) {
                                String mast = m.getChild("mastName").getText();
                                String state = m.getChild("mastState").getText();
                                SignalMast mst = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(mast);
                                if (mst != null) {
                                    list.put(mst, state);
                                }
                                log.debug("Unable to add Signal Mast {} as it does not exist in the panel file", mast);
                            }
                            logic.setMasts(list, dest);
                        }
                    }
                } else {
                    log.error("Destination Mast " + destination + " Not found, logic not loaded");
                    loadOk = false;
                }
            }
        } else {
            log.error("Source Mast " + source + " Not found, logic not loaded");
            loadOk = false;
        }
    }
    sml.initialise();
    return loadOk;
}
Also used : Element(org.jdom2.Element) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) NamedBeanHandle(jmri.NamedBeanHandle) Hashtable(java.util.Hashtable) SignalMastManager(jmri.SignalMastManager) Section(jmri.Section) SignalMastLogic(jmri.SignalMastLogic) Block(jmri.Block) Turnout(jmri.Turnout) Sensor(jmri.Sensor)

Example 3 with SignalMastManager

use of jmri.SignalMastManager in project JMRI by JMRI.

the class JsonSignalMastSocketServiceTest method testSignalMastChange.

@Test
@Ignore("Needs setup completed")
public void testSignalMastChange() {
    try {
        //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);
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
        JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        // TODO: test that service is listener in SignalMastManager
        Assert.assertEquals(JSON.ASPECT_UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Approach, and wait for change to show up
        s.setAspect("Approach");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Approach");
        }, "SignalMast is now Approach");
        Assert.assertEquals("Approach", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Stop, and wait for change to show up
        s.setAspect("Stop");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Stop");
        }, "SignalMast is now Stop");
        Assert.assertEquals("Stop", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        service.onClose();
    //            // TODO: test that service is no longer a listener in SignalMastManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SignalMastManager

use of jmri.SignalMastManager 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

SignalMastManager (jmri.SignalMastManager)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SignalMast (jmri.SignalMast)3 JsonException (jmri.server.json.JsonException)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 JmriException (jmri.JmriException)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Hashtable (java.util.Hashtable)1 Block (jmri.Block)1 NamedBeanHandle (jmri.NamedBeanHandle)1 Section (jmri.Section)1 Sensor (jmri.Sensor)1 SignalMastLogic (jmri.SignalMastLogic)1 SignalMastLogicManager (jmri.SignalMastLogicManager)1 Turnout (jmri.Turnout)1 Element (org.jdom2.Element)1