diff options
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r-- | apps/sm-get-value.cpp | 121 |
1 files changed, 58 insertions, 63 deletions
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 417fac7..2772b55 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -1,11 +1,6 @@ #include <iostream> #include <iomanip> #include <chrono> -#include <boost/log/core/core.hpp> -#include <boost/log/sinks.hpp> -#include <boost/log/trivial.hpp> -#include <boost/program_options.hpp> -#include <boost/utility/empty_deleter.hpp> #include <boost/uuid/uuid_io.hpp> #include <thread> #include "ble/Bluetooth.h" @@ -19,6 +14,7 @@ using namespace std::chrono; using namespace trygvis::apps; using namespace trygvis::bluetooth; using namespace trygvis::soil_moisture; +namespace po = boost::program_options; using json = nlohmann::json; enum class Format { @@ -122,77 +118,76 @@ void withConnection(BluetoothGatt &gatt) { } while (loop); } -int main(int argc, char *argv[]) { - namespace po = boost::program_options; - - po::options_description desc("Options"); - desc.add_options() - ("help", "produce help message") - ("device", po::value<string>()->required(), "MAC of device to poll") - ("sensor", po::value<vector<unsigned int>>(&sensors)->multitoken(), "Sensor to poll, defaults to all") - ("sleep", po::value<unsigned int>(&sleepTime)->default_value(0), - "How long to sleep in seconds between each poll. If not give, it will exit after first poll") - ("format", po::value<Format>(&format)->default_value(Format::PLAIN), "Output format"); - - desc.add(logging_options()); +namespace trygvis { +namespace apps { - po::variables_map vm; - auto parsed = po::parse_command_line(argc, argv, desc); - po::store(parsed, vm); - po::notify(vm); +class sm_get_value : public app { - auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional); +public: + void add_options(po::options_description_easy_init& options) override { + options + ("help", "produce help message") + ("device", po::value<string>()->required(), "MAC of device to poll") + ("sensor", po::value<vector<unsigned int>>(&sensors)->multitoken(), "Sensor to poll, defaults to all") + ("sleep", po::value<unsigned int>(&sleepTime)->default_value(0), + "How long to sleep in seconds between each poll. If not give, it will exit after first poll") + ("format", po::value<Format>(&format)->default_value(Format::PLAIN), "Output format"); - if (vm.count("help")) { - cerr << desc << "\n"; - return EXIT_FAILURE; } - if (unrecognized.size()) { - cerr << "Unrecognized option: " << unrecognized.at(0) << "\n"; - return EXIT_FAILURE; - } + int main(app_execution &execution) override { + __attribute__((unused)) + BluetoothSystem bluetoothSystem; + + auto desc = execution.desc; + auto vm = execution.vm; - setup_logging(vm); + try { + if (!vm.count("device")) { + cerr << "Missing required option: device" << endl; + cerr << desc << "\n"; + return EXIT_FAILURE; + } - __attribute__((unused)) - BluetoothSystem bluetoothSystem; + auto MAC = vm["device"].as<string>(); - try { - if (!vm.count("device")) { - cerr << "Missing required option: device" << endl; - cerr << desc << "\n"; - return EXIT_FAILURE; - } + Mac mac = Mac::parseMac(MAC); - auto MAC = vm["device"].as<string>(); - cout << "MAC " << MAC << "wat" << endl; - Mac mac = Mac::parseMac(MAC); + auto &adapter = getAdapter(0); - auto &adapter = getAdapter(0); + auto &device = adapter.getDevice(mac); - auto &device = adapter.getDevice(mac); + loop = sleepTime > 0; - loop = sleepTime > 0; + do { + cout << "Connecting to device: " << device.getMac().str() << endl; - do { - cout << "Connecting to device: " << device.getMac().str() << endl; + auto &gatt = device.connectGatt(); + try { + withConnection(gatt); + } catch (runtime_error &e) { + cout << "exception: " << e.what() << endl; + } + gatt.disconnect(); + } while (loop); - auto &gatt = device.connectGatt(); - try { - withConnection(gatt); - } catch (runtime_error &e) { - cout << "exception: " << e.what() << endl; - } - gatt.disconnect(); - } while (loop); - - return EXIT_SUCCESS; - } catch (std::runtime_error ex) { - cout << "std::runtime_error: " << ex.what() << endl; - return EXIT_FAILURE; - } catch (std::exception ex) { - cout << "std::exception: " << ex.what() << endl; - return EXIT_FAILURE; + return EXIT_SUCCESS; + } catch (std::runtime_error ex) { + cout << "std::runtime_error: " << ex.what() << endl; + return EXIT_FAILURE; + } catch (std::exception ex) { + cout << "std::exception: " << ex.what() << endl; + return EXIT_FAILURE; + } } + + +}; + +} +} + +int main(int argc, char *argv[]) { + sm_get_value app; + return trygvis::apps::launch_app(argc, argv, app); } |