Search in sources :

Example 1 with ConnectionStatusEvent

use of org.openhab.binding.satel.internal.event.ConnectionStatusEvent in project openhab1-addons by openhab.

the class SatelModule method disconnect.

private synchronized void disconnect() {
    // notifying about send failure
    while (!this.sendQueue.isEmpty()) {
        SatelCommand cmd = this.sendQueue.poll();
        cmd.setState(State.FAILED);
    }
    synchronized (this.channelLock) {
        if (this.channel != null) {
            this.channel.disconnect();
            this.channel = null;
            // notify about connection status change
            this.dispatchEvent(new ConnectionStatusEvent(false));
        }
    }
}
Also used : ConnectionStatusEvent(org.openhab.binding.satel.internal.event.ConnectionStatusEvent) SatelCommand(org.openhab.binding.satel.command.SatelCommand)

Example 2 with ConnectionStatusEvent

use of org.openhab.binding.satel.internal.event.ConnectionStatusEvent in project openhab1-addons by openhab.

the class SatelBinding method incomingEvent.

/**
     * {@inheritDoc}
     */
@Override
public void incomingEvent(SatelEvent event) {
    logger.trace("Handling incoming event: {}", event);
    // refresh all states that have changed
    if (event instanceof NewStatesEvent) {
        List<SatelCommand> commands = getRefreshCommands((NewStatesEvent) event);
        for (SatelCommand command : commands) {
            this.satelModule.sendCommand(command);
        }
    }
    // if just connected, force refreshing
    if (event instanceof ConnectionStatusEvent) {
        ConnectionStatusEvent statusEvent = (ConnectionStatusEvent) event;
        if (statusEvent.isConnected()) {
            switchForceRefresh(true);
        }
    }
    // update items
    for (SatelBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            SatelBindingConfig itemConfig = provider.getItemConfig(itemName);
            Item item = provider.getItem(itemName);
            State newState = itemConfig.convertEventToState(item, event);
            if (newState != null) {
                logger.debug("Updating item state: item = {}, state = {}, event = {}", itemName, newState, event);
                eventPublisher.postUpdate(itemName, newState);
                itemConfig.setItemInitialized();
            }
        }
    }
}
Also used : Item(org.openhab.core.items.Item) SatelBindingProvider(org.openhab.binding.satel.SatelBindingProvider) ConnectionStatusEvent(org.openhab.binding.satel.internal.event.ConnectionStatusEvent) State(org.openhab.core.types.State) NewStatesEvent(org.openhab.binding.satel.internal.event.NewStatesEvent) SatelCommand(org.openhab.binding.satel.command.SatelCommand) SatelBindingConfig(org.openhab.binding.satel.SatelBindingConfig)

Example 3 with ConnectionStatusEvent

use of org.openhab.binding.satel.internal.event.ConnectionStatusEvent in project openhab1-addons by openhab.

the class SatelModule method communicationLoop.

