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());
}
}
}
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);
}
}
}
});
}
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) {
}
}
Aggregations