[daikin] Use createHttpClient (#14481)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-02-22 20:48:06 +01:00 committed by GitHub
parent 20ac69725a
commit e7b0fc6c14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 24 deletions

View File

@ -30,7 +30,7 @@ import org.openhab.core.thing.ThingTypeUID;
@NonNullByDefault
public class DaikinBindingConstants {
private static final String BINDING_ID = "daikin";
public static final String BINDING_ID = "daikin";
// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_AC_UNIT = new ThingTypeUID(BINDING_ID, "ac_unit");

View File

@ -16,8 +16,11 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,37 +42,32 @@ public class DaikinHttpClientFactoryImpl implements DaikinHttpClientFactory {
private final Logger logger = LoggerFactory.getLogger(DaikinHttpClientFactoryImpl.class);
private @Nullable HttpClient httpClient;
private HttpClient httpClient;
@Activate
public DaikinHttpClientFactoryImpl(@Reference HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.createHttpClient(DaikinBindingConstants.BINDING_ID,
new SslContextFactory.Client(true));
try {
httpClient.start();
logger.debug("Daikin http client started");
} catch (Exception e) {
logger.warn("Could not start Daikin http client", e);
}
}
@Deactivate
protected void deactivate() {
if (httpClient != null) {
try {
httpClient.stop();
logger.debug("Daikin http client stopped");
} catch (Exception e) {
logger.debug("error while stopping Daikin http client", e);
}
httpClient = null;
try {
httpClient.stop();
logger.debug("Daikin http client stopped");
} catch (Exception e) {
logger.debug("error while stopping Daikin http client", e);
}
}
@Override
public @Nullable HttpClient getHttpClient() {
initialize();
return httpClient;
}
private synchronized void initialize() {
if (httpClient == null) {
httpClient = new HttpClient(new SslContextFactory.Client(true));
try {
httpClient.start();
logger.debug("Daikin http client started");
} catch (Exception e) {
logger.warn("Could not start Daikin http client", e);
httpClient = null;
}
}
}
}