Search in sources :

Example 16 with Item

use of org.openhab.core.items.Item 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 17 with Item

use of org.openhab.core.items.Item 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 18 with Item

use of org.openhab.core.items.Item in project openhab1-addons by openhab.

the class PlanetPublisher method publish.

/**
     * Iterates through all items and publishes the states.
     */
public void publish(final PlanetName planetName) {
    final Planet planet = context.getPlanet(planetName);
    new ItemIterator().iterate(new ItemIteratorCallback() {

        @Override
        public void next(AstroBindingConfig bindingConfig, Item item) {
            if (planetName == bindingConfig.getPlanetName()) {
                try {
                    Object value = PropertyUtils.getPropertyValue(planet, bindingConfig.getPlanetProperty());
                    if (!equalsCachedValue(value, item)) {
                        publishValue(item, value, bindingConfig);
                        itemCache.put(item.getName(), value);
                    }
                } catch (Exception ex) {
                    logger.warn(ex.getMessage(), ex);
                }
            }
        }
    });
}
Also used : Item(org.openhab.core.items.Item) ItemIteratorCallback(org.openhab.binding.astro.internal.util.ItemIterator.ItemIteratorCallback) Planet(org.openhab.binding.astro.internal.model.Planet) AstroBindingConfig(org.openhab.binding.astro.internal.config.AstroBindingConfig) ItemIterator(org.openhab.binding.astro.internal.util.ItemIterator)

Example 19 with Item

use of org.openhab.core.items.Item in project openhab1-addons by openhab.

the class ItemIterator method iterate.

/**
     * Iterates through all items and calls the callback.
     */
public void iterate(ItemIteratorCallback callback) {
    for (AstroBindingProvider provider : context.getProviders()) {
        for (String itemName : provider.getItemNames()) {
            AstroBindingConfig bindingConfig = provider.getBindingFor(itemName);
            Item item = provider.getItem(itemName);
            callback.next(bindingConfig, item);
        }
    }
}
Also used : Item(org.openhab.core.items.Item) AstroBindingProvider(org.openhab.binding.astro.AstroBindingProvider) AstroBindingConfig(org.openhab.binding.astro.internal.config.AstroBindingConfig)

Example 20 with Item

use of org.openhab.core.items.Item in project openhab1-addons by openhab.

the class AutelisBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.trace("internalReceiveCommand({},{}) is called!", itemName, command);
    for (AutelisBindingProvider provider : providers) {
        Item item = provider.getItem(itemName);
        String config = provider.getAutelisBindingConfigString(itemName);
        Matcher m = commandPattern.matcher(config);
        if (m.find() && m.groupCount() > 1) {
            String type = m.group(1);
            String name = m.group(2);
            if (type.equals(AUTELIS_TYPES_EQUIP)) {
                String cmd = AUTELIS_CMD_VALUE;
                int value;
                if (command == OnOffType.OFF) {
                    value = 0;
                } else if (command == OnOffType.ON) {
                    value = 1;
                } else if (command instanceof DecimalType) {
                    value = ((DecimalType) item.getStateAs(DecimalType.class)).intValue();
                    if (value >= 3) {
                        // this is a dim type. not sure what 2 does
                        cmd = AUTELIS_CMD_DIM;
                    }
                } else {
                    logger.error("Equipment commands must be of Decimal type not {}", command);
                    break;
                }
                String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value, TIMEOUT);
                logger.trace("equipment set {} {} {} : result {}", name, cmd, value, response);
            } else if (type.equals(AUTELIS_TYPES_TEMP)) {
                String value;
                if (command == IncreaseDecreaseType.INCREASE) {
                    value = AUTELIS_CMD_UP;
                } else if (command == IncreaseDecreaseType.DECREASE) {
                    value = AUTELIS_CMD_DOWN;
                } else {
                    value = command.toString();
                }
                String cmd;
                // name ending in sp are setpoints, ht are heat types?
                if (name.endsWith(AUTELIS_SETPOINT)) {
                    cmd = AUTELIS_TYPES_TEMP;
                } else if (name.endsWith(AUTELIS_HEATTYPE)) {
                    cmd = AUTELIS_CMD_HEAT;
                } else {
                    logger.error("Unknown temp type {}", name);
                    break;
                }
                String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?wait=1&name=" + name + "&" + cmd + "=" + value, TIMEOUT);
                logger.trace("temp set {} {} : result {}", cmd, value, response);
            }
        } else if (config.equals(AUTELIS_TYPES_LIGHTS)) {
            /*
                 * lighting command
                 * possible values, but we will let anything through.
                 * alloff, allon, csync, cset, cswim, party, romance, caribbean, american,
                 * sunset, royalty, blue, green, red, white, magenta, hold, recall
                 */
            String response = HttpUtil.executeUrl("GET", baseURL + "lights.cgi?val=" + command.toString(), TIMEOUT);
            logger.trace("lights set {} : result {}", command.toString(), response);
        } else {
            logger.error("Unsupported set config {}", config);
        }
    }
    scheduleClearTime(UPDATE_CLEARTIME);
}
Also used : AutelisBindingProvider(org.openhab.binding.autelis.AutelisBindingProvider) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) Matcher(java.util.regex.Matcher) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

Item (org.openhab.core.items.Item)59 SwitchItem (org.openhab.core.library.items.SwitchItem)23 NumberItem (org.openhab.core.library.items.NumberItem)20 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)16 State (org.openhab.core.types.State)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14 DecimalType (org.openhab.core.library.types.DecimalType)9 HistoricItem (org.openhab.core.persistence.HistoricItem)9 ArrayList (java.util.ArrayList)7 ColorItem (org.openhab.core.library.items.ColorItem)7 StringItem (org.openhab.core.library.items.StringItem)7 HomematicBindingConfig (org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6 Test (org.junit.Test)5 GenericItem (org.openhab.core.items.GenericItem)5 GroupItem (org.openhab.core.items.GroupItem)5 ItemRegistry (org.openhab.core.items.ItemRegistry)5 Calendar (java.util.Calendar)3