Search in sources :

Example 6 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class SayCommandTest method testSayCommand.

@Test
public void testSayCommand() throws IOException {
    String[] methodParameters = new String[2];
    methodParameters[0] = SUBCMD_SAY;
    if (defaultTTSService != null) {
        Dictionary<String, Object> config = new Hashtable<String, Object>();
        config.put(CONFIG_DEFAULT_TTS, defaultTTSService);
        ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
        String pid = "org.eclipse.smarthome.voice";
        Configuration configuration = configAdmin.getConfiguration(pid);
        configuration.update(config);
    }
    if (TTSServiceMockShouldBeRegistered) {
        registerService(ttsService);
    }
    if (shouldItemsBePassed) {
        VolatileStorageService volatileStorageService = new VolatileStorageService();
        registerService(volatileStorageService);
        ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
        assertNotNull(managedThingProvider);
        ItemRegistry itemRegistry = getService(ItemRegistry.class);
        assertNotNull(itemRegistry);
        Item item = new StringItem("itemName");
        if (shouldItemsBeRegistered) {
            itemRegistry.add(item);
        }
        methodParameters[1] = "%" + item.getName() + "%";
        if (shouldMultipleItemsBeRegistered) {
            Item item1 = new StringItem("itemName1");
            itemRegistry.add(item1);
            Item item2 = new StringItem("itemName2");
            itemRegistry.add(item2);
            Item item3 = new StringItem("itemName3");
            itemRegistry.add(item3);
            methodParameters[1] = "%itemName.%";
        }
    } else {
        methodParameters[1] = "hello";
    }
    extensionService.execute(methodParameters, console);
    assertThat(sink.isStreamProcessed(), is(shouldStreamBeExpected));
}
Also used : Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) StringItem(org.eclipse.smarthome.core.library.items.StringItem) Test(org.junit.Test)

Example 7 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class ChannelStateDescriptionProviderOSGiTest method setup.

