humireader/main.cpp

144 lines
5.9 KiB
C++

#include "humireader.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <functional>
void showHelp() {
fprintf(stderr, "# Parameters");
fprintf(stderr, "# [--once] [--help] [--(a|i)(t|h)min] [--(a|i)(t|h)max] <dev-path>\n");
fprintf(stderr, "# --once: run once then exit, other read/output continously \n");
fprintf(stderr, "# -- help: this\n");
fprintf(stderr, "# devPath: e.g. /dev/ttyUSB0");
fprintf(stderr, "# other parameters for emitting warnings when limits exceeded like:\n");
fprintf(stderr, "# (a|i) sensor A or sensor I of Humimaster\n");
fprintf(stderr, "# (t|h) limit for temp or humidty\n");
fprintf(stderr, "# min or max temp or humidity (degree C or 0-100 in percent)\n");
fprintf(stderr, "# On limit exceed output will by like TR_SENS_A_TEMPFAIL=1\n");
}
int main(int argc, char **argv)
{
std::string devPath;
// Min max limit for sensor a / i, humidity / temp
int ahmin=0;
int ahmax=100;
int atmin=-9999;
int atmax=9999;
int ihmin=0;
int ihmax=100;
int itmin=-999;
int itmax=999;
bool runOnce = false;
std::string prmMask = std::string("--abcde=");
std::cout << "# TempReader 0.1 (c) 2018 MH" << std::endl;
std::cout << "# (reads temp+humidty from 'HumiMaster')" << std::endl;
try {
for(int i=1; i < argc; i++) {
auto s = std::string{argv[i]};
auto p = s.substr(0, prmMask.size());
auto limitNam = std::string();
int *currLimit = nullptr;
//fprintf(stderr, "%i %i %s\n",i, argc, s.c_str());
if (p.compare("--ahmin=") == 0) {
limitNam = "TR_A_HUMI_MIN";
currLimit = &ahmin;
}
else if (p.compare("--ahmax=") == 0) {
limitNam = "TR_A_HUMI_MAX";
currLimit = &ahmax;
}
else if (p.compare("--atmin=") == 0) {
limitNam = "TR_A_TEMP_MIN";
currLimit = &atmin;
}
else if (p.compare("--atmax=") == 0) {
limitNam = "TR_A_TEMP_MAX";
currLimit = &atmax;
}
else if (p.compare("--ihmin=") == 0) {
limitNam = "TR_I_HUMI_MIN";
currLimit = &ihmin;
}
else if (p.compare("--ihmax=") == 0) {
limitNam = "TR_I_HUMI_MAX";
currLimit = &ihmax;
}
else if (p.compare("--itmin=") == 0) {
limitNam = "TR_I_TEMP_MIN";
currLimit = &itmin;
}
else if (p.compare("--itmax=") == 0) {
limitNam = "TR_I_TEMP_MAX";
currLimit = &itmax;
}
else if (s.compare("--once") == 0) {
runOnce = true;
}
else if (s.compare("--help") == 0) {
showHelp();
return 0;
}
else if (i == argc -1) {
devPath = s;
}
if (currLimit != nullptr) {
*currLimit=strtol(&s.c_str()[prmMask.size()], nullptr, 10);
std::cout << limitNam << "=" << *currLimit << std::endl;
}
}
if (argc == 0 || devPath.size() == 0) {
showHelp();
return 1;
}
HumiReader reader(devPath);
reader.setCallback([&, runOnce](const HumiResult& res) -> bool {
fprintf(stdout, "# ------------\n");
fprintf(stdout, "TR_NEW_CYCLE=1\n");
fprintf(stdout, "TR_SENS_A_TEMP=%.2i\n", res.sensorATemp);
fprintf(stdout, "TR_SENS_A_TEMP_DISP=\"%+.3i°C\"\n", res.sensorATemp);
fprintf(stdout, "TR_SENS_A_HUMI=%i\n", res.sensorARelHumi);
fprintf(stdout, "TR_SENS_A_HUMI_DISP=\"%.3i%%rH\"\n", res.sensorARelHumi);
fprintf(stdout, "TR_SENS_I_TEMP=%.2i\n", res.sensorITemp);
fprintf(stdout, "TR_SENS_I_TEMP_DISP=\"%+.3i°C\"\n", res.sensorITemp);
fprintf(stdout, "TR_SENS_I_HUMI=%i\n", res.sensorIRelHumi);
fprintf(stdout, "TR_SENS_I_HUMI_DISP=\"%.3i%%rH\"\n", res.sensorIRelHumi);
//fprintf(stdout, "A: %+.3iC°, %.3i%%rH | I: %+.3iC°, %.3i%%rH\n", res.sensorATemp, res.sensorARelHumi, res.sensorITemp, res.sensorIRelHumi);
int attrip = (res.sensorATemp < atmin || res.sensorATemp > atmax) ? 1 : 0;
int ahtrip = (res.sensorARelHumi < ahmin || res.sensorARelHumi > ahmax) ? 1 : 0;
int ittrip = (res.sensorITemp < itmin || res.sensorITemp > itmax) ? 1 : 0;
int ihtrip = (res.sensorIRelHumi < ihmin || res.sensorIRelHumi > ihmax) ? 1 : 0;
int trip = attrip || ahtrip || ittrip || ihtrip ? 1 : 0;
fprintf(stdout, "TR_SENS_A_TEMP_TRIP=%i\n", attrip);
fprintf(stdout, "TR_SENS_A_HUMI_TRIP=%i\n", ahtrip);
fprintf(stdout, "TR_SENS_I_TEMP_TRIP=%i\n", ittrip);
fprintf(stdout, "TR_SENS_I_HUMI_TRIP=%i\n", ihtrip);
fprintf(stdout, "TR_TRIP=%i\n",trip);
//fprintf(stdout, "\n", res.sensorITemp, res.sensorIRelHumi);
return !runOnce;
});
reader.run();
return 0;
}
catch (const std::exception& e) {
std::cerr << "Error executing " << e.what() << std::endl;
return 1;
}
}