Exemplo n.º 1
0
  public Turnout createNewTurnout(String systemName, String userName) {
    Turnout t;
    int addr = Integer.valueOf(systemName.substring(2)).intValue();
    t = new DccTurnout(addr);
    t.setUserName(userName);

    return t;
  }
Exemplo n.º 2
0
  public String getNextValidAddress(String curAddress, String prefix) throws JmriException {
    // If the hardware address past does not already exist then this can
    // be considered the next valid address.
    String tmpSName = "";
    try {
      tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
      jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class)
          .showErrorMessage(
              "Error",
              "Unable to convert " + curAddress + " to a valid Hardware Address",
              "" + ex,
              "",
              true,
              false);
      return null;
    }

    Turnout t = getBySystemName(tmpSName);
    if (t == null) {
      return curAddress;
    }

    // This bit deals with handling the curAddress, and how to get the next address.
    int iName = 0;
    try {
      iName = Integer.parseInt(curAddress);
    } catch (NumberFormatException ex) {
      log.error("Unable to convert " + curAddress + " Hardware Address to a number");
      jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class)
          .showErrorMessage(
              "Error",
              "Unable to convert " + curAddress + " to a valid Hardware Address",
              "" + ex,
              "",
              true,
              false);
      return null;
    }
    // The Number of Output Bits of the previous turnout will help determine the next
    // valid address.
    iName = iName + t.getNumberOutputBits();
    // Check to determine if the systemName is in use, return null if it is,
    // otherwise return the next valid address.
    t = getBySystemName(prefix + typeLetter() + iName);
    if (t != null) {
      for (int x = 1; x < 10; x++) {
        iName = iName + t.getNumberOutputBits();
        t = getBySystemName(prefix + typeLetter() + iName);
        if (t == null) {
          return Integer.toString(iName);
        }
      }
      return null;
    } else {
      return Integer.toString(iName);
    }
  }
Exemplo n.º 3
0
 public void testRemoveListener() {
   Listen ln = new Listen();
   t.addPropertyChangeListener(ln);
   t.removePropertyChangeListener(ln);
   listenerResult = false;
   t.setUserName("user id");
   Assert.assertTrue(
       "listener should not have heard message after removeListner", !listenerResult);
 }
Exemplo n.º 4
0
 public void testAddListener() {
   t.addPropertyChangeListener(new Listen());
   listenerResult = false;
   t.setUserName("user id");
   Assert.assertTrue("listener invoked by setUserName", listenerResult);
   listenerResult = false;
   t.setCommandedState(Turnout.CLOSED);
   Assert.assertTrue("listener invoked by setCommandedState", listenerResult);
 }
Exemplo n.º 5
0
 @Test
 public void testRename() {
   // get turnout
   Turnout t1 = l.newTurnout(getSystemName(getNumToTest1()), "before");
   Assert.assertNotNull("t1 real object ", t1);
   t1.setUserName("after");
   Turnout t2 = l.getByUserName("after");
   Assert.assertEquals("same object", t1, t2);
   Assert.assertEquals("no old object", null, l.getByUserName("before"));
 }
Exemplo n.º 6
0
 public Turnout createNewTurnout(String systemName, String userName) {
   int addr;
   try {
     addr = Integer.valueOf(systemName.substring(getSystemPrefix().length() + 1)).intValue();
   } catch (java.lang.NumberFormatException e) {
     log.error("failed to convert systemName " + systemName + " to a turnout address");
     return null;
   }
   Turnout t = new MarklinTurnout(addr, getSystemPrefix(), tc);
   t.setUserName(userName);
   return t;
 }
Exemplo n.º 7
0
  public void testIntantiateNoLock() throws JmriException {
    OsIndicator os = new OsIndicator("IT12", "IS34", "");

    Turnout t1 = InstanceManager.turnoutManagerInstance().provideTurnout("IT12");
    t1.setCommandedState(Turnout.CLOSED);

    Sensor s1 = InstanceManager.sensorManagerInstance().provideSensor("IS34");
    s1.setKnownState(Sensor.INACTIVE);

    Assert.assertEquals("sensor before", Sensor.INACTIVE, s1.getKnownState());
    Assert.assertEquals("output before", Turnout.CLOSED, t1.getCommandedState());

    os.instantiate();

    Assert.assertEquals("sensor after instantiate", Sensor.INACTIVE, s1.getKnownState());
    Assert.assertEquals("output after instantiate", Turnout.CLOSED, t1.getCommandedState());
  }
