2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.feican.internal;
15 import java.io.Closeable;
16 import java.io.IOException;
17 import java.net.DatagramPacket;
18 import java.net.DatagramSocket;
19 import java.net.InetAddress;
20 import java.net.SocketException;
21 import java.net.UnknownHostException;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
26 * Manages the connection to a Feican bulb.
28 * @author Hilbrand Bouwkamp - Initial contribution
31 public class Connection implements Closeable {
34 * UDP port to send command.
36 public static final int FEICAN_SEND_PORT = 5000;
38 * UDP port devices send discover replies back.
40 public static final int FEICAN_RECEIVE_PORT = 6000;
42 private final InetAddress iNetAddress;
43 private final DatagramSocket socket;
46 * Initializes a connection to the given IP address.
48 * @param ipAddress IP address of the connection
49 * @throws UnknownHostException if ipAddress could not be resolved.
50 * @throws SocketException if no Datagram socket connection could be made.
52 public Connection(String ipAddress) throws SocketException, UnknownHostException {
53 iNetAddress = InetAddress.getByName(ipAddress);
54 socket = new DatagramSocket();
58 * Sends the 9 bytes command to the Feican device.
60 * @param command the 9 bytes command
61 * @throws IOException Connection to the bulb failed
63 public void sendCommand(byte[] command) throws IOException {
64 DatagramPacket sendPkt = new DatagramPacket(command, command.length, iNetAddress, FEICAN_SEND_PORT);