Skip to content

Commit

Permalink
[tesla] Normalize thread names and avoid a possible IAE (openhab#14351)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and FordPrfkt committed Apr 19, 2023
1 parent 1444909 commit aa35049
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.websocket.api.Session;
Expand All @@ -32,6 +31,8 @@
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.openhab.binding.tesla.internal.protocol.Event;
import org.openhab.core.io.net.http.WebSocketFactory;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.util.ThingWebClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -54,19 +55,19 @@ public class TeslaEventEndpoint implements WebSocketListener, WebSocketPingPongL

private String endpointId;
protected WebSocketFactory webSocketFactory;
private static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger();

private WebSocketClient client;
private ConnectionState connectionState = ConnectionState.CLOSED;
private @Nullable Session session;
private EventHandler eventHandler;
private final Gson gson = new Gson();

public TeslaEventEndpoint(WebSocketFactory webSocketFactory) {
public TeslaEventEndpoint(ThingUID uid, WebSocketFactory webSocketFactory) {
try {
this.endpointId = "TeslaEventEndpoint-" + INSTANCE_COUNTER.incrementAndGet();
this.endpointId = "TeslaEventEndpoint-" + uid.getAsString();

client = webSocketFactory.createWebSocketClient(endpointId);
String name = ThingWebClientUtil.buildWebClientConsumerName(uid, null);
client = webSocketFactory.createWebSocketClient(name);
this.client.setConnectTimeout(TIMEOUT_MILLISECONDS);
this.client.setMaxIdleTimeout(IDLE_TIMEOUT_MILLISECONDS);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void initialize() {

if (enableEvents) {
if (eventThread == null) {
eventThread = new Thread(eventRunnable, "openHAB-Tesla-Events-" + getThing().getUID());
eventThread = new Thread(eventRunnable, "OH-binding-" + getThing().getUID() + "-events");
eventThread.start();
}
}
Expand Down Expand Up @@ -1130,7 +1130,7 @@ protected BigDecimal roundBigDecimal(BigDecimal value) {

@Override
public void run() {
eventEndpoint = new TeslaEventEndpoint(webSocketFactory);
eventEndpoint = new TeslaEventEndpoint(getThing().getUID(), webSocketFactory);
eventEndpoint.addEventHandler(new TeslaEventEndpoint.EventHandler() {
@Override
public void handleEvent(Event event) {
Expand Down

0 comments on commit aa35049

Please sign in to comment.