Skip to content

Commit

Permalink
fixes for @clinique review openhab#3
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Aug 27, 2024
1 parent cfc0754 commit f4b6ba6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.format.DateTimeFormatter;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.linky.internal.LinkyConfiguration;
import org.openhab.binding.linky.internal.LinkyException;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
import org.openhab.core.auth.client.oauth2.OAuthFactory;
Expand Down Expand Up @@ -62,12 +63,20 @@ public EnedisBridgeHandler(Bridge bridge, final @Reference HttpClientFactory htt

@Override
public String getClientId() {
return config.clientId;
LinkyConfiguration lcConfig = config;
if (lcConfig != null) {
return lcConfig.clientId;
}
return "";
}

@Override
public String getClientSecret() {
return config.clientSecret;
LinkyConfiguration lcConfig = config;
if (lcConfig != null) {
return lcConfig.clientSecret;
}
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,15 @@ public DateTimeFormatter getApiDateFormatYearsFirst() {

@Override
protected synchronized void connectionInit() throws LinkyException {
logger.debug("Starting login process for user : {}", config.username);
LinkyConfiguration lcConfig = config;
if (lcConfig == null) {
return;
}

logger.debug("Starting login process for user : {}", lcConfig.username);

try {
enedisApi.addCookie(LinkyConfiguration.INTERNAL_AUTH_ID, config.internalAuthId);
enedisApi.addCookie(LinkyConfiguration.INTERNAL_AUTH_ID, lcConfig.internalAuthId);
logger.debug("Step 1 : getting authentification");
String data = enedisApi.getData(URL_ENEDIS_AUTHENTICATE);

Expand Down Expand Up @@ -207,13 +212,13 @@ protected synchronized void connectionInit() throws LinkyException {

AuthData authData = gson.fromJson(result.getContentAsString(), AuthData.class);
if (authData == null || authData.callbacks.size() < 2 || authData.callbacks.get(0).input.isEmpty()
|| authData.callbacks.get(1).input.isEmpty() || !config.username
|| authData.callbacks.get(1).input.isEmpty() || !lcConfig.username
.equals(Objects.requireNonNull(authData.callbacks.get(0).input.get(0)).valueAsString())) {
logger.debug("auth1 - invalid template for auth data: {}", result.getContentAsString());
throw new LinkyException("Authentication error, the authentication_cookie is probably wrong");
}

authData.callbacks.get(1).input.get(0).value = config.password;
authData.callbacks.get(1).input.get(0).value = lcConfig.password;
logger.debug("Step 4 : auth2 - send the auth data");
result = httpClient.POST(authenticateUrl).header(HttpHeader.CONTENT_TYPE, "application/json")
.header("X-NoSession", "true").header("X-Password", "anonymous")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public abstract class LinkyBridgeHandler extends BaseBridgeHandler {
protected final EnedisHttpApi enedisApi;
protected final ThingRegistry thingRegistry;

protected final LinkyConfiguration config;
protected final Gson gson;

protected @Nullable LinkyConfiguration config;
protected boolean connected = false;

private static final int REQUEST_BUFFER_SIZE = 8000;
Expand Down Expand Up @@ -104,8 +104,6 @@ public LinkyBridgeHandler(Bridge bridge, final @Reference HttpClientFactory http

this.enedisApi = new EnedisHttpApi(this, gson, this.httpClient);

config = getConfigAs(LinkyConfiguration.class);

updateStatus(ThingStatus.UNKNOWN);
}

Expand All @@ -117,6 +115,8 @@ public BundleContext getBundleContext() {
public synchronized void initialize() {
logger.debug("Initializing Linky API bridge handler.");

config = getConfigAs(LinkyConfiguration.class);

updateStatus(ThingStatus.UNKNOWN);

scheduler.submit(() -> {
Expand Down

0 comments on commit f4b6ba6

Please sign in to comment.