Skip to content

Commit

Permalink
[hydrawise] Remove the access token when the thing is removed (openha…
Browse files Browse the repository at this point in the history
…b#14945)

Related to openhab#14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Matt Myers <mmyers75@icloud.com>
  • Loading branch information
lolodomo authored and matchews committed Aug 9, 2023
1 parent 36124fd commit a1d331c
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,14 +88,34 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@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
Expand Down Expand Up @@ -134,7 +154,7 @@ public void refreshData(int delaySeconds) {
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()) {
Expand Down

0 comments on commit a1d331c

Please sign in to comment.