@Override public void handleXPLMessage(xPL_MessageI theMessage) { for (XplBindingProvider provider : providers) { List<String> matchingItems = provider.hasMessage(theMessage); for (String itemName : matchingItems) { XplBindingConfig config = provider.getConfig(itemName); if (config == null) { continue; } String current = theMessage.getNamedValue(config.NamedParameter); Item item = provider.getItem(itemName); if (item != null) { if (item instanceof SwitchItem) { OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF; synchronized (item) { if (!item.getState().equals(status)) { eventPublisher.postUpdate(itemName, status); ((SwitchItem) item).setState(status); } } } else if (item instanceof NumberItem) { DecimalType value = new DecimalType(current); synchronized (item) { if (!item.getState().equals(value)) { eventPublisher.postUpdate(itemName, value); ((NumberItem) item).setState(value); } } } if (item instanceof StringItem) { StringType value = new StringType(current); synchronized (item) { if (!item.getState().equals(value)) { eventPublisher.postUpdate(itemName, value); ((StringItem) item).setState(value); } } } } } } }
/** * Stores the current states for a list of items in a map. A group item is not itself put into the * map, but instead all its members. * * @param items the items for which the state should be stored * @return the map of items with their states */ public static Map<Item, State> storeStates(Item... items) { Map<Item, State> statesMap = Maps.newHashMap(); if (items != null) { for (Item item : items) { if (item instanceof GroupItem) { GroupItem groupItem = (GroupItem) item; for (Item member : groupItem.getAllMembers()) { statesMap.put(member, member.getState()); } } else { statesMap.put(item, item.getState()); } } } return statesMap; }
/** * Creates a new Google Calendar Entry for each <code>item</code> and adds it to the processing * queue. The entries' title will either be the items name or <code>alias</code> if it is <code> * != null</code>. * * <p>The new Calendar Entry will contain a single command to be executed e.g.<br> * * <p><code>send <item.name> <item.state></code> * * @param item the item which state should be persisted. * @param alias the alias under which the item should be persisted. */ @Override public void store(final Item item, final String alias) { if (initialized) { String newAlias = alias != null ? alias : item.getName(); CalendarEventEntry myEntry = new CalendarEventEntry(); myEntry.setTitle(new PlainTextConstruct("[PresenceSimulation] " + newAlias)); myEntry.setContent( new PlainTextConstruct( String.format(executeScript, item.getName(), item.getState().toString()))); DateTime nowPlusOffset = new DateTime().plusDays(offset); com.google.gdata.data.DateTime time = com.google.gdata.data.DateTime.parseDateTime(nowPlusOffset.toString()); When eventTimes = new When(); eventTimes.setStartTime(time); eventTimes.setEndTime(time); myEntry.addTime(eventTimes); entries.offer(myEntry); logger.trace( "added new entry '{}' for item '{}' to upload queue", myEntry.getTitle().getPlainText(), item.getName()); } else { logger.debug( "GCal PresenceSimulation Service isn't initialized properly! No entries will be uploaded to your Google Calendar"); } }
/** {@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); } }
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; }
private StateTransformable getState(Item item) { StateTransformable state = null; if (item.getState() instanceof HSBType) { HSBType hsb = (HSBType) item.getState(); state = new HSBData(hsb.getHue().longValue(), hsb.getHue().longValue(), hsb.getHue().longValue()); } else if (item.getState() instanceof DateTimeType) { DateTimeType dt = (DateTimeType) item.getState(); DateTimeDataType data = new DateTimeDataType(dt.toString()); state = new DateTimeData(data); } else if (item.getState() instanceof DecimalType) { } else if (item.getState() instanceof OnOffType) { } else if (item.getState() instanceof OpenClosedType) { } else if (item.getState() instanceof PercentType) { } else if (item.getState() instanceof UpDownType) { } return state; }