[volvooncall] Adjust thread name for HTTP client (#14469)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-02-20 21:47:46 +01:00 committed by GitHub
parent cb160bdf63
commit 8c56a0f0b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 12 deletions

View File

@ -18,7 +18,6 @@ import java.time.ZonedDateTime;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.volvooncall.internal.handler.VehicleHandler;
import org.openhab.binding.volvooncall.internal.handler.VehicleStateDescriptionProvider;
import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
@ -55,13 +54,13 @@ public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(VolvoOnCallHandlerFactory.class);
private final VehicleStateDescriptionProvider stateDescriptionProvider;
private final Gson gson;
private final HttpClient httpClient;
private final HttpClientFactory httpClientFactory;
@Activate
public VolvoOnCallHandlerFactory(@Reference VehicleStateDescriptionProvider provider,
@Reference HttpClientFactory httpClientFactory) {
this.stateDescriptionProvider = provider;
this.httpClient = httpClientFactory.createHttpClient(BINDING_ID);
this.httpClientFactory = httpClientFactory;
this.gson = new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class,
(JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
@ -87,7 +86,7 @@ public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
return new VolvoOnCallBridgeHandler((Bridge) thing, gson, httpClient);
return new VolvoOnCallBridgeHandler((Bridge) thing, gson, httpClientFactory);
} else if (VEHICLE_THING_TYPE.equals(thingTypeUID)) {
return new VehicleHandler(thing, stateDescriptionProvider);
}

View File

@ -36,6 +36,7 @@ import org.openhab.binding.volvooncall.internal.dto.PostResponse;
import org.openhab.binding.volvooncall.internal.dto.VocAnswer;
import org.openhab.core.cache.ExpiringCacheMap;
import org.openhab.core.id.InstanceUUID;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -61,12 +62,12 @@ public class VocHttpApi {
private final HttpClient httpClient;
private final ApiBridgeConfiguration configuration;
public VocHttpApi(ApiBridgeConfiguration configuration, Gson gson, HttpClient httpClient)
throws VolvoOnCallException {
public VocHttpApi(String clientName, ApiBridgeConfiguration configuration, Gson gson,
HttpClientFactory httpClientFactory) throws VolvoOnCallException {
this.gson = gson;
this.cache = new ExpiringCacheMap<>(120 * 1000);
this.configuration = configuration;
this.httpClient = httpClient;
this.httpClient = httpClientFactory.createHttpClient(clientName);
httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, "openhab/voc_binding/" + InstanceUUID.get()));
try {

View File

@ -17,18 +17,19 @@ import java.util.Collections;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.volvooncall.internal.VolvoOnCallException;
import org.openhab.binding.volvooncall.internal.api.VocHttpApi;
import org.openhab.binding.volvooncall.internal.config.ApiBridgeConfiguration;
import org.openhab.binding.volvooncall.internal.discovery.VolvoVehicleDiscoveryService;
import org.openhab.binding.volvooncall.internal.dto.CustomerAccounts;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseBridgeHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.thing.util.ThingWebClientUtil;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -46,14 +47,14 @@ public class VolvoOnCallBridgeHandler extends BaseBridgeHandler {
private final Logger logger = LoggerFactory.getLogger(VolvoOnCallBridgeHandler.class);
private final Gson gson;
private final HttpClient httpClient;
private final HttpClientFactory httpClientFactory;
private @Nullable VocHttpApi api;
public VolvoOnCallBridgeHandler(Bridge bridge, Gson gson, HttpClient httpClient) {
public VolvoOnCallBridgeHandler(Bridge bridge, Gson gson, HttpClientFactory httpClientFactory) {
super(bridge);
this.gson = gson;
this.httpClient = httpClient;
this.httpClientFactory = httpClientFactory;
}
@Override
@ -62,7 +63,8 @@ public class VolvoOnCallBridgeHandler extends BaseBridgeHandler {
ApiBridgeConfiguration configuration = getConfigAs(ApiBridgeConfiguration.class);
try {
VocHttpApi vocApi = new VocHttpApi(configuration, gson, httpClient);
String clientName = ThingWebClientUtil.buildWebClientConsumerName(thing.getUID(), null);
VocHttpApi vocApi = new VocHttpApi(clientName, configuration, gson, httpClientFactory);
CustomerAccounts account = vocApi.getURL("customeraccounts/", CustomerAccounts.class);
if (account.username != null) {
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, account.username);