Search in sources :

Example 1 with GetServiceRequest

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

the class LifxLightCommunicationHandler method start.

public void start() {
    try {
        lock.lock();
        logger.debug("{} : Starting communication handler", logId);
        logger.debug("{} : Using '{}' as source identifier", logId, Long.toString(sourceId, 16));
        ScheduledFuture<?> localNetworkJob = networkJob;
        if (localNetworkJob == null || localNetworkJob.isCancelled()) {
            networkJob = scheduler.scheduleWithFixedDelay(this::receiveAndHandlePackets, 0, PACKET_INTERVAL, TimeUnit.MILLISECONDS);
        }
        currentLightState.setOffline();
        Selector localSelector = Selector.open();
        selector = localSelector;
        if (isBroadcastEnabled()) {
            broadcastKey = openBroadcastChannel(selector, logId, broadcastPort);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host, macAddress, broadcastKey, unicastKey);
            broadcastPacket(new GetServiceRequest());
        } else {
            unicastKey = openUnicastChannel(selector, logId, host);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host, macAddress, broadcastKey, unicastKey);
            sendPacket(new GetServiceRequest());
        }
    } catch (IOException e) {
        logger.error("{} while starting LIFX communication handler for light '{}' : {}", e.getClass().getSimpleName(), logId, e.getMessage(), e);
    } finally {
        lock.unlock();
    }
}
Also used : GetServiceRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetServiceRequest) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 2 with GetServiceRequest

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

the class LifxLightDiscovery method doScan.

protected void doScan() {
    try {
        if (!isScanning) {
            isScanning = true;
            if (selector != null) {
                closeSelector(selector, LOG_ID);
            }
            logger.debug("The LIFX discovery service will use '{}' as source identifier", Long.toString(sourceId, 16));
            Selector localSelector = Selector.open();
            selector = localSelector;
            broadcastKey = openBroadcastChannel(localSelector, LOG_ID, BROADCAST_PORT);
            networkJob = scheduler.schedule(this::receiveAndHandlePackets, 0, TimeUnit.MILLISECONDS);
            LifxSelectorContext selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, LOG_ID, broadcastKey);
            broadcastPacket(selectorContext, new GetServiceRequest());
        } else {
            logger.info("A discovery scan for LIFX lights is already underway");
        }
    } catch (Exception e) {
        logger.debug("{} while discovering LIFX lights : {}", e.getClass().getSimpleName(), e.getMessage());
    }
}
Also used : GetServiceRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetServiceRequest) Selector(java.nio.channels.Selector)

Example 3 with GetServiceRequest

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

the class LifxLightOnlineStateUpdater method sendEchoPackets.

public void sendEchoPackets() {
    try {
        lock.lock();
        logger.trace("{} : Polling light state", logId);
        if (currentLightState.isOnline()) {
            if (Duration.between(lastSeen, LocalDateTime.now()).getSeconds() > ECHO_POLLING_INTERVAL) {
                if (unansweredEchoPackets < MAXIMUM_POLLING_RETRIES) {
                    communicationHandler.sendPacket(GetEchoRequest.currentTimeEchoRequest());
                    unansweredEchoPackets++;
                } else {
                    currentLightState.setOfflineByCommunicationError();
                    unansweredEchoPackets = 0;
                }
            }
        } else {
            if (communicationHandler.isBroadcastEnabled()) {
                logger.trace("{} : Light is not online, broadcasting request", logId);
                communicationHandler.broadcastPacket(new GetServiceRequest());
            } else {
                logger.trace("{} : Light is not online, unicasting request", logId);
                communicationHandler.sendPacket(new GetServiceRequest());
            }
        }
    } catch (Exception e) {
        logger.error("Error occurred while polling the online state of a light ({})", logId, e);
    } finally {
        lock.unlock();
    }
}
Also used : GetServiceRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetServiceRequest)

Aggregations

GetServiceRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.GetServiceRequest)3 Selector (java.nio.channels.Selector)2 IOException (java.io.IOException)1