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]);
}
}
}
}
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;
}
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;
}
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) {
}
}
}
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);
}
}
Aggregations