MQTT message callbacks are executed in the MQTT thread context. when processing topics that control the huawei AC charger, we must avoid executing methods that are not thread-safe. this change bound the methods to be called to the respective parameters and executes them in the TaskScheduler context, such that they no longer need to be thread-safe.
32 lines
981 B
C++
32 lines
981 B
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#pragma once
|
|
|
|
#include "Configuration.h"
|
|
#include <Huawei_can.h>
|
|
#include <espMqttClient.h>
|
|
#include <TaskSchedulerDeclarations.h>
|
|
#include <mutex>
|
|
#include <deque>
|
|
#include <functional>
|
|
|
|
class MqttHandleHuaweiClass {
|
|
public:
|
|
void init(Scheduler& scheduler);
|
|
|
|
private:
|
|
void loop();
|
|
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
|
|
|
Task _loopTask;
|
|
|
|
uint32_t _lastPublishStats;
|
|
uint32_t _lastPublish;
|
|
|
|
// MQTT callbacks to process updates on subscribed topics are executed in
|
|
// the MQTT thread's context. we use this queue to switch processing the
|
|
// user requests into the main loop's context (TaskScheduler context).
|
|
mutable std::mutex _mqttMutex;
|
|
std::deque<std::function<void()>> _mqttCallbacks;
|
|
};
|
|
|
|
extern MqttHandleHuaweiClass MqttHandleHuawei; |