use of org.eclipse.smarthome.core.thing.ChannelUID in project smarthome by eclipse.
the class DmxChannel method getNewHiResValue.
/**
* Get the new value for this channel as determined by active actions or the
* current value.
*
* @param calculationTime UNIX timestamp
* @return value 0-65535
*/
public synchronized Integer getNewHiResValue(long calculationTime) {
if (hasRunningActions()) {
logger.trace("checking actions, list is {}", actions);
BaseAction action = actions.get(0);
value = action.getNewValue(this, calculationTime);
if (action.getState() == ActionState.COMPLETED && hasRunningActions()) {
switchToNextAction();
} else if (action.getState() == ActionState.COMPLETEDFINAL) {
clearAction();
}
}
// send updates not more than once in a second, and only on value change
if ((lastStateValue != value) && (calculationTime - lastStateTimestamp > refreshTime)) {
// notify value listeners if value changed
for (Entry<ChannelUID, DmxThingHandler> listener : valueListeners.entrySet()) {
int dmxValue = Util.toDmxValue(value >> 8);
(listener.getValue()).updateChannelValue(listener.getKey(), dmxValue);
logger.trace("sending VALUE={} (raw={}) status update to listener {} ({})", dmxValue, value, listener.getValue(), listener.getKey());
}
// notify on/off listeners if on/off state changed
if ((lastStateValue == 0) || (value == 0)) {
OnOffType state = (value == 0) ? OnOffType.OFF : OnOffType.ON;
for (Entry<ChannelUID, DmxThingHandler> listener : onOffListeners.entrySet()) {
(listener.getValue()).updateSwitchState(listener.getKey(), state);
logger.trace("sending ONOFF={} (raw={}), status update to listener {}", state, value, listener.getKey());
}
}
lastStateValue = value;
lastStateTimestamp = calculationTime;
}
return value;
}
use of org.eclipse.smarthome.core.thing.ChannelUID 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.ChannelUID 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.ChannelUID in project smarthome by eclipse.
the class ThingBuilderTest method testWithoutChannel_missing.
@Test
public void testWithoutChannel_missing() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
//
thingBuilder.withChannels(//
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), ChannelBuilder.create(new ChannelUID(THING_UID, "channel2"), "").build());
thingBuilder.withoutChannel(new ChannelUID(THING_UID, "channel3"));
assertThat(thingBuilder.build().getChannels().size(), is(equalTo(2)));
}
use of org.eclipse.smarthome.core.thing.ChannelUID in project smarthome by eclipse.
the class ThingBuilderTest method testWithChannel_duplicates.
@Test(expected = IllegalArgumentException.class)
public void testWithChannel_duplicates() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
}
Aggregations