use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class BridgeImpl method getHandler.
@Override
public BridgeHandler getHandler() {
BridgeHandler bridgeHandler = null;
ThingHandler thingHandler = super.getHandler();
if (thingHandler instanceof BridgeHandler) {
bridgeHandler = (BridgeHandler) thingHandler;
} else if (thingHandler != null) {
logger.warn("Handler of bridge '{}' must implement BridgeHandler interface.", getUID());
}
return bridgeHandler;
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ProfileCallbackImpl method handleUpdate.
@Override
public void handleUpdate(State state) {
Thing thing = thingProvider.apply(link.getLinkedUID().getThingUID());
if (thing != null) {
final ThingHandler handler = thing.getHandler();
if (handler != null) {
if (ThingHandlerHelper.isHandlerInitialized(thing)) {
logger.debug("Delegating update '{}' for item '{}' to handler for channel '{}'", state, link.getItemName(), link.getLinkedUID());
safeCaller.create(handler, ThingHandler.class).withTimeout(CommunicationManager.THINGHANDLER_EVENT_TIMEOUT).onTimeout(() -> {
logger.warn("Handler for thing '{}' takes more than {}ms for handling an update", handler.getThing().getUID(), CommunicationManager.THINGHANDLER_EVENT_TIMEOUT);
}).build().handleUpdate(link.getLinkedUID(), state);
} else {
logger.debug("Not delegating update '{}' for item '{}' to handler for channel '{}', " + "because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE but was {}).", state, link.getItemName(), link.getLinkedUID(), thing.getStatus());
}
} else {
logger.warn("Cannot delegate update '{}' for item '{}' to handler for channel '{}', " + "because no handler is assigned. Maybe the binding is not installed or not " + "propertly initialized.", state, link.getItemName(), link.getLinkedUID());
}
} else {
logger.warn("Cannot delegate update '{}' for item '{}' to handler for channel '{}', " + "because no thing with the UID '{}' could be found.", state, link.getItemName(), link.getLinkedUID(), link.getLinkedUID().getThingUID());
}
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testCreateChannelBuilder.
@Test
public void testCreateChannelBuilder() throws Exception {
registerThingTypeProvider();
registerChannelTypeProvider();
AtomicReference<ThingHandlerCallback> thc = new AtomicReference<>();
ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {
@Override
public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
return true;
}
@Override
@Nullable
protected ThingHandler createHandler(@NonNull Thing thing) {
ThingHandler mockHandler = mock(ThingHandler.class);
doAnswer(a -> {
thc.set((ThingHandlerCallback) a.getArguments()[0]);
return null;
}).when(mockHandler).setCallback(any(ThingHandlerCallback.class));
when(mockHandler.getThing()).thenReturn(THING);
return mockHandler;
}
};
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
new Thread((Runnable) () -> managedThingProvider.add(THING)).start();
waitForAssert(() -> {
assertNotNull(thc.get());
});
ChannelBuilder channelBuilder = thc.get().createChannelBuilder(new ChannelUID(THING_UID, "test"), CHANNEL_TYPE_UID);
Channel channel = channelBuilder.build();
assertThat(channel.getLabel(), is("Test Label"));
assertThat(channel.getDescription(), is("Test Description"));
assertThat(channel.getAcceptedItemType(), is("Switch"));
assertThat(channel.getDefaultTags().size(), is(1));
assertThat(channel.getDefaultTags().iterator().next(), is("Test Tag"));
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedInitializedThing.
@Test
public void testChildHandlerInitialized_replacedInitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
try {
childHandlerInitializedSemaphore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
try {
thingUpdatedSemapthore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
managedThingProvider.add(bridge);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, bridge.getStatus());
});
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.add(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing.getStatus());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
managedThingProvider.update(thing2);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing2.getStatus());
});
// childHandlerInitialized(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
// ThingHandler.thingUpdated(...) must be called
assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.
@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
ThingUID thingUID = new ThingUID("binding:type:thing");
Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
ThingHandler thingHandler = new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void handleConfigurationUpdate(@NonNull Map<@NonNull String, @NonNull Object> configurationParameters) {
changedParameters = configurationParameters;
}
};
thing.setHandler(thingHandler);
ThingProvider thingProvider = new ThingProvider() {
@Override
public void addProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
}
@Override
public Collection<@NonNull Thing> getAll() {
return Collections.singleton(thing);
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
}
};
registerService(thingProvider);
ThingRegistry thingRegistry = getService(ThingRegistry.class);
Map<String, Object> parameters = new HashMap<>();
parameters.put("param1", "value1");
parameters.put("param2", 1);
thingRegistry.updateConfiguration(thingUID, parameters);
assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Aggregations