Search in sources :

Example 1 with Value

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value in project smarthome by eclipse.

the class GenericThingHandler method createChannelState.

/**
 * For every Thing channel there exists a corresponding {@link ChannelState}. It consists of the MQTT state
 * and MQTT command topic, the ChannelUID and a value state.
 *
 * @param channelConfig The channel configuration that contains MQTT state and command topic and multiple other
 *            configurations.
 * @param channelUID The channel UID
 * @param valueState The channel value state
 * @return
 */
protected ChannelState createChannelState(ChannelConfig channelConfig, ChannelUID channelUID, Value valueState) {
    ChannelState state = new ChannelState(channelConfig, channelUID, valueState, this);
    String[] transformations;
    // Incoming value transformations
    transformations = channelConfig.transformationPattern.split("∩");
    Stream.of(transformations).filter(t -> StringUtils.isNotBlank(t)).map(t -> new ChannelStateTransformation(t, transformationServiceProvider)).forEach(t -> state.addTransformation(t));
    // Outgoing value transformations
    transformations = channelConfig.transformationPatternOut.split("∩");
    Stream.of(transformations).filter(t -> StringUtils.isNotBlank(t)).map(t -> new ChannelStateTransformation(t, transformationServiceProvider)).forEach(t -> state.addTransformationOut(t));
    return state;
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) StringUtils(org.apache.commons.lang.StringUtils) MqttChannelStateDescriptionProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TransformationServiceProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider) ChannelConfig(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig) ArrayList(java.util.ArrayList) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelStateUpdateListener(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) ValueFactory(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) ChannelStateTransformation(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateTransformation) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) Stream(java.util.stream.Stream) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) StateDescription(org.eclipse.smarthome.core.types.StateDescription) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ChannelStateTransformation(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateTransformation) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)

Example 2 with Value

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value in project smarthome by eclipse.

the class HomieThingHandlerTests method handleCommandUpdate.

@SuppressWarnings("null")
@Test
public void handleCommandUpdate() {
    // Create mocked homie device tree with one node and one writable property
    Node node = thingHandler.device.createNode("node", spy(new NodeAttributes()));
    doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(node.attributes).unsubscribe();
    node.attributes.name = "testnode";
    Property property = node.createProperty("property", spy(new PropertyAttributes()));
    doReturn(future).when(property.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(property.attributes).unsubscribe();
    property.attributes.name = "testprop";
    property.attributes.datatype = DataTypeEnum.string_;
    property.attributes.settable = true;
    property.attributesReceived();
    node.properties.put(property.propertyID, property);
    thingHandler.device.nodes.put(node.nodeID, node);
    ChannelState channelState = property.getChannelState();
    assertNotNull(channelState);
    // Pretend we called start()
    ChannelStateHelper.setConnection(channelState, connection);
    thingHandler.connection = connection;
    StringType updateValue = new StringType("UPDATE");
    thingHandler.handleCommand(property.channelUID, updateValue);
    assertThat(property.getChannelState().getCache().getChannelState().toString(), is("UPDATE"));
    verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
    // Check non writable property
    property.attributes.settable = false;
    property.attributesReceived();
    // Assign old value
    Value value = property.getChannelState().getCache();
    Command command = TypeParser.parseCommand(value.getSupportedCommandTypes(), "OLDVALUE");
    property.getChannelState().getCache().update(command);
    // Try to update with new value
    updateValue = new StringType("SOMETHINGNEW");
    thingHandler.handleCommand(property.channelUID, updateValue);
    // Expect old value and no MQTT publish
    assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE"));
    verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) StringType(org.eclipse.smarthome.core.library.types.StringType) Command(org.eclipse.smarthome.core.types.Command) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) Test(org.junit.Test)

Example 3 with Value

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value in project smarthome by eclipse.

the class GenericThingHandler method initialize.

