aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--apps/apps.cpp44
-rw-r--r--apps/apps.h24
-rw-r--r--apps/log4cplus-test.cpp3
-rw-r--r--apps/sm-get-value.cpp121
4 files changed, 126 insertions, 66 deletions
diff --git a/apps/apps.cpp b/apps/apps.cpp
index 21c4fe6..2da8359 100644
--- a/apps/apps.cpp
+++ b/apps/apps.cpp
@@ -6,6 +6,7 @@ namespace trygvis {
namespace apps {
using namespace log4cplus;
+using namespace std;
namespace po = boost::program_options;
const po::options_description logging_options() {
@@ -19,5 +20,48 @@ void setup_logging(po::variables_map vm) {
config.configure();
}
+int launch_app(int argc, char *argv[], app& app) {
+ po::options_description desc("Options");
+
+ auto x = desc.add_options();
+ app.add_options(x);
+
+ desc.add(logging_options());
+
+ po::variables_map vm;
+ auto parsed = po::parse_command_line(argc, argv, desc);
+ po::store(parsed, vm);
+
+ app_execution execution(desc, vm);
+
+ try {
+ po::notify(vm);
+ } catch (boost::program_options::required_option &e) {
+ cerr << "Missing required option: " << e.get_option_name() << endl;
+ execution.usage();
+ return EXIT_FAILURE;
+ }
+
+ auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional);
+
+ if (vm.count("help")) {
+ cerr << desc << "\n";
+ return EXIT_FAILURE;
+ }
+
+ if (unrecognized.size()) {
+ cerr << "Unrecognized option: " << unrecognized.at(0) << "\n";
+ return EXIT_FAILURE;
+ }
+
+ setup_logging(vm);
+
+ return app.main(execution);
+}
+
+void app_execution::usage() {
+ cerr << desc << endl;
+}
+
}
}
diff --git a/apps/apps.h b/apps/apps.h
index f1cdb8a..3b15bdd 100644
--- a/apps/apps.h
+++ b/apps/apps.h
@@ -6,9 +6,29 @@
namespace trygvis {
namespace apps {
-const boost::program_options::options_description logging_options();
+namespace po = boost::program_options;
-void setup_logging(boost::program_options::variables_map vm);
+class app_execution {
+public:
+ app_execution(po::options_description desc, po::variables_map vm) : desc(desc), vm(vm) {
+ }
+
+ po::options_description desc;
+ po::variables_map vm;
+
+ void usage();
+};
+
+class app {
+public:
+
+ virtual void add_options(po::options_description_easy_init& options) {
+ };
+
+ virtual int main(app_execution &execution) = 0;
+};
+
+int launch_app(int argc, char *argv[], app &app);
}
}
diff --git a/apps/log4cplus-test.cpp b/apps/log4cplus-test.cpp
index f53569f..39d9884 100644
--- a/apps/log4cplus-test.cpp
+++ b/apps/log4cplus-test.cpp
@@ -4,10 +4,11 @@
namespace trygvis {
using namespace log4cplus;
+using namespace std;
class LogSetup {
public:
- LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) {
+ LogSetup(string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) {
}
protected:
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);
}