Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Merge branch 'channel-xml-extensions' of https://github.com/qivicon/s…
Browse files Browse the repository at this point in the history
…marthome into qivicon-channel-xml-extensions

Conflicts:
	bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/ChannelUID.java
	bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/UID.java
	bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/type/ChannelTypeUID.java

Also-by: Alex Tugarev <telekom@tugarev.de>
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer committed Jan 6, 2015
2 parents 01bb10a + a3f9495 commit f7953f2
Show file tree
Hide file tree
Showing 54 changed files with 2,252 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
<description>The color channel allows to control the color of a light.
It is also possible to dim values and switch the light on and off.
</description>

<config-description>
<parameter name="lastDimValue" type="boolean" />
</config-description>
<category>ColorLight</category>
</channel-type>

<!-- Brightness Channel -->
Expand All @@ -24,10 +21,7 @@
<description>The brightness channel allows to control the brightness of a light.
It is also possible to switch the light on and off.
</description>

<config-description>
<parameter name="lastDimValue" type="boolean" />
</config-description>
<category>DimmableLight</category>
</channel-type>

<!-- Color Temperature Channel -->
Expand All @@ -36,6 +30,7 @@
<label>Color Temperature</label>
<description>The color temperature channel allows to set the color
temperature of a light from 0 (cold) to 100 (warm).</description>
<category>ColorLight</category>
</channel-type>

</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@
<item-type>Number</item-type>
<label>Temperature</label>
<description>Current temperature in degrees celsius (metric) or fahrenheit (us)</description>
<category>Temperature</category>
<state readOnly="true">
</state>
</channel-type>

<channel-type id="humidity">
<item-type>Number</item-type>
<label>Humidity</label>
<description>Current humidity in %</description>
<category>Humidity</category>
<state readOnly="true" pattern="%d %%">
</state>
</channel-type>

<channel-type id="pressure">
<item-type>Number</item-type>
<label>Pressure</label>
<description>Current pressure in millibar (metric) or inches of mercury (us)</description>
<category>Pressure</category>
<state readOnly="true">
</state>
</channel-type>
</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.eclipse.smarthome.config.xml;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -18,6 +19,7 @@
import org.eclipse.smarthome.config.xml.util.ConverterAttributeMapValidator;
import org.eclipse.smarthome.config.xml.util.ConverterValueMap;
import org.eclipse.smarthome.config.xml.util.GenericUnmarshaller;
import org.eclipse.smarthome.config.xml.util.NodeValue;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
Expand Down Expand Up @@ -115,8 +117,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
String description = valueMap.getString("description");

// read options and filter criteria
@SuppressWarnings("unchecked")
List<ParameterOption> options = (List<ParameterOption>) valueMap.getObject("options");
List<ParameterOption> options = readParameterOptions(valueMap.getObject("options"));
@SuppressWarnings("unchecked")
List<FilterCriteria> filterCriteria = (List<FilterCriteria>) valueMap.getObject("filter");

Expand All @@ -128,4 +129,21 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
return configDescriptionParam;
}

private List<ParameterOption> readParameterOptions(Object rawNodeValueList) {
if (rawNodeValueList instanceof List<?>) {
List<?> list = (List<?>) rawNodeValueList;
List<ParameterOption> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof NodeValue) {
NodeValue nodeValue = (NodeValue) object;
String value = nodeValue.getAttributes().get("value");
String label = nodeValue.getValue().toString();
result.add(new ParameterOption(value, label));
}
}
return result;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import org.eclipse.smarthome.config.xml.ConfigDescriptionConverter;
import org.eclipse.smarthome.config.xml.ConfigDescriptionParameterConverter;
import org.eclipse.smarthome.config.xml.FilterCriteriaConverter;
import org.eclipse.smarthome.config.xml.ParameterOptionConverter;
import org.eclipse.smarthome.config.xml.util.NodeAttributes;
import org.eclipse.smarthome.config.xml.util.NodeAttributesConverter;
import org.eclipse.smarthome.config.xml.util.NodeList;
import org.eclipse.smarthome.config.xml.util.NodeListConverter;
import org.eclipse.smarthome.config.xml.util.NodeValue;
import org.eclipse.smarthome.config.xml.util.NodeValueConverter;
import org.eclipse.smarthome.config.xml.util.XmlDocumentReader;

