Search in sources :

Example 41 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class StartupActionsManager method initialize.

/**
     * {@inheritDoc}
     *
     * Loads the startup action preferences and, if all required managers have
     * initialized without exceptions, performs those actions. Startup actions
     * are only performed if {@link apps.startup.StartupModel#isValid()} is true
     * for the action. It is assumed that the action has retained an Exception
     * that can be used to explain why isValid() is false.
     */
@Override
public void initialize(Profile profile) throws InitializationException {
    if (!this.isInitialized(profile)) {
        boolean perform = true;
        try {
            this.requiresNoInitializedWithExceptions(profile, Bundle.getMessage("StartupActionsManager.RefusalToInitialize"));
        } catch (InitializationException ex) {
            perform = false;
        }
        try {
            Element startup;
            try {
                startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE, true));
            } catch (NullPointerException ex) {
                log.debug("Reading element from version 2.9.6 namespace...");
                startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE_OLD, true));
            }
            for (Element action : startup.getChildren()) {
                // NOI18N
                String adapter = action.getAttributeValue("class");
                // NOI18N
                String name = action.getAttributeValue("name");
                String override = StartupActionModelUtil.getDefault().getOverride(name);
                if (override != null) {
                    action.setAttribute("name", override);
                    log.info("Overridding statup action class {} with {}", name, override);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override), Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override)));
                    // after logging difference and creating error message
                    name = override;
                }
                // NOI18N
                String type = action.getAttributeValue("type");
                log.debug("Read {} {} adapter {}", type, name, adapter);
                try {
                    log.debug("Creating {} {} adapter {}...", type, name, adapter);
                    // no perNode preferences
                    ((XmlAdapter) Class.forName(adapter).newInstance()).load(action, null);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
                    log.error("Unable to create {} for {}", adapter, action, ex);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsCreationError", adapter, name), // NOI18N
                    Bundle.getMessage("StartupActionsCreationError", adapter, name)));
                } catch (Exception ex) {
                    log.error("Unable to load {} into {}", action, adapter, ex);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsLoadError", adapter, name), // NOI18N
                    Bundle.getMessage("StartupActionsLoadError", adapter, name)));
                }
            }
        } catch (NullPointerException ex) {
            // ignore - this indicates migration has not occured
            log.debug("No element to read");
        }
        if (perform) {
            this.actions.stream().filter((action) -> (action.isValid())).forEachOrdered((action) -> {
                try {
                    action.performAction();
                } catch (JmriException ex) {
                    this.addInitializationException(profile, ex);
                }
            });
        }
        this.isDirty = false;
        this.restartRequired = false;
        this.setInitialized(profile, true);
        List<Exception> exceptions = this.getInitializationExceptions(profile);
        if (exceptions.size() == 1) {
            throw new InitializationException(exceptions.get(0));
        } else if (exceptions.size() > 1) {
            throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsMultipleErrors"), // NOI18N
            Bundle.getMessage("StartupActionsMultipleErrors"));
        }
    }
}
Also used : ConfigXmlManager(jmri.configurexml.ConfigXmlManager) StartupModel(apps.startup.StartupModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) RosterConfigManager(jmri.jmrit.roster.RosterConfigManager) JDOMUtil(jmri.util.jdom.JDOMUtil) ProgrammerConfigManager(jmri.jmrit.symbolicprog.ProgrammerConfigManager) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Profile(jmri.profile.Profile) XmlAdapter(jmri.configurexml.XmlAdapter) Logger(org.slf4j.Logger) FileLocationsPreferences(jmri.implementation.FileLocationsPreferences) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) JmriException(jmri.JmriException) WarrantPreferences(jmri.jmrit.logix.WarrantPreferences) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StartupActionModelUtil(apps.startup.StartupActionModelUtil) List(java.util.List) ManagerDefaultSelector(jmri.managers.ManagerDefaultSelector) PreferencesManager(jmri.spi.PreferencesManager) StartupModelFactory(apps.startup.StartupModelFactory) Element(org.jdom2.Element) Element(org.jdom2.Element) JmriException(jmri.JmriException) InitializationException(jmri.util.prefs.InitializationException) InitializationException(jmri.util.prefs.InitializationException) JDOMException(org.jdom2.JDOMException) JmriException(jmri.JmriException) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 42 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonSensorSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        // Sensor INACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        // Sensor ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        // Sensor UNKNOWN - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor INCONSISTENT - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor no value
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonException exception = null;
        try {
            service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        Assert.assertNull(exception);
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Sensor(jmri.Sensor)

Example 43 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonSensorSocketServiceTest method testSensorChange.

public void testSensorChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        // TODO: test that service is listener in SensorManager
        // -1 not possible value
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.ACTIVE;
        }, "Sensor ACTIVE");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.INACTIVE;
        }, "Sensor INACTIVE");
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        service.onClose();
    // TODO: test that service is no longer a listener in SensorManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Sensor(jmri.Sensor)

Example 44 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonSignalHeadSocketServiceTest method testOnMessageChange.

@Test
public void testOnMessageChange() {
    JsonNode message;
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
    //create a signalhead for testing
    String sysName = "IH1";
    String userName = "SH1";
    SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
    jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
    Assert.assertNotNull(s);
    try {
        // SignalHead Yellow
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
        //state should be Yellow
        Assert.assertEquals(SignalHead.YELLOW, s.getState());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
    Exception exception = null;
    try {
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
    } catch (IOException | JmriException | JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    //state should be Yellow
    Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
Also used : JsonException(jmri.server.json.JsonException) SignalHead(jmri.SignalHead) 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) JsonMockConnection(jmri.server.json.JsonMockConnection) Test(org.junit.Test)

Example 45 with JmriException

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

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