![](/img/trans.png)
[英]Google Cloud Functions can't use Pub/Sub dead-letter topic
[英]How can I use IoT telemetry events on Pub/Sub topic?
我已阅读有关Google Cloud IoT API的文档 。 而且我已经为Android编写了一个简单的应用程序。 基于此google的库 。 我的应用程序已成功连接到IoT平台,并且已经发送了测试数据。
我的应用程序代码。
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));
但是,有一种将设备传感器数据发送到IoT平台的方法(“ publishTelemetry(Parms ...)”)。
client.publishTelemetry(new TelemetryEvent("Sonata".getBytes(), null,TelemetryEvent.QOS_AT_LEAST_ONCE));
这段代码是可行的,但是我无法在Google Cloud Platform中找到此数据“ 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 Cloud Platform控制台,然后在IoT核心部分的设备页面上(IoT核心->注册->设备),其中有一个“配置和状态历史记录”标签,您应该在其中看到“测试数据”(与publishDeviceState调用设置一样)。 那应该确认至少其他所有工作都按预期进行。
假设是这种情况,现在您将要查看Pub / Sub文档,以开始了解使用Pub / Sub可以做什么。 这是主要的文档页面。 我的建议是将Google Cloud Functions作为快速启动和运行的地方。 根据您想做什么, Cloud Dataflow可能也是一个不错的选择。
这些产品中的每一个都会根据发布到Cloud Pub / Sub中的消息触发。 因此,一旦您调用“ publishTelemetry”,它将把遥测发送到IoT Core,然后将其发布到IoT Core注册表中指定时的Pub / Sub主题中。 然后触发的产品(GCF和数据流)接收发布/订阅对象,您可以从中获取遥测数据。 在文档中有关于如何执行此操作的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.