2017-08-09 10:00:11 +00:00
|
|
|
// Copyright (c) 2017 Fredrik Öhrström
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
2017-08-31 08:58:39 +00:00
|
|
|
|
|
|
|
#include"cmdline.h"
|
|
|
|
#include"meters.h"
|
2017-09-02 21:26:57 +00:00
|
|
|
#include"printer.h"
|
2017-08-09 10:00:11 +00:00
|
|
|
#include"serial.h"
|
2017-08-31 08:58:39 +00:00
|
|
|
#include"util.h"
|
2017-08-09 10:00:11 +00:00
|
|
|
#include"wmbus.h"
|
|
|
|
|
|
|
|
#include<string.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2017-08-30 12:10:23 +00:00
|
|
|
|
2017-09-02 21:26:57 +00:00
|
|
|
void oneshotCheck(CommandLine *cmdline, SerialCommunicationManager *manager, Meter *meter);
|
2017-08-31 08:58:39 +00:00
|
|
|
|
2017-08-09 10:00:11 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-09-02 21:26:57 +00:00
|
|
|
CommandLine *cmdline = parseCommandLine(argc, argv);
|
2017-08-31 08:58:39 +00:00
|
|
|
|
2017-09-02 10:17:54 +00:00
|
|
|
if (cmdline->need_help) {
|
2017-08-31 08:58:39 +00:00
|
|
|
printf("wmbusmeters version: " WMBUSMETERS_VERSION "\n");
|
|
|
|
printf("Usage: wmbusmeters [--verbose] [--robot] [usbdevice] { [meter_name] [meter_id] [meter_key] }* \n");
|
2017-09-02 10:17:54 +00:00
|
|
|
printf("\nAdd more meter quadruplets to listen to more meters.\n");
|
2017-08-31 08:58:39 +00:00
|
|
|
printf("Add --verbose for detailed debug information.\n");
|
2017-09-02 21:26:57 +00:00
|
|
|
printf(" --robot for json output.\n");
|
|
|
|
printf(" --meterfiles to create status files below tmp,\n"
|
2017-09-02 10:17:54 +00:00
|
|
|
" named /tmp/meter_name, containing the latest reading.\n");
|
2017-09-02 21:26:57 +00:00
|
|
|
printf(" --oneshot wait for an update from each meter, then quit.\n");
|
2017-08-30 12:10:23 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-09-02 10:17:54 +00:00
|
|
|
verboseEnabled(cmdline->verbose);
|
2017-08-31 08:58:39 +00:00
|
|
|
|
2017-08-09 10:00:11 +00:00
|
|
|
auto manager = createSerialCommunicationManager();
|
|
|
|
|
|
|
|
onExit(call(manager,stop));
|
|
|
|
|
2017-09-02 10:17:54 +00:00
|
|
|
auto wmbus = openIM871A(cmdline->usb_device, manager);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
|
|
|
wmbus->setLinkMode(C1a);
|
|
|
|
if (wmbus->getLinkMode()!=C1a) error("Could not set link mode to C1a\n");
|
|
|
|
|
2017-08-30 18:08:02 +00:00
|
|
|
// We want the data visible in the log file asap!
|
|
|
|
setbuf(stdout, NULL);
|
2017-09-02 21:26:57 +00:00
|
|
|
|
|
|
|
Printer *output = new Printer(cmdline->robot, cmdline->meterfiles);
|
2017-08-30 18:08:02 +00:00
|
|
|
|
2017-09-02 10:17:54 +00:00
|
|
|
if (cmdline->meters.size() > 0) {
|
|
|
|
for (auto &m : cmdline->meters) {
|
2017-08-31 08:58:39 +00:00
|
|
|
verbose("Configuring meter: \"%s\" \"%s\" \"%s\"\n", m.name, m.id, m.key);
|
2017-09-02 21:26:57 +00:00
|
|
|
m.meter = createMultical21(wmbus, m.name, m.id, m.key);
|
|
|
|
m.meter->onUpdate(calll(output,print,Meter*));
|
|
|
|
m.meter->onUpdate([cmdline,manager](Meter*meter) { oneshotCheck(cmdline,manager,meter); });
|
2017-08-30 12:10:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-31 08:58:39 +00:00
|
|
|
printf("No meters configured. Printing id:s of all telegrams heard! \n");
|
2017-08-30 12:10:23 +00:00
|
|
|
printf("To configure a meter, add a triplet to the command line: name id key\n");
|
|
|
|
printf("Where name is your string identifying the meter.\n");
|
|
|
|
printf(" id is the 8 digits printed on the meter.\n");
|
|
|
|
printf(" key is 32 hex digits with the aes key.\n\n");
|
|
|
|
|
2017-09-02 21:26:57 +00:00
|
|
|
wmbus->onTelegram([](Telegram *t){t->print();});
|
2017-08-30 12:10:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 10:00:11 +00:00
|
|
|
manager->waitForStop();
|
|
|
|
}
|
2017-09-02 21:26:57 +00:00
|
|
|
|
|
|
|
void oneshotCheck(CommandLine *cmdline, SerialCommunicationManager *manager, Meter *meter)
|
|
|
|
{
|
|
|
|
if (!cmdline->oneshot) return;
|
|
|
|
|
|
|
|
for (auto &m : cmdline->meters) {
|
|
|
|
if (m.meter->numUpdates() == 0) return;
|
|
|
|
}
|
|
|
|
// All meters have received at least one update! Stop!
|
|
|
|
manager->stop();
|
|
|
|
}
|