* bind the callback to a topic (enum value) such that there is no need to tokenize the full topic (string) to find out what value is being processed. tokenizing is expensive. * get rid of using the config in the callback, which improves thread-safety since the MQTT callback is running in the MQTT thread. * prefer C++ method stof to convert MQTT value to a float, which saves us from using new and delete for a buffer in particular. * prefer switch statements over if-else-trees. * split long lines. * get rid of topic #defines. * fix indention.
44 lines
1.2 KiB
C++
44 lines
1.2 KiB
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();
|
|
|
|
enum class Topic : unsigned {
|
|
LimitOnlineVoltage,
|
|
LimitOnlineCurrent,
|
|
LimitOfflineVoltage,
|
|
LimitOfflineCurrent,
|
|
Mode
|
|
};
|
|
|
|
void onMqttMessage(Topic t,
|
|
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; |