Search in sources :

Example 11 with Node

use of io.envoyproxy.envoy.config.core.v3.Node in project GeoGig by boundlessgeo.

the class EntityConverter method toEntity.

/**
     * Converts a Feature to a OSM Entity
     * 
     * @param feature the feature to convert
     * @param replaceId. The changesetId to use in case the feature has a negative one indicating a
     *        temporary value
     * @return
     */
public Entity toEntity(SimpleFeature feature, Long changesetId) {
    Entity entity;
    SimpleFeatureType type = feature.getFeatureType();
    long id = Long.parseLong(feature.getID());
    int version = ((Integer) feature.getAttribute("version")).intValue();
    Long changeset = (Long) feature.getAttribute("changeset");
    if (changesetId != null && changeset < 0) {
        changeset = changesetId;
    }
    Long milis = (Long) feature.getAttribute("timestamp");
    Date timestamp = new Date(milis);
    String user = (String) feature.getAttribute("user");
    String[] userTokens = user.split(":");
    OsmUser osmuser;
    try {
        osmuser = new OsmUser(Integer.parseInt(userTokens[1]), userTokens[0]);
    } catch (Exception e) {
        osmuser = OsmUser.NONE;
    }
    String tagsString = (String) feature.getAttribute("tags");
    Collection<Tag> tags = OSMUtils.buildTagsCollectionFromString(tagsString);
    CommonEntityData entityData = new CommonEntityData(id, version, timestamp, osmuser, changeset, tags);
    if (type.equals(OSMUtils.nodeType())) {
        Point pt = (Point) feature.getDefaultGeometryProperty().getValue();
        entity = new Node(entityData, pt.getY(), pt.getX());
    } else {
        List<WayNode> nodes = Lists.newArrayList();
        String nodesString = (String) feature.getAttribute("nodes");
        for (String s : nodesString.split(";")) {
            nodes.add(new WayNode(Long.parseLong(s)));
        }
        entity = new Way(entityData, nodes);
    }
    return entity;
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) CommonEntityData(org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) Date(java.util.Date) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Example 12 with Node

use of io.envoyproxy.envoy.config.core.v3.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 13 with Node

use of io.envoyproxy.envoy.config.core.v3.Node in project bboxdb by jnidzwetzki.

the class OSMDataConverter method start.

/**
 * Start the converter
 */
public void start() {
    try {
        // Open file handles
        for (final OSMType osmType : filter.keySet()) {
            final BufferedWriter bw = new BufferedWriter(new FileWriter(new File(output + File.separator + osmType.toString())));
            writerMap.put(osmType, bw);
        }
        System.out.format("Importing %s%n", filename);
        final OsmosisReader reader = new OsmosisReader(new FileInputStream(filename));
        reader.setSink(new Sink() {

            @Override
            public void close() {
            }

            @Override
            public void complete() {
            }

            @Override
            public void initialize(final Map<String, Object> metaData) {
            }

            @Override
            public void process(final EntityContainer entityContainer) {
                try {
                    if (entityContainer.getEntity() instanceof Node) {
                        // Nodes are cheap to handle, dispatching to another thread
                        // is more expensive
                        final Node node = (Node) entityContainer.getEntity();
                        handleNode(node);
                        statistics.incProcessedNodes();
                    } else if (entityContainer.getEntity() instanceof Way) {
                        // Ways are expensive to handle
                        final Way way = (Way) entityContainer.getEntity();
                        queue.put(way);
                        statistics.incProcessedWays();
                    }
                } catch (InterruptedException e) {
                    return;
                }
            }
        });
        // The way consumer
        for (int i = 0; i < CONSUMER_THREADS; i++) {
            threadPool.submit(new Consumer());
        }
        reader.run();
    } catch (IOException e) {
        logger.error("Got an exception during import", e);
    } finally {
        shutdown();
    }
}
Also used : FileWriter(java.io.FileWriter) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) SerializableNode(org.bboxdb.tools.converter.osm.util.SerializableNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) BufferedWriter(java.io.BufferedWriter) Sink(org.openstreetmap.osmosis.core.task.v0_6.Sink) File(java.io.File) OsmosisReader(crosby.binary.osmosis.OsmosisReader)

Example 14 with Node

use of io.envoyproxy.envoy.config.core.v3.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 15 with Node

use of io.envoyproxy.envoy.config.core.v3.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)

Aggregations

Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)9 Test (org.junit.Test)8 NodeAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)6 Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)6 Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)5 PropertyAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes)4 Way (org.openstreetmap.osmosis.core.domain.v0_6.Way)4 ArrayList (java.util.ArrayList)3 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)3 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 Node (io.opencensus.proto.agent.common.v1.Node)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Nullable (javax.annotation.Nullable)2 Device (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device)2 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2