Search in sources :

Example 1 with ItemRegistry

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

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

the class ItemStateRequestProcessor method getItemState.

public ItemStateData getItemState(String itemId) throws ServiceException {
    ItemRegistry itemRegistry = getItemRegistry();
    ItemStateData itemState = null;
    try {
        Item item = itemRegistry.getItem(itemId);
        StateTransformable state = getState(item);
        itemState = new ItemStateData(System.currentTimeMillis(), itemId, state);
    } catch (ItemNotFoundException ex) {
        logger.info(itemId + " not found", ex);
    }
    return itemState;
}
Also used : Item(org.openhab.core.items.Item) StateTransformable(org.creek.mailcontrol.model.data.StateTransformable) ItemStateData(org.creek.mailcontrol.model.data.ItemStateData) ItemRegistry(org.openhab.core.items.ItemRegistry) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 3 with ItemRegistry

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

the class ItemStateRequestProcessor method getItemStates.

public List<ItemStateData> getItemStates() throws ServiceException {
    List<ItemStateData> itemStates = new ArrayList<ItemStateData>();
    ItemRegistry itemRegistry = getItemRegistry();
    for (Item item : itemRegistry.getItems()) {
        logger.debug("Item: " + item.getName() + " " + item.getState());
        StateTransformable state = getState(item);
        ItemStateData itemState = new ItemStateData(System.currentTimeMillis(), item.getName(), state);
        itemStates.add(itemState);
    }
    return itemStates;
}
Also used : Item(org.openhab.core.items.Item) StateTransformable(org.creek.mailcontrol.model.data.StateTransformable) ArrayList(java.util.ArrayList) ItemStateData(org.creek.mailcontrol.model.data.ItemStateData) ItemRegistry(org.openhab.core.items.ItemRegistry)

Example 4 with ItemRegistry

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

the class BaseIntegrationTest method initService.

@BeforeClass
public static void initService() throws InterruptedException {
    items.put("dimmer", new DimmerItem("dimmer"));
    items.put("number", new NumberItem("number"));
    items.put("string", new StringItem("string"));
    items.put("switch", new SwitchItem("switch"));
    items.put("contact", new ContactItem("contact"));
    items.put("color", new ColorItem("color"));
    items.put("rollershutter", new RollershutterItem("rollershutter"));
    items.put("datetime", new DateTimeItem("datetime"));
    items.put("call", new CallItem("call"));
    items.put("location", new LocationItem("location"));
    service = new DynamoDBPersistenceService();
    service.setItemRegistry(new ItemRegistry() {

        @Override
        public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }

        @Override
        public boolean isValidItemName(String itemName) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems(String pattern) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems() {
            throw new NotImplementedException();
        }

        @Override
        public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
            throw new NotImplementedException();
        }

        @Override
        public Item getItem(String name) throws ItemNotFoundException {
            Item item = items.get(name);
            if (item == null) {
                throw new ItemNotFoundException(name);
            }
            return item;
        }

        @Override
        public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }
    });
    HashMap<String, Object> config = new HashMap<>();
    config.put("region", System.getProperty("DYNAMODBTEST_REGION"));
    config.put("accessKey", System.getProperty("DYNAMODBTEST_ACCESS"));
    config.put("secretKey", System.getProperty("DYNAMODBTEST_SECRET"));
    config.put("tablePrefix", "dynamodb-integration-tests-");
    for (Entry<String, Object> entry : config.entrySet()) {
        if (entry.getValue() == null) {
            logger.warn(String.format("Expecting %s to have value for integration tests. Integration tests will be skipped", entry.getKey()));
            service = null;
            return;
        }
    }
    service.activate(null, config);
    // Clear data
    for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string" }) {
        try {
            service.getDb().getDynamoClient().deleteTable(table);
            service.getDb().getDynamoDB().getTable(table).waitForDelete();
        } catch (ResourceNotFoundException e) {
        }
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang.NotImplementedException) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) ItemRegistry(org.openhab.core.items.ItemRegistry) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) CallItem(org.openhab.library.tel.items.CallItem) Item(org.openhab.core.items.Item) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) LocationItem(org.openhab.core.library.items.LocationItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ItemRegistryChangeListener(org.openhab.core.items.ItemRegistryChangeListener) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) Collection(java.util.Collection) CallItem(org.openhab.library.tel.items.CallItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) BeforeClass(org.junit.BeforeClass)

Example 5 with ItemRegistry

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

the class MiosBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    try {
        logger.debug("internalReceiveCommand: itemName '{}', command '{}'", itemName, command);
        // Lookup the MiOS Unit name and property for this item
        String unitName = getMiosUnitName(itemName);
        MiosUnitConnector connector = getMiosConnector(unitName);
        if (connector == null) {
            logger.warn("Received command ({}) for item '{}' but no connector found for MiOS Unit '{}', ignoring", new Object[] { command.toString(), itemName, unitName });
            return;
        }
        if (!connector.isConnected()) {
            logger.warn("Received command ({}) for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring", new Object[] { command.toString(), itemName, unitName });
            return;
        }
        for (BindingProvider provider : providers) {
            if (provider instanceof MiosBindingProvider) {
                MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
                MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);
                if (config != null) {
                    ItemRegistry reg = miosProvider.getItemRegistry();
                    if (reg != null) {
                        Item item = reg.getItem(config.getItemName());
                        State state = item.getState();
                        connector.invokeCommand(config, command, state);
                    } else {
                        logger.warn("internalReceiveCommand: Missing ItemRegistry for item '{}' command '{}'", itemName, command);
                    }
                } else {
                    logger.trace("internalReceiveCommand: Missing BindingConfig for item '{}' command '{}'", itemName, command);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error handling command", e);
    }
}
Also used : Item(org.openhab.core.items.Item) MiosBindingConfig(org.openhab.binding.mios.internal.config.MiosBindingConfig) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) State(org.openhab.core.types.State) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) ItemRegistry(org.openhab.core.items.ItemRegistry) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Aggregations

ItemRegistry (org.openhab.core.items.ItemRegistry)6 Item (org.openhab.core.items.Item)5 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)3 State (org.openhab.core.types.State)3 ItemStateData (org.creek.mailcontrol.model.data.ItemStateData)2 StateTransformable (org.creek.mailcontrol.model.data.StateTransformable)2 MiosBindingProvider (org.openhab.binding.mios.MiosBindingProvider)2 MiosBindingConfig (org.openhab.binding.mios.internal.config.MiosBindingConfig)2 BindingProvider (org.openhab.core.binding.BindingProvider)2 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 BeforeClass (org.junit.BeforeClass)1 EventPublisher (org.openhab.core.events.EventPublisher)1 ItemNotUniqueException (org.openhab.core.items.ItemNotUniqueException)1 ItemRegistryChangeListener (org.openhab.core.items.ItemRegistryChangeListener)1 ColorItem (org.openhab.core.library.items.ColorItem)1 ContactItem (org.openhab.core.library.items.ContactItem)1