Commit 181a662a authored by root's avatar root
Browse files

SysFsPusher w.i.p.

parent 4fd96664
include ../config.mk
CXXFLAGS = -O2 --std=c++11 -g -Wall -Werror -I$(DCDBDEPLOYPATH)/include
CXXFLAGS = -std=c++0x -O0 -g -Wall -Werror -I$(DCDBDEPLOYPATH)/include
OBJS = sysfspusher.o helper.o
LIBS = -L$(DCDBDEPLOYPATH)/lib -lrudeconfig -lmosquitto -lssl -lcrypto -lpthread
LIBS = -L$(DCDBDEPLOYPATH)/lib -lrudeconfig -lmosquitto -lpthread
TARGET = sysfspusher
$(TARGET): $(OBJS)
......
......@@ -7,15 +7,23 @@
#include <csignal>
#include <cerrno>
#include <unistd.h>
#include <rude/config.h>
#include <mosquitto.h>
#include "helper.h"
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
using namespace std;
typedef struct {
string name;
string path;
string mqtt;
bool convert;
int64_t minI;
int64_t maxI;
int64_t maxV;
FILE* file;
int interval;
uint64_t nextRead;
......@@ -23,6 +31,7 @@ typedef struct {
vector<sensor_t> sensorList;
string broker;
string mqttprefix;
int keepRunning = 1;
void sigHandler(int sig) {
......@@ -30,51 +39,129 @@ void sigHandler(int sig) {
}
int readConfiguration(string configFileName) {
rude::Config config;
if (config.load(configFileName.c_str())) {
for (int i = 0; i < config.getNumSections(); i++) {
const char* sectionName = config.getSectionNameAt(i);
if (strlen(sectionName) == 0) {
config.setSection(sectionName);
for (int j = 0; j < config.getNumDataMembers(); j++) {
const char* dataName = config.getDataNameAt(j);
if (strcasecmp(dataName, "broker") == 0) {
broker = config.getStringValue(dataName);
cout << "Broker: " << broker << endl;
} else {
cerr << "Unknown configuration parameter \"" << dataName << "\"";
}
using namespace boost::property_tree;
ptree pt;
ini_parser::read_ini(configFileName, pt);
broker = pt.get<string>("broker");
mqttprefix = pt.get<string>("mqttprefix");
BOOST_FOREACH(ptree::value_type &v, pt) {
if (!v.second.empty()) {
sensor_t sensorConfig;
BOOST_FOREACH(ptree::value_type &v2, pt.get_child(v.first.data())) {
sensorConfig.name = v.first.data();
sensorConfig.path = "";
sensorConfig.interval = 1000;
sensorConfig.mqtt = "";
sensorConfig.convert = false;
const char* dataName = v2.first.data();
string sn(v.first.data());
if (strcasecmp(dataName, "path") == 0) {
cout << "getting " << sn << ".path...";
sensorConfig.path = pt.get<char*>(sn + ".path");
} else if (strcasecmp(dataName, "interval") == 0) {
sensorConfig.interval = pt.get<int>(sn + ".interval");
} else if (strcasecmp(dataName, "mqtt") == 0) {
sensorConfig.mqtt = pt.get<char*>(sn + ".mqtt");
} else if (strcasecmp(dataName, "minI") == 0) {
sensorConfig.convert = true;
sensorConfig.minI = pt.get<uint64_t>(sn + ".minI");
} else if (strcasecmp(dataName, "maxI") == 0) {
sensorConfig.convert = true;
sensorConfig.maxI = pt.get<uint64_t>(sn + ".maxI");
} else if (strcasecmp(dataName, "maxV") == 0) {
sensorConfig.convert = true;
sensorConfig.maxV = pt.get<uint64_t>(sn + ".maxV");
}
}
cout << "Sensor \"" << sensorConfig.name << "\"" << endl;
cout << " Path : " << sensorConfig.path << endl;
cout << " MQTT : " << sensorConfig.mqtt << endl;
cout << " Interval: " << sensorConfig.interval << endl;
if (sensorConfig.convert) {
cout << " minI : " << sensorConfig.minI << endl;
cout << " maxI : " << sensorConfig.maxI << endl;
cout << " maxV : " << sensorConfig.maxV << endl;
}
sensorList.push_back(sensorConfig);
}
}
#if 0
for (int i = 0; i < config.getNumSections(); i++) {
const char* sectionName = config.getSectionNameAt(i);
if (strlen(sectionName) == 0) {
config.setSection(sectionName);
for (int j = 0; j < config.getNumDataMembers(); j++) {
const char* dataName = config.getDataNameAt(j);
if (strcasecmp(dataName, "broker") == 0) {
broker = config.getStringValue(dataName);
cout << "Broker: " << broker << endl;
}
} else {
sensor_t sensorConfig;
sensorConfig.name = sectionName;
sensorConfig.path = "";
sensorConfig.interval = 1000;
config.setSection(sectionName);
for (int j = 0; j < config.getNumDataMembers(); j++) {
const char* dataName = config.getDataNameAt(j);
if (strcasecmp(dataName, "path") == 0) {
sensorConfig.path = config.getStringValue(dataName);
} else if (strcasecmp(dataName, "interval") == 0) {
sensorConfig.interval = config.getIntValue(dataName);
}
else if (strcasecmp(dataName, "mqttprefix") == 0) {
mqttprefix = config.getStringValue(dataName);
cout << "MQTT prefix: " << mqttprefix << endl;
}
else {
cerr << "Unknown configuration parameter \"" << dataName << "\"";
}
}
} else {
sensor_t sensorConfig;
sensorConfig.name = sectionName;
sensorConfig.path = "";
sensorConfig.interval = 1000;
sensorConfig.mqtt = "";
sensorConfig.convert = false;
cout << "Sensor \"" << sensorConfig.name << "\"" << endl;
cout << " Path : " << sensorConfig.path << endl;
cout << " Interval: " << sensorConfig.interval << endl;
sensorList.push_back(sensorConfig);
config.setSection(sectionName);
for (int j = 0; j < config.getNumDataMembers(); j++) {
const char* dataName = config.getDataNameAt(j);
if (strcasecmp(dataName, "path") == 0) {
sensorConfig.path = config.getStringValue(dataName);
} else if (strcasecmp(dataName, "interval") == 0) {
sensorConfig.interval = config.getIntValue(dataName);
} else if (strcasecmp(dataName, "mqtt") == 0) {
sensorConfig.mqtt = config.getStringValue(dataName);
} else if (strcasecmp(dataName, "minI") == 0) {
sensorConfig.convert = true;
sensorConfig.minI = config.getIntValue(dataName);
} else if (strcasecmp(dataName, "maxI") == 0) {
sensorConfig.convert = true;
sensorConfig.maxI = config.getIntValue(dataName);
} else if (strcasecmp(dataName, "maxV") == 0) {
sensorConfig.convert = true;
sensorConfig.maxV = config.getIntValue(dataName);
}
}
cout << "Sensor \"" << sensorConfig.name << "\"" << endl;
cout << " Path : " << sensorConfig.path << endl;
cout << " MQTT : " << sensorConfig.mqtt << endl;
cout << " Interval: " << sensorConfig.interval << endl;
if (sensorConfig.convert) {
cout << " minI : " << sensorConfig.minI << endl;
cout << " maxI : " << sensorConfig.maxI << endl;
cout << " maxV : " << sensorConfig.maxV << endl;
}
sensorList.push_back(sensorConfig);
}
} else {
cerr << "Error reading configuration file: " << configFileName << endl;
}
config.clear();
#endif
return 0;
}
uint64_t translate(uint64_t val, uint64_t minI, uint64_t maxI, uint64_t maxV) {
uint64_t ret = 0;
if (val > minI) {
ret = (val-minI) * maxV / (maxI-minI);
}
return ret;
}
int main(int argc, char** argv) {
readConfiguration(argv[1]);
......@@ -92,7 +179,7 @@ int main(int argc, char** argv) {
cout << "Hostname: " << hostname << endl;
string clientId("/00/01/0203/04/05/06/07/10111213/FFFF/202");
string clientId(mqttprefix);
struct mosquitto* mosq;
mosq = mosquitto_new(clientId.c_str(), false, NULL);
if (!mosq) {
......@@ -138,9 +225,14 @@ int main(int argc, char** argv) {
buf[strlen(buf)-1] = 0;
payload[0] = atoi(buf);
payload[1] = currentTime;
cout << "[" << currentTime << "] " << sensorList[i].name << ": \"" << buf << "\""<< endl;
if (mosquitto_publish(mosq, NULL, (clientId + to_string(i)).c_str(), sizeof(payload), payload, 0, false) != MOSQ_ERR_SUCCESS) {
if (sensorList[i].convert) {
payload[0] = translate(payload[0], sensorList[i].minI, sensorList[i].maxI, sensorList[i].maxV);
}
cout << "[" << currentTime << "] " << sensorList[i].name << ": \"" << payload[0] << "\""<< endl;
if (mosquitto_publish(mosq, NULL, (clientId + sensorList[i].mqtt).c_str(), sizeof(payload), payload, 0, false) != MOSQ_ERR_SUCCESS) {
cerr << "Warning: cannot send message." << endl;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment