Add interval between temperature reads
This commit is contained in:
parent
aacee5f68d
commit
07879fa5e9
@ -2,6 +2,7 @@
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <memory_resource>
|
||||
#include <ranczo-io/utils/memory_resource.hpp>
|
||||
|
||||
@ -166,6 +167,9 @@ std::pmr::vector< std::pmr::string > OneWireBus::scanBus(std::pmr::memory_resour
|
||||
}
|
||||
|
||||
boost::asio::awaitable< void > OneWireBus::run() {
|
||||
boost::asio::steady_timer timer_{executor_};
|
||||
boost::asio::steady_timer timer_interval{executor_};
|
||||
|
||||
memory_resource::StackPool_8k_Resource mr;
|
||||
auto sensor_ids = scanBus(mr, bus_path_);
|
||||
for(auto & id : sensor_ids) {
|
||||
@ -199,6 +203,9 @@ boost::asio::awaitable< void > OneWireBus::run() {
|
||||
} else {
|
||||
spdlog::info("Read from sensor {} : {}°C", sensor.device_id(), *temp);
|
||||
co_await _pub.get().publish_measurement("DS18B20", sensor.device_id(), *temp);
|
||||
// we wait for a while
|
||||
timer_interval.expires_after(std::chrono::seconds{1});
|
||||
co_await timer_interval.async_wait(boost::asio::use_awaitable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ class DS18B20Sensor {
|
||||
class OneWireBus {
|
||||
public:
|
||||
OneWireBus(boost::asio::any_io_executor exec, MqttMeasurementPublisher &pub, std::string_view bus_path, std::chrono::seconds interval = std::chrono::seconds(2))
|
||||
: _mr{memory_resource::default_resource()}, _pub{pub}, executor_(exec), timer_(exec), bus_path_(std::move(bus_path), _mr), interval_(interval) {}
|
||||
: _mr{memory_resource::default_resource()}, _pub{pub}, executor_(exec), bus_path_(std::move(bus_path), _mr) {}
|
||||
|
||||
OneWireBus(const OneWireBus &) = delete;
|
||||
OneWireBus(OneWireBus && rhs) noexcept = default;
|
||||
@ -56,7 +56,7 @@ class OneWireBus {
|
||||
std::pmr::vector< std::pmr::string > scanBus(std::pmr::memory_resource & mr, const std::filesystem::path & bus_path);
|
||||
boost::asio::any_io_executor executor_;
|
||||
std::reference_wrapper<MqttMeasurementPublisher> _pub;
|
||||
boost::asio::steady_timer timer_;
|
||||
|
||||
|
||||
std::pmr::string bus_path_;
|
||||
std::chrono::seconds interval_;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user