forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shelly] Support for Duo, EM3, DW, Smoke, Addon; new CoAP-based updat…
…es; bug fixes (openhab#6985) * Re-checkin based on latest PR review status Signed-off-by: Markus Michels <markus7017@gmail.com>
- Loading branch information
1 parent
090a438
commit 66a11d6
Showing
37 changed files
with
4,553 additions
and
2,596 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
168 changes: 70 additions & 98 deletions
168
...ding.shelly/src/main/java/org/openhab/binding/shelly/internal/ShellyBindingConstants.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...ding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.shelly.internal.api; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.UnknownHostException; | ||
import java.text.MessageFormat; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.JsonSyntaxException; | ||
|
||
/** | ||
* The {@link CarNetException} implements an extension to the standard Exception class. This allows to keep also the | ||
* result of the last API call (e.g. including the http status code in the message). | ||
* | ||
* @author Markus Michels - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ShellyApiException extends Exception { | ||
private static final long serialVersionUID = -5809459454769761821L; | ||
|
||
private ShellyApiResult apiResult = new ShellyApiResult(); | ||
private static String EX_NONE = "none"; | ||
|
||
public ShellyApiException(Exception exception) { | ||
super(exception); | ||
} | ||
|
||
public ShellyApiException(String message) { | ||
super(message); | ||
} | ||
|
||
public ShellyApiException(ShellyApiResult res) { | ||
super(EX_NONE); | ||
apiResult = res; | ||
} | ||
|
||
public ShellyApiException(Exception exception, String message) { | ||
super(message, exception); | ||
} | ||
|
||
public ShellyApiException(ShellyApiResult result, Exception exception) { | ||
super(exception); | ||
apiResult = result; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return isEmpty() ? "" : nonNullString(super.getMessage()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String message = nonNullString(super.getMessage()); | ||
String cause = getCauseClass().toString(); | ||
if (!isEmpty()) { | ||
if (isUnknownHost()) { | ||
String[] string = message.split(": "); // java.net.UnknownHostException: api.rach.io | ||
message = MessageFormat.format("Unable to connect to {0} (Unknown host / Network down / Low signal)", | ||
string[1]); | ||
} else if (isMalformedURL()) { | ||
message = MessageFormat.format("Invalid URL: {0}", apiResult.getUrl()); | ||
} else if (isTimeout()) { | ||
message = MessageFormat.format("Device unreachable or API Timeout ({0})", apiResult.getUrl()); | ||
} else { | ||
message = MessageFormat.format("{0} ({1})", message, cause); | ||
} | ||
} else { | ||
message = apiResult.toString(); | ||
} | ||
return message; | ||
} | ||
|
||
public boolean isApiException() { | ||
return getCauseClass() == ShellyApiException.class; | ||
} | ||
|
||
public boolean isTimeout() { | ||
Class<?> extype = !isEmpty() ? getCauseClass() : null; | ||
return (extype != null) && ((extype == TimeoutException.class) || (extype == ExecutionException.class) | ||
|| (extype == InterruptedException.class) || getMessage().toLowerCase().contains("timeout")); | ||
} | ||
|
||
public boolean isHttpAccessUnauthorized() { | ||
return apiResult.isHttpAccessUnauthorized(); | ||
} | ||
|
||
public boolean isUnknownHost() { | ||
return getCauseClass() == MalformedURLException.class; | ||
} | ||
|
||
public boolean isMalformedURL() { | ||
return getCauseClass() == UnknownHostException.class; | ||
} | ||
|
||
public boolean IsJSONException() { | ||
return getCauseClass() == JsonSyntaxException.class; | ||
} | ||
|
||
public ShellyApiResult getApiResult() { | ||
return apiResult; | ||
} | ||
|
||
private boolean isEmpty() { | ||
return nonNullString(super.getMessage()).equals(EX_NONE); | ||
} | ||
|
||
private static String nonNullString(@Nullable String s) { | ||
return s != null ? s : ""; | ||
} | ||
|
||
private Class<?> getCauseClass() { | ||
Throwable cause = getCause(); | ||
if (getCause() != null) { | ||
return cause.getClass(); | ||
} | ||
return ShellyApiException.class; | ||
} | ||
} |
Oops, something went wrong.