wmbusmeters/src/config.cc

234 wiersze
6.1 KiB
C++
Czysty Zwykły widok Historia

/*
Copyright (C) 2019 Fredrik Öhrström
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"config.h"
#include"meters.h"
#include<vector>
#include<string>
using namespace std;
pair<string,string> getNextKeyValue(vector<char> &buf, vector<char>::iterator &i)
{
bool eof, err;
string key, value;
key = eatToSkipWhitespace(buf, i, '=', 64, &eof, &err);
if (eof || err) goto nomore;
value = eatToSkipWhitespace(buf, i, '\n', 4096, &eof, &err);
if (err) goto nomore;
return { key, value };
nomore:
return { "", "" };
}
2019-02-24 13:08:51 +00:00
void parseMeterConfig(Configuration *c, vector<char> &buf, string file)
{
auto i = buf.begin();
string name;
string type;
string id;
string key;
for (;;) {
auto p = getNextKeyValue(buf, i);
if (p.first == "") break;
if (p.first == "name") name = p.second;
if (p.first == "type") type = p.second;
if (p.first == "id") id = p.second;
if (p.first == "key") key = p.second;
}
MeterType mt = toMeterType(type);
2019-02-23 21:06:50 +00:00
bool use = true;
if (mt == UNKNOWN_METER) {
warning("Not a valid meter type \"%s\"\n", type.c_str());
use = false;
}
if (!isValidId(id)) {
warning("Not a valid meter id \"%s\"\n", id.c_str());
use = false;
}
if (!isValidKey(key)) {
warning("Not a valid meter key \"%s\"\n", key.c_str());
use = false;
}
2019-02-23 21:06:50 +00:00
if (use) {
c->meters.push_back(MeterInfo(name, type, id, key));
}
return;
}
2019-02-24 13:08:51 +00:00
void handleLoglevel(Configuration *c, string loglevel)
2019-02-23 20:21:17 +00:00
{
if (loglevel == "verbose") { c->verbose = true; }
else if (loglevel == "debug") { c->debug = true; }
else if (loglevel == "silent") { c->silence = true; }
else if (loglevel == "normal") { }
else {
warning("No such log level: \"%s\"\n", loglevel.c_str());
}
}
2019-02-24 13:08:51 +00:00
void handleDevice(Configuration *c, string device)
2019-02-23 20:21:17 +00:00
{
2019-02-25 21:03:20 +00:00
// device can be:
// /dev/ttyUSB00
// auto
// rtlwmbus:/usr/bin/rtl_sdr -f 868.9M -s 1600000 - 2>/dev/null | /usr/bin/rtl_wmbus
// simulation....txt (read telegrams from file)
size_t p = device.find (':');
if (p != string::npos)
{
c->device_extra = device.substr(p+1);
c->device = device.substr(0,p);
} else {
c->device = device;
}
2019-02-23 20:21:17 +00:00
}
2019-02-24 13:08:51 +00:00
void handleLogtelegrams(Configuration *c, string logtelegrams)
2019-02-23 20:21:17 +00:00
{
if (logtelegrams == "true") { c->logtelegrams = true; }
else if (logtelegrams == "false") { c->logtelegrams = false;}
else {
warning("No such logtelegrams setting: \"%s\"\n", logtelegrams.c_str());
}
}
2019-02-24 13:08:51 +00:00
void handleMeterfilesdir(Configuration *c, string meterfilesdir)
2019-02-23 20:21:17 +00:00
{
if (meterfilesdir.length() > 0)
{
c->meterfiles_dir = meterfilesdir;
c->meterfiles = true;
if (!checkIfDirExists(c->meterfiles_dir.c_str())) {
warning("Cannot write meter files into dir \"%s\"\n", c->meterfiles_dir.c_str());
}
}
}
2019-02-24 16:31:32 +00:00
void handleMeterfilestype(Configuration *c, string meterfilestype)
{
if (meterfilestype == "overwrite")
{
c->meterfiles_type = MeterFileType::Overwrite;
} else if (meterfilestype == "append")
{
c->meterfiles_type = MeterFileType::Append;
} else {
warning("No such meter file type \"%s\"\n", meterfilestype.c_str());
}
}
2019-02-24 14:20:55 +00:00
void handleLogfile(Configuration *c, string logfile)
{
if (logfile.length() > 0)
{
c->use_logfile = true;
c->logfile = logfile;
}
}
2019-02-24 13:08:51 +00:00
void handleRobot(Configuration *c, string robot)
2019-02-23 20:21:17 +00:00
{
if (robot == "json")
{
c->json = true;
c->fields = false;
}
else if (robot == "fields")
{
c->json = false;
c->fields = true;
c->separator = ';';
} else {
warning("Unknown output format: \"%s\"\n", robot.c_str());
}
}
2019-02-24 13:08:51 +00:00
void handleSeparator(Configuration *c, string s)
2019-02-23 20:21:17 +00:00
{
if (s.length() == 1) {
c->separator = s[0];
} else {
warning("Separator must be a single character.\n");
}
}
2019-02-24 16:31:32 +00:00
void handleShell(Configuration *c, string cmdline)
{
c->shells.push_back(cmdline);
}
2019-02-24 13:08:51 +00:00
unique_ptr<Configuration> loadConfiguration(string root)
{
2019-02-24 13:08:51 +00:00
Configuration *c = new Configuration;
2019-02-26 08:33:10 +00:00
// JSon is default when configuring from config files.
c->json = true;
vector<char> global_conf;
2019-02-26 08:33:10 +00:00
bool ok = loadFile(root+"/etc/wmbusmeters.conf", &global_conf);
2019-02-23 21:06:50 +00:00
global_conf.push_back('\n');
2019-02-26 08:33:10 +00:00
if (!ok) exit(1);
auto i = global_conf.begin();
for (;;) {
auto p = getNextKeyValue(global_conf, i);
if (p.first == "") break;
2019-02-23 20:21:17 +00:00
if (p.first == "loglevel") handleLoglevel(c, p.second);
else if (p.first == "device") handleDevice(c, p.second);
else if (p.first == "logtelegrams") handleLogtelegrams(c, p.second);
else if (p.first == "meterfilesdir") handleMeterfilesdir(c, p.second);
2019-02-24 16:31:32 +00:00
else if (p.first == "meterfilestype") handleMeterfilestype(c, p.second);
2019-02-24 14:20:55 +00:00
else if (p.first == "logfile") handleLogfile(c, p.second);
2019-02-23 20:21:17 +00:00
else if (p.first == "robot") handleRobot(c, p.second);
else if (p.first == "separator") handleSeparator(c, p.second);
2019-02-24 16:31:32 +00:00
else if (p.first == "shell") handleShell(c, p.second);
2019-02-23 20:21:17 +00:00
else
{
warning("No such key: %s\n", p.first.c_str());
}
}
2019-02-23 20:21:17 +00:00
vector<string> meters;
listFiles(root+"/etc/wmbusmeters.d", &meters);
2019-02-23 20:21:17 +00:00
for (auto& f : meters)
{
vector<char> meter_conf;
2019-02-23 20:21:17 +00:00
string file = root+"/etc/wmbusmeters.d/"+f;
loadFile(file.c_str(), &meter_conf);
2019-02-23 21:06:50 +00:00
meter_conf.push_back('\n');
parseMeterConfig(c, meter_conf, file);
}
2019-02-24 13:08:51 +00:00
return unique_ptr<Configuration>(c);
}