From 68b20665be3be6b7b06094e3d60a3333aef7e151 Mon Sep 17 00:00:00 2001 From: weetmuts Date: Mon, 5 Mar 2018 13:42:56 +0100 Subject: [PATCH] Added support for specifying in which dir to store the meterfiles (ie not just /tmp). --- README.md | 5 +++-- cmdline.cc | 14 +++++++++++++- cmdline.h | 1 + main.cc | 7 ++++--- printer.cc | 5 +++-- printer.h | 3 ++- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 05951fb..30e3206 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,9 @@ Add --verbose for detailed debug information. --robot or --robot=json for json output. --robot=fields for semicolon separated fields. --separator=X change field separator to X. - --meterfiles to create status files below tmp, - named /tmp/meter_name, containing the latest reading. + --meterfiles=dir to create status files below dir, + named dir/meter_name, containing the latest reading. + --meterfiles defaults dir to /tmp. --oneshot wait for an update from each meter, then quit. Specifying auto as the device will automatically look for usb diff --git a/cmdline.cc b/cmdline.cc index 74974ac..2de666f 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -88,8 +88,20 @@ CommandLine *parseCommandLine(int argc, char **argv) { i++; continue; } - if (!strcmp(argv[i], "--meterfiles")) { + if (!strncmp(argv[i], "--meterfiles", 12)) { c->meterfiles = true; + if (strlen(argv[i]) > 12 && argv[i][12] == '=') { + size_t len = strlen(argv[i])-13; + if (len > 0) { + c->meterfiles_dir = new char[len+1]; + strncpy((char*)c->meterfiles_dir, argv[i]+13, len); + } else { + c->meterfiles_dir = "/tmp"; + } + } else { + c->meterfiles_dir = "/tmp"; + } + verbose("Storing meter files here: %s\n", c->meterfiles_dir); i++; continue; } diff --git a/cmdline.h b/cmdline.h index 4d3d101..919fdfd 100644 --- a/cmdline.h +++ b/cmdline.h @@ -49,6 +49,7 @@ struct CommandLine { bool debug {}; bool logtelegrams {}; bool meterfiles {}; + const char *meterfiles_dir {}; bool json {}; bool fields {}; char separator { ';' }; diff --git a/main.cc b/main.cc index 13b7186..2c5c27c 100644 --- a/main.cc +++ b/main.cc @@ -43,8 +43,9 @@ int main(int argc, char **argv) printf(" --robot or --robot=json for json output.\n"); printf(" --robot=fields for semicolon separated fields.\n"); printf(" --separator=X change field separator to X.\n"); - printf(" --meterfiles to create status files below tmp,\n" - " named /tmp/meter_name, containing the latest reading.\n"); + printf(" --meterfiles=dir to create status files below dir,\n" + " named dir/meter_name, containing the latest reading.\n"); + printf(" --meterfiles defaults dir to /tmp.\n"); printf(" --oneshot wait for an update from each meter, then quit.\n\n"); printf("Specifying auto as the device will automatically look for usb\n"); printf("wmbus dongles on /dev/im871a and /dev/amb8465\n\n"); @@ -90,7 +91,7 @@ int main(int argc, char **argv) if (wmbus->getLinkMode()!=LinkModeC1) error("Could not set link mode to receive C1 telegrams.\n"); Printer *output = new Printer(cmdline->json, cmdline->fields, - cmdline->separator, cmdline->meterfiles); + cmdline->separator, cmdline->meterfiles, cmdline->meterfiles_dir); if (cmdline->meters.size() > 0) { for (auto &m : cmdline->meters) { diff --git a/printer.cc b/printer.cc index b20ada8..9b7596b 100644 --- a/printer.cc +++ b/printer.cc @@ -22,12 +22,13 @@ using namespace std; -Printer::Printer(bool json, bool fields, char separator, bool meterfiles) +Printer::Printer(bool json, bool fields, char separator, bool meterfiles, const char *meterfiles_dir) { json_ = json; fields_ = fields; separator_ = separator; meterfiles_ = meterfiles; + meterfiles_dir_ = meterfiles_dir; } void Printer::print(Meter *meter) @@ -37,7 +38,7 @@ void Printer::print(Meter *meter) if (meterfiles_) { char filename[128]; memset(filename, 0, sizeof(filename)); - snprintf(filename, 127, "/tmp/%s", meter->name().c_str()); + snprintf(filename, 127, "%s/%s", meterfiles_dir_, meter->name().c_str()); output = fopen(filename, "w"); } diff --git a/printer.h b/printer.h index 38506e5..0cd4a4d 100644 --- a/printer.h +++ b/printer.h @@ -24,12 +24,13 @@ using namespace std; struct Printer { - Printer(bool json, bool fields, char separator, bool meterfiles); + Printer(bool json, bool fields, char separator, bool meterfiles, const char *meterfiles_dir); void print(Meter *meter); private: bool json_, fields_, meterfiles_; + const char *meterfiles_dir_; char separator_; };