-
Notifications
You must be signed in to change notification settings - Fork 123
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 support. (#357)
* Cmake, readme, config changes to add libmosquitto. * 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. * adding libmosquitto-dev to travis-ci * tell libmosquitto to be thread safe * Add tls/cert support for MQTT * Add id with "unparse" name of channel as well to the topic. This should be to get a better understanding of what the channel actually is. E.g. will contains obis ids. * added reconnect handling if con. refused at startup This should allow startups where the mqtt server will be available later * Added a lock to protect chMap incase of multiple meters. * announce uuid only if not empty
- Loading branch information
Showing
15 changed files
with
656 additions
and
17 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
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,73 @@ | ||
/* | ||
* Author: Matthias Behr, mbehr (a) mcbehr dot de | ||
* (c) 2018 | ||
* */ | ||
|
||
#ifndef __mqtt_hpp_ | ||
#define __mqtt_hpp_ | ||
|
||
#include <string> | ||
#include <mutex> | ||
#include <unordered_map> | ||
#include <vector> | ||
#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; | ||
std::string _cafile; | ||
std::string _capath; | ||
std::string _certfile; | ||
std::string _keyfile; | ||
std::string _keypass; | ||
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::vector<std::pair<std::string, std::string>> _announceValues; | ||
void generateNames(const std::string &prefix, Channel &ch); | ||
}; | ||
std::mutex _chMapMutex; | ||
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.