aboutsummaryrefslogtreecommitdiff
path: root/sensor
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2016-04-12 20:06:47 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2016-04-12 20:06:47 +0200
commit2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d (patch)
tree19f9ce796886e216a608fa5e938bd2bd4f0d0b55 /sensor
parentce07550c57172443c10a66957b50085e273d20b3 (diff)
downloadble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.gz
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.bz2
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.xz
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.zip
Soil Moisture: Adding support for controlling lights.
Bluetooth: refectorying, trying to be more c++ idiomatic and modern. SM/Diller: adding bluetooth to Diller bridge.
Diffstat (limited to 'sensor')
-rw-r--r--sensor/include/trygvis/sensor.h43
-rw-r--r--sensor/include/trygvis/sensor/io.h2
-rw-r--r--sensor/main/io.cpp30
-rw-r--r--sensor/main/sensor.cpp2
-rw-r--r--sensor/test/SampleTest.cpp16
5 files changed, 47 insertions, 46 deletions
diff --git a/sensor/include/trygvis/sensor.h b/sensor/include/trygvis/sensor.h
index c109a2a..09372b9 100644
--- a/sensor/include/trygvis/sensor.h
+++ b/sensor/include/trygvis/sensor.h
@@ -5,15 +5,16 @@
#include <iostream>
#include <memory>
#include <vector>
-#include <boost/optional/optional.hpp>
+#include <algorithm>
+#include <experimental/optional>
namespace trygvis {
namespace sensor {
-using namespace std;
+//using namespace std;
-template<typename A>
-using o = boost::optional<A>;
+template<typename T>
+using o = std::experimental::optional<T>;
enum class sample_format_type {
AUTO,
@@ -24,7 +25,7 @@ enum class sample_format_type {
RRD,
};
-string to_string(const sample_format_type &arg);
+std::string to_string(const sample_format_type &arg);
std::ostream& operator<<(std::ostream& os, sample_format_type const& type);
@@ -39,9 +40,9 @@ std::ostream& operator<<(std::ostream& os, time_resolution const& type);
std::istream& operator>>(std::istream& is, time_resolution & type);
-class sample_exception : public runtime_error {
+class sample_exception : public std::runtime_error {
public:
- sample_exception(const string &what) : runtime_error(what) {
+ sample_exception(const std::string &what) : runtime_error(what) {
}
};
@@ -51,13 +52,13 @@ class SampleKey;
class KeyDictionary;
-using SampleKeyVector = vector<SampleKey *>;
+using SampleKeyVector = std::vector<SampleKey *>;
using SampleKeyIndex = SampleKeyVector::size_type;
struct SampleKey {
private:
SampleKey(const SampleKey& that) = delete;
- SampleKey(SampleKeyIndex index, const string &name) : index(index), name(name) {
+ SampleKey(SampleKeyIndex index, const std::string &name) : index(index), name(name) {
if (name.length() == 0) {
throw sample_exception("Bad sample key.");
}
@@ -72,7 +73,7 @@ public:
}
const SampleKeyIndex index;
- const string name;
+ const std::string name;
};
class KeyDictionary {
@@ -88,7 +89,7 @@ public:
KeyDictionary& operator=(const KeyDictionary&) = delete;
- SampleKey *indexOf(const string &key) {
+ const SampleKey *indexOf(const std::string &key) {
SampleKeyIndex i = 0;
for (auto ptr = keys.cbegin(); ptr != keys.cend(); ptr++, i++) {
if ((*ptr)->name == key) {
@@ -111,8 +112,8 @@ public:
return keys.at(i);
}
- vector<SampleKey *> findIndexes(SampleKeyVector &keys) {
- vector<SampleKey *> indexes;
+ std::vector<const SampleKey *> findIndexes(SampleKeyVector &keys) {
+ std::vector<const SampleKey *> indexes;
for (auto &key: keys) {
auto index = indexOf(key->name);
@@ -152,7 +153,7 @@ private:
class SampleRecord {
public:
- typedef vector<o<string>> vec;
+ typedef std::vector<o<std::string>> vec;
SampleRecord(KeyDictionary &dict) : dict(dict) {
}
@@ -179,19 +180,19 @@ public:
return values.empty();
}
- const o<string> at(const SampleKey *key) const {
+ const o<std::string> at(const SampleKey *key) const {
SampleKeyIndex index = key->index;
if (index >= values.size()) {
- return o<string>();
+ return o<std::string>();
}
return values.at(index);
}
SampleRecord& set(const SampleKey *key, const std::string &value) {
- values.resize(max(values.size(), key->index + 1));
+ values.resize(std::max(values.size(), key->index + 1));
- values.at(key->index).reset(value);
+ values.at(key->index) = value;
return *this;
}
@@ -199,9 +200,9 @@ public:
template<class A>
const o<A> lexical_at(const SampleKey *key) const;
- string to_string() const {
+ std::string to_string() const {
SampleKeyIndex i = 0;
- string s;
+ std::string s;
for (auto ptr = values.begin(); ptr != values.end(); ptr++, i++) {
auto o = *ptr;
@@ -209,7 +210,7 @@ public:
continue;
}
- auto value = o.get();
+ auto value = *o;
s += dict.at(i)->name + " = " + value + ", ";
}
diff --git a/sensor/include/trygvis/sensor/io.h b/sensor/include/trygvis/sensor/io.h
index 71b0b84..304091b 100644
--- a/sensor/include/trygvis/sensor/io.h
+++ b/sensor/include/trygvis/sensor/io.h
@@ -187,7 +187,7 @@ public:
void write(SampleRecord const &sample) override;
private:
- vector<SampleKey *> keys;
+ vector<const SampleKey *> keys;
shared_ptr<ostream> stream;
const SampleKey *timestamp_key;
};
diff --git a/sensor/main/io.cpp b/sensor/main/io.cpp
index 96a98f6..9e822c3 100644
--- a/sensor/main/io.cpp
+++ b/sensor/main/io.cpp
@@ -45,17 +45,17 @@ unique_ptr<SampleOutputStream> open_sample_output_stream(
auto tsf = options.find_option<timestamp_field_option>();
- auto timestamp_key = dict.indexOf(tsf ? tsf.get()->name : "timestamp");
+ auto timestamp_key = dict.indexOf(tsf ? tsf.value()->name : "timestamp");
return make_unique<RrdSampleOutputStream>(output, dict, timestamp_key, of);
} else if (type == sample_format_type::SQL) {
auto tno = options.find_option<table_name_option>();
- if (!tno.is_initialized()) {
+ if (!tno) {
throw missing_required_option_error("table name");
}
- return make_unique<SqlSampleOutputStream>(move(output), dict, tno.get()->name);
+ return make_unique<SqlSampleOutputStream>(move(output), dict, tno.value()->name);
} else {
throw sample_exception("No writer for format type: " + to_string(type));
}
@@ -144,7 +144,7 @@ void CsvSampleOutputStream::write(SampleRecord const &sample) {
auto o = sample.at(sampleKey);
if (o) {
- s << o.get();
+ s << o.value();
}
}
@@ -152,7 +152,7 @@ void CsvSampleOutputStream::write(SampleRecord const &sample) {
}
void CsvSampleOutputStream::writeHeader() {
- auto &s = *stream.get();
+ auto &s = *stream;
auto i = dict.begin();
while (i != dict.end()) {
@@ -187,7 +187,7 @@ void JsonSampleOutputStream::write(SampleRecord const &sample) {
auto value = sample.at(sampleKey);
if (value) {
- doc[key->name] = value.get();
+ doc[key->name] = value.value();
}
}
} else {
@@ -197,7 +197,7 @@ void JsonSampleOutputStream::write(SampleRecord const &sample) {
if (o) {
// Make sure that the key is registered in the dictionary
dict.indexOf(sampleKey->name);
- doc[sampleKey->name] = o.get();
+ doc[sampleKey->name] = o.value();
}
}
}
@@ -230,7 +230,7 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) {
} else {
*s << ", ";
}
- *s << key->name << "=" << value.get();
+ *s << key->name << "=" << value.value();
}
}
} else {
@@ -245,7 +245,7 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) {
}
// Make sure that the key is registered in the dictionary
dict.indexOf(sampleKey->name);
- *s << sampleKey->name << "=" << o.get();
+ *s << sampleKey->name << "=" << o.value();
}
}
}
@@ -260,7 +260,7 @@ RrdSampleOutputStream::RrdSampleOutputStream(shared_ptr<ostream> stream,
stream(move(stream)), timestamp_key(timestamp_key) {
if (output_fields) {
- for (auto field : output_fields.get()->fields) {
+ for (auto field : output_fields.value()->fields) {
keys.emplace_back(dict.indexOf(field));
}
} else {
@@ -284,7 +284,7 @@ void RrdSampleOutputStream::write(SampleRecord const &sample) {
return;
}
- auto timestamp = timestampO.get();
+ auto timestamp = timestampO.value();
s << timestamp;
@@ -303,7 +303,7 @@ void RrdSampleOutputStream::write(SampleRecord const &sample) {
s << ":";
}
- s << (value ? value.get() : "U");
+ s << (value ? value.value() : "U");
}
s << endl << flush;
@@ -319,7 +319,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) {
fs.reserve(1024);
vs.reserve(1024);
- auto &s = *stream.get();
+ auto &s = *stream;
bool first = true;
if (!dict.empty()) {
@@ -336,7 +336,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) {
vs += ", ";
}
fs += "\"" + key->name + "\"";
- vs += "'" + value.get() + "'";
+ vs += "'" + value.value() + "'";
}
}
} else {
@@ -354,7 +354,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) {
dict.indexOf(sample_key->name);
fs += "\"" + sample_key->name + "\"";
- vs += "'" + o.get() + "'";
+ vs += "'" + o.value() + "'";
}
}
}
diff --git a/sensor/main/sensor.cpp b/sensor/main/sensor.cpp
index 1c0c666..0988e0d 100644
--- a/sensor/main/sensor.cpp
+++ b/sensor/main/sensor.cpp
@@ -80,7 +80,7 @@ const o<long> SampleRecord::lexical_at(const SampleKey *key) const {
return o<long>();
}
- return o<long>(boost::lexical_cast<long>(value.get()));
+ return o<long>(boost::lexical_cast<long>(*value));
}
//
//template<class A>
diff --git a/sensor/test/SampleTest.cpp b/sensor/test/SampleTest.cpp
index 8479e75..0fffc04 100644
--- a/sensor/test/SampleTest.cpp
+++ b/sensor/test/SampleTest.cpp
@@ -49,13 +49,13 @@ BOOST_AUTO_TEST_CASE(key_value_parser2) {
SampleRecord& sample = buffer->samples[0];
auto it = dict.begin();
BOOST_CHECK_EQUAL((*it)->name, "now");
- BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
- BOOST_CHECK_EQUAL(sample.at(*it).get(), "1");
+ BOOST_CHECK_EQUAL(!!sample.at(*it), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).value(), "1");
BOOST_CHECK_EQUAL((*it++)->index, 0);
BOOST_CHECK_EQUAL((*it)->name, "sensor 1");
- BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
- BOOST_CHECK_EQUAL(sample.at(*it).get(), "0.000999999833333");
+ BOOST_CHECK_EQUAL(!!sample.at(*it), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).value(), "0.000999999833333");
BOOST_CHECK_EQUAL((*it++)->index, 1);
}
@@ -112,12 +112,12 @@ BOOST_AUTO_TEST_CASE(key_value_parser_without_newline) {
SampleRecord& sample = buffer->samples[0];
auto it = dict.begin();
BOOST_CHECK_EQUAL((*it)->name, "now");
- BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
- BOOST_CHECK_EQUAL(sample.at(*it).get(), "1");
+ BOOST_CHECK_EQUAL(!!sample.at(*it), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).value(), "1");
BOOST_CHECK_EQUAL((*it++)->index, 0);
BOOST_CHECK_EQUAL((*it)->name, "sensor");
- BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
- BOOST_CHECK_EQUAL(sample.at(*it).get(), "123");
+ BOOST_CHECK_EQUAL(!!sample.at(*it), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).value(), "123");
BOOST_CHECK_EQUAL((*it++)->index, 1);
}