Add zm_mqtt.cpp etc

This commit is contained in:
Isaac Connor 2021-12-22 16:18:03 -05:00
parent 675647006a
commit 355a6e70c7
3 changed files with 170 additions and 0 deletions

View File

@ -33,6 +33,7 @@ set(ZM_BIN_SRC_FILES
zm_local_camera.cpp zm_local_camera.cpp
zm_monitor.cpp zm_monitor.cpp
zm_monitorstream.cpp zm_monitorstream.cpp
zm_mqtt.cpp
zm_ffmpeg.cpp zm_ffmpeg.cpp
zm_ffmpeg_camera.cpp zm_ffmpeg_camera.cpp
zm_ffmpeg_input.cpp zm_ffmpeg_input.cpp

123
src/zm_mqtt.cpp Normal file
View File

@ -0,0 +1,123 @@
#include "zm.h"
#include "zm_logger.h"
#include "zm_mqtt.h"
#include "zm_time.h"
#include <sstream>
#include <string.h>
MQTT::MQTT(Monitor *monitor) :
mosquittopp(),
monitor_(monitor),
connected_(false)
{
mosqpp::lib_init();
}
void MQTT::connect(const char * hostname, unsigned int port, unsigned int keepalive) {
mosqpp::mosquittopp::connect(hostname, port, keepalive);
loop_start();
}
void MQTT::autoconfigure() {
}
void MQTT::disconnect() {
}
void MQTT::on_connect(int rc) {
Debug(1, "Connected with rc %d", rc);
if (rc == 0) connected_ = true;
}
void MQTT::on_message(const struct mosquitto_message *message) {
}
void MQTT::on_subscribe(int mid, int qos_count, const int *granted_qos) {
Debug(1, "Subscribed to topic ");
}
void MQTT::on_publish() {
}
void MQTT::send() {
while (!connected_) { std::this_thread::sleep_for(Seconds(1)); }
for (auto outer_iter=sensorList.begin(); outer_iter!=sensorList.end(); ++outer_iter) {
for (auto inner_iter=outer_iter->second.begin(); inner_iter!=outer_iter->second.end(); ++inner_iter) {
Debug(1, "%s %ld %f", outer_iter->first.c_str(), inner_iter->first.count(), inner_iter->second);
int mid;
std::stringstream mqtt_topic;
std::stringstream mqtt_payload;
mqtt_topic << "/a/";
mqtt_topic << api_key;
mqtt_topic << "/p/";
mqtt_topic << project_id;
mqtt_topic << "/device/" << device_id;
mqtt_topic << "/sensor/" << outer_iter->first;
mqtt_topic << "/data";
mqtt_payload << "{ \"value\":"<<inner_iter->second<<" }";
const std::string mqtt_topic_string = mqtt_topic.str();
const std::string mqtt_payload_string = mqtt_payload.str();
Debug(1, "DEBUG: MQTT TOPIC: %s", mqtt_topic_string.c_str());
publish(&mid, mqtt_topic_string.c_str(), mqtt_payload_string.length(), mqtt_payload_string.c_str(), 0, true);
auto this_iter = inner_iter;
inner_iter++;
outer_iter->second.erase(this_iter);
if (inner_iter == outer_iter->second.end()) {
break;
}
} // end foreach inner
} // end foreach outer
}
void MQTT::addSensor(std::string name, std::string type) {
std::map<std::chrono::milliseconds, double> valuesList;
sensorList.insert ( std::pair<std::string,std::map<std::chrono::milliseconds, double>>(name, valuesList));
}
void MQTT::addActuator(std::string name, std::function <void(int val)> f) {
int mid;
std::stringstream mqtt_topic;
mqtt_topic << "/a/";
mqtt_topic << api_key;
mqtt_topic << "/p/";
mqtt_topic << project_id;
mqtt_topic << "/device/" << device_id;
mqtt_topic << "/actuator/" << name;
mqtt_topic << "/state";
subscribe(&mid, mqtt_topic.str().c_str());
}
void MQTT::addValue(std::string name, double value) {
sensorListIterator = sensorList.find(name);
Debug(1, "found sensor: %s", sensorListIterator->first.c_str());
// if(it == sensorList.end()) {
// clog<<__FUNCTION__<<" Could not find coresponding sensor name"<<endl;
// } else {
//
// }
// valuesList.insert ( std::pair<std::string,double>(name, value));
std::chrono::milliseconds ms = std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::high_resolution_clock::now().time_since_epoch()
);
sensorListIterator->second.insert(std::pair<std::chrono::milliseconds, double>(ms, value));
}
void MQTT::listValues(const std::string &sensor_name) {
Debug(1, "%s", sensor_name.c_str());
auto sensorListIterator = sensorList.find(sensor_name);
Debug(1, "found sensor: %s", sensorListIterator->first.c_str());
for (auto inner_iter=sensorListIterator->second.begin(); inner_iter!=sensorListIterator->second.end(); ++inner_iter) {
std::cout << "ts: " << inner_iter->first.count() << ", value:" << inner_iter->second << std::endl;
}
}
MQTT::~MQTT() {
}

46
src/zm_mqtt.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef ZM_MQTT_H
#define ZM_MQTT_H
#include "mosquittopp.h"
#include <stdint.h>
#include <iostream>
#include <map>
#include <list>
#include <time.h>
#include <chrono>
#include <functional>
class Monitor;
class MQTT : public mosqpp::mosquittopp {
public:
MQTT(Monitor *);
~MQTT();
void connect(const char * hostname = "mqtt.zoneminder.com", unsigned int port = 1883, unsigned int keepalive = 60);
void autoconfigure();
void disconnect();
void send();
void addSensor(std::string name, std::string type);
void addActuator(std::string name, std::function <void(int val)> f);
void addValue(std::string name, double value);
void listValues(const std::string &sensor_name);
void on_connect(int rc);
void on_message(const struct mosquitto_message *message);
void on_subscribe(int mid, int qos_count, const int *granted_qos);
void on_publish();
enum sensorTypes {
NUMERIC = 0,
DIGITAL
};
private:
std::map<std::string, std::map<std::chrono::milliseconds, double>> sensorList;
std::map<std::string, std::map<std::chrono::milliseconds, double>>::iterator sensorListIterator;
std::map<std::string, int> actuatorList;
Monitor *monitor;
bool connected_;
};
#endif // ZM_MQTT_H