diff options
-rw-r--r-- | apps/apps.cpp | 30 | ||||
-rw-r--r-- | apps/apps.h | 2 | ||||
-rw-r--r-- | apps/sm-serial-read.cpp | 18 |
3 files changed, 42 insertions, 8 deletions
diff --git a/apps/apps.cpp b/apps/apps.cpp index 2da8359..ca7cd81 100644 --- a/apps/apps.cpp +++ b/apps/apps.cpp @@ -1,6 +1,7 @@ #include "apps.h" #include <log4cplus/logger.h> #include <log4cplus/configurator.h> +#include <netdb.h> namespace trygvis { namespace apps { @@ -59,6 +60,35 @@ int launch_app(int argc, char *argv[], app& app) { return app.main(execution); } + +std::string get_hostname() { + struct addrinfo hints, *info, *p; +// int gai_result; + + char hostname[1024]; + hostname[1023] = '\0'; + gethostname(hostname, 1023); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + + if (getaddrinfo(hostname, "http", &hints, &info)) { + return "uknown"; + } + + string s = "unknown"; + + if (info) { + s = string(info->ai_canonname); + } + + freeaddrinfo(info); + + return s; +} + void app_execution::usage() { cerr << desc << endl; } diff --git a/apps/apps.h b/apps/apps.h index 3b15bdd..b50699a 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -30,6 +30,8 @@ public: int launch_app(int argc, char *argv[], app &app); +std::string get_hostname(); + } } diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index ff311a1..f1cabad 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -56,10 +56,13 @@ using namespace trygvis::apps; namespace po = boost::program_options; using json = nlohmann::json; +Format format; +string hostname = get_hostname(); + class port_handler { public: - port_handler(Format format, string device, serial_port &serial_port) : - format(format), device(device), port(serial_port) { + port_handler(string device, serial_port &serial_port) : + device(device), port(serial_port) { } void run() { @@ -103,6 +106,7 @@ public: static const string device_type = "serial"; if (format == Format::JSON) { json j; + j["hostname"] = hostname; j["device_type"] = device_type; j["device"] = device; j["timestamp"] = timestamp; @@ -110,14 +114,15 @@ public: j["value"] = value; cout << j << endl; } else if (format == Format::SQL) { - cout << "INSERT INTO serial_sample(device_type, device, sensor, timestamp, value) VALUES(" + cout << "INSERT INTO serial_sample(hostname, device_type, device, sensor, timestamp, value) VALUES(" + << hostname << ", " << device_type << ", " << device << ", " << timestamp << ", " << sensor << ", " << value << ");" << endl; - } else {// plain + } else { // plain cout << "sensor #" << sensor << " = " << value << endl; } @@ -131,15 +136,12 @@ private: static const uint8_t packet_delimiter = '\n'; string device; serial_port &port; - Format format; uint8_t data[size]; mutable_buffers_1 buffer = boost::asio::buffer(data, size); }; class sm_serial_read : public app { - Format format; - public: void add_options(po::options_description_easy_init &options) override { options @@ -170,7 +172,7 @@ public: cerr << "port is not open" << endl; } - port_handler(format, port_name, port).run(); + port_handler(port_name, port).run(); return EXIT_SUCCESS; } |