wmbusmeters/main.cc

113 wiersze
4.2 KiB
C++
Czysty Zwykły widok Historia

2017-08-09 10:00:11 +00:00
// Copyright (c) 2017 Fredrik Öhrström
//
2017-08-09 10:00:11 +00:00
// 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:
//
2017-08-09 10:00:11 +00:00
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
2017-08-09 10:00:11 +00:00
// 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.
#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"
#include"util.h"
2017-08-09 10:00:11 +00:00
#include"wmbus.h"
#include<string.h>
using namespace std;
2017-09-02 21:26:57 +00:00
void oneshotCheck(CommandLine *cmdline, SerialCommunicationManager *manager, Meter *meter);
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);
if (cmdline->need_help) {
printf("wmbusmeters version: " WMBUSMETERS_VERSION "\n");
printf("Usage: wmbusmeters [options] (auto | /dev/ttyUSBx) { [meter_name] [meter_id] [meter_key] }* \n\n");
printf("Add more meter triplets to listen to more meters.\n");
printf("Add --verbose for more detailed information on communication.\n");
2017-09-02 21:26:57 +00:00
printf(" --robot for json output.\n");
printf(" --meterfiles to create status files below tmp,\n"
" named /tmp/meter_name, containing the latest reading.\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");
exit(0);
}
// We want the data visible in the log file asap!
setbuf(stdout, NULL);
warningSilenced(cmdline->silence);
verboseEnabled(cmdline->verbose);
debugEnabled(cmdline->debug);
2017-08-09 10:00:11 +00:00
auto manager = createSerialCommunicationManager();
onExit(call(manager,stop));
WMBus *wmbus = NULL;
2017-08-09 10:00:11 +00:00
auto type_and_device = detectMBusDevice(cmdline->usb_device, manager);
switch (type_and_device.first) {
case DEVICE_IM871A:
verbose("(im871a) detected on %s\n", type_and_device.second.c_str());
wmbus = openIM871A(type_and_device.second, manager);
break;
case DEVICE_AMB8465:
verbose("(amb8465) detected on %s\n", type_and_device.second.c_str());
wmbus = openAMB8465(type_and_device.second, manager);
break;
case DEVICE_UNKNOWN:
error("No wmbus device found!\n");
exit(1);
break;
}
wmbus->setLinkMode(LinkModeC1);
if (wmbus->getLinkMode()!=LinkModeC1) error("Could not set link mode to receive C1 telegrams.\n");
2017-09-02 21:26:57 +00:00
Printer *output = new Printer(cmdline->robot, cmdline->meterfiles);
if (cmdline->meters.size() > 0) {
for (auto &m : cmdline->meters) {
2017-09-02 21:26:57 +00:00
m.meter = createMultical21(wmbus, m.name, m.id, m.key);
verbose("(multical21) configured \"%s\" \"%s\" \"%s\"\n", m.name, m.id, m.key);
2017-09-02 21:26:57 +00:00
m.meter->onUpdate(calll(output,print,Meter*));
m.meter->onUpdate([cmdline,manager](Meter*meter) { oneshotCheck(cmdline,manager,meter); });
}
} else {
printf("No meters configured. Printing id:s of all telegrams heard!\n\n");
2017-09-02 21:26:57 +00:00
wmbus->onTelegram([](Telegram *t){t->print();});
}
manager->waitForStop();
2017-08-09 10:00:11 +00:00
}
2017-09-02 21:26:57 +00:00
void oneshotCheck(CommandLine *cmdline, SerialCommunicationManager *manager, Meter *meter)
{
if (!cmdline->oneshot) return;
2017-09-02 21:26:57 +00:00
for (auto &m : cmdline->meters) {
if (m.meter->numUpdates() == 0) return;
}
// All meters have received at least one update! Stop!
manager->stop();
}