Search in sources :

Example 11 with ChannelType

use of org.eclipse.smarthome.core.thing.type.ChannelType 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 12 with ChannelType

use of org.eclipse.smarthome.core.thing.type.ChannelType in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method registerChannelTypeProvider.

private void registerChannelTypeProvider() throws Exception {
    ChannelType channelType = new ChannelType(CHANNEL_TYPE_UID, false, "Switch", ChannelKind.STATE, "Test Label", "Test Description", "Test Category", Collections.singleton("Test Tag"), null, null, new URI("test:channel"));
    ChannelTypeProvider mockChannelTypeProvider = mock(ChannelTypeProvider.class);
    when(mockChannelTypeProvider.getChannelType(eq(CHANNEL_TYPE_UID), any())).thenReturn(channelType);
    registerService(mockChannelTypeProvider);
}
Also used : ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) URI(java.net.URI)

Example 13 with ChannelType

use of org.eclipse.smarthome.core.thing.type.ChannelType in project smarthome by eclipse.

the class ChannelStateDescriptionProvider method getStateDescription.

@Override
public StateDescription getStateDescription(String itemName, Locale locale) {
    Set<ChannelUID> boundChannels = itemChannelLinkRegistry.getBoundChannels(itemName);
    if (!boundChannels.isEmpty()) {
        ChannelUID channelUID = boundChannels.iterator().next();
        Channel channel = thingRegistry.getChannel(channelUID);
        if (channel != null) {
            StateDescription stateDescription = null;
            ChannelType channelType = thingTypeRegistry.getChannelType(channel, locale);
            if (channelType != null) {
                stateDescription = channelType.getState();
                if ((channelType.getItemType() != null) && ((stateDescription == null) || (stateDescription.getPattern() == null))) {
                    String pattern = null;
                    if (channelType.getItemType().equalsIgnoreCase(CoreItemFactory.STRING)) {
                        pattern = "%s";
                    } else if (channelType.getItemType().startsWith(CoreItemFactory.NUMBER)) {
                        pattern = "%.0f";
                    }
                    if (pattern != null) {
                        logger.trace("Provide a default pattern {} for item {}", pattern, itemName);
                        if (stateDescription == null) {
                            stateDescription = new StateDescription(null, null, null, pattern, false, null);
                        } else {
                            stateDescription = new StateDescription(stateDescription.getMinimum(), stateDescription.getMaximum(), stateDescription.getStep(), pattern, stateDescription.isReadOnly(), stateDescription.getOptions());
                        }
                    }
                }
            }
            StateDescription dynamicStateDescription = getDynamicStateDescription(channel, stateDescription, locale);
            if (dynamicStateDescription != null) {
                return dynamicStateDescription;
            }
            return stateDescription;
        }
    }
    return null;
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 14 with ChannelType

use of org.eclipse.smarthome.core.thing.type.ChannelType in project smarthome by eclipse.

the class ThingConfigDescriptionAliasProvider method getChannelConfigDescriptionURI.

@Nullable
private URI getChannelConfigDescriptionURI(URI uri) {
    String stringUID = uri.getSchemeSpecificPart();
    if (uri.getFragment() != null) {
        stringUID = stringUID + "#" + uri.getFragment();
    }
    ChannelUID channelUID = new ChannelUID(stringUID);
    ThingUID thingUID = channelUID.getThingUID();
    // First, get the thing so we get access to the channel type via the channel
    Thing thing = thingRegistry.get(thingUID);
    if (thing == null) {
        return null;
    }
    Channel channel = thing.getChannel(channelUID.getId());
    if (channel == null) {
        return null;
    }
    ChannelType channelType = channelTypeRegistry.getChannelType(channel.getChannelTypeUID());
    if (channelType == null) {
        return null;
    }
    // Get the config description URI for this channel type
    URI configURI = channelType.getConfigDescriptionURI();
    return configURI;
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) URI(java.net.URI) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 15 with ChannelType

use of org.eclipse.smarthome.core.thing.type.ChannelType in project smarthome by eclipse.

the class DsChannelTypeProvider method getChannelTypes.

@Override
public Collection<ChannelType> getChannelTypes(Locale locale) {
    List<ChannelType> channelTypeList = new LinkedList<ChannelType>();
    for (String channelTypeId : supportedOutputChannelTypes) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, channelTypeId), locale));
    }
    for (SensorEnum sensorType : SensorEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(sensorType)), locale));
    }
    for (MeteringTypeEnum meteringType : MeteringTypeEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(meteringType, MeteringUnitsEnum.WH)), locale));
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(TOTAL_PRE, meteringType, MeteringUnitsEnum.WH)), locale));
    }
    for (DeviceBinarayInputEnum binaryInput : DeviceBinarayInputEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(BINARY_INPUT_PRE, binaryInput)), locale));
    }
    return channelTypeList;
}
Also used : MeteringTypeEnum(org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) SensorEnum(org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum) DeviceBinarayInputEnum(org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.DeviceBinarayInputEnum) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) LinkedList(java.util.LinkedList)

Aggregations

ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)23 ChannelTypeUID (org.eclipse.smarthome.core.thing.type.ChannelTypeUID)9 StateDescription (org.eclipse.smarthome.core.types.StateDescription)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 Bundle (org.osgi.framework.Bundle)5 ChannelDefinition (org.eclipse.smarthome.core.thing.type.ChannelDefinition)4 Locale (java.util.Locale)3 Channel (org.eclipse.smarthome.core.thing.Channel)3 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)3 ChannelGroupType (org.eclipse.smarthome.core.thing.type.ChannelGroupType)3 ChannelTypeProvider (org.eclipse.smarthome.core.thing.type.ChannelTypeProvider)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 URI (java.net.URI)2 Collection (java.util.Collection)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2