1

我正在尝试每秒向 Azure IoT Hub 内置事件中心发布超过 100 条消息。我正在使用 libmosquitto 1.6.8 库。我正在使用 Azure IoT Hub 的免费层包,我知道每秒 100 条消息的限制。但这与那个问题无关。我什至无法将一半的消息发布到 AZ IoT Hub。

基本上,我有一个需要发送的 multimap 中的多个值的列表。指标列表:

std::multimap< const std::string, std::tuple< const std::string, const std::string, float> > calculatedMetricList;

我将遍历多重映射并将每个值构造成一个对象有效负载并将其发送出去。这意味着 mosquitto_publish 方法将被多次调用。

以下是发布消息的代码:

void MosquittoClient::sendDataToUpstreamSystem(){

StatisticalMethod statisticalMethod;
int rc;

MosquittoClient pub_mosq(
    "<IoT Hub Name>.azure-devices.net",
    "<deviceID>", 
    "<username>", 
    "<Password>", 
    "devices/<deviceID>/messages/events/");

printf("Using MQTT to get data payload from host: %s and on port: %d.\r\n", pub_mosq.get_host(), pub_mosq.get_port());
// init the mosquitto lib
mosquitto_lib_init();

// create the mosquito object
struct mosquitto* mosq = mosquitto_new(pub_mosq.get_deviceID(), false, NULL);

// add callback functions
mosquitto_connect_callback_set(mosq, MosquittoClient::connect_callback);
mosquitto_publish_callback_set(mosq, MosquittoClient::publish_callback);
mosquitto_message_callback_set(mosq, MosquittoClient::on_message);
mosquitto_disconnect_callback_set(mosq, MosquittoClient::on_disconnect_callback);

// set mosquitto username, password and options
mosquitto_username_pw_set(mosq, pub_mosq.get_userName(), pub_mosq.get_password());

// specify the certificate to use
std::ifstream infile(pub_mosq.get_certificate());
bool certExists = infile.good();
infile.close();

if (!certExists)
{
    printf("Warning: Could not find file '%s'! The mosquitto loop may fail.\r\n", pub_mosq.get_certificate());
}

printf("Using certificate: %s\r\n", pub_mosq.get_certificate());
mosquitto_tls_set(mosq, pub_mosq.get_certificate(), NULL, NULL, NULL, NULL);

// specify the mqtt version to use
int* option = new int(MQTT_PROTOCOL_V311);
rc = mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, option);
if (rc != MOSQ_ERR_SUCCESS)
{
    rc = pub_mosq.mosquitto_error(rc, "Error: opts_set protocol version");
}
else
{
    printf("Setting up options OK\r\n");
}

// connect
printf("Connecting...\r\n");
rc = mosquitto_connect_async(mosq, pub_mosq.get_host(), pub_mosq.get_port(), 4);
if (rc != MOSQ_ERR_SUCCESS)
{
    rc = pub_mosq.mosquitto_error(rc, NULL);
}
else
{
    printf("Connect returned OK\r\n");

    rc = mosquitto_loop_start(mosq);

    if (rc != MOSQ_ERR_SUCCESS)
    {
        rc = pub_mosq.mosquitto_error(rc, NULL);
    }
    else
    {
        do
        {
            for (auto itr = Metrics::calculatedMetricList.begin(); itr != Metrics::calculatedMetricList.end(); itr++) { 
                int msgId = rand();

                std::string test1= itr->first;
                std::string test2 = std::get<0>(itr->second);
                std::string test3= std::get<1>(itr->second); // metric type 
                float value = std::get<2>(itr->second); // value

                DataPayload objectPayload(
                    75162345,
                    496523,
                    test3,
                    value,
                    "test",
                    test1,
                    "test",
                    "test",
                    123,
                    213,
                    23
                );

                objectPayload.setPayload();
                std::string dataPayload = objectPayload.getPayload();

                //DEBUG
                std::cout << "dataPayload: " << dataPayload << std::endl;
                //DEBUG
                std::cout << "dataPayload Size: " << dataPayload.size() << std::endl;

                // once connected, we can publish (send) a Telemetry message
                printf("Publishing to topic: %s\r\n", pub_mosq.get_topic());

                rc = pub_mosq.publishToTopic(mosq, &msgId, dataPayload.size(), (char *)dataPayload.c_str());

                if (rc == MOSQ_ERR_SUCCESS)
                {
                    printf("Publish returned OK\r\n");
                }
                else 
                {               
                    rc = pub_mosq.mosquitto_error(rc, NULL);
                }
            } 

        } while (rc != MOSQ_ERR_SUCCESS);   
    }
}

mosquitto_loop_stop(mosq, true);
mosquitto_destroy(mosq);

mosquitto_lib_cleanup();}

发布方式:

    int MosquittoClient::publishToTopic(struct mosquitto *mosq, int *msgId, int sizeOfData, char *data)
{
    return mosquitto_publish(mosq, msgId, p_topic, sizeOfData, data, 1, true);
}

根据控制台,运行程序时,所有发布的消息都返回正常。但 Azure IoT 中心端只显示一两条消息。

下图是 IoT Hub 的监控,当时只有一条消息通过。 设备资源管理器孪生监控

我尝试了很多不同的解决方案,但程序无法发布所有消息。看起来发布方法正在等待完成第一条消息,但迭代正在移动到下一条消息,导致它被丢弃。如果这是丢弃消息的原因,那么对消息发送进行排序的最佳方法是什么?否则,还有什么可能导致消息被丢弃?

更新

问题是程序没有等到消息成功发布到代理(Azure IoT Hub)。如果返回了 publish_callback,您将知道消息是否成功发布到代理。

void MosquittoClient::publish_callback(struct mosquitto* mosq, void* userdata, int mid)
{
    printf("Publish OK.\r\n");
}

解决方案是在销毁之前休眠线程,清理调用并在建立连接之前启动 Mosquitto 循环。

sleep(30);
mosquitto_loop_stop(mosq, true);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
4

1 回答 1

0

mosquitto_publish()是异步的:让它返回MOSQ_ERR_SUCCESS仅仅意味着消息的发布已经正确地传递给 Mosquitto 线程。因此,目前您正在排队大量消息,然后让您的程序在它有机会实际发送数据包之前终止。

在停止循环和终止程序之前,您可以使用MosquittoClient::publish_callback回调来检查所有消息是否已有效发送。

于 2020-02-04T20:03:44.790 回答