Search in sources :

Example 1 with Device

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device in project kripton by xcesco.

the class Test209Model2Runtime method testRunSqlite1.

@Test
public void testRunSqlite1() {
    BindApp2DataSource ds = BindApp2DataSource.instance();
    ds.execute(new Transaction() {

        @Override
        public TransactionResult onExecute(BindApp2DaoFactory daoFactory) {
            Device device = new Device();
            device.name = "device-test";
            daoFactory.getDeviceDao().insert(device);
            User user = new User();
            user.userName = "user-test";
            daoFactory.getUserDao().insert(user);
            UserDevice userDevice = new UserDevice();
            userDevice.deviceId = device.id;
            userDevice.userId = user.id;
            daoFactory.getUserDeviceDao().insert(userDevice);
            List<Device> devices = daoFactory.getDeviceDao().getUserDevices(user.id);
            Assert.assertTrue(devices.size() == 1);
            return TransactionResult.ROLLBACK;
        }
    });
}
Also used : BindApp2DaoFactory(sqlite.kripton209.model2.BindApp2DaoFactory) TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) User(sqlite.kripton209.model2.User) Transaction(sqlite.kripton209.model2.BindApp2DataSource.Transaction) UserDevice(sqlite.kripton209.model2.UserDevice) Device(sqlite.kripton209.model2.Device) BindApp2DataSource(sqlite.kripton209.model2.BindApp2DataSource) List(java.util.List) UserDevice(sqlite.kripton209.model2.UserDevice) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 2 with Device

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device in project smarthome by eclipse.

the class HomieThingHandlerTests method handleCommandRefresh.

@SuppressWarnings("null")
@Test
public void handleCommandRefresh() {
    // Create mocked homie device tree with one node and one read-only 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 = false;
    property.attributesReceived();
    node.properties.put(property.propertyID, property);
    thingHandler.device.nodes.put(node.nodeID, node);
    thingHandler.connection = connection;
    thingHandler.handleCommand(property.channelUID, RefreshType.REFRESH);
    verify(callback).stateUpdated(argThat(arg -> property.channelUID.equals(arg)), argThat(arg -> property.getChannelState().getCache().getChannelState().equals(arg)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Channel(org.eclipse.smarthome.core.thing.Channel) ArgumentMatchers(org.mockito.ArgumentMatchers) ScheduledFuture(java.util.concurrent.ScheduledFuture) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) Command(org.eclipse.smarthome.core.types.Command) MockitoAnnotations(org.mockito.MockitoAnnotations) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) AbstractMqttAttributeClass(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.AbstractMqttAttributeClass) Thing(org.eclipse.smarthome.core.thing.Thing) StringType(org.eclipse.smarthome.core.library.types.StringType) 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) Configuration(org.eclipse.smarthome.config.core.Configuration) ChannelStateHelper(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateHelper) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) ReadyState(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes.ReadyState) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) RefreshType(org.eclipse.smarthome.core.types.RefreshType) List(java.util.List) NonNull(org.eclipse.jdt.annotation.NonNull) ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) Mock(org.mockito.Mock) TypeParser(org.eclipse.smarthome.core.types.TypeParser) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelKind(org.eclipse.smarthome.core.thing.type.ChannelKind) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThingChannelConstants.testHomieThing(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants.testHomieThing) Before(org.junit.Before) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) DelayedBatchProcessing(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Test(org.junit.Test) SubscribeFieldToMQTTtopic(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic) Field(java.lang.reflect.Field) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Mockito(org.mockito.Mockito) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) DataTypeEnum(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes.DataTypeEnum) Assert(org.junit.Assert) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) Test(org.junit.Test)

Example 3 with Device

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device 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 4 with Device

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device in project kripton by xcesco.

the class Test209Model1Runtime method testRunSqlite1.

@Test
public void testRunSqlite1() {
    BindApp1DataSource ds = BindApp1DataSource.instance();
    ds.execute(new Transaction() {

        @Override
        public TransactionResult onExecute(BindApp1DaoFactory daoFactory) {
            Device device = new Device();
            device.name = "device-test";
            daoFactory.getDeviceDao().insert(device);
            User user = new User();
            user.userName = "user-test";
            daoFactory.getUserDao().insert(user);
            UserDevice userDevice = new UserDevice();
            userDevice.deviceId = device.id;
            userDevice.userId = user.id;
            daoFactory.getUserDeviceDao().insert(userDevice);
            List<Device> devices = daoFactory.getDeviceDao().getUserDevices(user.id);
            Assert.assertTrue(devices.size() == 1);
            return TransactionResult.ROLLBACK;
        }
    });
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) User(sqlite.kripton209.model1.User) Transaction(sqlite.kripton209.model1.BindApp1DataSource.Transaction) BindApp1DataSource(sqlite.kripton209.model1.BindApp1DataSource) BindApp1DaoFactory(sqlite.kripton209.model1.BindApp1DaoFactory) UserDevice(sqlite.kripton209.model1.UserDevice) Device(sqlite.kripton209.model1.Device) List(java.util.List) UserDevice(sqlite.kripton209.model1.UserDevice) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 5 with Device

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device in project smarthome by eclipse.

