From aff25a9ffd93f069acd73a06dda13c4fbad67f63 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 23 Dec 2020 21:25:55 -0800 Subject: [PATCH] Addressed feedback Signed-off-by: Dan Cunningham --- .../GeneracMobileLinkDiscoveryService.java | 15 +++- .../GeneracMobileLinkAccountHandler.java | 90 ++++++++----------- .../resources/OH-INF/thing/thing-types.xml | 2 +- 3 files changed, 50 insertions(+), 57 deletions(-) diff --git a/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/discovery/GeneracMobileLinkDiscoveryService.java b/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/discovery/GeneracMobileLinkDiscoveryService.java index bf67fc2f54e67..c0de87f4437cd 100644 --- a/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/discovery/GeneracMobileLinkDiscoveryService.java +++ b/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/discovery/GeneracMobileLinkDiscoveryService.java @@ -17,9 +17,13 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants; +import org.openhab.binding.generacmobilelink.internal.dto.GeneratorStatusDTO; import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; +import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.thing.ThingTypeUID; +import org.openhab.core.thing.ThingUID; /** * The {@link GeneracMobileLinkDiscoveryService} is responsible for discovering generator things @@ -48,8 +52,13 @@ public boolean isBackgroundDiscoveryEnabled() { return false; } - @Override - public void thingDiscovered(DiscoveryResult result) { - super.thingDiscovered(result); + public void generatorDiscovered(GeneratorStatusDTO generator, ThingUID bridgeUID) { + DiscoveryResult result = DiscoveryResultBuilder + .create(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR, bridgeUID, + String.valueOf(generator.gensetID))) + .withLabel("MobileLink Generator " + generator.generatorName) + .withProperty("generatorId", String.valueOf(generator.gensetID)) + .withRepresentationProperty("generatorId").withBridge(bridgeUID).build(); + thingDiscovered(result); } } diff --git a/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/handler/GeneracMobileLinkAccountHandler.java b/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/handler/GeneracMobileLinkAccountHandler.java index 010e5128d78cd..c19b5a2520665 100644 --- a/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/handler/GeneracMobileLinkAccountHandler.java +++ b/bundles/org.openhab.binding.generacmobilelink/src/main/java/org/openhab/binding/generacmobilelink/internal/handler/GeneracMobileLinkAccountHandler.java @@ -36,8 +36,6 @@ import org.openhab.binding.generacmobilelink.internal.dto.GeneratorStatusResponseDTO; import org.openhab.binding.generacmobilelink.internal.dto.LoginRequestDTO; import org.openhab.binding.generacmobilelink.internal.dto.LoginResponseDTO; -import org.openhab.core.config.discovery.DiscoveryResult; -import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -125,60 +123,56 @@ private void restartPoll() { } private void poll() { - if (authToken == null) { - logger.debug("Attempting Login"); - login(); + try { + if (authToken == null) { + logger.debug("Attempting Login"); + login(); + } + getStatuses(true); + } catch (InterruptedException e) { } - getStatuses(true); } - private synchronized void login() { + private synchronized void login() throws InterruptedException { GeneracMobileLinkAccountConfiguration config = getConfigAs(GeneracMobileLinkAccountConfiguration.class); refreshIntervalSeconds = config.refreshInterval; - try { - HTTPResult result = sendRequest(BASE_URL + "/Users/login", HttpMethod.POST, null, - new StringContentProvider( - gson.toJson(new LoginRequestDTO(SHARED_KEY, config.username, config.password))), - "application/json"); - if (result.responseCode == HttpStatus.OK_200) { - LoginResponseDTO loginResponse = gson.fromJson(result.content, LoginResponseDTO.class); - if (loginResponse != null) { - authToken = loginResponse.authToken; - updateStatus(ThingStatus.ONLINE); - } - } else { - handleErrorResponse(result); - if (thing.getStatusInfo().getStatusDetail() == ThingStatusDetail.CONFIGURATION_ERROR) { - // bad credentials, stop trying to login - stopPoll(); - } + HTTPResult result = sendRequest(BASE_URL + "/Users/login", HttpMethod.POST, null, + new StringContentProvider( + gson.toJson(new LoginRequestDTO(SHARED_KEY, config.username, config.password))), + "application/json"); + if (result.responseCode == HttpStatus.OK_200) { + LoginResponseDTO loginResponse = gson.fromJson(result.content, LoginResponseDTO.class); + if (loginResponse != null) { + authToken = loginResponse.authToken; + updateStatus(ThingStatus.ONLINE); + } + } else { + handleErrorResponse(result); + if (thing.getStatusInfo().getStatusDetail() == ThingStatusDetail.CONFIGURATION_ERROR) { + // bad credentials, stop trying to login + stopPoll(); } - } catch (InterruptedException e) { } } - private void getStatuses(boolean retry) { + private void getStatuses(boolean retry) throws InterruptedException { if (authToken == null) { return; } - try { - HTTPResult result = sendRequest(BASE_URL + "/Generator/GeneratorStatus", HttpMethod.GET, authToken, null, - null); - if (result.responseCode == HttpStatus.OK_200) { - generators = gson.fromJson(result.content, GeneratorStatusResponseDTO.class); - updateGeneratorThings(); - if (getThing().getStatus() != ThingStatus.ONLINE) { - updateStatus(ThingStatus.ONLINE); - } + HTTPResult result = sendRequest(BASE_URL + "/Generator/GeneratorStatus", HttpMethod.GET, authToken, null, null); + if (result.responseCode == HttpStatus.OK_200) { + generators = gson.fromJson(result.content, GeneratorStatusResponseDTO.class); + updateGeneratorThings(); + if (getThing().getStatus() != ThingStatus.ONLINE) { + updateStatus(ThingStatus.ONLINE); + } + } else { + if (retry) { + logger.debug("Retrying status request"); + getStatuses(false); } else { - if (retry) { - logger.debug("Retrying status request"); - getStatuses(false); - } else { - handleErrorResponse(result); - } + handleErrorResponse(result); } - } catch (InterruptedException e) { } } @@ -234,7 +228,7 @@ private void updateGeneratorThings() { Thing thing = getThing().getThing(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR, getThing().getUID(), String.valueOf(generator.gensetID))); if (thing == null) { - generatorDiscovered(generator); + discoveryService.generatorDiscovered(generator, getThing().getUID()); } else { ThingHandler handler = thing.getHandler(); if (handler != null) { @@ -245,16 +239,6 @@ private void updateGeneratorThings() { } } - private void generatorDiscovered(GeneratorStatusDTO generator) { - DiscoveryResult result = DiscoveryResultBuilder - .create(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR, getThing().getUID(), - String.valueOf(generator.gensetID))) - .withLabel("MobileLink Generator " + generator.generatorName) - .withProperty("generatorId", String.valueOf(generator.gensetID)) - .withRepresentationProperty("generatorId").withBridge(getThing().getUID()).build(); - discoveryService.thingDiscovered(result); - } - public static class HTTPResult { public @Nullable String content; public final int responseCode; diff --git a/bundles/org.openhab.binding.generacmobilelink/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.generacmobilelink/src/main/resources/OH-INF/thing/thing-types.xml index 853e6d1e2eba2..020894830779f 100644 --- a/bundles/org.openhab.binding.generacmobilelink/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.generacmobilelink/src/main/resources/OH-INF/thing/thing-types.xml @@ -15,7 +15,7 @@ - Generator + MobileLink Generator