Skip to content

Commit

Permalink
RM3
Browse files Browse the repository at this point in the history
  • Loading branch information
wardru committed Feb 4, 2024
1 parent 1d083e2 commit 86c151d
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 59 deletions.
10 changes: 6 additions & 4 deletions examples/bps.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <pza/core/client.hxx>
#include <pza/core/core.hxx>
#include <pza/interfaces/ammeter.hxx>
#include <pza/interfaces/bps_chan_ctrl.hxx>
#include <pza/interfaces/bpc.hxx>
#include <pza/interfaces/voltmeter.hxx>

static const std::string addr = "localhost";
Expand Down Expand Up @@ -35,7 +35,7 @@ int main()
return -1;
}

auto ctrl = cli->get_interface<pza::itf::bps_chan_ctrl>("default", "Panduza_FakeBps", "channel", 0, "ctrl");
auto ctrl = cli->get_interface<pza::itf::bpc>("default", "Panduza_FakeBps", "channel", 0, "ctrl");
if (!ctrl) {
spdlog::error("ctrl not found");
return -1;
Expand All @@ -55,7 +55,9 @@ int main()

am->register_measure_callback([&]() { spdlog::info("current: {}", am->get_measure()); });

while (true)
;
cli->disconnect();

//while (true)
// ;
return 0;
}
2 changes: 2 additions & 0 deletions include/pza/core/device.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public:

[[nodiscard]] std::set<std::string> get_interface_groups() const;

[[nodiscard]] size_t get_interface_groups_count() const;

[[nodiscard]] std::vector<itf_base::s_ptr> get_interfaces() const;

private:
Expand Down
9 changes: 8 additions & 1 deletion include/pza/core/interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
struct itf_impl;
class mqtt_service;
class attribute;
struct itf_info;

