use of org.openhab.core.items.ItemNotFoundException in project openhab1-addons by openhab.
the class DiyOnXBeeBinding method internalReceiveCommand.
/**
*
* @return if the command was sent successfully
*/
private boolean internalReceiveCommand(DiyOnXBeeBindingProvider provider, String itemName, Command command) {
final String remote = provider.getRemote(itemName);
final int[] remoteAddress = FormatUtil.fromReadableAddress(remote);
Item item;
try {
item = itemRegistry.getItem(itemName);
} catch (ItemNotFoundException e1) {
logger.error("unable to get item {}", itemName, e1);
return false;
}
final String commandValue = createCommand(item, command);
if (commandValue == null) {
logger.warn("unable to create command {} for item {}", commandValue, itemName);
return false;
} else {
logger.debug("created command {} for item {}", commandValue, itemName);
}
final String commandString = new StringBuilder().append(provider.getId(itemName)).append('=').append(commandValue).append('\n').toString();
final ZNetTxRequest request = new ZNetTxRequest(new XBeeAddress64(remoteAddress), createPayload(commandString));
xbeeUsageLock.lock();
try {
if (xbee == null) {
logger.error("cannot send command to {} as the XBee module isn't initialized", itemName);
return false;
} else {
// TODO:
final XBeeResponse response = xbee.sendSynchronous(request);
// response?
return true;
}
} catch (XBeeTimeoutException e) {
logger.error("failed sending {} to {}", command, itemName, e);
} catch (XBeeException e) {
logger.error("failed sending {} to {}", command, itemName, e);
} finally {
xbeeUsageLock.unlock();
}
return false;
}
use of org.openhab.core.items.ItemNotFoundException 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;
}
use of org.openhab.core.items.ItemNotFoundException 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.ItemNotFoundException 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.ItemNotFoundException in project openhab1-addons by openhab.
the class SappBinding method executeSappCommand.
/**
* executes the real command on pnmas device
*/
private void executeSappCommand(String itemName, Command command) {
SappBindingProvider provider = findFirstMatchingBindingProvider(itemName);
if (provider == null) {
logger.error("cannot find a provider, skipping command");
}
try {
Item item = itemRegistry.getItem(itemName);
logger.debug("found item {}", item);
if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigSwitchItem);
if (sappBindingConfigSwitchItem.isPollerSuspender()) {
if (pollingEnabled) {
// turning off polling
pollingEnabled = false;
// force updates of polling switches because polling is
updatePollingSwitchesState(provider);
// off
} else {
// turning on polling
provider.getSappUpdatePendingRequests().replaceAllPendingUpdateRequests(ALL_UPDATE_REQUEST_KEY);
pollingEnabled = true;
}
} else {
SappAddressOnOffControl controlAddress = sappBindingConfigSwitchItem.getControl();
if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigSwitchItem);
return;
}
try {
if (command instanceof OnOffType) {
switch(controlAddress.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), command.equals(OnOffType.ON) ? controlAddress.getOnValue() : controlAddress.getOffValue(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
}
} else if (item instanceof NumberItem) {
SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigNumberItem);
SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigNumberItem);
return;
}
try {
if (command instanceof DecimalType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((DecimalType) command).toBigDecimal()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else if (item instanceof RollershutterItem) {
SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigRollershutterItem);
SappAddressRollershutterControl controlAddress = null;
if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.UP) {
controlAddress = sappBindingConfigRollershutterItem.getUpControl();
} else if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.DOWN) {
controlAddress = sappBindingConfigRollershutterItem.getDownControl();
} else if (command instanceof StopMoveType && ((StopMoveType) command) == StopMoveType.STOP) {
controlAddress = sappBindingConfigRollershutterItem.getStopControl();
}
if (controlAddress != null) {
if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigRollershutterItem);
return;
}
try {
switch(controlAddress.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), controlAddress.getActivateValue(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
break;
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} else if (item instanceof DimmerItem) {
SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigDimmerItem);
SappAddressDimmer address = sappBindingConfigDimmerItem.getStatus();
if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigDimmerItem);
return;
}
try {
if (command instanceof OnOffType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((OnOffType) command) == OnOffType.ON ? address.getOriginalMaxScale() : address.getOriginalMinScale(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else if (command instanceof IncreaseDecreaseType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE ? Math.min(previousValue + address.getIncrement(), address.getOriginalMaxScale()) : Math.max(previousValue - address.getIncrement(), address.getOriginalMinScale()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else if (command instanceof PercentType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((PercentType) command).toBigDecimal()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else {
logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
}
} catch (ItemNotFoundException e) {
logger.error("Item {} not found", itemName);
}
}
Aggregations