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"));
}
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;
}
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)));
}
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()));
}
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(",")));
}
}
Aggregations