Search in sources :

Example 1 with LFXClient

use of com.github.besherman.lifx.LFXClient in project ha-bridge by bwssytems.

the class LifxHome method createHome.

@Override
public Home createHome(BridgeSettings bridgeSettings) {
    lifxMap = null;
    aGsonHandler = null;
    validLifx = bridgeSettings.getBridgeSettingsDescriptor().isValidLifx();
    log.info("LifxDevice Home created." + (validLifx ? "" : " No LifxDevices configured."));
    if (validLifx) {
        try {
            log.info("Open Lifx client....");
            InetAddress configuredAddress = InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress());
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(configuredAddress);
            InetAddress bcastInetAddr = null;
            if (networkInterface != null) {
                for (InterfaceAddress ifaceAddr : networkInterface.getInterfaceAddresses()) {
                    InetAddress addr = ifaceAddr.getAddress();
                    if (addr instanceof Inet4Address) {
                        bcastInetAddr = ifaceAddr.getBroadcast();
                        break;
                    }
                }
            }
            if (bcastInetAddr != null) {
                lifxMap = new HashMap<String, LifxDevice>();
                log.info("Opening LFX Client with broadcast address: " + bcastInetAddr.getHostAddress());
                client = new LFXClient(bcastInetAddr.getHostAddress());
                client.getLights().addLightCollectionListener(new MyLightListener(lifxMap));
                client.getGroups().addGroupCollectionListener(new MyGroupListener(lifxMap));
                client.open(false);
                aGsonHandler = new GsonBuilder().create();
            } else {
                log.warn("Could not open LIFX, no bcast addr available, check your upnp config address.");
                client = null;
                validLifx = false;
                return this;
            }
        } catch (IOException e) {
            log.warn("Could not open LIFX, with IO Exception", e);
            client = null;
            validLifx = false;
            return this;
        } catch (InterruptedException e) {
            log.warn("Could not open LIFX, with Interruprted Exception", e);
            client = null;
            validLifx = false;
            return this;
        }
    }
    return this;
}
Also used : Inet4Address(java.net.Inet4Address) GsonBuilder(com.google.gson.GsonBuilder) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) LFXClient(com.github.besherman.lifx.LFXClient) InetAddress(java.net.InetAddress)

Aggregations

LFXClient (com.github.besherman.lifx.LFXClient)1 GsonBuilder (com.google.gson.GsonBuilder)1 IOException (java.io.IOException)1 Inet4Address (java.net.Inet4Address)1 InetAddress (java.net.InetAddress)1 InterfaceAddress (java.net.InterfaceAddress)1 NetworkInterface (java.net.NetworkInterface)1