namespace pza
{
struct itf_info
{
std::string bench;
std::string device_name;
std::string name;
std::string type;
};

// Can't use the name "interface" because it's a reserved keyford for Windows in C++
class itf_base
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace pza::itf
{
class bps_chan_ctrl : public itf_base
class bpc : public itf_base
{
public:
using s_ptr = std::shared_ptr<bps_chan_ctrl>;
using u_ptr = std::unique_ptr<bps_chan_ctrl>;
using w_ptr = std::weak_ptr<bps_chan_ctrl>;
using s_ptr = std::shared_ptr<bpc>;
using u_ptr = std::unique_ptr<bpc>;
using w_ptr = std::weak_ptr<bpc>;

explicit bps_chan_ctrl(mqtt_service *mqtt, itf_info &info);
bps_chan_ctrl(const bps_chan_ctrl &) = delete;
bps_chan_ctrl(bps_chan_ctrl &&) = delete;
bps_chan_ctrl &operator=(const bps_chan_ctrl &) = delete;
bps_chan_ctrl &operator=(bps_chan_ctrl &&) = delete;
~bps_chan_ctrl() override;
explicit bpc(mqtt_service *mqtt, itf_info &info);
bpc(const bpc &) = delete;
bpc(bpc &&) = delete;
bpc &operator=(const bpc &) = delete;
bpc &operator=(bpc &&) = delete;
~bpc() override;

int set_voltage(double volts);
int set_current(double amps);
Expand Down
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ target_sources(${LIBRARY_NAME}
interfaces/meter.cxx
interfaces/voltmeter.cxx
interfaces/ammeter.cxx
interfaces/bps_chan_ctrl.cxx
interfaces/bpc.cxx

${CMAKE_CURRENT_BINARY_DIR}/version.cxx
)
53 changes: 41 additions & 12 deletions source/core/client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct client_impl : mqtt_service {
int disconnect();
bool is_connected() const
{
return (_paho_client->is_connected());
return _paho_client->is_connected();
}

const std::string &get_addr() const
Expand Down Expand Up @@ -157,21 +157,27 @@ int client_impl::connect()
connOpts.set_keep_alive_interval(interval);
connOpts.set_clean_session(true);

if (_paho_client->connect(connOpts)->wait_for(std::chrono::milliseconds(_conn_timeout)) == false) {
spdlog::error("failed to connect to client: {}", _id);
try {
_paho_client->connect(connOpts)->wait_for(std::chrono::milliseconds(_conn_timeout));
}
catch (const mqtt::exception &exc) {
spdlog::error("Client {} failed to connect on {}:{}", _id, _addr, _port);
return -1;
}

spdlog::info("connected to {}", _addr);
spdlog::info("connected to {}:{}", _addr, _port);
return 0;
}

int client_impl::disconnect()
{
spdlog::debug("Attempting to disconnect from {}...", _addr);

if (_paho_client->disconnect()->wait_for(std::chrono::milliseconds(_conn_timeout)) == false) {
spdlog::error("failed to disconnect from client: {}", _id);
try {
_paho_client->disconnect()->wait_for(std::chrono::milliseconds(_conn_timeout));
}
catch (const mqtt::exception &exc) {
spdlog::error("Client {} failed to disconnect on {}:{}", _id, _addr, _port);
return -1;
}
spdlog::info("disconnected from {}", _addr);
Expand All @@ -190,8 +196,11 @@ int client_impl::publish(const std::string &topic, const std::string &payload)

int client_impl::publish(mqtt::const_message_ptr msg)
{
if (_paho_client->publish(msg)->wait_for(std::chrono::milliseconds(msg_timeout_default_ms)) == false) {
spdlog::error("failed to publish from client: {}", _id);
try {
_paho_client->publish(msg)->wait_for(std::chrono::milliseconds(msg_timeout_default_ms));
}
catch (const mqtt::exception &exc) {
spdlog::error("Client {} failed to publish", _id);
return -1;
}
spdlog::trace("published message {} to {}", msg->get_payload_str(), msg->get_topic());
Expand All @@ -204,11 +213,15 @@ int client_impl::subscribe(const std::string &topic, const std::function<void(mq

t = topic::regexify_topic(topic);
_listeners[t] = cb;
if (_paho_client->subscribe(topic, 0)->wait_for(std::chrono::seconds(_conn_timeout)) == false) {
spdlog::error("failed to subscribe to topic: {} on client {}", topic, _id);
try {
_paho_client->subscribe(topic, 0)->wait_for(std::chrono::seconds(_conn_timeout));
}
catch (const mqtt::exception &exc) {
spdlog::error("Client {} failed to subscribe to topic: {}", _id, topic);
_listeners.erase(t);
return -1;
}

spdlog::trace("subscribed to topic: {}", topic);
return 0;
}
Expand All @@ -217,8 +230,11 @@ int client_impl::unsubscribe(const std::string &topic)
{
std::string t;

if (_paho_client->unsubscribe(topic)->wait_for(std::chrono::seconds(_conn_timeout)) == false) {
spdlog::error("failed to unsubscribe from topic: {} on client {}", topic, _id);
try {
_paho_client->unsubscribe(topic)->wait_for(std::chrono::seconds(_conn_timeout));
}
catch (const mqtt::exception &exc) {
spdlog::error("Client {} failed to unsubscribe from topic: {}", _id, topic);
return -1;
}
spdlog::trace("unsubscribed from topic: {}", topic);
Expand Down Expand Up @@ -554,3 +570,16 @@ itf_base::s_ptr client::get_interface(const std::string &group, const std::strin
return nullptr;
return dev->get_interface(interface_name);
}

std::vector<itf_base::s_ptr> client::get_interfaces() const
{
auto dev = _impl->get_devices();
std::vector<itf_base::s_ptr> ret;

for (auto const &elem : dev) {
auto itf = elem->get_interfaces();
ret.insert(ret.end(), itf.begin(), itf.end());
}

return ret;
}
14 changes: 13 additions & 1 deletion source/core/device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ struct device_impl {
std::vector<itf_base::s_ptr> get_interfaces_in_group(const std::string &group, unsigned int index) const;
std::set<std::string> get_interface_groups() const;

size_t get_interface_groups_count() const;

std::vector<itf_base::s_ptr> get_interfaces() const;

struct device_info info;
std::unordered_map<std::string, std::string> interfaces_scanned;
itf::device::s_ptr device_interface = nullptr;

std::unordered_map<std::string, itf_base::s_ptr> interfaces;
std::map<std::string, itf_base::s_ptr> interfaces;
};

device_impl::device_impl(mqtt_service *mqtt, const struct device_info &info)
Expand Down Expand Up @@ -178,6 +180,11 @@ std::vector<itf_base::s_ptr> device_impl::get_interfaces() const
return vec;
}

size_t device_impl::get_interface_groups_count() const
{
return get_interface_groups().size();
}

device::device(mqtt_service *mqtt, struct device_info &info)
: _impl(std::make_unique<device_impl>(mqtt, info))
{
Expand Down Expand Up @@ -281,3 +288,8 @@ std::vector<itf_base::s_ptr> device::get_interfaces() const
{
return _impl->get_interfaces();
}

size_t device::get_interface_groups_count() const
{
return _impl->get_interface_groups_count();
}
2 changes: 1 addition & 1 deletion source/core/interface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static constexpr unsigned int attributes_timeout = 2;

itf_impl::itf_impl(mqtt_service *mqtt, itf_info &info)
: info(info),
topic_base("pza/" + info.group + "/" + info.device_name + "/" + info.name),
topic_base("pza/" + info.bench + "/" + info.device_name + "/" + info.name),
topic_cmd(topic_base + "/cmds/set"),
mqtt(mqtt)
{
Expand Down
7 changes: 0 additions & 7 deletions source/core/interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

using namespace pza;

struct itf_info {
std::string group;
std::string device_name;
std::string name;
std::string type;
};

struct itf_impl {
explicit itf_impl(mqtt_service *mqtt, itf_info &info);

Expand Down
6 changes: 3 additions & 3 deletions source/core/interface_factory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <pza/core/device.hxx>

#include <pza/interfaces/ammeter.hxx>
#include <pza/interfaces/bps_chan_ctrl.hxx>
#include <pza/interfaces/bpc.hxx>
#include <pza/interfaces/device.hxx>
#include <pza/interfaces/platform.hxx>
#include <pza/interfaces/voltmeter.hxx>
Expand All @@ -18,7 +18,7 @@ static const std::unordered_map<std::string, interface_factory::factory_function
{"platform", allocate_interface<itf::platform>},
{"ammeter", allocate_interface<itf::ammeter>},
{"voltmeter", allocate_interface<itf::voltmeter>},
{"bpc", allocate_interface<itf::bps_chan_ctrl>}};
{"bpc", allocate_interface<itf::bpc>}};

itf_base::s_ptr interface_factory::create_interface(mqtt_service *mqtt, const std::string &group,
const std::string &device_name, const std::string &name,
Expand All @@ -27,7 +27,7 @@ itf_base::s_ptr interface_factory::create_interface(mqtt_service *mqtt, const st
itf_info info;

info.name = name;
info.group = group;
info.bench = group;
info.device_name = device_name;
info.type = type;

Expand Down
Loading

0 comments on commit 86c151d

Please sign in to comment.