Skip to content

Commit

Permalink
[gardena] Adjust thread name when thing UID can't be used
Browse files Browse the repository at this point in the history
OH core method expects a name length between 4 and 20.

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Feb 5, 2023
1 parent fbbce11 commit d4a416f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.openhab.binding.gardena.internal.model.dto.command.GardenaCommandRequest;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.io.net.http.WebSocketFactory;
import org.openhab.core.thing.ThingUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,23 +102,44 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList
private final Object newDeviceTasksLock = new Object();
private final List<ScheduledFuture<?>> newDeviceTasks = new ArrayList<>();

public GardenaSmartImpl(String id, GardenaConfig config, GardenaSmartEventListener eventListener,
public GardenaSmartImpl(ThingUID uid, GardenaConfig config, GardenaSmartEventListener eventListener,
ScheduledExecutorService scheduler, HttpClientFactory httpClientFactory, WebSocketFactory webSocketFactory)
throws GardenaException {
this.id = id;
this.id = uid.getId();
this.config = config;
this.eventListener = eventListener;
this.scheduler = scheduler;

logger.debug("Starting GardenaSmart");
try {
httpClient = httpClientFactory.createHttpClient(id);
String name = uid.getAsString().replace(':', '-');
if (name.length() < 4 || name.length() > 20) {
String id = uid.getId();
if (id.length() > 12) {
id = Integer.toHexString(uid.getAsString().hashCode());
if (id.length() > 12) {
id = id.substring(id.length() - 12);
}
}
name = "gardena-" + id;
}
httpClient = httpClientFactory.createHttpClient(name);
httpClient.setConnectTimeout(config.getConnectionTimeout() * 1000L);
httpClient.setIdleTimeout(httpClient.getConnectTimeout());
httpClient.start();

String webSocketId = String.valueOf(hashCode());
webSocketClient = webSocketFactory.createWebSocketClient(webSocketId);
name = "ws-" + uid.getAsString().replace(':', '-');
if (name.length() < 4 || name.length() > 20) {
String id = uid.getId();
if (id.length() > 9) {
id = Integer.toHexString(uid.getAsString().hashCode());
if (id.length() > 9) {
id = id.substring(id.length() - 9);
}
}
name = "ws-gardena-" + id;
}
webSocketClient = webSocketFactory.createWebSocketClient(name);
webSocketClient.setConnectTimeout(config.getConnectionTimeout() * 1000L);
webSocketClient.setStopTimeout(3000);
webSocketClient.setMaxIdleTimeout(150000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ private synchronized void initializeGardena() {
GardenaConfig gardenaConfig = getThing().getConfiguration().as(GardenaConfig.class);
logger.debug("{}", gardenaConfig);

String id = getThing().getUID().getId();
gardenaSmart = new GardenaSmartImpl(id, gardenaConfig, this, scheduler, httpClientFactory,
gardenaSmart = new GardenaSmartImpl(getThing().getUID(), gardenaConfig, this, scheduler, httpClientFactory,
webSocketFactory);
final GardenaDeviceDiscoveryService discoveryService = this.discoveryService;
if (discoveryService != null) {
Expand Down

0 comments on commit d4a416f

Please sign in to comment.