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