Search in sources :

Example 1 with Sensor

use of jmri.Sensor in project JMRI by JMRI.

the class JsonRouteHttpServiceTest method testDoGetWithRouteSensor.

public void testDoGetWithRouteSensor() throws JmriException {
    JsonRouteHttpService service = new JsonRouteHttpService(new ObjectMapper());
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    Route route1 = manager.provideRoute("IR1", "Route1");
    Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
    route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
    JsonNode result;
    try {
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonRouteServiceFactory.ROUTE, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.ACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.INACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 2 with Sensor

use of jmri.Sensor in project JMRI by JMRI.

the class TamsSensorManager method getNextValidAddress.

@Override
public String getNextValidAddress(String curAddress, String prefix) {
    String tmpSName = "";
    try {
        tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
        jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showInfoMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
        return null;
    }
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    Sensor s = getBySystemName(tmpSName);
    if (s != null) {
        port++;
        while (port < 17) {
            try {
                tmpSName = createSystemName(board + ":" + port, prefix);
            } catch (Exception e) {
                log.error("Error creating system name for " + board + ":" + port);
            }
            s = getBySystemName(tmpSName);
            if (s == null) {
                StringBuilder sb = new StringBuilder();
                sb.append(board);
                sb.append(":");
                //Little work around to pad single digit address out.
                padPortNumber(port, sb);
                return sb.toString();
            }
            port++;
        }
        return null;
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append(board);
        sb.append(":");
        //Little work around to pad single digit address out.
        padPortNumber(port, sb);
        return sb.toString();
    }
}
Also used : JmriException(jmri.JmriException) JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 3 with Sensor

use of jmri.Sensor in project JMRI by JMRI.

the class AbstractSensorManagerConfigXML method loadSensors.

/**
     * Utility method to load the individual Sensor objects. If there's no
     * additional info needed for a specific sensor type, invoke this with the
     * parent of the set of Sensor elements.
     *
     * @param sensors Element containing the Sensor elements to load.
     * @return true if succeeded
     */
@SuppressWarnings("unchecked")
public boolean loadSensors(Element sensors) throws jmri.configurexml.JmriConfigureXmlException {
    boolean result = true;
    List<Element> sensorList = sensors.getChildren("sensor");
    if (log.isDebugEnabled()) {
        log.debug("Found " + sensorList.size() + " sensors");
    }
    SensorManager tm = InstanceManager.sensorManagerInstance();
    long goingActive = 0L;
    long goingInActive = 0L;
    if (sensors.getChild("globalDebounceTimers") != null) {
        Element timer = sensors.getChild("globalDebounceTimers");
        try {
            if (timer.getChild("goingActive") != null) {
                String active = timer.getChild("goingActive").getText();
                goingActive = Long.valueOf(active);
                tm.setDefaultSensorDebounceGoingActive(goingActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
        try {
            if (timer.getChild("goingInActive") != null) {
                String inActive = timer.getChild("goingInActive").getText();
                goingInActive = Long.valueOf(inActive);
                tm.setDefaultSensorDebounceGoingInActive(goingInActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
    }
    for (int i = 0; i < sensorList.size(); i++) {
        String sysName = getSystemName(sensorList.get(i));
        if (sysName == null) {
            handleException("Unexpected missing system name while loading sensors", null, null, null, null);
            result = false;
            break;
        }
        boolean inverted = false;
        String userName = getUserName(sensorList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (sensorList.get(i).getAttribute("inverted") != null) {
            if (sensorList.get(i).getAttribute("inverted").getValue().equals("true")) {
                inverted = true;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("create sensor: (" + sysName + ")");
        }
        Sensor s;
        try {
            s = tm.newSensor(sysName, userName);
        } catch (IllegalArgumentException e) {
            handleException("Could not create sensor", null, sysName, userName, null);
            result = false;
            continue;
        }
        // load common parts
        loadCommon(s, sensorList.get(i));
        if (sensorList.get(i).getChild("debounceTimers") != null) {
            Element timer = sensorList.get(i).getChild("debounceTimers");
            try {
                if (timer.getChild("goingActive") != null) {
                    String active = timer.getChild("goingActive").getText();
                    s.setSensorDebounceGoingActiveTimer(Long.valueOf(active));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
            try {
                if (timer.getChild("goingInActive") != null) {
                    String inActive = timer.getChild("goingInActive").getText();
                    s.setSensorDebounceGoingInActiveTimer(Long.valueOf(inActive));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
        }
        if (sensorList.get(i).getChild("useGlobalDebounceTimer") != null) {
            if (sensorList.get(i).getChild("useGlobalDebounceTimer").getText().equals("yes")) {
                s.useDefaultTimerSettings(true);
            }
        }
        s.setInverted(inverted);
        if (sensorList.get(i).getChild("pullResistance") != null) {
            String pull = sensorList.get(i).getChild("pullResistance").getText();
            log.debug("setting pull to {} for sensor {}", pull, s);
            s.setPullResistance(jmri.Sensor.PullResistance.getByShortName(pull));
        }
    }
    return result;
}
Also used : SensorManager(jmri.SensorManager) Element(org.jdom2.Element) Sensor(jmri.Sensor)

Example 4 with Sensor

use of jmri.Sensor in project JMRI by JMRI.

the class AbstractSensorManagerConfigXML method store.

public Element store(Object o, Element sensors) {
    setStoreElementClass(sensors);
    SensorManager tm = (SensorManager) o;
    if (tm.getDefaultSensorDebounceGoingActive() > 0 || tm.getDefaultSensorDebounceGoingInActive() > 0) {
        Element elem = new Element("globalDebounceTimers");
        elem.addContent(new Element("goingActive").addContent(String.valueOf(tm.getDefaultSensorDebounceGoingActive())));
        elem.addContent(new Element("goingInActive").addContent(String.valueOf(tm.getDefaultSensorDebounceGoingInActive())));
        sensors.addContent(elem);
    }
    java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
    // don't return an element if there are not sensors to include
    if (!iter.hasNext()) {
        return null;
    }
    // store the sensors
    while (iter.hasNext()) {
        String sname = iter.next();
        log.debug("system name is " + sname);
        Sensor s = tm.getBySystemName(sname);
        String inverted = s.getInverted() ? "true" : "false";
        Element elem = new Element("sensor").setAttribute("inverted", inverted);
        elem.addContent(new Element("systemName").addContent(sname));
        log.debug("store sensor " + sname);
        if (s.useDefaultTimerSettings()) {
            elem.addContent(new Element("useGlobalDebounceTimer").addContent("yes"));
        } else {
            if (s.getSensorDebounceGoingActiveTimer() > 0 || s.getSensorDebounceGoingInActiveTimer() > 0) {
                Element timer = new Element("debounceTimers");
                timer.addContent(new Element("goingActive").addContent(String.valueOf(s.getSensorDebounceGoingActiveTimer())));
                timer.addContent(new Element("goingInActive").addContent(String.valueOf(s.getSensorDebounceGoingInActiveTimer())));
                elem.addContent(timer);
            }
        }
        if (tm.isPullResistanceConfigurable()) {
            // store the sensor's value for pull resistance.
            elem.addContent(new Element("pullResistance").addContent(s.getPullResistance().getShortName()));
        }
        // store common part
        storeCommon(s, elem);
        sensors.addContent(elem);
    }
    return sensors;
}
Also used : SensorManager(jmri.SensorManager) Element(org.jdom2.Element) Sensor(jmri.Sensor)

Example 5 with Sensor

use of jmri.Sensor in project JMRI by JMRI.

the class JsonRouteSocketService method addListenersToChildren.

private void addListenersToChildren() {
    InstanceManager.getDefault(RouteManager.class).getSystemNameList().stream().forEach((rn) -> {
        //add listeners to each child (if not already)
        if (!routeListeners.containsKey(rn)) {
            log.debug("adding RouteListener for Route {}", rn);
            Route route = InstanceManager.getDefault(RouteManager.class).getRoute(rn);
            if (route != null) {
                Sensor sensor = route.getTurnoutsAlgdSensor();
                if (sensor != null) {
                    RouteListener listener = new RouteListener(route);
                    sensor.addPropertyChangeListener(listener);
                    this.routeListeners.put(rn, listener);
                }
            }
        }
    });
}
Also used : Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Aggregations

Sensor (jmri.Sensor)242 Test (org.junit.Test)51 SensorManager (jmri.SensorManager)31 Turnout (jmri.Turnout)26 JmriException (jmri.JmriException)24 Route (jmri.Route)14 JsonException (jmri.server.json.JsonException)13 SignalHead (jmri.SignalHead)11 SignalMast (jmri.SignalMast)11 Block (jmri.Block)10 Light (jmri.Light)9 NamedBean (jmri.NamedBean)9 Element (org.jdom2.Element)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 IOException (java.io.IOException)8 RouteManager (jmri.RouteManager)6 ActionEvent (java.awt.event.ActionEvent)5 JButton (javax.swing.JButton)5 JLabel (javax.swing.JLabel)5 Memory (jmri.Memory)5