Exemplo n.º 8
0
 /**
  * Deactivates a LightControl by control type. This method tests the control type, and deactivates
  * the control mechanism, appropriate for the control type.
  */
 public void deactivateLightControl() {
   // skip if Light Control is not active
   if (_active) {
     // deactivate according to control type
     switch (_controlType) {
       case Light.SENSOR_CONTROL:
         if (_sensorListener != null) {
           _namedControlSensor.getBean().removePropertyChangeListener(_sensorListener);
           _sensorListener = null;
         }
         break;
       case Light.FAST_CLOCK_CONTROL:
         if ((_clock != null) && (_timebaseListener != null)) {
           _clock.removeMinuteChangeListener(_timebaseListener);
           _timebaseListener = null;
         }
         break;
       case Light.TURNOUT_STATUS_CONTROL:
         if (_turnoutListener != null) {
           _controlTurnout.removePropertyChangeListener(_turnoutListener);
           _turnoutListener = null;
         }
         break;
       case Light.TIMED_ON_CONTROL:
         if (_timedSensorListener != null) {
           _namedTimedControlSensor.getBean().removePropertyChangeListener(_timedSensorListener);
           _timedSensorListener = null;
         }
         if (_lightOnTimerActive) {
           _timedControlTimer.stop();
           _lightOnTimerActive = false;
         }
         if (_timedControlTimer != null) {
           if (_timedControlListener != null) {
             _timedControlTimer.removeActionListener(_timedControlListener);
             _timedControlListener = null;
           }
           _timedControlTimer = null;
         }
         break;
       case Light.TWO_SENSOR_CONTROL:
         if (_sensorListener != null) {
           _namedControlSensor.getBean().removePropertyChangeListener(_sensorListener);
           _sensorListener = null;
         }
         if (_sensor2Listener != null) {
           _namedControlSensor2.getBean().removePropertyChangeListener(_sensor2Listener);
           _sensor2Listener = null;
         }
         break;
       default:
         log.warn(
             "Unexpected control type when activating Light: " + _parentLight.getSystemName());
     }
     _active = false;
   }
 }
Exemplo n.º 9
0
  public Turnout newTurnout(String systemName, String userName) {
    if (log.isDebugEnabled()) {
      log.debug(
          "newTurnout:"
              + ((systemName == null) ? "null" : systemName)
              + ";"
              + ((userName == null) ? "null" : userName));
    }
    if (systemName == null) {
      log.error(
          "SystemName cannot be null. UserName was " + ((userName == null) ? "null" : userName));
      throw new IllegalArgumentException(
          "SystemName cannot be null. UserName was " + ((userName == null) ? "null" : userName));
    }
    // is system name in correct format?
    if (!systemName.startsWith(getSystemPrefix() + typeLetter())) {
      log.error(
          "Invalid system name for turnout: "
              + systemName
              + " needed "
              + getSystemPrefix()
              + typeLetter());
      throw new IllegalArgumentException(
          "Invalid system name for turnout: "
              + systemName
              + " needed "
              + getSystemPrefix()
              + typeLetter());
    }

    // return existing if there is one
    Turnout s;
    if ((userName != null) && ((s = getByUserName(userName)) != null)) {
      if (getBySystemName(systemName) != s) {
        log.error(
            "inconsistent user ("
                + userName
                + ") and system name ("
                + systemName
                + ") results; userName related to ("
                + s.getSystemName()
                + ")");
      }
      return s;
    }
    if ((s = getBySystemName(systemName)) != null) {
      if ((s.getUserName() == null) && (userName != null)) {
        s.setUserName(userName);
      } else if (userName != null) {
        log.warn(
            "Found turnout via system name ("
                + systemName
                + ") with non-null user name ("
                + s.getUserName()
                + "). Turnout \""
                + systemName
                + "("
                + userName
                + ")\" cannot be used.");
      }
      return s;
    }

    // doesn't exist, make a new one
    s = createNewTurnout(systemName, userName);

    // if that failed, blame it on the input arguements
    if (s == null) {
      throw new IllegalArgumentException("Unable to create turnout from " + systemName);
    }

    // save in the maps if successful
    register(s);
    try {
      s.setStraightSpeed("Global");
    } catch (jmri.JmriException ex) {
      log.error(ex.toString());
    }

    try {
      s.setDivergingSpeed("Global");
    } catch (jmri.JmriException ex) {
      log.error(ex.toString());
    }
    return s;
  }
