use of jmri.Block in project JMRI by JMRI.
the class BlockValueFile method readBlockValues.
/*
* Reads Block values from a file in the user's preferences directory
* If the file containing block values does not exist this routine returns quietly.
* If a Block named in the file does not exist currently, that entry is quietly ignored.
*/
@SuppressWarnings("unchecked")
public void readBlockValues() throws org.jdom2.JDOMException, java.io.IOException {
log.debug("entered readBlockValues");
List<String> blocks = blockManager.getSystemNameList();
// check if file exists
if (checkFile(defaultFileName)) {
// file is present,
root = rootFromName(defaultFileName);
if ((root != null) && (blocks.size() > 0)) {
// there is a file and there are Blocks defined
Element blockvalues = root.getChild("blockvalues");
if (blockvalues != null) {
// there are values defined, read and set block values if Block exists.
List<Element> blockList = blockvalues.getChildren("block");
for (int i = 0; i < blockList.size(); i++) {
if ((blockList.get(i)).getAttribute("systemname") == null) {
log.warn("unexpected null in systemName " + blockList.get(i) + " " + blockList.get(i).getAttributes());
break;
}
String sysName = blockList.get(i).getAttribute("systemname").getValue();
// get Block - ignore entry if block not found
Block b = blockManager.getBySystemName(sysName);
if (b != null) {
// Block was found, set its value
Object v = blockList.get(i).getAttribute("value").getValue();
if (blockList.get(i).getAttribute("valueClass") != null) {
if (blockList.get(i).getAttribute("valueClass").getValue().equals("jmri.jmrit.roster.RosterEntry")) {
jmri.jmrit.roster.RosterEntry re = jmri.jmrit.roster.Roster.getDefault().getEntryForId(((String) v));
if (re != null) {
v = re;
}
}
}
b.setValue(v);
// set direction if there is one
int dd = jmri.Path.NONE;
Attribute a = blockList.get(i).getAttribute("dir");
if (a != null) {
try {
dd = a.getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert direction attribute");
}
}
b.setDirection(dd);
}
}
}
}
}
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method removeAdjacency.
void removeAdjacency(LayoutBlock layoutBlock) {
if (enableDeleteRouteLogging) {
log.info("From " + this.getDisplayName() + " Adjacency to be removed " + layoutBlock.getDisplayName());
}
Block removedBlock = layoutBlock.getBlock();
//Work our way backward through the list of neighbours
//We need to work out which routes to remove first.
//here we simply remove the routes which are advertised from the removed neighbour
ArrayList<Routes> tmpBlock = removeRouteRecievedFromNeighbour(removedBlock);
for (int i = neighbours.size() - 1; i > -1; i--) {
//Use to check against direction but don't now.
if ((neighbours.get(i).getBlock() == removedBlock)) {
//Was previously before the for loop.
//Pos move the remove list and remove thoughpath out of this for loop.
layoutBlock.removePropertyChangeListener(this);
if (enableDeleteRouteLogging) {
log.info("From " + this.getDisplayName() + " block " + removedBlock.getDisplayName() + " found and removed");
}
LayoutBlock layoutBlockToNotify = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(neighbours.get(i).getBlock());
getAdjacency(neighbours.get(i).getBlock()).dispose();
neighbours.remove(i);
layoutBlockToNotify.notifiedNeighbourNoLongerMutual(this);
}
}
for (int i = throughPaths.size() - 1; i > -1; i--) {
if (throughPaths.get(i).getSourceBlock() == removedBlock) {
//only mark for removal if the source isn't in the adjcency table
if (getAdjacency(throughPaths.get(i).getSourceBlock()) == null) {
if (enableDeleteRouteLogging) {
log.info("remove " + throughPaths.get(i).getSourceBlock().getDisplayName() + " to " + throughPaths.get(i).getDestinationBlock().getDisplayName());
}
throughPaths.remove(i);
}
} else if (throughPaths.get(i).getDestinationBlock() == removedBlock) {
//only mark for removal if the destination isn't in the adjcency table
if (getAdjacency(throughPaths.get(i).getDestinationBlock()) == null) {
if (enableDeleteRouteLogging) {
log.info("remove " + throughPaths.get(i).getSourceBlock().getDisplayName() + " to " + throughPaths.get(i).getDestinationBlock().getDisplayName());
}
throughPaths.remove(i);
}
}
}
if (enableDeleteRouteLogging) {
log.info("From " + this.getDisplayName() + " neighbour has been removed - Number of routes to this neighbour removed" + tmpBlock.size());
}
notifyNeighboursOfRemoval(tmpBlock, removedBlock);
}
use of jmri.Block in project JMRI by JMRI.
the class ConnectivityUtil method getDirectionFromAnchor.
/**
* Matches the anchor point to an Entry Point, and returns the direction
* specified in the Entry Point If no match is found, UNKNOWN is returned,
* indicating that the block boundary is internal to the Section.
*/
public int getDirectionFromAnchor(ArrayList<EntryPoint> mForwardEntryPoints, ArrayList<EntryPoint> mReverseEntryPoints, PositionablePoint p) {
Block block1 = p.getConnect1().getLayoutBlock().getBlock();
Block block2 = p.getConnect2().getLayoutBlock().getBlock();
for (int i = 0; i < mForwardEntryPoints.size(); i++) {
EntryPoint ep = mForwardEntryPoints.get(i);
if (((ep.getBlock() == block1) && (ep.getFromBlock() == block2)) || ((ep.getBlock() == block2) && (ep.getFromBlock() == block1))) {
return EntryPoint.FORWARD;
}
}
for (int j = 0; j < mReverseEntryPoints.size(); j++) {
EntryPoint ep = mReverseEntryPoints.get(j);
if (((ep.getBlock() == block1) && (ep.getFromBlock() == block2)) || ((ep.getBlock() == block2) && (ep.getFromBlock() == block1))) {
return EntryPoint.REVERSE;
}
}
return EntryPoint.UNKNOWN;
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method addAdjacency.
//This works out our direction of route flow correctly
void addAdjacency(jmri.Path addPath) {
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " path to be added " + addPath.getBlock().getDisplayName() + " " + Path.decodeDirection(addPath.getToBlockDirection()));
}
Block destBlockToAdd = addPath.getBlock();
int ourWorkingDirection = RXTX;
if (destBlockToAdd == null) {
log.error("Found null destination block for path from " + this.getDisplayName());
return;
}
if (this.getBlock().isBlockDenied(destBlockToAdd.getDisplayName())) {
ourWorkingDirection = RXONLY;
} else if (destBlockToAdd.isBlockDenied(this.getBlock().getDisplayName())) {
ourWorkingDirection = TXONLY;
}
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " to block " + addPath.getBlock().getDisplayName() + " we should therefore be... " + decodePacketFlow(ourWorkingDirection));
}
addNeighbour(addPath.getBlock(), addPath.getToBlockDirection(), ourWorkingDirection);
}
use of jmri.Block 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;
}
Aggregations