Search in sources :

Example 1 with ThingRegistry

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();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) ThingTypeRegistry(org.eclipse.smarthome.core.thing.type.ThingTypeRegistry) ServiceReference(org.osgi.framework.ServiceReference) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry)

Example 2 with ThingRegistry

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())));
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) HashMap(java.util.HashMap) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) HashMap(java.util.HashMap) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ThingRegistry

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);
}
Also used : Locale(java.util.Locale) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) Before(org.junit.Before)

Example 4 with ThingRegistry

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()));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) HueLightDiscoveryService(org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService) HueBridgeHandler(org.eclipse.smarthome.binding.hue.handler.HueBridgeHandler) DiscoveryService(org.eclipse.smarthome.config.discovery.DiscoveryService) HueLightDiscoveryService(org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) Before(org.junit.Before)

Example 5 with ThingRegistry

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());
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) StateOption(org.eclipse.smarthome.core.types.StateOption) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) StateDescription(org.eclipse.smarthome.core.types.StateDescription) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)8 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 Test (org.junit.Test)4 Configuration (org.eclipse.smarthome.config.core.Configuration)3 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)3 Before (org.junit.Before)3 HashMap (java.util.HashMap)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 ThingProvider (org.eclipse.smarthome.core.thing.ThingProvider)2 Locale (java.util.Locale)1 Map (java.util.Map)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 HueBridgeHandler (org.eclipse.smarthome.binding.hue.handler.HueBridgeHandler)1 HueLightDiscoveryService (org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService)1 ConfigDescriptionValidator (org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator)1 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)1 Inbox (org.eclipse.smarthome.config.discovery.inbox.Inbox)1