aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-get-value.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-01 23:05:02 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-01 23:20:41 +0100
commit8c5aad737561837f0b8d616cc03130b7546e45a9 (patch)
treeff19e700f3fc5e4f27f028caa57e026ea833e846 /apps/sm-get-value.cpp
parente64d925b45ff4802fe924ea2e8108bb1932b4d01 (diff)
downloadble-toys-8c5aad737561837f0b8d616cc03130b7546e45a9.tar.gz
ble-toys-8c5aad737561837f0b8d616cc03130b7546e45a9.tar.bz2
ble-toys-8c5aad737561837f0b8d616cc03130b7546e45a9.tar.xz
ble-toys-8c5aad737561837f0b8d616cc03130b7546e45a9.zip
o Trying to structure how apps are made.
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r--apps/sm-get-value.cpp121
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);
}