Exemplo n.º 10
0
 public void testCommandThrown() throws InterruptedException {
   t.setCommandedState(Turnout.THROWN);
   // check
   Assert.assertEquals("commanded state", jmri.Turnout.THROWN, t.getCommandedState());
   checkThrownMsgSent();
 }
Exemplo n.º 11
0
 public void testCommandClosed() throws InterruptedException {
   t.setCommandedState(Turnout.CLOSED);
   // check
   Assert.assertEquals("commanded state", jmri.Turnout.CLOSED, t.getCommandedState());
   checkClosedMsgSent();
 }
Exemplo n.º 12
0
  @Test
  public void testUpperLower() {
    Turnout t = l.provideTurnout("" + getNumToTest2());

    Assert.assertNull(l.getTurnout(t.getSystemName().toLowerCase()));
  }
Exemplo n.º 13
0
 // start of common tests
 // test creation - real work is in the setup() routine
 public void testCreate() {
   // initial commanded state when created must be UNKNOWN
   Assert.assertEquals("initial commanded state", Turnout.UNKNOWN, t.getCommandedState());
   // initial commanded state when created must be UNKNOWN
   Assert.assertEquals("initial known state", Turnout.UNKNOWN, t.getKnownState());
 }
Exemplo n.º 14
0
  /**
   * Activates a Light Control by control type. This method tests the control type, and set up a
   * control mechanism, appropriate for the control type.
   */
  public void activateLightControl() {
    // skip if Light Control is already active
    if (!_active) {
      // activate according to control type
      switch (_controlType) {
        case Light.SENSOR_CONTROL:
          _namedControlSensor = null;
          if (_controlSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
            _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
          }
          if (_namedControlSensor != null) {
            // if sensor state is currently known, set light accordingly
            int kState = _namedControlSensor.getBean().getKnownState();
            if (kState == Sensor.ACTIVE) {
              if (_controlSensorSense == Sensor.ACTIVE) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (kState == Sensor.INACTIVE) {
              if (_controlSensorSense == Sensor.INACTIVE) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in sensor state
            _namedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _sensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                              return; // ignore property change if user disabled Light
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                              int now = _namedControlSensor.getBean().getKnownState();
                              if (now == Sensor.ACTIVE) {
                                if (_controlSensorSense == Sensor.ACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                } else {
                                  // Turn light off
                                  _parentLight.setState(Light.OFF);
                                }
                              } else if (now == Sensor.INACTIVE) {
                                if (_controlSensorSense == Sensor.INACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                } else {
                                  // Turn light off
                                  _parentLight.setState(Light.OFF);
                                }
                              }
                            }
                          }
                        },
                    _controlSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: "
                    + _controlSensorName);
            return;
          }
          break;

        case Light.FAST_CLOCK_CONTROL:
          if (_clock == null) {
            _clock = InstanceManager.timebaseInstance();
          }
          // set up time as minutes in a day
          _timeOn = _fastClockOnHour * 60 + _fastClockOnMin;
          _timeOff = _fastClockOffHour * 60 + _fastClockOffMin;
          // initialize light based on current fast time
          updateClockControlLight();
          // set up to listen for time changes on a minute basis
          _clock.addMinuteChangeListener(
              _timebaseListener =
                  new java.beans.PropertyChangeListener() {
                    public void propertyChange(java.beans.PropertyChangeEvent e) {
                      if (_parentLight.getEnabled()) { // don't change light if not enabled
                        // update control if light is enabled
                        updateClockControlLight();
                      }
                    }
                  });
          _active = true;
          break;
        case Light.TURNOUT_STATUS_CONTROL:
          _controlTurnout =
              InstanceManager.turnoutManagerInstance().provideTurnout(_controlTurnoutName);
          if (_controlTurnout != null) {
            // set light based on current turnout state if known
            int tState = _controlTurnout.getKnownState();
            if (tState == Turnout.CLOSED) {
              if (_turnoutState == Turnout.CLOSED) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (tState == Turnout.THROWN) {
              if (_turnoutState == Turnout.THROWN) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in turnout state
            _controlTurnout.addPropertyChangeListener(
                _turnoutListener =
                    new java.beans.PropertyChangeListener() {
                      public void propertyChange(java.beans.PropertyChangeEvent e) {
                        if (!_parentLight.getEnabled()) {
                          return; // ignore property change if user disabled light
                        }
                        if (e.getPropertyName().equals("KnownState")) {
                          int now = _controlTurnout.getKnownState();
                          if (now == Turnout.CLOSED) {
                            if (_turnoutState == Turnout.CLOSED) {
                              // Turn light on
                              _parentLight.setState(Light.ON);
                            } else {
                              // Turn light off
                              _parentLight.setState(Light.OFF);
                            }
                          } else if (now == Turnout.THROWN) {
                            if (_turnoutState == Turnout.THROWN) {
                              // Turn light on
                              _parentLight.setState(Light.ON);
                            } else {
                              // Turn light off
                              _parentLight.setState(Light.OFF);
                            }
                          }
                        }
                      }
                    });
            _active = true;
          } else {
            // control turnout does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Turnout that does not exist: "
                    + _controlSensorName);
            return;
          }
          break;
        case Light.TIMED_ON_CONTROL:
          if (_timedSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_timedSensorName);
            _namedTimedControlSensor = nbhm.getNamedBeanHandle(_timedSensorName, sen);
          }
          if (_namedTimedControlSensor != null) {
            // set initial state off
            _parentLight.setState(Light.OFF);
            // listen for change in timed control sensor state
            _namedTimedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _timedSensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                              return; // ignore property change if user disabled light
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                              int now = _namedTimedControlSensor.getBean().getKnownState();
                              if (!_lightOnTimerActive) {
                                if (now == Sensor.ACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                  // Create a timer if one does not exist
                                  if (_timedControlTimer == null) {
                                    _timedControlListener = new TimeLight();
                                    _timedControlTimer =
                                        new Timer(_timeOnDuration, _timedControlListener);
                                  }
                                  // Start the Timer to turn the light OFF
                                  _lightOnTimerActive = true;
                                  _timedControlTimer.start();
                                }
                              }
                            }
                          }
                        },
                    _timedSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // timed control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: "
                    + _timedSensorName);
            return;
          }
          break;
        case Light.TWO_SENSOR_CONTROL:
          _namedControlSensor = null;
          _namedControlSensor2 = null;
          if (_controlSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
            _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
          }
          if (_controlSensor2Name.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensor2Name);
            _namedControlSensor2 = nbhm.getNamedBeanHandle(_controlSensor2Name, sen);
          }
          if ((_namedControlSensor != null) && (_namedControlSensor2 != null)) {
            // if sensor state is currently known, set light accordingly
            int kState = _namedControlSensor.getBean().getKnownState();
            int kState2 = _namedControlSensor2.getBean().getKnownState();
            if (_controlSensorSense == Sensor.ACTIVE) {
              if ((kState == Sensor.ACTIVE) || (kState2 == Sensor.ACTIVE)) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (_controlSensorSense == Sensor.INACTIVE) {
              if ((kState == Sensor.INACTIVE) || (kState2 == Sensor.INACTIVE)) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in sensor states
            _namedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _sensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                          }
                        },
                    _controlSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _namedControlSensor2
                .getBean()
                .addPropertyChangeListener(
                    _sensor2Listener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                          }
                        },
                    _controlSensor2Name,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // at least one control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: ");
            return;
          }
          break;
        default:
          log.warn(
              "Unexpected control type when activating Light: " + _parentLight.getSystemName());
      }
    }
  }
