aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-serial-read.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-06 21:07:34 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-06 21:07:34 +0100
commita83e6f5960a549d54991495336bd12d549127d91 (patch)
treed21f63b718d65074215fff6edbbcdbd5f26caf4a /apps/sm-serial-read.cpp
parente03bf5d416776cb5ea27c7354657920939c04e71 (diff)
downloadble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.gz
ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.bz2
ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.xz
ble-toys-a83e6f5960a549d54991495336bd12d549127d91.zip
o Starting on a tool to convert between sample formats.
Diffstat (limited to 'apps/sm-serial-read.cpp')
-rw-r--r--apps/sm-serial-read.cpp81
1 files changed, 47 insertions, 34 deletions
diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp
index f1cabad..d605ae4 100644
--- a/apps/sm-serial-read.cpp
+++ b/apps/sm-serial-read.cpp
@@ -1,12 +1,10 @@
-#include <iostream>
-#include <iomanip>
+#include "SoilMoistureIo.h"
+#include "json.hpp"
+#include "apps.h"
#include <chrono>
#include <thread>
#include <boost/asio/serial_port.hpp>
#include <boost/regex.hpp>
-#include <chrono>
-#include "json.hpp"
-#include "apps.h"
enum class Format {
PLAIN,
@@ -53,6 +51,7 @@ using namespace boost::asio;
using namespace std;
using namespace std::chrono;
using namespace trygvis::apps;
+using namespace trygvis::soil_moisture;
namespace po = boost::program_options;
using json = nlohmann::json;
@@ -61,8 +60,8 @@ string hostname = get_hostname();
class port_handler {
public:
- port_handler(string device, serial_port &serial_port) :
- device(device), port(serial_port) {
+ port_handler(string device, serial_port &serial_port, shared_ptr<SampleOutputStream> stream) :
+ device(device), port(serial_port), stream(stream) {
}
void run() {
@@ -86,12 +85,11 @@ public:
}
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());
cerr << "packet: " << s << endl;
- static const boost::regex e("#(\\d+) = (\\d+)");
+ static const boost::regex e("([a-zA-Z0-9]+)=([0-9]+)");
std::string::const_iterator start = s.begin();
std::string::const_iterator end = s.end();
@@ -104,27 +102,17 @@ public:
start = what[0].second;
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;
- j["sensor"] = sensor;
- j["value"] = value;
- cout << j << endl;
- } else if (format == Format::SQL) {
- cout << "INSERT INTO serial_sample(hostname, device_type, device, sensor, timestamp, value) VALUES("
- << hostname << ", "
- << device_type << ", "
- << device << ", "
- << timestamp << ", "
- << sensor << ", "
- << value << ");"
- << endl;
- } else { // plain
- cout << "sensor #" << sensor << " = " << value << endl;
- }
+ map<string, string> values;
+ values["hostname"] = hostname;
+ values["device_type"] = device_type;
+ values["device"] = device;
+ values["timestamp"] = to_string(timestamp);
+ values["sensor"] = sensor;
+ values["value"] = value;
+
+// cerr << sensor << " => " << value << endl;
+
+ stream->write(values);
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
@@ -138,6 +126,8 @@ private:
serial_port &port;
uint8_t data[size];
mutable_buffers_1 buffer = boost::asio::buffer(data, size);
+
+ shared_ptr<SampleOutputStream> stream;
};
class sm_serial_read : public app {
@@ -159,12 +149,13 @@ public:
io_service io_service;
- serial_port port(io_service, port_name);
+ serial_port port(io_service);
+ port.open(port_name);
port.set_option(serial_port_base::baud_rate(baud_rate));
port.set_option(serial_port_base::character_size(8));
+ port.set_option(serial_port_base::parity(serial_port_base::parity::none));
+ port.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
port.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
- port.set_option(serial_port_base::parity(serial_port_base::parity::even));
- port.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::two));
if (port.is_open()) {
cerr << "port is open" << endl;
@@ -172,7 +163,28 @@ public:
cerr << "port is not open" << endl;
}
- port_handler(port_name, port).run();
+ auto field_names = vector<string>({
+ "hostname",
+ "device_type",
+ "device",
+ "timestamp",
+ "sensor",
+ "value"
+ });
+ shared_ptr <SampleOutputStream> sampleStream;
+
+ if (format == Format::JSON) {
+ sampleStream = make_shared<JsonSampleOutputStream>(cout, field_names);
+ } else if (format == Format::SQL) {
+ sampleStream = make_shared<SqlSampleOutputStream>(cout, field_names);
+ } else if (format == Format::PLAIN) {
+ sampleStream = make_shared<CsvSampleOutputStream>(cout, field_names);
+ } else {
+ cerr << "Unsupported format: " << boost::lexical_cast<string>(format) << endl;
+ return EXIT_FAILURE;
+ }
+
+ port_handler(port_name, port, sampleStream).run();
return EXIT_SUCCESS;
}
@@ -183,6 +195,7 @@ public:
}
using namespace trygvis::apps;
+
int main(int argc, char *argv[]) {
sm_serial_read app;
return launch_app(argc, argv, app);