예제 #1
0
  /** @param args */
  public static void main(final String[] args) {
    // System.out is the standard output stream of our environment (here, most likely terminal)
    System.out.println(
        "This example shows how to establish a KNX connection " + "to a KNXnet/IP server.");

    try {
      // KNXNetworkLink is the base interface of a Calimero link to a KNX network.
      // For all the Calimero interfaces you don't know yet, check the Calimero API
      // documentation.
      final KNXNetworkLink knxLink;

      // We have to provide our own local and remote endpoints to the network link
      // using an InetSocketAddress, which we create first. This approach is of advantage if
      // our computer is multi-homed, i.e., has several network interface cards, the default
      // configuration of local host (via InetAddress.getLocalHost) is not correct or not
      // desired, or if the KNXnet/IP server (i.e., remote endpoint) uses a non-default port.
      final InetSocketAddress localEP = new InetSocketAddress(InetAddress.getByName(localHost), 0);
      // now the remote port
      final InetSocketAddress remoteEP = new InetSocketAddress(remoteHost, knxServerPort);

      System.out.println("Try connecting to " + remoteHost + " on port " + knxServerPort + "...");

      // Here, we create the IP-based link, which tries to establish a connection to the
      // remote endpoint in its constructor.
      // This constructor also allows to specify the service mode of the link, i.e.,
      // KNX tunneling or routing according to the standard, and whether to use NAT-aware
      // (Network Address Translation) addressing. The last parameter tells our
      // link that the KNX network is based on twisted-pair medium, with TP1 currently being
      // the most commonly used one for KNX networks.
      knxLink =
          new KNXNetworkLinkIP(
              KNXNetworkLinkIP.TUNNELING, localEP, remoteEP, false, TPSettings.TP1);

      System.out.println("Connection to " + remoteHost + " successfully established");

      // We always make sure to close the connection after we used it
      knxLink.close();

      System.out.println("Connection got closed");
    } catch (final KNXException e) {

      // All checked Calimero-specific exceptions are subtypes of KNXException
      System.out.println("Error connecting to " + remoteHost + ": " + e.getMessage());
    } catch (final InterruptedException e) {

      // Longer tasks that might block, like connection procedures, are interruptible, e.g.,
      // by using Ctrl+C from the terminal; in such case, an instance of InterruptedException
      // is thrown.
      // If a task got interrupted, Calimero will clean up its internal state and resources.
      // Any deviation of this behavior (e.g., if not feasible) will be noted in the
      // Calimero API documentation.
      System.out.println("Connecting to " + remoteHost + " was interrupted, quit");
    } catch (final UnknownHostException e) {
      System.out.println("Host resolution for local endpoint " + localHost + " failed");
    }
  }
 /**
  * Creates a new property adapter for remote property access.
  *
  * @param link KNX network link used for communication with the KNX network
  * @param remote KNX individual address to access its interface objects
  * @param l property adapter listener to get notified about adapter events, use <code>null</code>
  *     for no listener
  * @param connOriented <code>true</code> to use connection oriented mode for access, <code>false
  *     </code> to use connectionless mode
  * @throws KNXLinkClosedException if the network link is closed
  */
 public RemotePropertyServiceAdapter(
     final KNXNetworkLink link,
     final IndividualAddress remote,
     final PropertyAdapterListener l,
     final boolean connOriented)
     throws KNXLinkClosedException {
   mc = new ManagementClientImpl(link);
   dst = mc.createDestination(remote, connOriented);
   pal = l;
   nll = pal != null ? new NLListener() : null;
   if (nll != null) link.addLinkListener(nll);
   key = null;
 }
 /* (non-Javadoc)
  * @see tuwien.auto.calimero.mgmt.PropertyAdapter#close()
  */
 public void close() {
   final KNXNetworkLink lnk = mc.detach();
   if (lnk != null && nll != null) lnk.removeLinkListener(nll);
 }