use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_modifiedUninitializedThing.
@Test
public void testChildHandlerInitialized_modifiedUninitializedThing() {
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() {
if (getBridge() == null) {
throw new RuntimeException("Fail because of missing bridge");
}
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.UNINITIALIZED, thing.getStatus());
assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
thing.setBridgeUID(bridge.getUID());
managedThingProvider.update(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing.getStatus());
});
// childHandlerInitialized(...) must be called
waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
// thingUpdated(...) is not called
assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedUnitializedThing.
@Test
public void testChildHandlerInitialized_replacedUnitializedThing() {
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() {
if (getBridge() == null) {
throw new RuntimeException("Fail because of missing bridge");
}
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.UNINITIALIZED, thing.getStatus());
assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
});
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(...) must be called
waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
// thingUpdated(...) is not called
assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ZoneTemperatureControlHandler method getDssBridgeHandler.
private synchronized BridgeHandler getDssBridgeHandler() {
if (this.dssBridgeHandler == null) {
Bridge bridge = getBridge();
if (bridge == null) {
logger.debug("Bride cannot be found");
return null;
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof BridgeHandler) {
dssBridgeHandler = (BridgeHandler) handler;
} else {
return null;
}
}
return dssBridgeHandler;
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class BridgeHandler method setStatus.
private void setStatus(ThingStatus status) {
logger.debug("set status to: {}", status);
updateStatus(status);
for (Thing thing : getThing().getThings()) {
ThingHandler handler = thing.getHandler();
if (handler != null) {
handler.bridgeStatusChanged(getThing().getStatusInfo());
}
}
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class Job method scheduleEvent.
/**
* Schedules an {@link EventJob} instance
*
* @param thingUID the Thing UID
* @param astroHandler the {@link ThingHandler} instance
* @param eventAt the {@link Calendar} instance denoting scheduled instant
* @param events the event IDs to schedule
* @param channelId the channel ID
*/
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt, List<String> events, String channelId, boolean configAlreadyApplied) {
boolean thingNull = checkNull(thingUID, "Thing UID is null");
boolean astroHandlerNull = checkNull(astroHandler, "AstroThingHandler is null");
boolean eventAtNull = checkNull(eventAt, "Scheduled Instant is null");
boolean eventsNull = checkNull(events, "Events list is null");
boolean channelIdNull = checkNull(channelId, "Channel ID is null");
if (thingNull || astroHandlerNull || eventAtNull || eventsNull || channelIdNull || events.isEmpty()) {
return;
}
final Calendar instant;
if (!configAlreadyApplied) {
final Channel channel = astroHandler.getThing().getChannel(channelId);
if (channel == null) {
LOGGER.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
return;
}
AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
instant = applyConfig(eventAt, config);
} else {
instant = eventAt;
}
List<Job> jobs = events.stream().map(e -> new EventJob(thingUID, channelId, e)).collect(toList());
schedule(thingUID, astroHandler, new CompositeJob(thingUID, jobs), instant);
}
Aggregations