private void communicationLoop(TimeoutTimer timeoutTimer) {
    long reconnectionTime = 10 * 1000;
    boolean receivedResponse = false;
    SatelCommand command = null;
    try {
        while (!Thread.currentThread().isInterrupted()) {
            // connect, if not connected yet
            if (this.channel == null) {
                long connectStartTime = System.currentTimeMillis();
                synchronized (this) {
                    this.channel = connect();
                }
                if (this.channel == null) {
                    // notify about connection failure
                    this.dispatchEvent(new ConnectionStatusEvent(false));
                    // try to reconnect after a while, if connection hasn't
                    // been established
                    Thread.sleep(reconnectionTime - System.currentTimeMillis() + connectStartTime);
                    continue;
                }
            }
            // get next command and send it
            command = this.sendQueue.take();
            logger.debug("Sending message: {}", command.getRequest());
            timeoutTimer.start();
            boolean sent = this.writeMessage(command.getRequest());
            timeoutTimer.stop();
            if (!sent) {
                break;
            }
            command.setState(State.SENT);
            // command sent, wait for response
            logger.trace("Waiting for response");
            timeoutTimer.start();
            SatelMessage response = this.readMessage();
            timeoutTimer.stop();
            if (response == null) {
                break;
            }
            logger.debug("Got response: {}", response);
            if (!receivedResponse) {
                receivedResponse = true;
                // notify about connection success after first
                // response from the module
                this.dispatchEvent(new ConnectionStatusEvent(true));
            }
            if (command.handleResponse(this, response)) {
                command.setState(State.SUCCEEDED);
            } else {
                command.setState(State.FAILED);
            }
            command = null;
        }
    } catch (InterruptedException e) {
    // exit thread
    } catch (Exception e) {
        // unexpected error, log and exit thread
        logger.info("Unhandled exception occurred in communication loop, disconnecting.", e);
    } finally {
        // stop counting if thread interrupted
        timeoutTimer.stop();
    }
    // either send or receive failed
    if (command != null) {
        command.setState(State.FAILED);
    }
    disconnect();
}
Also used : ConnectionStatusEvent(org.openhab.binding.satel.internal.event.ConnectionStatusEvent) SatelCommand(org.openhab.binding.satel.command.SatelCommand) IOException(java.io.IOException)

Example 4 with ConnectionStatusEvent

use of org.openhab.binding.satel.internal.event.ConnectionStatusEvent in project openhab1-addons by openhab.

the class ConnectionStatusBindingConfig method convertEventToState.

/**
     * {@inheritDoc}
     */
@Override
public State convertEventToState(Item item, SatelEvent event) {
    if (!(event instanceof ConnectionStatusEvent)) {
        // if current items state is uninitialized, "generate" fake event to update its state
        if (event instanceof NewStatesEvent && this.connectedSince == null) {
            event = new ConnectionStatusEvent(true);
        } else {
            return null;
        }
    }
    ConnectionStatusEvent statusEvent = (ConnectionStatusEvent) event;
    boolean invertState = hasOptionEnabled(Options.INVERT_STATE);
    // update internal values
    if (statusEvent.isConnected()) {
        this.connectionFailures = 0;
        if (this.connectedSince == null) {
            this.connectedSince = Calendar.getInstance();
        }
    } else {
        this.connectionFailures += 1;
        this.connectedSince = null;
    }
    switch(this.statusType) {
        case CONNECTED:
            return booleanToState(item, statusEvent.isConnected() ^ invertState);
        case CONNECTED_SINCE:
            if (this.connectedSince == null) {
                return UnDefType.NULL;
            } else if (item.getAcceptedDataTypes().contains(DateTimeType.class)) {
                return new DateTimeType(this.connectedSince);
            }
            break;
        case CONNECTION_ERRORS:
            if (item.getAcceptedDataTypes().contains(DecimalType.class)) {
                return new DecimalType(this.connectionFailures);
            }
            break;
    }
    return null;
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) ConnectionStatusEvent(org.openhab.binding.satel.internal.event.ConnectionStatusEvent) DecimalType(org.openhab.core.library.types.DecimalType) NewStatesEvent(org.openhab.binding.satel.internal.event.NewStatesEvent)

Aggregations

ConnectionStatusEvent (org.openhab.binding.satel.internal.event.ConnectionStatusEvent)4 SatelCommand (org.openhab.binding.satel.command.SatelCommand)3 NewStatesEvent (org.openhab.binding.satel.internal.event.NewStatesEvent)2 IOException (java.io.IOException)1 SatelBindingConfig (org.openhab.binding.satel.SatelBindingConfig)1 SatelBindingProvider (org.openhab.binding.satel.SatelBindingProvider)1 Item (org.openhab.core.items.Item)1 DateTimeType (org.openhab.core.library.types.DateTimeType)1 DecimalType (org.openhab.core.library.types.DecimalType)1 State (org.openhab.core.types.State)1