使用 Lambda 函数处理 AWS IoT 消息

Processing AWS IoT messages with Lambda functions

我正在尝试在 C# 中开发一个 lambda 函数来处理和转换我从 AWS IoT Core 接收的数据,就像我处理来自 DynamoDB、S3 或 SQS 的数据一样。

我想知道的是,如果我必须强制订阅主题,转换每条消息并最终将其发送到 s3、DyanmoDB 或其他任何东西,或者我可以直接访问这些数据而无需订阅。

代码:

 var CaCert = X509Certificate.CreateFromCertFile(@"C:\...\rootCA.pem");
                var clientCert = new X509Certificate2(@"C:\...\amazon.pfx");
                string ClientID = Guid.NewGuid().ToString();


                var IotClient = new MqttClient(IotEndPoint, BrokerPort, true, CaCert, clientCert, MqttSslProtocols.TLSv1_2);
                IotClient.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;

                IotClient.Connect(ClientID);
                Console.WriteLine("Connected to IoT Core. Waiting for the frames...");
                IotClient.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });

您必须订阅主题才能访问数据。但是,您可以简单地编写您的主题订阅 SQL 表达式以使用像 this example from the official documentation 这样的通配符来避免必须订阅每个单独的主题:

You can use the # wildcard character to match any subpath in a topic filter:

Example:

Incoming payload published on topic 'a/b': {temperature: 50}.

Incoming payload published on topic 'a/c': {temperature: 60}.

Incoming payload published on topic 'a/e/f': {temperature: 70}.

Incoming payload published on topic 'b/x': {temperature: 80}.

SQL: "SELECT temperature AS t FROM 'a/#'".

The rule is subscribed to any topic beginning with 'a', so it is executed three times, sending outgoing payloads of {t: 50} (for a/b), {t: 60} (for a/c), and {t: 70} (for a/e/f) to its actions. It is not subscribed to 'b/x', so the rule will not be triggered for the {temperature: 80} message.