]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hydrawise] Remove the access token when the thing is removed (#14945)
authorlolodomo <lg.hc@free.fr>
Sat, 6 May 2023 20:21:40 +0000 (22:21 +0200)
committerGitHub <noreply@github.com>
Sat, 6 May 2023 20:21:40 +0000 (22:21 +0200)
Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/handler/HydrawiseAccountHandler.java

index ef711034d6e84d699e2094d91cae8283e6b48a11..71dd547672682ae2bf752dcee64d677559f7be35 100644 (file)
@@ -68,18 +68,18 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
     private static final String SCOPE = "all";
     private final List<HydrawiseControllerListener> controllerListeners = Collections
             .synchronizedList(new ArrayList<HydrawiseControllerListener>());
-    private final HydrawiseGraphQLClient apiClient;
-    private final OAuthClientService oAuthService;
+    private final HttpClient httpClient;
+    private final OAuthFactory oAuthFactory;
+    private @Nullable OAuthClientService oAuthService;
+    private @Nullable HydrawiseGraphQLClient apiClient;
     private @Nullable ScheduledFuture<?> pollFuture;
     private @Nullable Customer lastData;
     private int refresh;
 
     public HydrawiseAccountHandler(final Bridge bridge, final HttpClient httpClient, final OAuthFactory oAuthFactory) {
         super(bridge);
-        this.oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL, AUTH_URL, CLIENT_ID,
-                CLIENT_SECRET, SCOPE, false);
-        oAuthService.addAccessTokenRefreshListener(this);
-        this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService);
+        this.httpClient = httpClient;
+        this.oAuthFactory = oAuthFactory;
     }
 
     @Override
@@ -88,14 +88,34 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
 
     @Override
     public void initialize() {
+        OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL,
+                AUTH_URL, CLIENT_ID, CLIENT_SECRET, SCOPE, false);
+        this.oAuthService = oAuthService;
+        oAuthService.addAccessTokenRefreshListener(this);
+        this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService);
         logger.debug("Handler initialized.");
-        scheduler.schedule(this::configure, 0, TimeUnit.SECONDS);
+        scheduler.schedule(() -> configure(oAuthService), 0, TimeUnit.SECONDS);
     }
 
     @Override
     public void dispose() {
         logger.debug("Handler disposed.");
         clearPolling();
+        OAuthClientService oAuthService = this.oAuthService;
+        if (oAuthService != null) {
+            oAuthService.removeAccessTokenRefreshListener(this);
+            oAuthFactory.ungetOAuthService(getThing().toString());
+            this.oAuthService = null;
+        }
+    }
+
+    @Override
+    public void handleRemoval() {
+        OAuthClientService oAuthService = this.oAuthService;
+        if (oAuthService != null) {
+            oAuthFactory.deleteServiceAndAccessToken(getThing().toString());
+        }
+        super.handleRemoval();
     }
 
     @Override
@@ -134,7 +154,7 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
         initPolling(delaySeconds, this.refresh);
     }
 
-    private void configure() {
+    private void configure(OAuthClientService oAuthService) {
         HydrawiseAccountConfiguration config = getConfig().as(HydrawiseAccountConfiguration.class);
         try {
             if (!config.userName.isEmpty() && !config.password.isEmpty()) {