aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-04 08:14:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-04 08:14:18 +0100
commitf89ec560be3436050f7f55eec1da4a40662c0de8 (patch)
tree22be1dad96dd7a789de69332db9288621eaae8e3
parent1cf0d4c224b32e31d9cd9ba3a4719f57118321fb (diff)
downloadble-toys-f89ec560be3436050f7f55eec1da4a40662c0de8.tar.gz
ble-toys-f89ec560be3436050f7f55eec1da4a40662c0de8.tar.bz2
ble-toys-f89ec560be3436050f7f55eec1da4a40662c0de8.tar.xz
ble-toys-f89ec560be3436050f7f55eec1da4a40662c0de8.zip
o Adding JSON output format.
-rw-r--r--apps/sm-serial-read.cpp121
1 files changed, 73 insertions, 48 deletions
diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp
index 8d5e70b..4dab17b 100644
--- a/apps/sm-serial-read.cpp
+++ b/apps/sm-serial-read.cpp
@@ -4,6 +4,7 @@
#include <thread>
#include <boost/asio/serial_port.hpp>
#include <boost/regex.hpp>
+#include <chrono>
#include "json.hpp"
#include "apps.h"
@@ -55,65 +56,89 @@ using namespace trygvis::apps;
namespace po = boost::program_options;
using json = nlohmann::json;
-class sm_serial_read : public app {
-
- Format format;
-
- class port_handler {
- public:
- port_handler(serial_port &serial_port) : port(serial_port) {
- }
+class port_handler {
+public:
+ port_handler(Format format, string device, serial_port &serial_port) :
+ format(format), device(device), port(serial_port) {
+ }
- void run() {
- auto packet = make_shared<vector<uint8_t>>(1024);
+ void run() {
+ auto packet = make_shared<vector<uint8_t>>(1024);
- while (port.is_open()) {
- size_t some = port.read_some(buffer);
- for (int i = 0; i < some; i++) {
- uint8_t b = data[i];
+ while (port.is_open()) {
+ size_t some = port.read_some(buffer);
+ for (int i = 0; i < some; i++) {
+ uint8_t b = data[i];
- if (b == packet_delimiter) {
- on_packet(packet);
- packet = make_shared<vector<uint8_t>>(1024);
- } else {
- packet->emplace_back(b);
- }
+ if (b == packet_delimiter) {
+ on_packet(packet);
+ packet = make_shared<vector<uint8_t>>(1024);
+ } else {
+ packet->emplace_back(b);
}
}
-
- cout << "port closed" << endl;
}
- void on_packet(shared_ptr<vector<uint8_t>> packet) {
- auto s = std::string((char *) packet->data(), packet->size());
- cout << "packet: " << s << endl;
-
- static const boost::regex e("#(\\d+) = (\\d+)");
-
- std::string::const_iterator start = s.begin();
- std::string::const_iterator end = s.end();
- boost::match_results<std::string::const_iterator> what;
- boost::match_flag_type flags = boost::match_default;
-
- while(regex_search(start, end, what, e, flags)) {
- auto sensor = what[1];
- auto value = what[2];
- start = what[0].second;
+ cout << "port closed" << endl;
+ }
+ void on_packet(shared_ptr<vector<uint8_t>> packet) {
+// std::chrono::time_point<std::chrono::system_clock> ;
+ auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
+ auto s = std::string((char *) packet->data(), packet->size());
+ cout << "packet: " << s << endl;
+
+ static const boost::regex e("#(\\d+) = (\\d+)");
+
+ std::string::const_iterator start = s.begin();
+ std::string::const_iterator end = s.end();
+ boost::match_results<std::string::const_iterator> what;
+ boost::match_flag_type flags = boost::match_default;
+
+ while (regex_search(start, end, what, e, flags)) {
+ auto sensor = static_cast<string>(what[1]);
+ auto value = static_cast<string>(what[2]);
+ start = what[0].second;
+
+ static const string device_type = "serial";
+ if (format == Format::JSON) {
+ json j;
+ j["device_type"] = device_type;
+ j["device"] = device;
+ j["timestamp"] = timestamp;
+ j["sensor"] = sensor;
+ j["value"] = value;
+ cout << j << endl;
+ } else if (format == Format::SQL) {
+ cout << "INSERT INTO serial_sample(device_type, device, sensor, timestamp, value) VALUES("
+ << device_type << ", "
+ << device << ", "
+ << timestamp << ", "
+ << sensor << ", "
+ << value << ");"
+ << endl;
+ } else {// plain
cout << "sensor #" << sensor << " = " << value << endl;
-
- flags |= boost::match_prev_avail;
- flags |= boost::match_not_bob;
}
+
+ flags |= boost::match_prev_avail;
+ flags |= boost::match_not_bob;
}
+ }
- private:
- static const size_t size = 1024;
- static const uint8_t packet_delimiter = '\n';
- uint8_t data[size];
- mutable_buffers_1 buffer = boost::asio::buffer(data, size);
- serial_port &port;
- };
+private:
+ static const size_t size = 1024;
+ 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 {
@@ -145,7 +170,7 @@ public:
cout << "port is not open" << endl;
}
- port_handler(port).run();
+ port_handler(format, port_name, port).run();
return EXIT_SUCCESS;
}