use of org.eclipse.smarthome.core.thing.ThingRegistry in project smarthome by eclipse.
the class BaseThingHandler method setBundleContext.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setBundleContext(final BundleContext bundleContext) {
this.bundleContext = bundleContext;
thingRegistryServiceTracker = new ServiceTracker(this.bundleContext, ThingRegistry.class.getName(), null) {
@Override
public Object addingService(@Nullable final ServiceReference reference) {
thingRegistry = (ThingRegistry) bundleContext.getService(reference);
return thingRegistry;
}
@Override
public void removedService(@Nullable final ServiceReference reference, @Nullable final Object service) {
synchronized (BaseThingHandler.this) {
thingRegistry = null;
}
}
};
thingRegistryServiceTracker.open();
thingTypeRegistryServiceTracker = new ServiceTracker(this.bundleContext, ThingTypeRegistry.class.getName(), null) {
@Override
public Object addingService(@Nullable final ServiceReference reference) {
thingTypeRegistry = (ThingTypeRegistry) bundleContext.getService(reference);
return thingTypeRegistry;
}
@Override
public void removedService(@Nullable final ServiceReference reference, @Nullable final Object service) {
synchronized (BaseThingHandler.this) {
thingTypeRegistry = null;
}
}
};
thingTypeRegistryServiceTracker.open();
configDescriptionValidatorServiceTracker = new ServiceTracker(this.bundleContext, ConfigDescriptionValidator.class.getName(), null) {
@Override
public Object addingService(@Nullable final ServiceReference reference) {
configDescriptionValidator = (ConfigDescriptionValidator) bundleContext.getService(reference);
return configDescriptionValidator;
}
@Override
public void removedService(@Nullable final ServiceReference reference, @Nullable final Object service) {
synchronized (BaseThingHandler.this) {
configDescriptionValidator = null;
}
}
};
configDescriptionValidatorServiceTracker.open();
}
use of org.eclipse.smarthome.core.thing.ThingRegistry 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())));
}
use of org.eclipse.smarthome.core.thing.ThingRegistry in project smarthome by eclipse.
the class NtpOSGiTest method setUp.
@Before
public void setUp() {
VolatileStorageService volatileStorageService = new VolatileStorageService();
registerService(volatileStorageService);
managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
assertNotNull(managedThingProvider);
thingRegistry = getService(ThingRegistry.class);
assertNotNull(thingRegistry);
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
channelTypeUID = new ChannelTypeUID(NtpBindingConstants.BINDING_ID + ":channelType");
channelTypeProvider = mock(ChannelTypeProvider.class);
when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(new ChannelType(channelTypeUID, false, "Switch", ChannelKind.STATE, "label", null, null, null, null, null, null));
registerService(channelTypeProvider);
}
use of org.eclipse.smarthome.core.thing.ThingRegistry in project smarthome by eclipse.
the class HueLightDiscoveryServiceOSGiTest method setUp.
@Before
public void setUp() {
registerVolatileStorageService();
thingRegistry = getService(ThingRegistry.class, ThingRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
Configuration configuration = new Configuration();
configuration.put(HOST, "1.2.3.4");
configuration.put(USER_NAME, "testUserName");
configuration.put(SERIAL_NUMBER, "testSerialNumber");
hueBridge = (Bridge) thingRegistry.createThingOfType(BRIDGE_THING_TYPE_UID, BRIDGE_THING_UID, null, "Bridge", configuration);
assertThat(hueBridge, is(notNullValue()));
thingRegistry.add(hueBridge);
hueBridgeHandler = getThingHandler(hueBridge, HueBridgeHandler.class);
assertThat(hueBridgeHandler, is(notNullValue()));
discoveryService = getService(DiscoveryService.class, HueLightDiscoveryService.class);
assertThat(discoveryService, is(notNullValue()));
}
use of org.eclipse.smarthome.core.thing.ThingRegistry in project smarthome by eclipse.
the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.
/**
* Assert that item's state description is present.
*/
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
ThingRegistry thingRegistry = getService(ThingRegistry.class);
ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
assertNotNull(thing);
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
linkRegistry.add(link);
//
final Collection<Item> items = itemRegistry.getItems();
assertEquals(false, items.isEmpty());
Item item = itemRegistry.getItem("TestItem");
assertEquals("Number", item.getType());
StateDescription state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.TEN, state.getStep());
assertEquals("%d Peek", state.getPattern());
assertEquals(true, state.isReadOnly());
List<StateOption> opts = state.getOptions();
assertEquals(1, opts.size());
final StateOption opt = opts.get(0);
assertEquals("SOUND", opt.getValue());
assertEquals("My great sound.", opt.getLabel());
item = itemRegistry.getItem("TestItem2");
assertEquals("Number", item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(256), state.getMaximum());
assertEquals(BigDecimal.valueOf(8), state.getStep());
assertEquals("%.0f", state.getPattern());
assertEquals(false, state.isReadOnly());
opts = state.getOptions();
assertEquals(0, opts.size());
item = itemRegistry.getItem("TestItem3");
assertEquals("String", item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertNull(state.getMinimum());
assertNull(state.getMaximum());
assertNull(state.getStep());
assertEquals("%s", state.getPattern());
assertEquals(false, state.isReadOnly());
opts = state.getOptions();
assertEquals(0, opts.size());
item = itemRegistry.getItem("TestItem4");
assertEquals("Color", item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem5");
assertEquals("Dimmer", item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem6");
assertEquals("Switch", item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem7_1");
assertEquals("Number", item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.valueOf(10), state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.valueOf(5), state.getStep());
assertEquals("VALUE %d", state.getPattern());
assertEquals(false, state.isReadOnly());
opts = state.getOptions();
assertNotNull(opts);
assertEquals(2, opts.size());
final StateOption opt0 = opts.get(0);
assertNotNull(opt0);
assertEquals(opt0.getValue(), "value0");
assertEquals(opt0.getLabel(), "label0");
final StateOption opt1 = opts.get(1);
assertNotNull(opt1);
assertEquals(opt1.getValue(), "value1");
assertEquals(opt1.getLabel(), "label1");
item = itemRegistry.getItem("TestItem7_2");
assertEquals("Number", item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.valueOf(1), state.getMinimum());
assertEquals(BigDecimal.valueOf(101), state.getMaximum());
assertEquals(BigDecimal.valueOf(20), state.getStep());
assertEquals("NEW %d Peek", state.getPattern());
assertEquals(true, state.isReadOnly());
opts = state.getOptions();
assertNotNull(opts);
assertEquals(1, opts.size());
final StateOption opt2 = opts.get(0);
assertEquals("SOUND", opt2.getValue());
assertEquals("My great sound.", opt2.getLabel());
}
Aggregations