Search in sources :

Example 1 with LcnBindingNotification

use of org.openhab.binding.lcn.common.LcnBindingNotification in project openhab1-addons by openhab.

the class ConnectionManager method run.

/**
     * Main method of the thread.
     * Establishes new connections and waits for input data.
     */
@Override
public void run() {
    final Object sync = new Object();
    long lastCloseDetectTime = System.nanoTime();
    while (!this.threadTreminate) {
        long currTime = System.nanoTime();
        try {
            int timeoutMSec = DETECT_CLOSED_CHANNELS_INTERVAL_MSEC - (int) ((currTime - lastCloseDetectTime) / 1000000L);
            if (timeoutMSec <= 0 || this.selector.select(timeoutMSec) == 0) {
                // Detect closed channels
                synchronized (sync) {
                    this.callback.runOnRefreshThreadAsync(new LcnBindingNotification() {

                        @Override
                        public void execute() {
                            for (Connection conn : ConnectionManager.this.connectionsById.values()) {
                                if (!conn.isChannelConnected() && !conn.isChannelConnecting()) {
                                    logger.warn(String.format("Channel \"%s\" was closed unexpectedly (reconnecting...).", conn.getSets().getId()));
                                    conn.beginReconnect(RECONNECT_INTERVAL_MSEC);
                                }
                            }
                            synchronized (sync) {
                                sync.notify();
                            }
                        }
                    });
                    sync.wait();
                }
                lastCloseDetectTime = currTime;
            } else {
                // Process selected keys (connect or read events)
                Iterator<SelectionKey> iter = this.selector.selectedKeys().iterator();
                while (iter.hasNext()) {
                    final SelectionKey key = iter.next();
                    // The invocation is done sync. to keep the SelectionKey valid
                    synchronized (sync) {
                        this.callback.runOnRefreshThreadAsync(new LcnBindingNotification() {

                            @Override
                            public void execute() {
                                Connection conn = (Connection) key.attachment();
                                try {
                                    if (key.isConnectable()) {
                                        conn.finishConnect();
                                    } else if (key.isReadable()) {
                                        conn.readAndProcess();
                                    }
                                } catch (IOException ex) {
                                    logger.warn(String.format("Cannot process channel \"%s\" (reconnecting...): %s", conn.getSets().getId(), ex.getMessage()));
                                    conn.beginReconnect(RECONNECT_INTERVAL_MSEC);
                                }
                                synchronized (sync) {
                                    sync.notify();
                                }
                            }
                        });
                        sync.wait();
                    }
                    iter.remove();
                }
            }
            synchronized (this.channelRegisterSync) {
            }
        // Force a wait here if a new channel is registering
        } catch (InterruptedException ex) {
        } catch (IOException ex) {
            logger.error("Selection failure: " + ex.getMessage());
        }
    }
}
Also used : LcnBindingNotification(org.openhab.binding.lcn.common.LcnBindingNotification) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException)

Example 2 with LcnBindingNotification

use of org.openhab.binding.lcn.common.LcnBindingNotification in project openhab1-addons by openhab.

the class LcnBinding method internalReceiveCommand.

/** {@inheritDoc} */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    final String itemName2 = itemName;
    final Command command2 = command;
    this.runOnRefreshThreadAsync(new LcnBindingNotification() {

        @Override
        public void execute() {
            for (LcnGenericBindingProvider provider : LcnBinding.this.providers) {
                LcnBindingConfig itemConfig = provider.getLcnItemConfig(itemName2);
                if (itemConfig != null) {
                    itemConfig.send(LcnBinding.this.connections, command2);
                }
            }
        }
    });
}
Also used : LcnBindingNotification(org.openhab.binding.lcn.common.LcnBindingNotification) Command(org.openhab.core.types.Command)

Example 3 with LcnBindingNotification

use of org.openhab.binding.lcn.common.LcnBindingNotification in project openhab1-addons by openhab.

the class LcnBinding method execute.

/** Processes notifications. */
@Override
public void execute() {
    // Update connections
    this.connections.update();
    this.connections.flush();
    // Process notifications
    try {
        long startTime = System.nanoTime();
        int timeoutMSec = REFRESH_INTERVAL_MSEC;
        while (timeoutMSec >= 0 && this.notificationsSem.tryAcquire(timeoutMSec, TimeUnit.MILLISECONDS)) {
            LcnBindingNotification notification;
            synchronized (this.notifications) {
                notification = this.notifications.pollFirst();
            }
            notification.execute();
            this.connections.flush();
            // Next
            timeoutMSec = REFRESH_INTERVAL_MSEC - (int) ((System.nanoTime() - startTime) / 1000000L);
        }
    } catch (InterruptedException ex) {
    }
}
Also used : LcnBindingNotification(org.openhab.binding.lcn.common.LcnBindingNotification)

Aggregations

LcnBindingNotification (org.openhab.binding.lcn.common.LcnBindingNotification)3 IOException (java.io.IOException)1 SelectionKey (java.nio.channels.SelectionKey)1 Command (org.openhab.core.types.Command)1