use of jmri.SensorManager in project JMRI by JMRI.
the class LayoutPanelServlet method getXmlPanel.
@Override
protected String getXmlPanel(String name) {
log.debug("Getting {} for {}", getPanelType(), name);
try {
LayoutEditor editor = (LayoutEditor) getEditor(name);
Element panel = new Element("panel");
panel.setAttribute("name", name);
panel.setAttribute("paneltype", getPanelType());
panel.setAttribute("height", Integer.toString(editor.getLayoutHeight()));
panel.setAttribute("width", Integer.toString(editor.getLayoutWidth()));
panel.setAttribute("panelheight", Integer.toString(editor.getLayoutHeight()));
panel.setAttribute("panelwidth", Integer.toString(editor.getLayoutWidth()));
panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
panel.setAttribute("xscale", Float.toString((float) editor.getXScale()));
panel.setAttribute("yscale", Float.toString((float) editor.getYScale()));
panel.setAttribute("mainlinetrackwidth", Integer.toString(editor.getMainlineTrackWidth()));
panel.setAttribute("sidetrackwidth", Integer.toString(editor.getSideTrackWidth()));
panel.setAttribute("turnoutcircles", (editor.getTurnoutCircles()) ? "yes" : "no");
panel.setAttribute("turnoutcirclesize", Integer.toString(editor.getTurnoutCircleSize()));
panel.setAttribute("turnoutdrawunselectedleg", (editor.getTurnoutDrawUnselectedLeg()) ? "yes" : "no");
if (editor.getBackgroundColor() == null) {
panel.setAttribute("backgroundcolor", ColorUtil.colorToString(Color.lightGray));
} else {
panel.setAttribute("backgroundcolor", ColorUtil.colorToString(editor.getBackgroundColor()));
}
panel.setAttribute("defaulttrackcolor", editor.getDefaultTrackColor());
panel.setAttribute("defaultoccupiedtrackcolor", editor.getDefaultOccupiedTrackColor());
panel.setAttribute("defaultalternativetrackcolor", editor.getDefaultAlternativeTrackColor());
panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
panel.setAttribute("turnoutcirclecolor", editor.getTurnoutCircleColor());
// include positionable elements
List<Positionable> contents = editor.getContents();
log.debug("N positionable elements: {}", contents.size());
for (Positionable sub : contents) {
if (sub != null) {
try {
Element e = ConfigXmlManager.elementFromObject(sub);
if (e != null) {
if ("signalmasticon".equals(e.getName())) {
//insert icon details into signalmast
e.addContent(getSignalMastIconsElement(e.getAttributeValue("signalmast")));
}
try {
e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
} catch (NullPointerException ex) {
if (sub.getNamedBean() == null) {
log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
} else {
log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
}
}
parsePortableURIs(e);
panel.addContent(e);
}
} catch (Exception ex) {
log.error("Error storing panel element: " + ex, ex);
}
}
}
// include PositionablePoints
int num = editor.pointList.size();
log.debug("N positionablepoint elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.pointList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel positionalpoint element: " + e);
}
}
}
// include LayoutBlocks
LayoutBlockManager tm = InstanceManager.getDefault(LayoutBlockManager.class);
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
SensorManager sm = InstanceManager.sensorManagerInstance();
num = 0;
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during LayoutBlock store");
}
LayoutBlock b = tm.getBySystemName(sname);
if (b.getUseCount() > 0) {
// save only those LayoutBlocks that are in use--skip abandoned ones
Element elem = new Element("layoutblock").setAttribute("systemname", sname);
String uname = b.getUserName();
if (uname != null && !uname.isEmpty()) {
elem.setAttribute("username", uname);
}
// get occupancy sensor from layoutblock if it is valid
if (!b.getOccupancySensorName().isEmpty()) {
Sensor s = sm.getSensor(b.getOccupancySensorName());
if (s != null) {
//send systemname
elem.setAttribute("occupancysensor", s.getSystemName());
}
//if layoutblock has no occupancy sensor, use one from block, if it is populated
} else {
Sensor s = b.getBlock().getSensor();
if (s != null) {
//send systemname
elem.setAttribute("occupancysensor", s.getSystemName());
}
}
elem.setAttribute("occupiedsense", Integer.toString(b.getOccupiedSense()));
elem.setAttribute("trackcolor", ColorUtil.colorToString(b.getBlockTrackColor()));
elem.setAttribute("occupiedcolor", ColorUtil.colorToString(b.getBlockOccupiedColor()));
elem.setAttribute("extracolor", ColorUtil.colorToString(b.getBlockExtraColor()));
if (!b.getMemoryName().isEmpty()) {
elem.setAttribute("memory", b.getMemoryName());
}
if (!b.useDefaultMetric()) {
elem.addContent(new Element("metric").addContent(Integer.toString(b.getBlockMetric())));
}
//add to the panel xml
panel.addContent(elem);
num++;
}
}
log.debug("N layoutblock elements: {}", num);
// include LevelXings
num = editor.xingList.size();
log.debug("N levelxing elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.xingList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel levelxing element: " + e);
}
}
}
// include LayoutTurnouts
num = editor.turnoutList.size();
log.debug("N layoutturnout elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.turnoutList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel layoutturnout element: " + e);
}
}
}
// include TrackSegments
num = editor.trackList.size();
log.debug("N tracksegment elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.trackList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel tracksegment element: " + e);
}
}
}
// include LayoutSlips
num = editor.slipList.size();
log.debug("N layoutSlip elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.slipList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel layoutSlip element: " + e);
}
}
}
// include LayoutTurntables
num = editor.turntableList.size();
log.debug("N turntable elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.turntableList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel turntable element: " + e);
}
}
}
//write out formatted document
Document doc = new Document(panel);
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
return fmt.outputString(doc);
} catch (NullPointerException ex) {
log.warn("Requested Layout panel [" + name + "] does not exist.");
return "ERROR Requested panel [" + name + "] does not exist.";
}
}
use of jmri.SensorManager 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());
}
}
use of jmri.SensorManager 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());
}
}
use of jmri.SensorManager in project JMRI by JMRI.
the class JsonSensorHttpServiceTest method testDoGet.
public void testDoGet() throws JmriException {
JsonSensorHttpService service = new JsonSensorHttpService(new ObjectMapper());
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
// no value
Sensor sensor1 = manager.provideSensor("IS1");
JsonNode result;
try {
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonSensor.SENSOR, result.path(JSON.TYPE).asText());
Assert.assertEquals("IS1", result.path(JSON.DATA).path(JSON.NAME).asText());
// -1 is not a possible value
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.ACTIVE);
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.INACTIVE);
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(Sensor.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.SensorManager in project JMRI by JMRI.
the class PositionableShape method setControlSensor.
/**
* Attach a named sensor to shape
*
* @param pName Used as a system/user name to lookup the sensor object
* @param hide true if sensor should be hidden
* @param level level at which sensor is placed
* @return error message, if any
*/
public String setControlSensor(String pName, boolean hide, int level) {
String msg = null;
if (pName == null || pName.trim().isEmpty()) {
setControlSensorHandle(null);
return null;
}
NamedBeanHandle<Sensor> senHandle = null;
_saveLevel = super.getDisplayLevel();
Optional<SensorManager> sensorManager = InstanceManager.getOptionalDefault(SensorManager.class);
if (sensorManager.isPresent()) {
Sensor sensor = sensorManager.get().getSensor(pName);
Optional<NamedBeanHandleManager> nbhm = InstanceManager.getOptionalDefault(NamedBeanHandleManager.class);
if (sensor != null) {
if (nbhm.isPresent()) {
senHandle = nbhm.get().getNamedBeanHandle(pName, sensor);
}
_doHide = hide;
_changeLevel = level;
if (_changeLevel <= 0) {
_changeLevel = super.getDisplayLevel();
}
} else {
// NOI18N
msg = Bundle.getMessage("badSensorName", pName);
}
} else {
// NOI18N
msg = Bundle.getMessage("NoSensorManager");
}
if (msg != null) {
log.warn("{} for {} sensor", msg, Bundle.getMessage("VisibleSensor"));
}
setControlSensorHandle(senHandle);
return msg;
}
Aggregations