@Before
public void setup() {
    initMocks(this);
    Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContext);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescription state = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(100), BigDecimal.TEN, "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
    final StateDescription state2 = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(256), BigDecimal.valueOf(8), null, false, null);
    final ChannelType channelType = new ChannelType(new ChannelTypeUID("hue:alarm"), false, "Number", " ", "", null, null, state, null);
    final ChannelType channelType2 = new ChannelType(new ChannelTypeUID("hue:num"), false, "Number", " ", "", null, null, state2, null);
    final ChannelType channelType3 = new ChannelType(new ChannelTypeUID("hue:info"), true, "String", " ", "", null, null, null, null);
    final ChannelType channelType4 = new ChannelType(new ChannelTypeUID("hue:color"), false, "Color", "Color", "", "ColorLight", null, null, null);
    final ChannelType channelType5 = new ChannelType(new ChannelTypeUID("hue:brightness"), false, "Dimmer", "Brightness", "", "DimmableLight", null, null, null);
    final ChannelType channelType6 = new ChannelType(new ChannelTypeUID("hue:switch"), false, "Switch", "Switch", "", "Light", null, null, null);
    final ChannelType channelType7 = new ChannelType(new ChannelTypeUID("hue:num-dynamic"), false, "Number", " ", "", "Light", null, state, null);
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    channelTypes.add(channelType4);
    channelTypes.add(channelType5);
    channelTypes.add(channelType6);
    channelTypes.add(channelType7);
    registerService(new ChannelTypeProvider() {

        @Override
        public Collection<ChannelType> getChannelTypes(Locale locale) {
            return channelTypes;
        }

        @Override
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }

        @Override
        public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
            return null;
        }

        @Override
        public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
            return Collections.emptySet();
        }
    });
    registerService(new DynamicStateDescriptionProvider() {

        final StateDescription newState = new StateDescription(BigDecimal.valueOf(10), BigDecimal.valueOf(100), BigDecimal.valueOf(5), "VALUE %d", false, Arrays.asList(new StateOption("value0", "label0"), new StateOption("value1", "label1")));

        @Override
        @Nullable
        public StateDescription getStateDescription(@NonNull Channel channel, @Nullable StateDescription original, @Nullable Locale locale) {
            String id = channel.getUID().getIdWithoutGroup();
            if ("7_1".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                return newState;
            } else if ("7_2".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                StateDescription newState2 = new StateDescription(original.getMinimum().add(BigDecimal.ONE), original.getMaximum().add(BigDecimal.ONE), original.getStep().add(BigDecimal.TEN), "NEW " + original.getPattern(), true, original.getOptions());
                return newState2;
            }
            return null;
        }
    });
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinition("1", channelType.getUID()));
    channelDefinitions.add(new ChannelDefinition("2", channelType2.getUID()));
    channelDefinitions.add(new ChannelDefinition("3", channelType3.getUID()));
    channelDefinitions.add(new ChannelDefinition("4", channelType4.getUID()));
    channelDefinitions.add(new ChannelDefinition("5", channelType5.getUID()));
    channelDefinitions.add(new ChannelDefinition("6", channelType6.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_1", channelType7.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_2", channelType7.getUID()));
    registerService(new SimpleThingTypeProvider(Collections.singleton(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem"));
    items.add(new NumberItem("TestItem2"));
    items.add(new StringItem("TestItem3"));
    items.add(new ColorItem("TestItem4"));
    items.add(new DimmerItem("TestItem5"));
    items.add(new SwitchItem("TestItem6"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
    stateDescriptionProvider = getService(StateDescriptionProvider.class);
    assertNotNull(stateDescriptionProvider);
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) ChannelGroupType(org.eclipse.smarthome.core.thing.type.ChannelGroupType) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) 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) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelGroupTypeUID(org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) StringItem(org.eclipse.smarthome.core.library.items.StringItem) StateOption(org.eclipse.smarthome.core.types.StateOption) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ChannelDefinition(org.eclipse.smarthome.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescriptionProvider(org.eclipse.smarthome.core.types.StateDescriptionProvider) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Before(org.junit.Before)

Example 8 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method setUp.

@Before
public void setUp() throws Exception {
    CONFIG_DESCRIPTION_THING = new URI("test:test");
    CONFIG_DESCRIPTION_CHANNEL = new URI("test:channel");
    THING = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(// 
    Collections.singletonList(// 
    ChannelBuilder.create(CHANNEL_UID, "Switch").withType(CHANNEL_TYPE_UID).build())).build();
    registerVolatileStorageService();
    configureAutoLinking(false);
    managedThingProvider = getService(ManagedThingProvider.class);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
    assertNotNull(itemChannelLinkRegistry);
    readyService = getService(ReadyService.class);
    assertNotNull(readyService);
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ReadyMarker.class, "(esh.xmlThingTypes=" + bundleContext.getBundle().getSymbolicName() + ")"), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            throw new RuntimeException(e);
        }
    });
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ChannelItemProvider.class, null), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ReadyService(org.eclipse.smarthome.core.service.ReadyService) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ReadyMarker(org.eclipse.smarthome.core.service.ReadyMarker) URI(java.net.URI) Before(org.junit.Before)

Example 9 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class BusEvent method postUpdate.

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public static Object postUpdate(String itemName, String stateString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
            if (state != null) {
                publisher.post(ItemEventFactory.createStateEvent(itemName, state));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName, getAcceptedDataTypeNames(item));
            }
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) State(org.eclipse.smarthome.core.types.State) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)9 Item (org.eclipse.smarthome.core.items.Item)6 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)5 Before (org.junit.Before)4 Test (org.junit.Test)4 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)3 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Collection (java.util.Collection)2 Locale (java.util.Locale)2 Rule (org.eclipse.smarthome.automation.Rule)2 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 StringItem (org.eclipse.smarthome.core.library.items.StringItem)2 ItemChannelLinkRegistry (org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry)2 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)2 ChannelTypeProvider (org.eclipse.smarthome.core.thing.type.ChannelTypeProvider)2 VolatileStorageService (org.eclipse.smarthome.test.storage.VolatileStorageService)2