import com.thoughtworks.xstream.XStream;
Expand All @@ -45,10 +48,11 @@ public ConfigDescriptionReader() {

@Override
public void registerConverters(XStream xstream) {
xstream.registerConverter(new NodeValueConverter());
xstream.registerConverter(new NodeListConverter());
xstream.registerConverter(new NodeAttributesConverter());
xstream.registerConverter(new ConfigDescriptionConverter());
xstream.registerConverter(new ConfigDescriptionParameterConverter());
xstream.registerConverter(new ParameterOptionConverter());
xstream.registerConverter(new FilterCriteriaConverter());
}

Expand All @@ -58,8 +62,8 @@ public void registerAliases(XStream xstream) {
xstream.alias("config-description", ConfigDescription.class);
xstream.alias("config-description-ref", NodeAttributes.class);
xstream.alias("parameter", ConfigDescriptionParameter.class);
xstream.alias("options", List.class);
xstream.alias("option", ParameterOption.class);
xstream.alias("options", NodeList.class);
xstream.alias("option", NodeValue.class);
xstream.alias("filter", List.class);
xstream.alias("criteria", FilterCriteria.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public NodeAttributesConverter() {
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String nodeName = reader.getNodeName();
Map<String, String> attributes = ConverterAttributeMapValidator.readValidatedAttributes(
reader, null);

Map<String, String> attributes =
ConverterAttributeMapValidator.readValidatedAttributes(reader, null);

return new NodeAttributes(nodeName, attributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package org.eclipse.smarthome.config.xml.util;

import java.util.Map;


/**
* The {@link NodeValue} class contains the node name and its according value for an XML tag.
Expand All @@ -21,23 +23,29 @@
public class NodeValue implements NodeName {

private String nodeName;
private Map<String, String> attributes;
private Object value;


/**
* Creates a new instance of this class with the specified parameters.
*
* @param nodeName the name of the node this object belongs to (must neither be null, nor empty)
* @param attributes the attributes of the node this object belongs to (could be null or empty)
* @param value the value of the node this object belongs to (could be null or empty)
*
* @throws IllegalArgumentException if the name of the node is null or empty
*/
public NodeValue(String nodeName, Object value) throws IllegalArgumentException {
public NodeValue(String nodeName, Map<String, String> attributes, Object value)
throws IllegalArgumentException {

if ((nodeName == null) || (nodeName.isEmpty())) {
throw new IllegalArgumentException(
"The name of the node must neither be null nor empty!");
}

this.nodeName = nodeName;
this.attributes = attributes;
this.value = formatText(value);
}

Expand All @@ -56,9 +64,18 @@ public String getNodeName() {
}

/**
* Returns the value of the node
* Returns the attributes of the node.
*
* @return the attributes of the node (could be null or empty)
*/
public Map<String, String> getAttributes() {
return this.attributes;
}

/**
* Returns the value of the node.
*
* @return the value of the node (could be null or empty).
* @return the value of the node (could be null or empty)
*/
public Object getValue() {
return this.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package org.eclipse.smarthome.config.xml.util;

import java.util.Map;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
Expand All @@ -27,7 +29,10 @@ public NodeValueConverter() {

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return new NodeValue(reader.getNodeName(), reader.getValue());
Map<String, String> attributes =
ConverterAttributeMapValidator.readValidatedAttributes(reader, null);

return new NodeValue(reader.getNodeName(), attributes, reader.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.FilterCriteria;
import org.eclipse.smarthome.config.core.ParameterOption;
import org.eclipse.smarthome.config.xml.ConfigDescriptionConverter;
import org.eclipse.smarthome.config.xml.ConfigDescriptionParameterConverter;
import org.eclipse.smarthome.config.xml.FilterCriteriaConverter;
import org.eclipse.smarthome.config.xml.ParameterOptionConverter;
import org.eclipse.smarthome.config.xml.util.NodeAttributes;
import org.eclipse.smarthome.config.xml.util.NodeAttributesConverter;
import org.eclipse.smarthome.config.xml.util.NodeList;
import org.eclipse.smarthome.config.xml.util.NodeValue;
import org.eclipse.smarthome.config.xml.util.NodeValueConverter;
import org.eclipse.smarthome.config.xml.util.XmlDocumentReader;
Expand Down Expand Up @@ -51,7 +50,6 @@ public void registerConverters(XStream xstream) {
xstream.registerConverter(new BindingInfoConverter());
xstream.registerConverter(new ConfigDescriptionConverter());
xstream.registerConverter(new ConfigDescriptionParameterConverter());
xstream.registerConverter(new ParameterOptionConverter());
xstream.registerConverter(new FilterCriteriaConverter());
}

Expand All @@ -64,8 +62,8 @@ public void registerAliases(XStream xstream) {
xstream.alias("config-description", ConfigDescription.class);
xstream.alias("config-description-ref", NodeAttributes.class);
xstream.alias("parameter", ConfigDescriptionParameter.class);
xstream.alias("options", List.class);
xstream.alias("option", ParameterOption.class);
xstream.alias("options", NodeList.class);
xstream.alias("option", NodeValue.class);
xstream.alias("filter", List.class);
xstream.alias("criteria", FilterCriteria.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ class UIDTest {
new ThingUID("binding:type:ID")
new ThingUID("00:type:ID")
}

@Test
void 'channel UID with group'() {
def channelUID = new ChannelUID("binding", "thing-type", "thing", "group", "id")
assertThat channelUID.toString(), is(equalTo("binding:thing-type:thing:group#id"))
assertThat channelUID.isInGroup(), is(true)
assertThat channelUID.getId(), is("group#id")
assertThat channelUID.getIdWithoutGroup(), is("id")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import org.eclipse.smarthome.core.thing.ThingTypeUID
import org.eclipse.smarthome.core.thing.ThingUID
import org.eclipse.smarthome.core.thing.type.BridgeType
import org.eclipse.smarthome.core.thing.type.ChannelDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupType
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID
import org.eclipse.smarthome.core.thing.type.ChannelType
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID
import org.eclipse.smarthome.core.thing.type.ThingType
import org.junit.Test


/**
* ThingFactoryTest is a test for the ThingFactory class.
*
Expand Down Expand Up @@ -74,8 +76,8 @@ class ThingFactoryTest {

private List getChannelDefinitions(){
List channelDefinitions = new ArrayList<ChannelDefinition>()
def cd1 = new ChannelDefinition("channel1", new ChannelType(new ChannelTypeUID("channel:cd1"), "itemType", "channelLabel", "description", new HashSet<String>(), new URI("scheme", "channelType:cd1", null)))
def cd2 = new ChannelDefinition("channel2", new ChannelType(new ChannelTypeUID("channel:cd2"), "itemType2", "label2", "description22222", new HashSet<String>(), new URI("scheme", "channelType:cd2",null)))
def cd1 = new ChannelDefinition("channel1", new ChannelType(new ChannelTypeUID("channel:cd1"), false, "itemType", "channelLabel", "description", "category", new HashSet<String>(), null, new URI("scheme", "channelType:cd1", null)))
def cd2 = new ChannelDefinition("channel2", new ChannelType(new ChannelTypeUID("channel:cd2"), false, "itemType2", "label2", "description22222", "category", new HashSet<String>(), null, new URI("scheme", "channelType:cd2",null)))
channelDefinitions.add(cd1)
channelDefinitions.add(cd2)
return channelDefinitions;
Expand All @@ -84,7 +86,7 @@ class ThingFactoryTest {

@Test
void 'create Thing with Default values'(){
def thingType = new ThingType(new ThingTypeUID("myThingType","myThing"), null, "label", "description", getChannelDefinitions(), new URI("scheme", "thingType", null))
def thingType = new ThingType(new ThingTypeUID("myThingType","myThing"), null, "label", "description", getChannelDefinitions(), null, new URI("scheme", "thingType", null))
def configuration = new Configuration()

def configDescriptionRegistry = new ConfigDescriptionRegistry() {
Expand All @@ -108,7 +110,7 @@ class ThingFactoryTest {

@Test
void 'create Thing with different default value types'(){
def thingType = new ThingType(new ThingTypeUID("myThingType","myThing"), null, "label", "description", null, new URI("scheme", "thingType", null))
def thingType = new ThingType(new ThingTypeUID("myThingType","myThing"), null, "label", "description", null, null, new URI("scheme", "thingType", null))
def configuration = new Configuration()

def configDescriptionRegistry = new ConfigDescriptionRegistry() {
Expand Down Expand Up @@ -138,13 +140,13 @@ class ThingFactoryTest {
@Test
void 'create Thing with Channels'() {

ChannelType channelType1 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId1"), "Color", "label", "description", new HashSet([ "tag1", "tag2" ]), null)
ChannelType channelType2 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId2"), "Dimmer", "label", "description", new HashSet([ "tag3" ]), null)
ChannelType channelType1 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId1"), false, "Color", "label", "description", "category", new HashSet([ "tag1", "tag2" ]), null, null)
ChannelType channelType2 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId2"), false, "Dimmer", "label", "description", "category", new HashSet([ "tag3" ]), null, null)

ChannelDefinition channelDef1 = new ChannelDefinition("ch1", channelType1)
ChannelDefinition channelDef2 = new ChannelDefinition("ch2", channelType2)

def thingType = new ThingType(new ThingTypeUID("bindingId:thingType"), [], "label", null, [channelDef1, channelDef2], null)
def thingType = new ThingType(new ThingTypeUID("bindingId:thingType"), [], "label", null, [channelDef1, channelDef2], null, null)
def configuration = new Configuration();

def thing = ThingFactory.createThing(thingType, new ThingUID(thingType.getUID(), "thingId"), configuration)
Expand All @@ -159,4 +161,30 @@ class ThingFactoryTest {
assertThat thing.getChannels().get(1).getDefaultTags().contains("tag2"), is(false)
assertThat thing.getChannels().get(1).getDefaultTags().contains("tag3"), is(true)
}

@Test
void 'create Thing with channels groups'() {

ChannelType channelType1 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId1"), false, "Color", "label", "description", "category", new HashSet([ "tag1", "tag2" ]), null, null)
ChannelType channelType2 = new ChannelType(new ChannelTypeUID("bindingId:channelTypeId2"), false, "Dimmer", "label", "description", "category", new HashSet([ "tag3" ]), null, null)

ChannelDefinition channelDef1 = new ChannelDefinition("ch1", channelType1)
ChannelDefinition channelDef2 = new ChannelDefinition("ch2", channelType2)

ChannelGroupType channelGroupType1 = new ChannelGroupType(new ChannelGroupTypeUID("bindingid:groupTypeId1"), false, "label", "description", [channelDef1, channelDef2])
ChannelGroupType channelGroupType2 = new ChannelGroupType(new ChannelGroupTypeUID("bindingid:groupTypeId2"), false, "label", "description", [channelDef1])

ChannelGroupDefinition channelGroupDef1 = new ChannelGroupDefinition("group1", channelGroupType1)
ChannelGroupDefinition channelGroupDef2 = new ChannelGroupDefinition("group2", channelGroupType2)

def thingType = new ThingType(new ThingTypeUID("bindingId:thingType"), [], "label", null, null, [channelGroupDef1, channelGroupDef2], null)
def configuration = new Configuration();

def thing = ThingFactory.createThing(thingType, new ThingUID(thingType.getUID(), "thingId"), configuration)

assertThat thing.getChannels().size, is(3)
assertThat thing.getChannels().get(0).getUID().toString(), is(equalTo("bindingId:thingType:thingId:group1#ch1"))
assertThat thing.getChannels().get(1).getUID().toString(), is(equalTo("bindingId:thingType:thingId:group1#ch2"))
assertThat thing.getChannels().get(2).getUID().toString(), is(equalTo("bindingId:thingType:thingId:group2#ch1"))
}
}
Loading

0 comments on commit f7953f2

Please sign in to comment.