the class HomieImplementationTests method parseHomieTree.

@SuppressWarnings("null")
@Test
public void parseHomieTree() throws InterruptedException, ExecutionException, TimeoutException {
    // Create a Homie Device object. Because spied Nodes are required for call verification,
    // the full Device constructor need to be used and a ChildMap object need to be created manually.
    ChildMap<Node> nodeMap = new ChildMap<>();
    Device device = spy(new Device(ThingChannelConstants.testHomieThing, callback, new DeviceAttributes(), nodeMap));
    // Intercept creating a node in initialize()->start() and inject a spy'ed node.
    doAnswer(this::createSpyNode).when(device).createNode(any());
    // initialize the device, subscribe and wait.
    device.initialize(baseTopic, deviceID, Collections.emptyList());
    device.subscribe(connection, scheduler, 200).get();
    assertThat(device.isInitialized(), is(true));
    // Check device attributes
    assertThat(device.attributes.homie, is("3.0"));
    assertThat(device.attributes.name, is("Name"));
    assertThat(device.attributes.state, is(ReadyState.ready));
    assertThat(device.attributes.nodes.length, is(1));
    verify(device, times(4)).attributeChanged(any(), any(), any(), any(), anyBoolean());
    verify(callback).readyStateChanged(eq(ReadyState.ready));
    verify(device).attributesReceived(any(), any(), anyInt());
    // Expect 1 node
    assertThat(device.nodes.size(), is(1));
    // Check node and node attributes
    Node node = device.nodes.get("testnode");
    verify(node).subscribe(any(), any(), anyInt());
    verify(node).attributesReceived(any(), any(), anyInt());
    verify(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    assertThat(node.attributes.type, is("Type"));
    assertThat(node.attributes.name, is("Testnode"));
    // Expect 2 property
    assertThat(node.properties.size(), is(3));
    // Check property and property attributes
    Property property = node.properties.get("temperature");
    assertThat(property.attributes.settable, is(true));
    assertThat(property.attributes.retained, is(true));
    assertThat(property.attributes.name, is("Testprop"));
    assertThat(property.attributes.unit, is("°C"));
    assertThat(property.attributes.datatype, is(DataTypeEnum.float_));
    assertThat(property.attributes.format, is("-100:100"));
    verify(property).attributesReceived();
    assertNotNull(property.getChannelState());
    assertThat(property.getType().getState().getMinimum().intValue(), is(-100));
    assertThat(property.getType().getState().getMaximum().intValue(), is(100));
    // Check property and property attributes
    Property propertyBell = node.properties.get("doorbell");
    verify(propertyBell).attributesReceived();
    assertThat(propertyBell.attributes.settable, is(false));
    assertThat(propertyBell.attributes.retained, is(false));
    assertThat(propertyBell.attributes.name, is("Doorbell"));
    assertThat(propertyBell.attributes.datatype, is(DataTypeEnum.boolean_));
    // The device->node->property tree is ready. Now subscribe to property values.
    device.startChannels(connection, scheduler, 50, handler).get();
    assertThat(propertyBell.getChannelState().isStateful(), is(false));
    assertThat(propertyBell.getChannelState().getCache().getChannelState(), is(UnDefType.UNDEF));
    assertThat(property.getChannelState().getCache().getChannelState(), is(new DecimalType(10)));
    property = node.properties.get("testRetain");
    WaitForTopicValue watcher = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
    // Watch the topic. Publish a retain=false value to MQTT
    property.getChannelState().publishValue(OnOffType.OFF).get();
    assertThat(watcher.waitForTopicValue(50), is("false"));
    // Publish a retain=false value to MQTT.
    property.getChannelState().publishValue(OnOffType.ON).get();
    // This test is flaky if the MQTT broker does not get a time to "forget" this non-retained value
    Thread.sleep(50);
    // No value is expected to be retained on this MQTT topic
    watcher = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
    assertNull(watcher.waitForTopicValue(50));
}
Also used : ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) WaitForTopicValue(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.WaitForTopicValue) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)6 Device (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device)5 Test (org.junit.Test)5 List (java.util.List)4 DeviceAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes)4 NodeAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)4 Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)4 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)3 ChildMap (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap)3 DelayedBatchProcessing (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing)3 BaseAndroidTest (base.BaseAndroidTest)2 TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 MqttBindingConstants (org.eclipse.smarthome.binding.mqtt.generic.internal.MqttBindingConstants)2 ReadyState (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes.ReadyState)2 PropertyAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes)2 MqttChannelTypeProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider)2