Example #1
0
  /**
   * Tries to connect either by IP or serial bus, depending on supplied config data.
   *
   * @return true if connection was established, false otherwise
   */
  public static synchronized boolean connect() {
    if (cvIp != null && cvPort > 0) {
      OwfsConnectionFactory owfsConnectorFactory = new OwfsConnectionFactory(cvIp, cvPort);
      OwfsConnectionConfig owConnectionConfig = new OwfsConnectionConfig(cvIp, cvPort);
      owConnectionConfig.setTemperatureScale(cvTempScale);
      owConnectionConfig.setPersistence(OwPersistence.ON);
      owConnectionConfig.setBusReturn(OwBusReturn.ON);
      owfsConnectorFactory.setConnectionConfig(owConnectionConfig);

      try {
        cvOwConnection = owfsConnectorFactory.createNewConnection();
        cvOwConnection.listDirectory("/");
        logger.info("Connected to owserver [IP '" + cvIp + "' Port '" + cvPort + "']");
        cvIsEstablished = true;
        return true;
      } catch (Exception exception) {
        logger.error(
            "Couldn't connect to owserver [IP '" + cvIp + "' Port '" + cvPort + "']: ",
            exception.getLocalizedMessage());
        cvIsEstablished = false;
        return false;
      }
    } else {
      logger.warn(
          "Couldn't connect to owserver because of missing connection parameters [IP '{}' Port '{}'].",
          cvIp,
          cvPort);
      return false;
    }
  }
Example #2
0
 /**
  * Reconnects to owserver
  *
  * @return
  */
 public static synchronized boolean reconnect() {
   logger.info("Trying to reconnect to owserver...");
   try {
     cvOwConnection.disconnect();
   } catch (Exception lvException) {
     logger.error("Error while disconnecting from owserver: " + lvException, lvException);
   }
   cvOwConnection = null;
   return connect();
 }