Search in sources :

Example 1 with ChannelConfig

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

the class GenericThingHandlerTests method processMessage.

@Test
public void processMessage() {
    TextValue textValue = new TextValue();
    ChannelState channelConfig = spy(new ChannelState(ChannelConfigBuilder.create("test/state", "test/state/set").build(), textChannelUID, textValue, thingHandler));
    doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
    thingHandler.initialize();
    byte[] payload = "UPDATE".getBytes();
    // Test process message
    channelConfig.processMessage("test/state", payload);
    verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
    verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "UPDATE".equals(arg.toString())));
    assertThat(textValue.getChannelState().toString(), is("UPDATE"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) MqttChannelStateDescriptionProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) 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) Assert.assertThat(org.junit.Assert.assertThat) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) MockitoAnnotations(org.mockito.MockitoAnnotations) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelConfigBuilder(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder) StringType(org.eclipse.smarthome.core.library.types.StringType) Configuration(org.eclipse.smarthome.config.core.Configuration) Before(org.junit.Before) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) ThingChannelConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants) ValueFactory(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) RefreshType(org.eclipse.smarthome.core.types.RefreshType) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Test(org.junit.Test) Mockito(org.mockito.Mockito) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) Test(org.junit.Test)

Example 2 with ChannelConfig

use of org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig 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 3 with ChannelConfig

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

the class GenericThingHandlerTests method initialize.

@Test
public void initialize() {
    thingHandler.initialize();
    verify(thingHandler).bridgeStatusChanged(any());
    verify(thingHandler).start(any());
    assertThat(thingHandler.connection, is(connection));
    ChannelState channelConfig = thingHandler.channelStateByChannelUID.get(textChannelUID);
    assertThat(channelConfig.getStateTopic(), is("test/state"));
    assertThat(channelConfig.getCommandTopic(), is("test/command"));
    verify(connection).subscribe(eq(channelConfig.getStateTopic()), eq(channelConfig));
    verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.ONLINE) && arg.getStatusDetail().equals(ThingStatusDetail.NONE)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) MqttChannelStateDescriptionProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) 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) Assert.assertThat(org.junit.Assert.assertThat) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) MockitoAnnotations(org.mockito.MockitoAnnotations) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelConfigBuilder(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder) StringType(org.eclipse.smarthome.core.library.types.StringType) Configuration(org.eclipse.smarthome.config.core.Configuration) Before(org.junit.Before) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) ThingChannelConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants) ValueFactory(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) RefreshType(org.eclipse.smarthome.core.types.RefreshType) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Test(org.junit.Test) Mockito(org.mockito.Mockito) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) Test(org.junit.Test)

Example 4 with ChannelConfig

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

the class GenericThingHandlerTests method initializeWithUnknownThingUID.

@Test(expected = IllegalArgumentException.class)
public void initializeWithUnknownThingUID() {
    ChannelConfig config = textConfiguration().as(ChannelConfig.class);
    thingHandler.createChannelState(config, new ChannelUID(testGenericThing, "test"), ValueFactory.createValueState(config, unknownChannel.getId()));
}
Also used : ChannelConfig(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Test(org.junit.Test)

Example 5 with ChannelConfig

use of org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig 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)

Aggregations

ChannelConfig (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig)5 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)4 MqttChannelStateDescriptionProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider)4 TransformationServiceProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider)4 ValueFactory (org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory)4 Thing (org.eclipse.smarthome.core.thing.Thing)4 ThingStatus (org.eclipse.smarthome.core.thing.ThingStatus)4 ThingStatusDetail (org.eclipse.smarthome.core.thing.ThingStatusDetail)4 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)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