Search in sources :

Example 1 with StateLabelResponse

use of org.eclipse.smarthome.binding.lifx.internal.protocol.StateLabelResponse in project smarthome by eclipse.

the class LifxLightDiscovery method handlePacket.

private void handlePacket(Packet packet, InetSocketAddress address) {
    logger.trace("Discovery : Packet type '{}' received from '{}' for '{}' with sequence '{}' and source '{}'", new Object[] { packet.getClass().getSimpleName(), address.toString(), packet.getTarget().getHex(), packet.getSequence(), Long.toString(packet.getSource(), 16) });
    if (packet.getSource() == sourceId || packet.getSource() == 0) {
        MACAddress macAddress = packet.getTarget();
        DiscoveredLight light = discoveredLights.get(macAddress);
        if (packet instanceof StateServiceResponse) {
            int port = (int) ((StateServiceResponse) packet).getPort();
            if (port != 0) {
                try {
                    InetSocketAddress socketAddress = new InetSocketAddress(address.getAddress(), port);
                    if (light == null || (!socketAddress.equals(light.socketAddress))) {
                        if (light != null) {
                            light.cancelUnicastKey();
                        }
                        Selector lightSelector = selector;
                        if (lightSelector != null) {
                            String logId = getLogId(macAddress, socketAddress);
                            light = new DiscoveredLight(lightSelector, macAddress, socketAddress, logId, openUnicastChannel(lightSelector, logId, socketAddress));
                            discoveredLights.put(macAddress, light);
                        }
                    }
                } catch (Exception e) {
                    logger.warn("{} while connecting to IP address: {}", e.getClass().getSimpleName(), e.getMessage());
                    return;
                }
            }
        } else if (light != null) {
            if (packet instanceof StateLabelResponse) {
                light.label = ((StateLabelResponse) packet).getLabel().trim();
            } else if (packet instanceof StateVersionResponse) {
                try {
                    light.product = Products.getProductFromProductID(((StateVersionResponse) packet).getProduct());
                    light.productVersion = ((StateVersionResponse) packet).getVersion();
                } catch (IllegalArgumentException e) {
                    logger.debug("Discovered an unsupported light ({}): {}", light.macAddress.getAsLabel(), e.getMessage());
                    light.supportedProduct = false;
                }
            }
        }
        if (light != null && light.isDataComplete()) {
            try {
                thingDiscovered(createDiscoveryResult(light));
            } catch (IllegalArgumentException e) {
                logger.trace("{} while creating discovery result of light ({})", e.getClass().getSimpleName(), light.logId, e);
            }
        }
    }
}
Also used : MACAddress(org.eclipse.smarthome.binding.lifx.internal.fields.MACAddress) InetSocketAddress(java.net.InetSocketAddress) StateLabelResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateLabelResponse) StateServiceResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateServiceResponse) StateVersionResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateVersionResponse) Selector(java.nio.channels.Selector)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 Selector (java.nio.channels.Selector)1 MACAddress (org.eclipse.smarthome.binding.lifx.internal.fields.MACAddress)1 StateLabelResponse (org.eclipse.smarthome.binding.lifx.internal.protocol.StateLabelResponse)1 StateServiceResponse (org.eclipse.smarthome.binding.lifx.internal.protocol.StateServiceResponse)1 StateVersionResponse (org.eclipse.smarthome.binding.lifx.internal.protocol.StateVersionResponse)1