Exemplo n.º 15
0
 // DCCpp-specific methods
 public Turnout createNewTurnout(String systemName, String userName) {
   int addr = Integer.valueOf(systemName.substring(prefix.length() + 1)).intValue();
   Turnout t = new DCCppTurnout(prefix, addr, tc);
   t.setUserName(userName);
   return t;
 }
Exemplo n.º 16
0
 public void testDispose() {
   t.setCommandedState(Turnout.CLOSED); // in case registration with TrafficController
   // is deferred to after first use
   t.dispose();
   Assert.assertEquals("controller listeners remaining", 0, numListeners());
 }
Exemplo n.º 17
0
  /** Make the OPath from the icons in the Iterator */
  private OPath makeOPath(String name, ArrayList<Positionable> pathGp, boolean showMsg) {
    if (pathGp.size() == 0) {
      if (showMsg) {
        JOptionPane.showMessageDialog(
            this,
            Bundle.getMessage("noPathIcons"),
            Bundle.getMessage("makePath"),
            JOptionPane.INFORMATION_MESSAGE);
      }
      return null;
    }
    Iterator<Positionable> it = pathGp.iterator();
    ArrayList<BeanSetting> settings = new ArrayList<BeanSetting>();
    Portal fromPortal = null;
    Portal toPortal = null;
    boolean hasTrack = false;
    int portalIconCount = 0;
    while (it.hasNext()) {
      Positionable pos = it.next();
      if (pos instanceof IndicatorTurnoutIcon) {
        jmri.Turnout t = ((IndicatorTurnoutIcon) pos).getTurnout();
        String turnoutName = ((IndicatorTurnoutIcon) pos).getNamedTurnout().getName();
        int state = t.getKnownState();
        if (state != Turnout.CLOSED && state != Turnout.THROWN) {
          if (showMsg) {
            JOptionPane.showMessageDialog(
                this,
                Bundle.getMessage("turnoutNotSet", t.getDisplayName()),
                Bundle.getMessage("makePath"),
                JOptionPane.INFORMATION_MESSAGE);
          }
          return null;
        }
        settings.add(new BeanSetting(t, turnoutName, state));
        hasTrack = true;
      } else if (pos instanceof PortalIcon) {
        if (toPortal == null) {
          toPortal = ((PortalIcon) pos).getPortal();
        } else if (fromPortal == null) {
          fromPortal = ((PortalIcon) pos).getPortal();
        }
        portalIconCount++;
      } else if (pos instanceof IndicatorTrack) {
        hasTrack = true;
      }
    }
    if (showMsg) {
      if (!hasTrack) {
        JOptionPane.showMessageDialog(
            this,
            Bundle.getMessage("noPathIcons"),
            Bundle.getMessage("makePath"),
            JOptionPane.INFORMATION_MESSAGE);
        return null;
      }
      if (toPortal == null && fromPortal == null) {
        JOptionPane.showMessageDialog(
            this,
            Bundle.getMessage("tooFewPortals"),
            Bundle.getMessage("makePath"),
            JOptionPane.INFORMATION_MESSAGE);
        return null;
      }
      if (portalIconCount == 0) {
        JOptionPane.showMessageDialog(
            this,
            Bundle.getMessage("noPortalIcons"),
            Bundle.getMessage("makePath"),
            JOptionPane.INFORMATION_MESSAGE);
      }
      if (portalIconCount > 2) {
        JOptionPane.showMessageDialog(
            this,
            Bundle.getMessage("tooManyPortals"),
            Bundle.getMessage("makePath"),
            JOptionPane.INFORMATION_MESSAGE);
        return null;
      }
    }

    if (hasTrack && portalIconCount > 0 && portalIconCount < 3) {
      return new OPath(name, _block, fromPortal, toPortal, settings);
    }
    return null;
  }