Search in sources :

Example 1 with NeeoRoomProtocol

use of org.openhab.binding.neeo.internal.NeeoRoomProtocol in project openhab-addons by openhab.

the class NeeoRoomHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    Objects.requireNonNull(channelUID, "channelUID cannot be null");
    Objects.requireNonNull(command, "command cannot be null");
    final NeeoRoomProtocol protocol = roomProtocol.get();
    if (protocol == null) {
        logger.debug("Protocol is null - ignoring update: {}", channelUID);
        return;
    }
    final String[] channelIds = UidUtils.parseChannelId(channelUID);
    if (channelIds.length == 0) {
        logger.debug("Bad group declaration: {}", channelUID);
        return;
    }
    final String localGroupId = channelUID.getGroupId();
    final String groupId = localGroupId == null || localGroupId.isEmpty() ? "" : localGroupId;
    final String channelId = channelIds[0];
    final String channelKey = channelIds.length > 1 ? channelIds[1] : "";
    if (command instanceof RefreshType) {
        refreshChannel(protocol, groupId, channelKey, channelId);
    } else {
        switch(groupId) {
            case NeeoConstants.ROOM_GROUP_RECIPE_ID:
                switch(channelId) {
                    case NeeoConstants.ROOM_CHANNEL_STATUS:
                        // Ignore OFF status updates
                        if (command == OnOffType.ON) {
                            protocol.startRecipe(channelKey);
                        }
                        break;
                }
                break;
            case NeeoConstants.ROOM_GROUP_SCENARIO_ID:
                switch(channelId) {
                    case NeeoConstants.ROOM_CHANNEL_STATUS:
                        if (command instanceof OnOffType) {
                            protocol.setScenarioStatus(channelKey, command == OnOffType.ON);
                        }
                        break;
                }
                break;
            default:
                logger.debug("Unknown channel to set: {}", channelUID);
                break;
        }
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) NeeoRoomProtocol(org.openhab.binding.neeo.internal.NeeoRoomProtocol) RefreshType(org.openhab.core.types.RefreshType)

Example 2 with NeeoRoomProtocol

use of org.openhab.binding.neeo.internal.NeeoRoomProtocol in project openhab-addons by openhab.

the class NeeoRoomHandler method processAction.

/**
 * Processes the action if it applies to this room
 *
 * @param action a non-null action to process
 */
void processAction(NeeoAction action) {
    Objects.requireNonNull(action, "action cannot be null");
    final NeeoRoomProtocol protocol = roomProtocol.get();
    if (protocol != null) {
        protocol.processAction(action);
    }
}
Also used : NeeoRoomProtocol(org.openhab.binding.neeo.internal.NeeoRoomProtocol)

Example 3 with NeeoRoomProtocol

use of org.openhab.binding.neeo.internal.NeeoRoomProtocol in project openhab-addons by openhab.

the class NeeoRoomHandler method initializeTask.

/**
 * Initializes the task be creating the {@link NeeoRoomProtocol}, going online and then scheduling the refresh task.
 */
