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

Commit

Permalink
Denon: Enhancements/fixes for #3243 and #4777. (#4952)
Browse files Browse the repository at this point in the history
* Enhancements/fixes for #3243 and #4777.

* Reduce nesting depth of the updated() method.

* Lowered severity to debug instead of warn.

* Reformat.

Also fix Eclipse ActivationPolicy warning.

* Update to use Objects.toString() for compatibility with OH2.

* Remove the warning.
  • Loading branch information
9037568 authored Dec 31, 2016
1 parent 071c977 commit 3785265
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Import-Package: javax.xml.bind,
Export-Package: org.openhab.binding.denon
Bundle-DocURL: http://www.openhab.org
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/binding.xml, OSGI-INF/genericbindingprovider.xml
Bundle-ClassPath: .,
lib/async-http-client-1.8.4.jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import org.openhab.binding.denon.DenonBindingProvider;
import org.openhab.core.binding.AbstractActiveBinding;
Expand All @@ -33,7 +34,7 @@
* @author Jeroen Idserda
* @since 1.7.0
*/
public class DenonBinding extends AbstractActiveBinding<DenonBindingProvider>implements ManagedService {
public class DenonBinding extends AbstractActiveBinding<DenonBindingProvider> implements ManagedService {

private static final String CONFIG_REFRESH = "refresh";

Expand Down Expand Up @@ -137,73 +138,74 @@ protected void removeBindingProvider(DenonBindingProvider bindingProvider) {
*/
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
if (config != null) {
logger.debug("Denon binding updated");
logger.debug("Denon binding updated");

Enumeration<String> keys = config.keys();
if (config == null) {
return;
}

while (keys.hasMoreElements()) {
Enumeration<String> keys = config.keys();

String key = keys.nextElement();
if (CONFIG_SERVICE_PID.equals(key)) {
continue;
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (CONFIG_SERVICE_PID.equals(key)) {
continue;
}

String[] parts = key.split("\\.");
String value = ((String) config.get(key)).trim();
String[] parts = key.split("\\.");
String value = Objects.toString(config.get(key), null);

if (parts.length == 1) {
String option = parts[0];
if (CONFIG_REFRESH.equals(option)) {
refreshInterval = Integer.valueOf(value);
}
} else {
String instance = parts[0];

DenonConnectionProperties connection = connections.get(instance);
if (connection == null) {
connection = new DenonConnectionProperties();
connection.setInstance(instance);
connections.put(instance, connection);
}
if (parts.length == 1) {
String option = parts[0];
if (CONFIG_REFRESH.equals(option)) {
refreshInterval = Integer.valueOf(value);
}
} else {
String instance = parts[0];

DenonConnectionProperties connection = connections.get(instance);
if (connection == null) {
connection = new DenonConnectionProperties();
connection.setInstance(instance);
connections.put(instance, connection);
}

String option = parts[1].trim();
String option = parts[1].trim();

if (CONFIG_HOST.equals(option)) {
connection.setHost(value);
} else if (CONFIG_UPDATE_TYPE.equals(option)) {
connection.setTelnet(value.equals(CONFIG_UPDATE_TYPE_TELNET));
connection.setHttp(value.equals(CONFIG_UPDATE_TYPE_HTTP));
if (CONFIG_HOST.equals(option)) {
connection.setHost(value);
} else if (CONFIG_UPDATE_TYPE.equals(option)) {
connection.setTelnet(value.equals(CONFIG_UPDATE_TYPE_TELNET));
connection.setHttp(value.equals(CONFIG_UPDATE_TYPE_HTTP));

if (!value.equals(CONFIG_UPDATE_TYPE_TELNET) && !value.equals(CONFIG_UPDATE_TYPE_HTTP)) {
logger.warn("Invalid connection type {} for instance {}, using default", value, instance);
}
if (!value.equals(CONFIG_UPDATE_TYPE_TELNET) && !value.equals(CONFIG_UPDATE_TYPE_HTTP)) {
logger.warn("Invalid connection type {} for instance {}, using default", value, instance);
}
}
}
}

boolean isActiveBinding = false;

for (Entry<String, DenonConnectionProperties> entry : connections.entrySet()) {
DenonConnectionProperties connection = entry.getValue();
boolean isActiveBinding = false;

logger.debug("Denon receiver configured at {}", connection.getHost());
DenonConnector connector = new DenonConnector(connection, new DenonPropertyUpdatedCallback() {
@Override
public void updated(String instance, String property, State state) {
processPropertyUpdated(instance, property, state);
}
});
connection.setConnector(connector);
connector.connect();
for (Entry<String, DenonConnectionProperties> entry : connections.entrySet()) {
DenonConnectionProperties connection = entry.getValue();

if (connection.isHttp()) {
isActiveBinding = true;
logger.debug("Denon receiver configured at {}", connection.getHost());
DenonConnector connector = new DenonConnector(connection, new DenonPropertyUpdatedCallback() {
@Override
public void updated(String instance, String property, State state) {
processPropertyUpdated(instance, property, state);
}
}
});
connection.setConnector(connector);
connector.connect();

setProperlyConfigured(isActiveBinding);
if (connection.isHttp()) {
isActiveBinding = true;
}
}

setProperlyConfigured(isActiveBinding);
}

private void updateInitialState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
Expand Down Expand Up @@ -216,7 +217,7 @@ public void updateState() {
callback.updated(connection.getInstance(), state.getKey(), state.getValue());
}

logger.trace("Refresh took {}ms", new Date().getTime() - start.getTime());
logger.trace("Refresh took {} ms", new Date().getTime() - start.getTime());
}

/**
Expand Down Expand Up @@ -449,7 +450,12 @@ private void updateMainZone() {
stateCache.put(DenonProperty.POWER_MAINZONE.getCode(),
mainZone.getPower().getValue() ? OnOffType.ON : OnOffType.OFF);
stateCache.put(DenonProperty.MUTE.getCode(), mainZone.getMute().getValue() ? OnOffType.ON : OnOffType.OFF);
stateCache.put(DenonProperty.SURROUND_MODE.getCode(), new StringType(mainZone.getSurrMode().getValue()));
if (mainZone.getSurrMode() == null) {
logger.debug("Unable to get the SURROUND_MODE. MainZone update may not be correct.");
} else {
stateCache.put(DenonProperty.SURROUND_MODE.getCode(),
new StringType(mainZone.getSurrMode().getValue()));
}
}
}

Expand Down Expand Up @@ -556,6 +562,7 @@ private AsyncHttpClientConfig createAsyncHttpClientConfig() {
private <T> T getDocument(String uri, Class<T> response) {
try {
String result = doHttpRequest("GET", uri, null);
logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);

if (StringUtils.isNotBlank(result)) {
JAXBContext jc = JAXBContext.newInstance(response);
Expand All @@ -568,10 +575,12 @@ private <T> T getDocument(String uri, Class<T> response) {

return obj;
}
} catch (UnmarshalException e) {
logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
} catch (JAXBException e) {
logger.debug("Encoding error in get", e);
logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
} catch (XMLStreamException e) {
logger.debug("Communication error in get", e);
logger.debug("Communication error: {}", e.getMessage());
}

return null;
Expand Down

0 comments on commit 3785265

Please sign in to comment.