Search in sources :

Example 1 with StateVersionResponse

use of org.eclipse.smarthome.binding.lifx.internal.protocol.StateVersionResponse 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)

Example 2 with StateVersionResponse

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

the class LifxLightPropertiesUpdater method handleResponsePacket.

public void handleResponsePacket(Packet packet) {
    if (!updating) {
        return;
    }
    if (packet instanceof StateVersionResponse) {
        Products products = Products.getProductFromProductID(((StateVersionResponse) packet).getProduct());
        long productVersion = ((StateVersionResponse) packet).getVersion();
        properties.put(LifxBindingConstants.PROPERTY_PRODUCT_ID, Long.toString(products.getProduct()));
        properties.put(LifxBindingConstants.PROPERTY_PRODUCT_NAME, products.getName());
        properties.put(LifxBindingConstants.PROPERTY_PRODUCT_VERSION, Long.toString(productVersion));
        properties.put(LifxBindingConstants.PROPERTY_VENDOR_ID, Long.toString(products.getVendor()));
        properties.put(LifxBindingConstants.PROPERTY_VENDOR_NAME, products.getVendorName());
        receivedPacketTypes.add(packet.getPacketType());
    } else if (packet instanceof StateHostFirmwareResponse) {
        String hostVersion = ((StateHostFirmwareResponse) packet).getVersion().toString();
        properties.put(LifxBindingConstants.PROPERTY_HOST_VERSION, hostVersion);
        receivedPacketTypes.add(packet.getPacketType());
    } else if (packet instanceof StateWifiFirmwareResponse) {
        String wifiVersion = ((StateWifiFirmwareResponse) packet).getVersion().toString();
        properties.put(LifxBindingConstants.PROPERTY_WIFI_VERSION, wifiVersion);
        receivedPacketTypes.add(packet.getPacketType());
    }
    if (receivedAllResponsePackets()) {
        updating = false;
        propertiesUpdateListeners.forEach(listener -> listener.handlePropertiesUpdate(properties));
        logger.debug("{} : Finished updating light properties", logId);
    }
}
Also used : Products(org.eclipse.smarthome.binding.lifx.internal.protocol.Products) StateHostFirmwareResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateHostFirmwareResponse) StateWifiFirmwareResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateWifiFirmwareResponse) StateVersionResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.StateVersionResponse)

Aggregations

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