Skip to content

Commit

Permalink
Merge pull request openhab#18 from stefan-kaestle/feature/code-format
Browse files Browse the repository at this point in the history
Ran mvn spotless:apply to apply correct code formatting
  • Loading branch information
coeing authored Jul 21, 2020
2 parents 3386900 + e8cce6b commit 408de49
Show file tree
Hide file tree
Showing 22 changed files with 584 additions and 589 deletions.
49 changes: 24 additions & 25 deletions bundles/org.openhab.binding.boschshc/pom.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.1-SNAPSHOT</version>
</parent>
<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.1-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.boschshc</artifactId>
<artifactId>org.openhab.binding.boschshc</artifactId>

<name>openHAB Add-ons :: Bundles :: BoschSHC Binding</name>
<name>openHAB Add-ons :: Bundles :: BoschSHC Binding</name>

<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.boschshc-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
<features name="org.openhab.binding.boschshc-${project.version}"
xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-boschshc" description="BoschSHC Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.boschshc/${project.version}</bundle>
</feature>
<feature name="openhab-binding-boschshc" description="BoschSHC Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.boschshc/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
Expand Up @@ -25,115 +25,116 @@
public class BoschHttpClient extends HttpClient {

private static final int MAX_PAIR_TRIES = 12;

private final Logger logger = LoggerFactory.getLogger(BoschHttpClient.class);

private String ipAddress;
private String systempassword;
private final Logger logger = LoggerFactory.getLogger(BoschHttpClient.class);

public BoschHttpClient(String ipAddress, String systempassword, SslContextFactory sslContextFactory) {
super(sslContextFactory);
this.ipAddress = ipAddress;
this.systempassword = systempassword;
}
private String ipAddress;
private String systempassword;

private String getCertFromSslContextFactory() throws KeyStoreException, CertificateEncodingException {
Certificate cert = this.getSslContextFactory().getKeyStore().getCertificate(BoschSslUtil.getBoschSHCId());
return new String(Base64.getEncoder().encode(cert.getEncoded()));
}
public BoschHttpClient(String ipAddress, String systempassword, SslContextFactory sslContextFactory) {
super(sslContextFactory);
this.ipAddress = ipAddress;
this.systempassword = systempassword;
}

public void checkAccessAndPairIfNecessary() throws InterruptedException, PairingFailedException {
private String getCertFromSslContextFactory() throws KeyStoreException, CertificateEncodingException {
Certificate cert = this.getSslContextFactory().getKeyStore().getCertificate(BoschSslUtil.getBoschSHCId());
return new String(Base64.getEncoder().encode(cert.getEncoded()));
}

public void checkAccessAndPairIfNecessary() throws InterruptedException, PairingFailedException {
// test if pairing is needed
int counter = 0;
boolean accessPossible = isAccessPossible();
while (!accessPossible) {
// sleep some seconds after every try, except the first one
if (counter > 0) {
logger.info("Last Pairing failed, starting retry number {}/{} in few seconds", counter, MAX_PAIR_TRIES);
Thread.sleep(15000);
} else {
logger.info("Pairing needed, because access to Bosch SmartHomeController not possible.");
}
// Timeout after max tries with an exception that pairing failed
if (counter >= MAX_PAIR_TRIES) {
// all tries during the last MAX_PAIR_TRIES*1500 milliseconds failed
throw new PairingFailedException("Pairing abourted, because it failed too many times!");
}
// try to pair if no access is possible
counter++;
doPairing();
// check access
accessPossible = isAccessPossible();
}

}

public Boolean isAccessPossible() {
try {
ContentResponse contentResponse = newRequest("https://" + ipAddress + ":8444/smarthome/devices").header("Content-Type", "application/json")
.header("Accept", "application/json").method(GET).send();
String content = contentResponse.getContentAsString();
logger.debug("Access check response complete: {} - return code: {}", content, contentResponse.getStatus());
return true;
} catch (InterruptedException | TimeoutException | ExecutionException e) {
logger.debug("Access check response failed!", e);
return false;
}
}


public boolean doPairing() {
logger.info("Starting pairing OpenHab Client with Bosch SmartHomeController!");
logger.info("Please press the Bosch SHC button until LED starts blinking");

ContentResponse contentResponse;
try {
String publicCert = getCertFromSslContextFactory();
logger.info("Pairing this Client '{}' with SHC {} using Cert {}", BoschSslUtil.getBoschSHCId(), ipAddress, publicCert);

// JSON Rest content
Map<String, String> items = new HashMap<>();
items.put("@type", "client");
items.put("id", BoschSslUtil.getBoschSHCId()); // Client Id contains the unique OpenHab instance Id
items.put("name", "oss_OpenHAB_Binding"); // Client name according to https://github.com/BoschSmartHome/bosch-shc-api-docs#terms-and-conditions
items.put("primaryRole", "ROLE_RESTRICTED_CLIENT");
items.put("certificate", "-----BEGIN CERTIFICATE-----\r" + publicCert + "\r-----END CERTIFICATE-----");

contentResponse = this.POST("https://" + ipAddress + ":8443/smarthome/clients")
.header("Systempassword",
new String(Base64.getEncoder().encode(systempassword.getBytes(StandardCharsets.UTF_8))))
.header("Content-Type", "application/json").header("Accept", "application/json")
.content(new StringContentProvider(new Gson().toJson(items))).send();

logger.debug("Pairing response complete: {} - return code: {}", contentResponse.getContentAsString(), contentResponse.getStatus());
if (201 == contentResponse.getStatus()) {
logger.info("Pairing successful.");
return true;
} else {
logger.info("Pairing failed with responce status {}.", contentResponse.getStatus());
return false;
}

} catch (InterruptedException | TimeoutException | CertificateEncodingException
| KeyStoreException e) {
logger.error("Pairing failed with an exception");
logger.debug("Pairing failed with: {}", e);
return false;
} catch (ExecutionException e) {
// javax.net.ssl.SSLHandshakeException: General SSLEngine problem
// => pairing failed, because hardware button was not pressed.
logger.warn("Pairing failed, was the Bosch SHC button pressed?");
logger.debug("Pairing failed with: {}", e);
return false;
}

}


// // TODO add request function like newBoschRequest("smarthome/devices")
//public ContentResponse newBoschRequest(String restData) throws InterruptedException, TimeoutException, ExecutionException {
// return GET("https://" + m_ipAddress + ":8444/" + restData)
// .header("Content-Type", "application/json").header("Accept", "application/json")
// .send();
//}
int counter = 0;
boolean accessPossible = isAccessPossible();
while (!accessPossible) {
// sleep some seconds after every try, except the first one
if (counter > 0) {
logger.info("Last Pairing failed, starting retry number {}/{} in few seconds", counter, MAX_PAIR_TRIES);
Thread.sleep(15000);
} else {
logger.info("Pairing needed, because access to Bosch SmartHomeController not possible.");
}
// Timeout after max tries with an exception that pairing failed
if (counter >= MAX_PAIR_TRIES) {
// all tries during the last MAX_PAIR_TRIES*1500 milliseconds failed
throw new PairingFailedException("Pairing abourted, because it failed too many times!");
}
// try to pair if no access is possible
counter++;
doPairing();
// check access
accessPossible = isAccessPossible();
}

}

public Boolean isAccessPossible() {
try {
ContentResponse contentResponse = newRequest("https://" + ipAddress + ":8444/smarthome/devices")
.header("Content-Type", "application/json").header("Accept", "application/json").method(GET).send();
String content = contentResponse.getContentAsString();
logger.debug("Access check response complete: {} - return code: {}", content, contentResponse.getStatus());
return true;
} catch (InterruptedException | TimeoutException | ExecutionException e) {
logger.debug("Access check response failed!", e);
return false;
}
}

public boolean doPairing() {
logger.info("Starting pairing OpenHab Client with Bosch SmartHomeController!");
logger.info("Please press the Bosch SHC button until LED starts blinking");

ContentResponse contentResponse;
try {
String publicCert = getCertFromSslContextFactory();
logger.info("Pairing this Client '{}' with SHC {} using Cert {}", BoschSslUtil.getBoschSHCId(), ipAddress,
publicCert);

// JSON Rest content
Map<String, String> items = new HashMap<>();
items.put("@type", "client");
items.put("id", BoschSslUtil.getBoschSHCId()); // Client Id contains the unique OpenHab instance Id
items.put("name", "oss_OpenHAB_Binding"); // Client name according to
// https://github.com/BoschSmartHome/bosch-shc-api-docs#terms-and-conditions
items.put("primaryRole", "ROLE_RESTRICTED_CLIENT");
items.put("certificate", "-----BEGIN CERTIFICATE-----\r" + publicCert + "\r-----END CERTIFICATE-----");

contentResponse = this.POST("https://" + ipAddress + ":8443/smarthome/clients")
.header("Systempassword",
new String(Base64.getEncoder().encode(systempassword.getBytes(StandardCharsets.UTF_8))))
.header("Content-Type", "application/json").header("Accept", "application/json")
.content(new StringContentProvider(new Gson().toJson(items))).send();

logger.debug("Pairing response complete: {} - return code: {}", contentResponse.getContentAsString(),
contentResponse.getStatus());
if (201 == contentResponse.getStatus()) {
logger.info("Pairing successful.");
return true;
} else {
logger.info("Pairing failed with responce status {}.", contentResponse.getStatus());
return false;
}

} catch (InterruptedException | TimeoutException | CertificateEncodingException | KeyStoreException e) {
logger.error("Pairing failed with an exception");
logger.debug("Pairing failed with: {}", e);
return false;
} catch (ExecutionException e) {
// javax.net.ssl.SSLHandshakeException: General SSLEngine problem
// => pairing failed, because hardware button was not pressed.
logger.warn("Pairing failed, was the Bosch SHC button pressed?");
logger.debug("Pairing failed with: {}", e);
return false;
}

}

// // TODO add request function like newBoschRequest("smarthome/devices")
// public ContentResponse newBoschRequest(String restData) throws InterruptedException, TimeoutException,
// ExecutionException {
// return GET("https://" + m_ipAddress + ":8444/" + restData)
// .header("Content-Type", "application/json").header("Accept", "application/json")
// .send();
// }
}
Loading

0 comments on commit 408de49

Please sign in to comment.