Search in sources :

Example 26 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class PlugwiseGenericBindingProvider method getIntervalList.

@Override
public List<PlugwiseBindingConfigElement> getIntervalList() {
    List<PlugwiseBindingConfigElement> result = new ArrayList<PlugwiseBindingConfigElement>();
    for (String itemName : getItemNames()) {
        PlugwiseBindingConfig pbConfig = (PlugwiseBindingConfig) bindingConfigs.get(itemName);
        for (Command command : pbConfig.keySet()) {
            boolean found = false;
            PlugwiseBindingConfigElement element = pbConfig.get(command);
            // check if we already have a reference to this {ID,command}
            Iterator<PlugwiseBindingConfigElement> elementIterator = result.iterator();
            while (elementIterator.hasNext()) {
                PlugwiseBindingConfigElement resultElement = elementIterator.next();
                boolean sameJobName = resultElement.getId().equals(element.getId());
                Class<? extends Job> resultJobClass = resultElement.getCommandType().getJobClass();
                Class<? extends Job> elementJobClass = element.getCommandType().getJobClass();
                boolean sameJobClass = (resultJobClass == null && elementJobClass == null) || (resultJobClass != null && resultJobClass.equals(elementJobClass));
                if (sameJobName && sameJobClass) {
                    // bingo - now check if the interval is smaller
                    found = true;
                    if (resultElement.getInterval() > element.getInterval()) {
                        result.remove(resultElement);
                        result.add(element);
                        break;
                    }
                }
            }
            if (!found) {
                result.add(element);
            }
        }
    }
    return result;
}
Also used : Command(org.openhab.core.types.Command) ArrayList(java.util.ArrayList)

Example 27 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class EventUtils method parseContent.

private static List<EventContent> parseContent(CalDavEvent event, ItemRegistry itemRegistry, Item itemIn, String expectedScope, String defaultItemOnBegin) {
    final List<EventContent> outMap = new ArrayList<EventUtils.EventContent>();
    // no content, nothing to parse
    if (StringUtils.isEmpty(event.getContent())) {
        return outMap;
    }
    try {
        final BufferedReader reader = new BufferedReader(new StringReader(event.getContent()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            Item item = itemIn;
            final EventLine eventLine = parseEventLine(line.trim(), event, defaultItemOnBegin);
            if (eventLine == null) {
                continue;
            }
            if (expectedScope != null && !expectedScope.equals(eventLine.scope)) {
                continue;
            }
            if (item == null) {
                if (itemRegistry == null) {
                    log.error("item is null, but itemRegistry as well");
                    continue;
                }
                try {
                    item = itemRegistry.getItem(eventLine.itemName);
                } catch (ItemNotFoundException e) {
                    log.error("cannot find item: {}", eventLine.itemName);
                    continue;
                }
            }
            if (!item.getName().equals(eventLine.itemName)) {
                log.trace("name of item {} does not match itemName {}", item.getName(), eventLine.itemName);
                continue;
            }
            final State state = TypeParser.parseState(item.getAcceptedDataTypes(), eventLine.stateString);
            final Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), eventLine.stateString);
            log.trace("add item {} to action list (scope={}, state={}, time={})", item, eventLine.scope, state, eventLine.time);
            outMap.add(new EventContent(eventLine.scope, item, state, command, eventLine.time));
        }
    } catch (IOException e) {
        log.error("cannot parse event content", e);
    }
    return outMap;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Item(org.openhab.core.items.Item) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 28 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class ExecuteCommandJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    String content = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_CONTENT_KEY);
    ItemRegistry registry = GCalActivator.itemRegistryTracker.getService();
    EventPublisher publisher = GCalActivator.eventPublisherTracker.getService();
    if (registry == null) {
        logger.warn("Sorry, no item registry service available!");
        return;
    }
    if (publisher == null) {
        logger.warn("Sorry, no event publisher service available!");
        return;
    }
    if (content.startsWith("[PresenceSimulation]")) {
        try {
            Item item = registry.getItem("PresenceSimulation");
            if (item.getState() != OnOffType.ON) {
                logger.debug("Presence Simulation job detected, but PresenceSimulation is not in ON state. Job is not executed");
                return;
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Presence Simulation job detected, but PresenceSimulation item does not exists. Check configuration");
            return;
        }
    }
    if (StringUtils.isNotBlank(content)) {
        String[] commands = parseCommands(content);
        for (String command : commands) {
            String[] args = parseCommand(command);
            try {
                if (args[0].equals("send")) {
                    if (args.length > 2) {
                        Item item = registry.getItem(args[1]);
                        Command cmd = TypeParser.parseCommand(item.getAcceptedCommandTypes(), args[2]);
                        if (cmd != null) {
                            publisher.sendCommand(item.getName(), cmd);
                            logger.debug("Command {} has been sent", Arrays.asList(args));
                        } else {
                            logger.warn("Command '{}' is not valid. Command not sent.", Arrays.asList(args));
                        }
                    }
                } else if (args[0].equals("update")) {
                    if (args.length > 2) {
                        Item item = registry.getItem(args[1]);
                        State state = TypeParser.parseState(item.getAcceptedDataTypes(), args[2]);
                        publisher.postUpdate(item.getName(), state);
                        logger.debug("Published update {}", Arrays.asList(args));
                    } else {
                        logger.warn("Command '{}' is not valid. Update not sent.", Arrays.asList(args));
                    }
                } else {
                    logger.warn("Command {} not supported", args[0]);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Executing command failed. Item {} not found", args[1]);
            }
        }
    }
}
Also used : Item(org.openhab.core.items.Item) EventPublisher(org.openhab.core.events.EventPublisher) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) ItemRegistry(org.openhab.core.items.ItemRegistry) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 29 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class CalDavBinding method updateItemState.

