- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 Web API,可以接收消息并向 Raspberry Pi 发送消息。连接工作正常,使用 Web API 上的 azure-event-hubs 接收消息,使用 azure-iothub 将消息发送到树莓派。
我遇到的问题是当我尝试在 onMessage 函数中发送消息时(因此每当我在 webapi 中收到消息时),设备不会收到它。这是我的代码:
WebApi:
const { EventHubClient, EventPosition } = require('azure-event-hubs');
var connectionString = 'myConnectionString'
var sendingClient = require('../azure/sendingClient')
async function main() {
sendingClient.sendMessage('raspberry',{},"allDevices") //The raspberry receives this
const client = await
EventHubClient.createFromIotHubConnectionString(connectionString);
const onError = (err) => {
console.log("An error occurred on the receiver ", err);
};
const onMessage = (msg) => {
console.log(msg.body);
sendingClient.sendMessage('raspberry',{},"allDevices")// the raspberry doesn't receive this
};
const receiveHandler = client.receive("1", onMessage, onError, {
eventPosition: EventPosition.fromEnqueuedTime(Date.now())
});
// To stop receiving events later on...
await receiveHandler.stop();
await client.close();
}
main().catch((err) => {
console.log(err);
});
发送客户端:
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = 'myConnectionString'
var sendingClient = Client.fromConnectionString(connectionString);
exports.sendMessage = (targetDevice, content, messageId) => {
sendingClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
var message = new Message(content);
message.ack = 'full';
message.messageId = 'message'
message.properties.add('message',messageId)
console.log('Sending message: ' + message.getData());
console.log('Sending message to : ' + targetDevice);
sendingClient.send(targetDevice, message,);
}
});
}
树莓派上的接收器:
var iothub = require('azure-iothub');
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
var client = Client.fromConnectionString(connectionString, Protocol)
client.open((err) => {
if (err) console.error('Could not connect: ' + err.message)
else {
client.on('message', (msg) => {
switch (msg.properties.propertyList[1].value) {
case 'allDevices':
devices = JSON.parse(msg.data.toString())
response(devices) //passing the message content
}
});
client.on('error', (err) => {
console.error(err.message);
});
client.on('disconnect', () => {
clearInterval(sendInterval);
client.removeAllListeners();
client.open(connectCallback);
});
}
})
RaspberryPi 上的发送者:
exports.sendMessage = (data, connectionString, key) => {
var client = Client.fromConnectionString(connectionString, Protocol)
client.open((err) => {
if (err) console.error('Send message error: ' + err.message)
else {
data = JSON.stringify(data);
var message = new Message(data);
message.properties.add('message', key);
client.sendEvent(message);
console.log('Message sent ' + key);
}
})
}
最佳答案
我认为这是由于事件中心客户端无法接收来自分区“1”的消息。无法将消息发送到 IoT 中心中的特定分区,分区在内部使用以允许扩展 IoT(事件中心)并允许扩展消费者应用程序。您可以尝试使用以下代码来接收来自所有分区的消息。
const partitionIds = await client.getPartitionIds();
partitionIds.forEach(function(id,index){
const receiveHandler = client.receive(id, onMessage, onError, { eventPosition: EventPosition.fromEnqueuedTime(Date.now()) });
});
更新:
WebApi.js
const { EventHubClient, EventPosition } = require('azure-event-hubs');
var connectionString = '{iot-hub-connectionstring}';
var sendingClient = require('./sendingClient');
async function main() {
sendingClient.sendMessage('raspberry',{},"raspberry-first") //The raspberry receives this
const client = await EventHubClient.createFromIotHubConnectionString(connectionString);
const onError = (err) => {
console.log("An error occurred on the receiver ", err);
};
const onMessage = (msg) => {
console.log("receive response");
console.log(msg.body);
sendingClient.sendMessage('raspberry',{},"raspberry-second")// the raspberry doesn't receive this
};
const partitionIds = await client.getPartitionIds();
partitionIds.forEach(function(id,index){
const receiveHandler = client.receive(id, onMessage, onError, { eventPosition: EventPosition.fromEnqueuedTime(Date.now()) });
});
}
main().catch((err) => {
console.log(err);
});
sendingClient.js
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = '{iot-hub-connectionstring}';
var sendingClient = Client.fromConnectionString(connectionString);
exports.sendMessage = (targetDevice, content, messageId) => {
sendingClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
var message = new Message(content);
message.ack = 'full';
message.messageId = 'message'
message.properties.add('message',messageId)
console.log('Sending message: ' + message.getData());
console.log('Sending message to : ' + targetDevice);
sendingClient.send(targetDevice, message,);
}
});
}
Receiver.js var Protocol = require('azure-iot-device-mqtt').Mqtt; var Client = require('azure-iot-device').Client; var Message = require('azure-iot-device').Message;
var connectionString = "{iot-hub-device-connectionstring}";
// fromConnectionString must specify a transport constructor, coming from any transport package.
var client = Client.fromConnectionString(connectionString, Protocol);
var connectCallback = function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
client.on('message', function (msg) {
// When using MQTT the following line is a no-op.
client.complete(msg, printResultFor('completed'));
console.log(msg.properties.propertyList[1].value);
});
client.on('error', function (err) {
console.error(err.message);
});
client.on('disconnect', function () {
client.removeAllListeners();
client.open(connectCallback);
});
}
};
client.open(connectCallback);
// Helper function to print results in the console
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.constructor.name);
};
}
顺便说一句,我已将所有 azure-* 库更新到最新版本。
关于node.js - 未收到从 azure-event-hubs onMessage 函数发送的 azure-iothub 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50624195/
我正在使用 Azure Monitor 查看 IoTHub 的诊断/日志。在 metrics available for IoTHub有 deviceDataUsage。 据我了解,这是连接到此 Io
我正在尝试在 ARM 模板中获取与事件中心兼容的连接字符串,然后将其传递到我的 HDInsight 群集以进行进一步处理。 我正在尝试创建我选择的事件中心连接字符串,但生成的事件中心名称不相同,并且包
我正在使用 Azure Monitor 查看 IoTHub 的诊断/日志。在 metrics available for IoTHub有 deviceDataUsage。 据我了解,这是连接到此 Io
我正在尝试在 ARM 模板中获取与事件中心兼容的连接字符串,然后将其传递到我的 HDInsight 群集以进行进一步处理。 我正在尝试创建我选择的事件中心连接字符串,但生成的事件中心名称不相同,并且包
Azure IoTHub 上的每台设备是否有消息接收限制?如果有的话,我可以在不注册其他设备的情况下删除或提高上限吗? 我测试了两件事,以确保在未来的负载测试中我是否可以在 Azure IoT 中心上
这是场景: 设备使用一些设备信息调用端点(Function App/WebService)。 端点将嵌入中间证书 (X509)(使用根证书创建),此中间证书已上传到注册组 端点将为设备创 build
我正在尝试查看通过 iothub-explorer 从我的 esp8266 模块发送的消息,但出现以下错误。 我正在运行的命令 iothub-explorer monitor-events Devic
我是 IoTHub 新手。我已经使用python成功地将消息发送到IOT集线器(D2C)。我们使用的协议(protocol)是mqtt。我们正在尝试使用java从云(IOT集线器)检索数据,但无法找到
getConnectionState() 连接/断开连接取决于设备。如果它正在发送消息,我应该看到已连接,如果它不发送,我应该断开连接。但是每次我运行下面的 java 程序时,无论设备是否发送消息,我
我是 IoTHub 的新手。我对数据发送到 IoTHub\EventHub 时如何...有一个基本查询。从我的阅读中,我将 IoT 中心和事件中心视为不同的实体。 我的要求 我需要开发一个应用程序,将
我连接了一个事件生成器并将消息发布到 IoT 中心。如何查看已收到哪些消息?我对不同的确定方式感兴趣(以编程方式、仪表板等) 最佳答案 有跨平台Azure IoT Toolkit Visual Stu
在 Azure IoT 中心,您可以添加 IoT 设备的配置。此配置包括“目标条件”,以确定该配置将应用到哪些设备。某些目标条件示例可能基于标签 (tags.environment = product
问题 用于物联网边缘设备的 Azure IotHub 指标收集器是我正在尝试实现的一个非常有趣的功能,尤其是通过 IotMessage 的选项 2,因此将带有指标数据的设备发布到云消息。因此,我阅读了
我想更改 IoT 中心的位置 image attached 。由于某种原因,似乎没有任何方法可以在门户内执行此操作。我是否必须删除 IoT 中心并重新创建它? 最佳答案 无法将 Azure 资源移动到
我使用 Azure IoTHub 从我的设备接收数据。为了授权这些设备在我的 iothub 上发送数据,我为每个设备生成并指定一个 SAS token 。 在我的测试过程中,有几次,即使指定了 1 个
我有一个名为“myDPS”的 Azure 设备预配服务设置,并链接了以下 IoT 中心。 IoTHub-Dev-Asia IoTHub-Prod-欧洲 目前没有报名名单。我使用下面的 C# 代码来注册
我正在尝试测试使用 RegistryManager 与 IoThub 通信的类。 我面临的问题是,在创建继承自 RegistryManager 的模拟类时,我能够重写除 ExportRegistryA
我正在尝试测试使用 RegistryManager 与 IoThub 通信的类。 我面临的问题是,在创建继承自 RegistryManager 的模拟类时,我能够重写除 ExportRegistryA
我对 SendEventAsync() 方法有疑问。 我测试了LAN电缆的插入和拔出 _sendDeviceClient.SetRetryPolicy(no); _sendDeviceClient.O
如何正确设置和配置 IoTHubTrigger 以触发 IoTHub 消息的 Azure Function (C#)?在哪里以及如何插入 IoTHub 的连接字符串? 最佳答案 使用 Visual S
我是一名优秀的程序员,十分优秀!