private void initializeTask() {
    final NeeoRoomConfig config = getConfigAs(NeeoRoomConfig.class);
    final String roomKey = config.getRoomKey();
    if (roomKey == null || roomKey.isEmpty()) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Room key (from the parent room bridge) was not found");
        return;
    }
    try {
        NeeoUtil.checkInterrupt();
        final NeeoBrainApi brainApi = getNeeoBrainApi();
        if (brainApi == null) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "Cannot find the NEEO Brain API");
            return;
        }
        final NeeoRoom room = brainApi.getRoom(roomKey);
        final ThingUID thingUid = getThing().getUID();
        final Map<String, String> properties = new HashMap<>();
        properties.put("Key", roomKey);
        final ThingBuilder thingBuilder = editThing();
        thingBuilder.withLabel(room.getName() + " (NEEO " + brainApi.getBrain().getKey() + ")").withProperties(properties).withChannels(ChannelUtils.generateChannels(thingUid, room));
        updateThing(thingBuilder.build());
        NeeoUtil.checkInterrupt();
        final NeeoRoomProtocol protocol = new NeeoRoomProtocol(new NeeoHandlerCallback() {

            @Override
            public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
                updateStatus(status, detail, msg);
            }

            @Override
            public void stateChanged(String channelId, State state) {
                updateState(channelId, state);
            }

            @Override
            public void setProperty(String propertyName, String propertyValue) {
                getThing().setProperty(propertyName, propertyValue);
            }

            @Override
            public void scheduleTask(Runnable task, long milliSeconds) {
                scheduler.schedule(task, milliSeconds, TimeUnit.MILLISECONDS);
            }

            @Override
            public void triggerEvent(String channelID, String event) {
                triggerChannel(channelID, event);
            }

            @Nullable
            @Override
            public NeeoBrainApi getApi() {
                return getNeeoBrainApi();
            }
        }, roomKey);
        roomProtocol.getAndSet(protocol);
        NeeoUtil.checkInterrupt();
        updateStatus(ThingStatus.ONLINE);
        if (config.getRefreshPolling() > 0) {
            NeeoUtil.checkInterrupt();
            NeeoUtil.cancel(refreshTask.getAndSet(scheduler.scheduleWithFixedDelay(() -> {
                try {
                    refreshState();
                } catch (InterruptedException e) {
                    logger.debug("Refresh State was interrupted", e);
                }
            }, 0, config.getRefreshPolling(), TimeUnit.SECONDS)));
        }
    } catch (IOException e) {
        logger.debug("IOException during initialization", e);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Room " + config.getRoomKey() + " couldn't be found");
    } catch (InterruptedException e) {
        logger.debug("Initialization was interrupted", e);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "Initialization was interrupted");
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) HashMap(java.util.HashMap) ThingStatus(org.openhab.core.thing.ThingStatus) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) IOException(java.io.IOException) NeeoBrainApi(org.openhab.binding.neeo.internal.NeeoBrainApi) NeeoRoom(org.openhab.binding.neeo.internal.models.NeeoRoom) State(org.openhab.core.types.State) ThingUID(org.openhab.core.thing.ThingUID) NeeoRoomProtocol(org.openhab.binding.neeo.internal.NeeoRoomProtocol) NeeoRoomConfig(org.openhab.binding.neeo.internal.NeeoRoomConfig) Nullable(org.eclipse.jdt.annotation.Nullable) NeeoHandlerCallback(org.openhab.binding.neeo.internal.NeeoHandlerCallback)

Example 4 with NeeoRoomProtocol

use of org.openhab.binding.neeo.internal.NeeoRoomProtocol in project openhab-addons by openhab.

the class NeeoRoomHandler method refreshState.

/**
 * Refreshes the state of the room by calling {@link NeeoRoomProtocol#refreshState()}
 *
 * @throws InterruptedException if the call is interrupted
 */
private void refreshState() throws InterruptedException {
    NeeoUtil.checkInterrupt();
    final NeeoRoomProtocol protocol = roomProtocol.get();
    if (protocol != null) {
        NeeoUtil.checkInterrupt();
        protocol.refreshState();
    }
}
Also used : NeeoRoomProtocol(org.openhab.binding.neeo.internal.NeeoRoomProtocol)

Aggregations

NeeoRoomProtocol (org.openhab.binding.neeo.internal.NeeoRoomProtocol)4 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 NeeoBrainApi (org.openhab.binding.neeo.internal.NeeoBrainApi)1 NeeoHandlerCallback (org.openhab.binding.neeo.internal.NeeoHandlerCallback)1 NeeoRoomConfig (org.openhab.binding.neeo.internal.NeeoRoomConfig)1 NeeoRoom (org.openhab.binding.neeo.internal.models.NeeoRoom)1 OnOffType (org.openhab.core.library.types.OnOffType)1 ThingStatus (org.openhab.core.thing.ThingStatus)1 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)1 ThingUID (org.openhab.core.thing.ThingUID)1 ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)1 RefreshType (org.openhab.core.types.RefreshType)1 State (org.openhab.core.types.State)1