Search in sources :

Example 61 with Node

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project GeoGig by boundlessgeo.

the class EntityConverter method toFeature.

public SimpleFeature toFeature(Entity entity, Geometry geom) {
    SimpleFeatureType ft = entity instanceof Node ? OSMUtils.nodeType() : OSMUtils.wayType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft, FEATURE_FACTORY);
    // TODO: Check this!
    builder.set("visible", Boolean.TRUE);
    builder.set("version", Integer.valueOf(entity.getVersion()));
    builder.set("timestamp", Long.valueOf(entity.getTimestamp().getTime()));
    builder.set("changeset", Long.valueOf(entity.getChangesetId()));
    String tags = OSMUtils.buildTagsString(entity.getTags());
    builder.set("tags", tags);
    String user = entity.getUser().getName() + ":" + Integer.toString(entity.getUser().getId());
    builder.set("user", user);
    if (entity instanceof Node) {
        builder.set("location", geom);
    } else if (entity instanceof Way) {
        builder.set("way", geom);
        String nodes = buildNodesString(((Way) entity).getWayNodes());
        builder.set("nodes", nodes);
    } else {
        throw new IllegalArgumentException();
    }
    String fid = String.valueOf(entity.getId());
    SimpleFeature simpleFeature = builder.buildFeature(fid);
    return simpleFeature;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 62 with Node

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node 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)

Example 63 with Node

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project smarthome by eclipse.

the class HomieImplementationTests method createSpyProperty.

// Inject a spy'ed property
public Property createSpyProperty(InvocationOnMock invocation) {
    final Node node = (Node) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    Property property = spy(node.createProperty(id, spy(new PropertyAttributes())));
    return property;
}
Also used : PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)

Example 64 with Node

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project smarthome by eclipse.

the class HomieImplementationTests method createSpyNode.

// Inject a spy'ed node
public Node createSpyNode(InvocationOnMock invocation) {
    final Device device = (Device) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    // Create the node
    Node node = spy(device.createNode(id, spy(new NodeAttributes())));
    // Intercept creating a property in the next call and inject a spy'ed property.
    doAnswer(this::createSpyProperty).when(node).createProperty(any());
    return node;
}
Also used : Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)

Example 65 with Node

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node 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)

Aggregations

Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)52 Test (org.junit.Test)27 CommonEntityData (org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData)26 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)21 Date (java.util.Date)18 NodeContainer (org.openstreetmap.osmosis.core.container.v0_6.NodeContainer)17 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)16 Node (org.flyte.api.v1.Node)15 Test (org.junit.jupiter.api.Test)15 ArrayList (java.util.ArrayList)14 OsmUser (org.openstreetmap.osmosis.core.domain.v0_6.OsmUser)14 Way (org.openstreetmap.osmosis.core.domain.v0_6.Way)13 TaskNode (org.flyte.api.v1.TaskNode)10 Bound (org.openstreetmap.osmosis.core.domain.v0_6.Bound)10 List (java.util.List)9 Map (java.util.Map)9 Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)9 Node (org.openhab.binding.mqtt.homie.internal.homie300.Node)9 EntityContainer (org.openstreetmap.osmosis.core.container.v0_6.EntityContainer)9 Node (ch.ethz.globis.phtree.v16.Node)8