@Override
public void initialize() {
    List<ChannelUID> configErrors = new ArrayList<>();
    for (Channel channel : thing.getChannels()) {
        final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
        if (channelTypeUID == null) {
            logger.warn("Channel {} has no type", channel.getLabel());
            continue;
        }
        final ChannelConfig channelConfig = channel.getConfiguration().as(ChannelConfig.class);
        try {
            Value value = ValueFactory.createValueState(channelConfig, channelTypeUID.getId());
            ChannelState channelState = createChannelState(channelConfig, channel.getUID(), value);
            channelStateByChannelUID.put(channel.getUID(), channelState);
            StateDescription description = value.createStateDescription(channelConfig.unit, StringUtils.isBlank(channelConfig.commandTopic));
            stateDescProvider.setDescription(channel.getUID(), description);
        } catch (IllegalArgumentException e) {
            logger.warn("Channel configuration error", e);
            configErrors.add(channel.getUID());
        }
    }
    // in question to the user.
    if (configErrors.isEmpty()) {
        super.initialize();
    } else {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Remove and recreate: " + configErrors.stream().map(e -> e.getAsString()).collect(Collectors.joining(",")));
    }
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) StringUtils(org.apache.commons.lang.StringUtils) MqttChannelStateDescriptionProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TransformationServiceProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider) ChannelConfig(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig) ArrayList(java.util.ArrayList) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelStateUpdateListener(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) ValueFactory(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) ChannelStateTransformation(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateTransformation) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) Stream(java.util.stream.Stream) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) StateDescription(org.eclipse.smarthome.core.types.StateDescription) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ChannelConfig(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel) ArrayList(java.util.ArrayList) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 4 with Value

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value in project smarthome by eclipse.

the class Property method createChannelFromAttribute.

public void createChannelFromAttribute() {
    final String commandTopic = topic + "/set";
    final String stateTopic = topic;
    Value value;
    Boolean isDecimal = null;
    switch(attributes.datatype) {
        case boolean_:
            value = new OnOffValue("true", "false");
            break;
        case color_:
            value = new ColorValue(attributes.format.contains("rgb"), null, null, 100);
            break;
        case enum_:
            String[] enumValues = attributes.format.split(",");
            value = new TextValue(enumValues);
            break;
        case float_:
        case integer_:
            isDecimal = attributes.datatype == DataTypeEnum.float_;
            String[] s = attributes.format.split("\\:");
            BigDecimal min = s.length == 2 ? convertFromString(s[0]) : null;
            BigDecimal max = s.length == 2 ? convertFromString(s[1]) : null;
            BigDecimal step = (min != null && max != null) ? max.subtract(min).divide(new BigDecimal(100.0), new MathContext(isDecimal ? 2 : 0)) : null;
            if (step != null && !isDecimal && step.intValue() <= 0) {
                step = new BigDecimal(1);
            }
            value = new NumberValue(min, max, step);
            break;
        case string_:
        case unknown:
        default:
            value = new TextValue();
            break;
    }
    ChannelConfigBuilder b = ChannelConfigBuilder.create().makeTrigger(!attributes.retained).withStateTopic(stateTopic);
    if (isDecimal != null && !isDecimal) {
        // Apply formatter to only publish integers
        b = b.withFormatter("%d");
    }
    if (attributes.settable) {
        b = b.withCommandTopic(commandTopic);
    }
    final ChannelState channelState = new ChannelState(b.build(), channelUID, value, callback);
    this.channelState = channelState;
    final ChannelType type = createChannelType(attributes, channelState);
    this.type = type;
    this.channel = ChannelBuilder.create(channelUID, type.getItemType()).withType(type.getUID()).withKind(type.getKind()).withLabel(attributes.name).withConfiguration(new Configuration(attributes.asMap())).build();
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) Configuration(org.eclipse.smarthome.config.core.Configuration) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) ChannelConfigBuilder(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder) NumberValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) NumberValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType)

Aggregations

ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)4 Value (org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 StringUtils (org.apache.commons.lang.StringUtils)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 ChannelConfig (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig)2 ChannelStateTransformation (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateTransformation)2 ChannelStateUpdateListener (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener)2 MqttChannelStateDescriptionProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider)2 TransformationServiceProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider)2