Search in sources :

Example 1 with DelayedBatchProcessing

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

the class HomieThingHandlerTests method setUp.

@Before
public void setUp() {
    final ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
    MockitoAnnotations.initMocks(this);
    final Configuration config = new Configuration();
    config.put("basetopic", "homie");
    config.put("deviceid", deviceID);
    thing = ThingBuilder.create(MqttBindingConstants.HOMIE300_MQTT_THING, testHomieThing.getId()).withConfiguration(config).build();
    thing.setStatusInfo(thingStatus);
    // Return the mocked connection object if the bridge handler is asked for it
    when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
    doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribeAll();
    doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(), anyBoolean());
    doReturn(false).when(scheduledFuture).isDone();
    doReturn(scheduledFuture).when(scheduler).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    final HomieThingHandler handler = new HomieThingHandler(thing, channelTypeProvider, 30, 5);
    thingHandler = spy(handler);
    thingHandler.setCallback(callback);
    final Device device = new Device(thing.getUID(), thingHandler, spy(new DeviceAttributes()), spy(new ChildMap<>()));
    thingHandler.setInternalObjects(spy(device), spy(new DelayedBatchProcessing<Object>(500, thingHandler, scheduler)));
    // Return the bridge handler if the thing handler asks for it
    doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
    // We are by default online
    doReturn(thingStatus).when(thingHandler).getBridgeStatus();
}
Also used : DelayedBatchProcessing(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing) Configuration(org.eclipse.smarthome.config.core.Configuration) ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) TimeUnit(java.util.concurrent.TimeUnit) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Before(org.junit.Before)

Example 2 with DelayedBatchProcessing

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

the class HomieThingHandler method accept.

/**
 * Callback of {@link DelayedBatchProcessing}.
 * Add all newly discovered nodes and properties to the Thing and start subscribe to each channel state topic.
 */
@Override
public void accept(@Nullable List<Object> t) {
    if (!device.isInitialized()) {
        return;
    }
    List<Channel> channels = device.nodes().stream().flatMap(n -> n.properties.stream()).map(prop -> prop.getChannel()).collect(Collectors.toList());
    updateThing(editThing().withChannels(channels).build());
    updateProperty(MqttBindingConstants.HOMIE_PROPERTY_VERSION, device.attributes.homie);
    final MqttBrokerConnection connection = this.connection;
    if (connection != null) {
        device.startChannels(connection, scheduler, attributeReceiveTimeout, this).thenRun(() -> {
            logger.trace("Homie device {} fully attached", device.attributes.name);
        });
    }
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) HandlerConfiguration(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.HandlerConfiguration) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.eclipse.smarthome.core.thing.Thing) MqttBindingConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.MqttBindingConstants) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) MqttChannelTypeProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) ReadyState(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes.ReadyState) Logger(org.slf4j.Logger) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) DelayedBatchProcessing(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) Collectors(java.util.stream.Collectors) DeviceCallback(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceCallback) Consumer(java.util.function.Consumer) List(java.util.List) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Channel(org.eclipse.smarthome.core.thing.Channel)

Aggregations

Device (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device)2 DeviceAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes)2 DelayedBatchProcessing (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing)2 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 MqttBindingConstants (org.eclipse.smarthome.binding.mqtt.generic.internal.MqttBindingConstants)1 ReadyState (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes.ReadyState)1 DeviceCallback (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceCallback)1 HandlerConfiguration (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.HandlerConfiguration)1 Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)1 Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)1 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)1 MqttChannelTypeProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider)1 ChildMap (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap)1