public synchronized int setRelayByName(String p, boolean v) {
   boolean state = getRelayStateByName(p);
   if (state != v) {
     print("Controller setting relay " + p + " to " + v + " #Controller");
     return environment.setRelayByName(p, v);
   } else {
     return getRelayNumberByName(p);
   }
 }
  // Do work!
  public void run() {
    // Startup the serial comm you'll use
    SerialComm tempSerial = new SerialComm(this);

    Server tempServe = new Server(this);

    // let you're new model know who daddy is
    model.setController(this);

    // Create and start the serialComm, wait to hear back
    Thread sThread = new Thread(tempSerial);
    print("SThread Created, about to run #Controller");
    sThread.start();
    // Create and start the Server thread, wait to hear back
    Thread servThread = new Thread(tempServe);
    servThread.start();

    while (!serialIsSet) {
      // Wait for serial to start up
      Thread.yield();
    }

    while (!serverIsSet) {
      // Wait for server to come up
      Thread.yield();
    }

    print("Serial and Server Confirmed #controller");

    // tell the environment we're setup and ready to go!
    environment.setController(this);

    sendSerial(ARDUINO_INIT);

    print("Configuring Pins on the Arduino + controller");
    // set the pins on the Arduino
    ArrayList<Pins> outputs = model.getOutputs();
    for (int i = 0; i < outputs.size(); i++) {
      int pinNum = outputs.get(i).getPinNumber();
      sendSerial(SET_DIGITAL_OUT_PIN);
      sendSerial(pinNum);
    }
    ArrayList<Pins> inputs = model.getInputs();
    for (int i = 0; i < inputs.size(); i++) {
      int pinNum = inputs.get(i).getPinNumber();
      sendSerial(SET_DIGITAL_IN_PIN);
      sendSerial(pinNum);
    }
    ArrayList<Pins> analogInputs = model.getAnalogInputs();
    for (int i = 0; i < analogInputs.size(); i++) {
      int pinNum = analogInputs.get(i).getPinNumber();
      sendSerial(SET_ANALOG_IN_PIN);
      sendSerial(pinNum);
    }

    // Tell arduino to load new settings
    sendSerial(ARDUINO_LOAD_PINS);

    // Send the initialization command to the serialcomm
    print("Sending init serial message #controller");
    sendSerial(INITIALIZATION_COMMAND);

    Thread.yield();

    rules.setController(this);

    Thread ruleMan = new Thread(rules);
    ruleMan.start();

    while (!rulesIsSet) {
      Thread.yield();
    }

    while (!terminus) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        print("Interrupted? #Controller");
      }
    }

    ruleMan.interrupt();
    servThread.interrupt();
    sThread.interrupt();
    print("rules and serial and server told to terminuate #controller");
  }
 public ArrayList<Button> getButtons() {
   return environment.getButtons();
 }
 public ArrayList<Relay> getRelays() {
   return environment.getRelays();
 }
 public float getSensorValueByName(String s) {
   return environment.getSensorValueByName(s);
 }
 public Boolean getButtonStateByName(String s) {
   //	print("Controller getting button state of " + s + " #controller");
   return environment.getButtonStateByName(s);
 }
 public int getRelaysInGroup(String s) {
   return environment.getNumberOfRelaysInGroup(s);
 }
 public String[] getRelayGroups() {
   return environment.getRelayGroups();
 }
 public int getRelayNumberByName(String s) {
   return environment.getRelayNumByName(s);
 }
 public String getRelayGroupByName(String s) {
   return environment.getRelayGroupByName(s);
 }
 public boolean getRelayStateByName(String s) {
   // print("Controller getting relay state by name " + s + " #Controller");
   return environment.getRelayStateByName(s);
 }
 public synchronized void toggleRelayByName(String s) {
   environment.toggleRelay(s);
   print("Controller telling environment to toggle relay " + s + " #Controller");
 }
 // Convenience method for a rule to toggle a relay
 public synchronized void toggleRelay(int r) {
   environment.toggleRelay(r);
   print("Controller telling environment to toggle relay " + r + " #Controller");
 }
 // model to send a pushed digital pin update to environment
 public void updateRelayByPin(int p, boolean v) {
   environment.updateRelayByPin(p, v);
   print("Controller updating relay value by pin " + p + " " + v + " #controller");
 }
 public synchronized void updateSensorByPin(int p, int value) {
   environment.updateSensorByPin(p, value);
   // print("Controller updateing environment on sensor: " + p + " to value " + value + "
   // #Controller");
 }
 // Model updating the environment to a digital input (button press)
 public synchronized void updateButtonByPin(int p, int value) {
   boolean b = (value == 0) ? false : true;
   environment.updateButtonByPin(p, b);
   print("Controller updating environment on button: " + p + " press: " + value + " #Controller");
 }