52 lines
1.5 KiB
C++
52 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <boost/align/detail/align.hpp>
|
|
#include <boost/asio/any_io_executor.hpp>
|
|
#include <boost/asio/co_spawn.hpp>
|
|
#include <boost/asio/detached.hpp>
|
|
#include <boost/json/object.hpp>
|
|
#include <boost/regex/config.hpp>
|
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
|
|
|
#include <chrono>
|
|
#include <config.hpp>
|
|
|
|
#include <ranczo-io/utils/logger.hpp>
|
|
#include <ranczo-io/utils/modbus.hpp>
|
|
#include <ranczo-io/utils/mqtt_client.hpp>
|
|
|
|
namespace ranczo {
|
|
class Relay {
|
|
public:
|
|
virtual ~Relay() = default;
|
|
enum class State { On, Off };
|
|
|
|
virtual awaitable_expected< State > state() const noexcept = 0;
|
|
virtual awaitable_expected< void > on() noexcept = 0;
|
|
virtual awaitable_expected< void > off() noexcept = 0;
|
|
};
|
|
|
|
class ModbusRelay : public Relay {
|
|
/// cached
|
|
mutable Relay::State _state;
|
|
mutable std::optional< std::chrono::system_clock::time_point > _lastRead;
|
|
|
|
std::chrono::nanoseconds timeSinceLastRead() const;
|
|
|
|
boost::asio::any_io_executor _executor;
|
|
ModuleLogger _log;
|
|
std::shared_ptr< ranczo::ModbusDevice > _dev;
|
|
int _channel;
|
|
std::uint16_t _coil_addr; // adres cewki (0-based)
|
|
|
|
public:
|
|
// Prost y mapping: coil_addr = base_addr + channel
|
|
ModbusRelay(boost::asio::any_io_executor ex, std::shared_ptr< ranczo::ModbusDevice > dev, int channel, std::uint16_t base_coil_addr = 0);
|
|
|
|
awaitable_expected< Relay::State > state() const noexcept override;
|
|
awaitable_expected< void > on() noexcept override;
|
|
awaitable_expected< void > off() noexcept override;
|
|
};
|
|
|
|
} // namespace ranczo
|