private void updateItemState(CalDavNextEventConfig config, List<CalDavEvent> events) {
    String itemName = config.getItemNameToListenTo();
    String itemNamePreview = config.getItemName();
    logger.trace("update item state for item: {}", itemName);
    Command state = null;
    DateTime time = null;
    if (calDavLoader == null) {
        logger.warn("caldav loader is not set");
        return;
    }
    for (CalDavEvent calDavEvent : events) {
        try {
            final Item item = this.itemRegistry.getItem(itemName);
            final List<EventUtils.EventContent> parseContent = EventUtils.parseContent(calDavEvent, item);
            for (EventUtils.EventContent eventContent : parseContent) {
                if (!eventContent.getTime().isBefore(DateTime.now()) && (time == null || time.isAfter(eventContent.getTime()))) {
                    time = eventContent.getTime();
                    state = eventContent.getCommand();
                }
            }
        } catch (ItemNotFoundException e) {
            logger.error("item {} could not be found", itemName);
        }
    }
    if (time == null && config.getType() != CalDavType.DISABLE) {
        // no item found
        eventPublisher.postUpdate(itemNamePreview, org.openhab.core.types.UnDefType.UNDEF);
        return;
    }
    CalDavType type = config.getType();
    logger.trace("handling event of type: {}", type);
    if (type == CalDavType.VALUE) {
        logger.debug("setting value for '{}' to: {}", itemNamePreview, state);
        eventPublisher.sendCommand(itemNamePreview, state);
    } else if (type == CalDavType.DATE) {
        Command c = new DateTimeType(FORMATTER.print(time));
        logger.debug("setting value for '{}' to: {}", itemNamePreview, c);
        eventPublisher.sendCommand(itemNamePreview, c);
    } else if (type == CalDavType.DISABLE) {
        // nothing to do
        return;
    } else {
        logger.warn("unhandled type: {}", type);
    }
}
Also used : Item(org.openhab.core.items.Item) DateTimeType(org.openhab.core.library.types.DateTimeType) Command(org.openhab.core.types.Command) CalDavEvent(org.openhab.io.caldav.CalDavEvent) EventUtils(org.openhab.io.caldav.EventUtils) DateTime(org.joda.time.DateTime) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 30 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class ModuleChannelGroupTest method canSendGroup1Update.

@Test
public void canSendGroup1Update() throws Exception {
    ModuleChannel item = group1.addChannel("test4", 4, new ArrayList<Class<? extends Command>>());
    item.setState(OnOffType.ON);
    group1.publishStateToNikobus(item, binding);
    Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
    NikobusCommand cmd = command.getAllValues().get(0);
    assertEquals("$1E156C94000000FF0000FF60E149", cmd.getCommand());
}
Also used : Command(org.openhab.core.types.Command) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) Test(org.junit.Test)

Aggregations

Command (org.openhab.core.types.Command)61 ArrayList (java.util.ArrayList)20 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)14 State (org.openhab.core.types.State)12 Matcher (java.util.regex.Matcher)10 DecimalType (org.openhab.core.library.types.DecimalType)8 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 NikobusCommand (org.openhab.binding.nikobus.internal.core.NikobusCommand)6 SchedulerException (org.quartz.SchedulerException)6 StringType (org.openhab.core.library.types.StringType)5 JobDataMap (org.quartz.JobDataMap)5 JobDetail (org.quartz.JobDetail)5 Scheduler (org.quartz.Scheduler)5 Trigger (org.quartz.Trigger)5 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 IOException (java.io.IOException)4 SocketChannel (java.nio.channels.SocketChannel)4 IllegalClassException (org.apache.commons.lang.IllegalClassException)4 PercentType (org.openhab.core.library.types.PercentType)4