Skip to content

Commit

Permalink
fix sat errors & warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 13, 2024
1 parent d9e0c1b commit bd63abb
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class SmartthingsBaseServlet extends HttpServlet {

private static final long serialVersionUID = -4719613645562518231L;
private final Logger logger = LoggerFactory.getLogger(SmartthingsAuthServlet.class);
private final Logger logger = LoggerFactory.getLogger(SmartthingsBaseServlet.class);

protected final SmartthingsBridgeHandler bridgeHandler;
protected final HttpService httpService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openhab.binding.smartthings.internal.dto.SMEvent.device;
import org.openhab.binding.smartthings.internal.dto.SmartthingsLocation;
import org.openhab.binding.smartthings.internal.handler.SmartthingsBridgeHandler;
import org.openhab.binding.smartthings.internal.type.SmartthingsException;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -160,21 +161,25 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
template = indexTemplate;
}

if (template == selectLocationTemplate) {
if (selectLocationTemplate.equals(template)) {
SetupApp();

SmartthingsLocation[] locationList = api.GetAllLocations();
for (SmartthingsLocation loc : locationList) {
optionBuffer.append("<option value=\"" + loc.locationId + "\">" + loc.name + "</option>");
try {
SmartthingsLocation[] locationList = api.GetAllLocations();
for (SmartthingsLocation loc : locationList) {
optionBuffer.append("<option value=\"" + loc.locationId + "\">" + loc.name + "</option>");
}
} catch (SmartthingsException ex) {
optionBuffer.append("Unable to retrieve locations !!");
}
setupInProgress = true;
} else if (template == poolTemplate) {
} else if (poolTemplate.equals(template)) {
if (setupInProgress) {
replaceMap.put(KEY_POOL_STATUS, "false");
} else {
replaceMap.put(KEY_POOL_STATUS, "true");
}
} else if (template == confirmationTemplate) {
} else if (confirmationTemplate.equals(template)) {
replaceMap.put(KEY_LOCATION, installedLocation);
replaceMap.put(KEY_APP_ID, installedAppId);
}
Expand Down Expand Up @@ -222,8 +227,12 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
String token = resultObj.installData.authToken;
installedAppId = resultObj.installData.installedApp.installedAppId;

SmartthingsLocation loc = api.GetLocation(resultObj.installData.installedApp.locationId);
installedLocation = loc.name;
try {
SmartthingsLocation loc = api.GetLocation(resultObj.installData.installedApp.locationId);
installedLocation = loc.name;
} catch (SmartthingsException ex) {
installedLocation = "Unable to retrieve location!!";
}

setupInProgress = false;
logger.info("");
Expand Down Expand Up @@ -309,7 +318,11 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
logger.error("error during confirmation {}", confirmUrl);
}

api.CreateAppOAuth(appId);
try {
api.CreateAppOAuth(appId);
} catch (SmartthingsException ex) {
logger.error("Unable to setup app oauth settings!!");
}

logger.trace("CONFIRMATION {}", confirmUrl);
}
Expand All @@ -321,9 +334,13 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
protected void SetupApp() {
SmartthingsApi api = bridgeHandler.getSmartthingsApi();

AppResponse appResponse = api.SetupApp();
if (appResponse.oauthClientId != null && appResponse.oauthClientSecret != null) {
bridgeHandler.updateConfig(appResponse.oauthClientId, appResponse.oauthClientSecret);
try {
AppResponse appResponse = api.SetupApp();
if (appResponse.oauthClientId != null && appResponse.oauthClientSecret != null) {
bridgeHandler.updateConfig(appResponse.oauthClientId, appResponse.oauthClientSecret);
}
} catch (SmartthingsException ex) {
logger.info("Unable to setup Smartthings app !!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openhab.binding.smartthings.internal.dto.SmartthingsDevice;
import org.openhab.binding.smartthings.internal.dto.SmartthingsLocation;
import org.openhab.binding.smartthings.internal.dto.SmartthingsRoom;
import org.openhab.binding.smartthings.internal.type.SmartthingsException;
import org.openhab.core.auth.client.oauth2.OAuthClientService;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -73,12 +74,12 @@ public SmartthingsApi(HttpClientFactory httpClientFactory, SmartthingsNetworkCon
this.networkConnector = networkConnector;
}

public SmartthingsDevice[] GetAllDevices() {
public SmartthingsDevice[] GetAllDevices() throws SmartthingsException {
SmartthingsDevice[] devices = DoRequest(SmartthingsDevice[].class, baseUrl + deviceEndPoint);
return devices;
}

public AppResponse SetupApp() {
public AppResponse SetupApp() throws SmartthingsException {
SmartthingsApp[] appList = GetAllApps();

Optional<SmartthingsApp> appOptional = Arrays.stream(appList).filter(x -> APP_NAME.equals(x.appName))
Expand All @@ -100,75 +101,75 @@ public AppResponse SetupApp() {
}
}

public SmartthingsCapabilitie[] GetAllCapabilities() {
public SmartthingsCapabilitie[] GetAllCapabilities() throws SmartthingsException {
try {
String uri = baseUrl + capabilitiesEndPoint;
SmartthingsCapabilitie[] listCapabilities = DoRequest(SmartthingsCapabilitie[].class, uri);
return listCapabilities;

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve capabilities", e);
}
}

public SmartthingsCapabilitie GetCapabilitie(String capabilityId, String version) {
public SmartthingsCapabilitie GetCapabilitie(String capabilityId, String version) throws SmartthingsException {
try {
String uri = baseUrl + capabilitiesEndPoint + "/" + capabilityId + "/" + version;
SmartthingsCapabilitie capabilitie = DoRequest(SmartthingsCapabilitie.class, uri);
return capabilitie;

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve capability", e);
}
}

public SmartthingsLocation[] GetAllLocations() {
public SmartthingsLocation[] GetAllLocations() throws SmartthingsException {
try {
String uri = baseUrl + locationEndPoint;
SmartthingsLocation[] listLocations = DoRequest(SmartthingsLocation[].class, uri);
return listLocations;

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve locations", e);
}
}

public SmartthingsLocation GetLocation(String locationId) {
public SmartthingsLocation GetLocation(String locationId) throws SmartthingsException {
try {
String uri = baseUrl + locationEndPoint + "/" + locationId;

SmartthingsLocation loc = DoRequest(SmartthingsLocation.class, uri);

return loc;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve location", e);
}
}

public SmartthingsRoom[] GetRooms(String locationId) {
public SmartthingsRoom[] GetRooms(String locationId) throws SmartthingsException {
try {
String uri = baseUrl + locationEndPoint + "/" + locationId + roomsEndPoint;
SmartthingsRoom[] listRooms = DoRequest(SmartthingsRoom[].class, uri);
return listRooms;

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve rooms", e);
}
}

public SmartthingsRoom GetRoom(String locationId, String roomId) {
public SmartthingsRoom GetRoom(String locationId, String roomId) throws SmartthingsException {
try {
String uri = baseUrl + locationEndPoint + "/" + locationId + roomsEndPoint + "/" + roomId;

SmartthingsRoom loc = DoRequest(SmartthingsRoom.class, uri);

return loc;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve room", e);
}
}

public SmartthingsApp[] GetAllApps() {
public SmartthingsApp[] GetAllApps() throws SmartthingsException {
try {
String uri = baseUrl + appEndPoint;

Expand All @@ -178,23 +179,23 @@ public SmartthingsApp[] GetAllApps() {
return listApps;

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve apps", e);
}
}

public SmartthingsApp GetApp(String appId) {
public SmartthingsApp GetApp(String appId) throws SmartthingsException {
try {
String uri = baseUrl + appEndPoint + "/" + appId;

SmartthingsApp app = DoRequest(SmartthingsApp.class, uri);

return app;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to retrieve app", e);
}
}

public AppResponse CreateApp() {
public AppResponse CreateApp() throws SmartthingsException {
try {
String uri = baseUrl + appEndPoint + "?signatureType=ST_PADLOCK&requireConfirmation=true";

Expand All @@ -215,11 +216,11 @@ public AppResponse CreateApp() {
} catch (

final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to create app", e);
}
}

public void CreateAppOAuth(String appId) {
public void CreateAppOAuth(String appId) throws SmartthingsException {
try {
String uri = baseUrl + appEndPoint + "/" + appId + "/oauth";

Expand All @@ -238,7 +239,7 @@ public void CreateAppOAuth(String appId) {

// return appResponse;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to create oauth settings", e);
}
}

Expand All @@ -260,26 +261,27 @@ public void SendCommand(String deviceId, String jsonMsg) {
String uri = baseUrl + deviceEndPoint + "/" + deviceId + "/commands";
DoRequest(JsonObject.class, uri, jsonMsg, false);
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new RuntimeException("SmartthingsApi : Unable to send command", e);
}
}

public @Nullable JsonObject SendStatus(String deviceId, String jsonMsg) {
public @Nullable JsonObject SendStatus(String deviceId, String jsonMsg) throws SmartthingsException {
try {
String uri = baseUrl + deviceEndPoint + "/" + deviceId + "/status";

JsonObject res = DoRequest(JsonObject.class, uri, jsonMsg, false);
return res;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to send status", e);
}
}

public <T> T DoRequest(Class<T> resultClass, String uri) {
public <T> T DoRequest(Class<T> resultClass, String uri) throws SmartthingsException {
return DoRequest(resultClass, uri, null, false);
}

public <T> T DoRequest(Class<T> resultClass, String uri, @Nullable String body, Boolean update) {
public <T> T DoRequest(Class<T> resultClass, String uri, @Nullable String body, Boolean update)
throws SmartthingsException {
try {
HttpMethod httpMethod = HttpMethod.GET;
if (body != null) {
Expand All @@ -292,7 +294,7 @@ public <T> T DoRequest(Class<T> resultClass, String uri, @Nullable String body,
T res = networkConnector.DoRequest(resultClass, uri, null, getToken(), body, httpMethod);
return res;
} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new SmartthingsException("SmartthingsApi : Unable to do request", e);
}
}
}
Loading

0 comments on commit bd63abb

Please sign in to comment.