如何在 Pub/Sub 主题上使用 IoT 遥测事件?

How can I use IoT telemetry events on Pub/Sub topic?

我读过documentation about Google cloud IoT API. And I had wrote simple application for Android of things. Based on this google's library。我的应用程序已成功连接到物联网平台,我已发送测试数据。

我的申请代码。

ConnectionParams connectionParams = new ConnectionParams.Builder()
            .setProjectId("my_pid")
            .setRegistry("my_reg", "my_server")
            .setDeviceId("my_device_name")
            .build();
IotCoreClient client = new IotCoreClient.Builder()
            .setConnectionParams(connectionParams)
            .setKeyPair(keys)
            .setTelemetryQueue(new LinkedTransferQueue<TelemetryEvent>())
            .build();

client.connect();

client.publishDeviceState("Test data\n".getBytes());

client.publishTelemetry(new TelemetryEvent("Sonata".getBytes(), null,TelemetryEvent.QOS_AT_LEAST_ONCE));

但是有将设备传感器数据发送到物联网平台的方法(“publishTelemetry(Parms...))。

client.publishTelemetry(new TelemetryEvent("Sonata".getBytes(), null,TelemetryEvent.QOS_AT_LEAST_ONCE));

此代码有效,但我在 google 云平台中找不到此数据 "Sonata",我不明白如何在 Pub/Sub 上使用遥测事件主题?

已更新

我找到了解决方案。第一步,我已将订阅者添加到主题。例子。主题 projects/my-project-id/topics/firstTop 订阅者示例(fsub 是订阅者名称)projects/my-project-id/subscriptions/fsub 我在 java 中编写了简单的订阅者代码,并从 Android 设备发送消息。我得到了遥测数据。

这是 java

中的订阅者代码
import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.common.collect.Lists;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class SubscriberExample {
    private static final String PROJECT_ID = "my-project-id";
    private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>();

    static class MessageReceiverExample implements MessageReceiver {
        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            messages.offer(message);
            consumer.ack();
        }
    }

    public static void main(String... args) throws Exception {
        String subscriptionId = "YOUR_SUBSCRIBER_ID";
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
        Subscriber subscriber = null;
        try {
            GoogleCredentials credentials = GoogleCredentials
                    .fromStream(new FileInputStream("~/google_cloud_pubsub-Project-0b66ab8c5060.json")) // you can get here https://cloud.google.com/docs/authentication/getting-started
                    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
            subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample())
                    .setCredentialsProvider(new CredentialsProvider() {
                        @Override
                        public Credentials getCredentials() throws IOException {
                            return credentials;
                        }
                    }).build();
            subscriber.startAsync().awaitRunning();
            while (true) {
                PubsubMessage message = messages.take();
                System.out.println("Message Id: " + message.getMessageId());
                System.out.println("Data: " + message.getData().toStringUtf8());
            }
        } finally {
            if (subscriber != null) {
                subscriber.stopAsync();
            }
        }
    }
}

首先检查它是否正常工作以及连接是否确实连接到您的项目,最简单的方法是查看您项目的 Google 云平台控制台,以及 IoT 核心内的设备页面部分(IoT Core->registry->device),有一个 "Configuration & state history" 的选项卡,您应该在那里看到 "Test data"(通过 publishDeviceState 调用设置)。这应该确认至少其他一切都按预期工作。

假设是这种情况,现在您需要查看 Pub/Sub 文档以开始了解您可以使用 Pub/Sub 做什么。 Here is the main doc page. My recommendation is to look into Google Cloud Functions as a place to get up and running quickly. Depending on what you want to do, Cloud Dataflow 也可能是一个不错的选择。

这些产品中的每一个都基于发布到云中的消息触发 Pub/Sub。因此,一旦您调用 "publishTelemetry",它就会将遥测数据发送到 IoT Core,然后 IoT Core 将消息发布到您创建它时在 IoT Core 注册表中指定的 Pub/Sub 主题。然后触发的产品(GCF 和数据流)接收 Pub/Sub 对象,您可以从中获取遥测数据。文档中有关于如何操作的示例。