Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Add functionality to show and adjust XBMC volume through an item of t…
Browse files Browse the repository at this point in the history
…ype Dimmer. Including corrections after review comments.
  • Loading branch information
avdleeuw committed Aug 26, 2014
1 parent a06690a commit a89d8c6
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface XbmcBindingProvider extends BindingProvider {
String getXbmcInstance(String itemname);
String getProperty(String itemname);
boolean isInBound(String itemname);
boolean isOutBound(String itemname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ private void registerWatch(XbmcBindingProvider xbmcProvider, String itemName) {
// update the player status so any current value is initialised
if (connector.isConnected())
connector.updatePlayerStatus();

if (property.startsWith("Application")) {
connector.requestApplicationUpdate();
}
}
}

Expand Down Expand Up @@ -170,6 +174,18 @@ private boolean isInBound(String itemName) {
return false;
}

private boolean isOutBound(String itemName) {
for (BindingProvider provider : providers) {
if (provider instanceof XbmcBindingProvider) {
XbmcBindingProvider xbmcProvider = (XbmcBindingProvider) provider;
if (xbmcProvider.getItemNames().contains(itemName)) {
return xbmcProvider.isOutBound(itemName);
}
}
}
return false;
}

private XbmcConnector getXbmcConnector(String xbmcInstance) {
// sanity check
if (xbmcInstance == null)
Expand Down Expand Up @@ -249,7 +265,7 @@ protected void execute() {
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// only interested in 'outbound' items
if (isInBound(itemName)) {
if (!isOutBound(itemName)) {
logger.warn("Received command ({}) for item {} which is configured as 'in-bound', ignoring", command.toString(), itemName);
return;
}
Expand All @@ -272,14 +288,16 @@ protected void internalReceiveCommand(String itemName, Command command) {
// TODO: handle other commands
if (property.equals("Player.PlayPause"))
connector.playerPlayPause();
if (property.equals("Player.Open"))
else if (property.equals("Player.Open"))
connector.playerOpen(command.toString());
if (property.equals("Player.Stop"))
else if (property.equals("Player.Stop"))
connector.playerStop();
if (property.equals("GUI.ShowNotification"))
else if (property.equals("GUI.ShowNotification"))
connector.showNotification("openHAB", command.toString());
if (property.equals("System.Shutdown") && command == OnOffType.OFF)
else if (property.equals("System.Shutdown") && command == OnOffType.OFF)
connector.systemShutdown();
else if (property.equals("Application.Volume"))
connector.applicationSetVolume(command.toString());
} catch (Exception e) {
logger.error("Error handling command", e);
}
Expand All @@ -292,23 +310,28 @@ protected void internalReceiveCommand(String itemName, Command command) {
protected void internalReceiveUpdate(String itemName, State newState) {
try {
String property = getProperty(itemName);

// TODO: handle other updates
if (property.equals("GUI.ShowNotification")) {
String xbmcInstance = getXbmcInstance(itemName);
XbmcConnector connector = getXbmcConnector(xbmcInstance);

if (connector == null) {
logger.warn("Received update ({}) for item {} but no XBMC connector found for {}, ignoring", newState.toString(), itemName, xbmcInstance);
return;
}
if (!connector.isConnected()) {
logger.warn("Received update ({}) for item {} but the connection to the XBMC instance {} is down, ignoring", newState.toString(), itemName, xbmcInstance);
return;
}
String xbmcInstance = getXbmcInstance(itemName);
XbmcConnector connector = getXbmcConnector(xbmcInstance);

connector.showNotification("openHAB", newState.toString());
if (connector == null) {
logger.warn("Received update ({}) for item {} but no XBMC connector found for {}, ignoring", newState.toString(), itemName, xbmcInstance);
return;
}
if (!connector.isConnected()) {
logger.warn("Received update ({}) for item {} but the connection to the XBMC instance {} is down, ignoring", newState.toString(), itemName, xbmcInstance);
return;
}

// TODO: handle other updates
if (property.equals("GUI.ShowNotification")) {
connector.showNotification("openHAB", newState.toString());
} else if (property.equals("Player.Open")) {
connector.playerOpen(newState.toString());
} else if (property.equals("Application.SetVolume")) {
connector.applicationSetVolume(newState.toString());
}

} catch (Exception e) {
logger.error("Error handling update", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openhab.binding.xbmc.XbmcBindingProvider;
import org.openhab.core.binding.BindingConfig;
import org.openhab.core.items.Item;
import org.openhab.core.library.items.DimmerItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.model.item.binding.AbstractGenericBindingProvider;
Expand All @@ -35,10 +36,11 @@ public String getBindingType() {

@Override
public void validateItemType(Item item, String bindingConfig) throws BindingConfigParseException {
if (!(item instanceof StringItem) && !(item instanceof SwitchItem)){
if (!(item instanceof StringItem) && !(item instanceof SwitchItem) &&
!(item instanceof DimmerItem)){
throw new BindingConfigParseException( "item '"+item.getName()
+ "' is of type '" + item.getClass().getSimpleName()
+ "', but only String or Switch items are allowed.");
+ "', but only String, Switch or Dimmer items are allowed.");
}
}

Expand All @@ -55,11 +57,26 @@ public void processBindingConfiguration(String context, Item item, String bindin
} else if (bindingConfig.startsWith(">")) {
XbmcBindingConfig config = parseOutgoingBindingConfig(item, bindingConfig);
addBindingConfig(item, config);
} else if (bindingConfig.startsWith("=")) {
XbmcBindingConfig config = parseBidirectionalBindingConfig(item, bindingConfig);
addBindingConfig(item, config);
} else {
throw new BindingConfigParseException("Item '"+item.getName()+"' does not start with < or >.");
throw new BindingConfigParseException("Item '"+item.getName()+"' does not start with <, > or =.");
}
}

private XbmcBindingConfig parseBidirectionalBindingConfig( Item item, String bindingConfig) throws BindingConfigParseException{
Matcher matcher = CONFIG_PATTERN.matcher(bindingConfig);

if( !matcher.matches())
throw new BindingConfigParseException("Config for item '"+item.getName()+"' could not be parsed.");

String xbmcInstance = matcher.group(1);
String property = matcher.group(2);

return new XbmcBindingConfig(xbmcInstance, property, true, true);
}

private XbmcBindingConfig parseIncomingBindingConfig( Item item, String bindingConfig) throws BindingConfigParseException{
Matcher matcher = CONFIG_PATTERN.matcher(bindingConfig);

Expand All @@ -69,7 +86,7 @@ private XbmcBindingConfig parseIncomingBindingConfig( Item item, String bindingC
String xbmcInstance = matcher.group(1);
String property = matcher.group(2);

return new XbmcBindingConfig(xbmcInstance, property, true);
return new XbmcBindingConfig(xbmcInstance, property, true, false);
}

private XbmcBindingConfig parseOutgoingBindingConfig( Item item, String bindingConfig) throws BindingConfigParseException{
Expand All @@ -81,7 +98,7 @@ private XbmcBindingConfig parseOutgoingBindingConfig( Item item, String bindingC
String xbmcInstance = matcher.group(1);
String property = matcher.group(2);

return new XbmcBindingConfig(xbmcInstance, property, false);
return new XbmcBindingConfig(xbmcInstance, property, false, true);
}

@Override
Expand All @@ -102,18 +119,32 @@ public boolean isInBound(String itemname) {
return bindingConfig != null ? bindingConfig.isInBound(): false;
}

@Override
public boolean isOutBound(String itemname) {
XbmcBindingConfig bindingConfig = (XbmcBindingConfig) bindingConfigs.get(itemname);
return bindingConfig != null ? bindingConfig.isOutBound(): false;
}

class XbmcBindingConfig implements BindingConfig {

private String xbmcInstance;
private String property;
private boolean inBound;
private boolean inBound = false;
private boolean outBound = false;

public XbmcBindingConfig(String xbmcInstance, String property, boolean inBound) {
this.xbmcInstance = xbmcInstance;
this.property = property;
this.inBound = inBound;
}


public XbmcBindingConfig(String xbmcInstance, String property, boolean inBound, boolean outBound) {
this.xbmcInstance = xbmcInstance;
this.property = property;
this.inBound = inBound;
this.outBound = outBound;
}

public String getXbmcInstance() {
return xbmcInstance;
}
Expand All @@ -125,5 +156,9 @@ public String getProperty() {
public boolean isInBound() {
return inBound;
}

public boolean isOutBound() {
return outBound;
}
}
}
Loading

0 comments on commit a89d8c6

Please sign in to comment.