*/
public class RobonectBindingConstants {
- private static final String BINDING_ID = "robonect";
+ public static final String BINDING_ID = "robonect";
// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_AUTOMOWER = new ThingTypeUID(BINDING_ID, "mower");
*/
package org.openhab.binding.robonect.internal;
-import static org.openhab.binding.robonect.internal.RobonectBindingConstants.THING_TYPE_AUTOMOWER;
+import static org.openhab.binding.robonect.internal.RobonectBindingConstants.*;
-import java.util.Collections;
import java.util.Set;
-import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.robonect.internal.handler.RobonectHandler;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
*
* @author Marco Meyer - Initial contribution
*/
+@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.robonect")
public class RobonectHandlerFactory extends BaseThingHandlerFactory {
- private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_AUTOMOWER);
+ private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_AUTOMOWER);
- private HttpClient httpClient;
+ private HttpClientFactory httpClientFactory;
private TimeZoneProvider timeZoneProvider;
@Activate
public RobonectHandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference TimeZoneProvider timeZoneProvider) {
- this.httpClient = httpClientFactory.getCommonHttpClient();
+ this.httpClientFactory = httpClientFactory;
this.timeZoneProvider = timeZoneProvider;
}
}
@Override
- protected ThingHandler createHandler(Thing thing) {
+ protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE_AUTOMOWER)) {
- return new RobonectHandler(thing, httpClient, timeZoneProvider);
+ return new RobonectHandler(thing, httpClientFactory, timeZoneProvider);
}
return null;
import org.openhab.binding.robonect.internal.model.VersionInfo;
import org.openhab.binding.robonect.internal.model.cmd.ModeCommand;
import org.openhab.core.i18n.TimeZoneProvider;
+import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
+import org.openhab.core.thing.util.ThingWebClientUtil;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
private RobonectClient robonectClient;
- public RobonectHandler(Thing thing, HttpClient httpClient, TimeZoneProvider timeZoneProvider) {
+ public RobonectHandler(Thing thing, HttpClientFactory httpClientFactory, TimeZoneProvider timeZoneProvider) {
super(thing);
- this.httpClient = httpClient;
+ httpClient = httpClientFactory
+ .createHttpClient(ThingWebClientUtil.buildWebClientConsumerName(thing.getUID(), null));
this.timeZoneProvider = timeZoneProvider;
}
if (info.getHealth() != null) {
updateState(CHANNEL_HEALTH_TEMP,
new QuantityType<>(info.getHealth().getTemperature(), SIUnits.CELSIUS));
- updateState(CHANNEL_HEALTH_HUM, new QuantityType(info.getHealth().getHumidity(), Units.PERCENT));
+ updateState(CHANNEL_HEALTH_HUM, new QuantityType<>(info.getHealth().getHumidity(), Units.PERCENT));
}
if (info.getTimer() != null) {
if (info.getTimer().getNext() != null) {
try {
httpClient.start();
- robonectClient = new RobonectClient(httpClient, endpoint);
} catch (Exception e) {
- logger.error("Exception while trying to start http client", e);
- throw new RuntimeException("Exception while trying to start http client", e);
+ logger.error("Exception while trying to start HTTP client", e);
+ throw new IllegalStateException("Could not create HttpClient");
}
+
+ robonectClient = new RobonectClient(httpClient, endpoint);
Runnable runnable = new MowerChannelPoller(TimeUnit.SECONDS.toMillis(robonectConfig.getOfflineTimeout()));
int pollInterval = robonectConfig.getPollInterval();
pollingJob = scheduler.scheduleWithFixedDelay(runnable, 0, pollInterval, TimeUnit.SECONDS);
@Override
public void dispose() {
+ ScheduledFuture<?> pollingJob = this.pollingJob;
if (pollingJob != null) {
pollingJob.cancel(true);
- pollingJob = null;
+ this.pollingJob = null;
}
- httpClient = null;
+ try {
+ httpClient.stop();
+ } catch (Exception e) {
+ logger.warn("Exception while trying to stop HTTP client", e);
+ }
}
/**
import java.time.ZoneId;
import java.time.ZonedDateTime;
-import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openhab.binding.robonect.internal.model.Timer;
import org.openhab.binding.robonect.internal.model.Wlan;
import org.openhab.core.i18n.TimeZoneProvider;
+import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
private @Mock Thing robonectThingMock;
private @Mock RobonectClient robonectClientMock;
private @Mock ThingHandlerCallback callbackMock;
- private @Mock HttpClient httpClientMock;
+ private @Mock HttpClientFactory httpClientFactoryMock;
private @Mock TimeZoneProvider timezoneProvider;
@BeforeEach
public void setUp() {
- subject = new RobonectHandler(robonectThingMock, httpClientMock, timezoneProvider);
+ Mockito.when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
+ Mockito.when(timezoneProvider.getTimeZone()).thenReturn(ZoneId.of("Europe/Berlin"));
+
+ subject = new RobonectHandler(robonectThingMock, httpClientFactoryMock, timezoneProvider);
subject.setCallback(callbackMock);
subject.setRobonectClient(robonectClientMock);
-
- Mockito.when(timezoneProvider.getTimeZone()).thenReturn(ZoneId.of("Europe/Berlin"));
}
@Test
// when
when(robonectClientMock.getMowerInfo()).thenReturn(mowerInfo);
- when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
subject.handleCommand(new ChannelUID(new ThingUID("1:2:3"), RobonectBindingConstants.CHANNEL_TIMER_NEXT_TIMER),
RefreshType.REFRESH);
// when
when(robonectClientMock.getMowerInfo()).thenReturn(mowerInfo);
when(robonectClientMock.errorList()).thenReturn(errorList);
- when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
subject.handleCommand(new ChannelUID(new ThingUID("1:2:3"), RobonectBindingConstants.CHANNEL_STATUS),
RefreshType.REFRESH);
// when
when(robonectClientMock.getMowerInfo()).thenReturn(mowerInfo);
- when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
subject.handleCommand(new ChannelUID(new ThingUID("1:2:3"), RobonectBindingConstants.CHANNEL_STATUS),
RefreshType.REFRESH);
// when
when(robonectClientMock.getMowerInfo()).thenReturn(mowerInfo);
- when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
subject.handleCommand(new ChannelUID(new ThingUID("1:2:3"), RobonectBindingConstants.CHANNEL_STATUS),
RefreshType.REFRESH);
// when
when(robonectClientMock.getMowerInfo()).thenReturn(mowerInfo);
when(robonectClientMock.errorList()).thenReturn(errorList);
- when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3"));
subject.handleCommand(new ChannelUID(new ThingUID("1:2:3"), RobonectBindingConstants.CHANNEL_STATUS),
RefreshType.REFRESH);