forked from volkszaehler/vzlogger
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of mqtt client support.
Adds - ENABLE_MQTT cmake option. Defaults to On but turns off automatically if libmosquitto not found. - basic mqtt client support using libmosquitto - topics generated are: vzlogger/<channel-id>/uuid vzlogger/<channel-id>/raw vzlogger/<channel-id>/agg - added config options for - host - port (currently no tls/sll support!) - user - password - topic (prefix used instead of vzlogger in above example) - (some more, see etc/vzlogger.conf) - agg values get's preferred instead of raw. Using config option rawAndAgg this can be changed.
- Loading branch information
Showing
12 changed files
with
519 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Author: Matthias Behr, mbehr (a) mcbehr dot de | ||
* (c) 2018 | ||
* */ | ||
|
||
#ifndef __mqtt_hpp_ | ||
#define __mqtt_hpp_ | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include "Channel.hpp" | ||
#include "Reading.hpp" | ||
|
||
struct mosquitto; // forward decl. to avoid pulling the header here | ||
|
||
class MqttClient | ||
{ | ||
public: | ||
MqttClient(struct json_object *option); | ||
MqttClient() = delete; // no default constr. | ||
MqttClient(const MqttClient &) = delete; // no copy constr. | ||
~MqttClient(); | ||
bool isConfigured() const; | ||
|
||
void publish(Channel::Ptr ch, Reading &rds, bool aggregate = false); // thread safe, non blocking | ||
protected: | ||
friend void *mqtt_client_thread(void *); | ||
void connect_callback(struct mosquitto *mosq, int result); | ||
void disconnect_callback(struct mosquitto *mosq, int result); | ||
void message_callback(struct mosquitto *mosq, const struct mosquitto_message *msg); | ||
|
||
bool _enabled; | ||
std::string _host; | ||
int _port = 0; | ||
int _keepalive = 10; | ||
std::string _user; | ||
std::string _pwd; | ||
bool _retain = false; | ||
bool _rawAndAgg = false; | ||
std::string _topic; | ||
|
||
bool _isConnected = false; | ||
|
||
struct mosquitto *_mcs = nullptr; // mosquitto client session data | ||
|
||
struct ChannelEntry | ||
{ | ||
bool _announced = false; | ||
bool _sendRaw = true; | ||
bool _sendAgg = true; | ||
std::string _fullTopicRaw; | ||
std::string _fullTopicAgg; | ||
std::string _announceName; | ||
std::string _announceValue; | ||
void generateNames(const std::string &prefix, Channel &ch); | ||
}; | ||
std::unordered_map<std::string, ChannelEntry> _chMap; | ||
}; | ||
|
||
extern MqttClient *mqttClient; | ||
|
||
void *mqtt_client_thread(void *arg); | ||
void end_mqtt_client_thread(); // notifies the thread to stop. does not wait for the thread | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.