Skip to content

Commit

Permalink
RM2
Browse files Browse the repository at this point in the history
  • Loading branch information
wardru committed Jan 20, 2024
1 parent b2620d1 commit 1d083e2
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmake/format.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
find_program(CLANG_FORMAT "clang-format")
find_program(CLANG_FORMAT "clang-format" REQUIRED)

file(GLOB_RECURSE ALL_SOURCE_FILES
${CMAKE_SOURCE_DIR}/source/*.cxx
Expand Down
2 changes: 1 addition & 1 deletion examples/bps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main()
if (cli->register_devices() < 0)
return -1;

for (auto& dev : cli->get_devices())
for (auto &dev : cli->get_devices())
spdlog::info("device: {}", dev->get_name());

auto bps = cli->get_device("default", "Panduza_FakeBps");
Expand Down
2 changes: 1 addition & 1 deletion include/pza/core/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace pza::utils

struct itf_group_info {
std::string group_name;
unsigned int index;
unsigned int index = 0;
std::string interface_name;
};

Expand Down
20 changes: 16 additions & 4 deletions source/core/attribute.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ public:
attribute &operator=(attribute &&) = delete;
~attribute();

const std::string &get_name() const { return _name; }
const std::string &get_name() const
{
return _name;
}

template <typename T> void register_field(const std::string &name) { _fields[name] = T(); }
template <typename T> void register_field(const std::string &name)
{
_fields[name] = T();
}

template <typename T> const T &get_field(const std::string &name)
{
Expand Down Expand Up @@ -61,10 +67,16 @@ public:

void on_message(mqtt::const_message_ptr msg);

void register_callback(const std::function<void(void)> &cb) { _callbacks.push_back(cb); }
void register_callback(const std::function<void(void)> &cb)
{
_callbacks.push_back(cb);
}
void remove_callback(const std::function<void(void)> &cb);

void set_msg_callback(const std::function<int(const nlohmann::json &data)> &cb) { _msg_cb = cb; }
void set_msg_callback(const std::function<int(const nlohmann::json &data)> &cb)
{
_msg_cb = cb;
}

private:
using field_types = std::variant<std::string, unsigned int, int, double, bool>;
Expand Down
90 changes: 72 additions & 18 deletions source/core/client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,32 @@ struct client_impl : mqtt_service {

int connect();
int disconnect();
bool is_connected() const { return (_paho_client->is_connected()); }
bool is_connected() const
{
return (_paho_client->is_connected());
}

const std::string &get_addr() const { return _addr; }
const std::string &get_id() const { return _id; }
int get_port() const { return _port; }
const std::string &get_addr() const
{
return _addr;
}
const std::string &get_id() const
{
return _id;
}
int get_port() const
{
return _port;
}

void set_connection_timeout(unsigned int timeout) { _conn_timeout = timeout; }
unsigned int get_connection_timeout() const { return _conn_timeout; }
void set_connection_timeout(unsigned int timeout)
{
_conn_timeout = timeout;
}
unsigned int get_connection_timeout() const
{
return _conn_timeout;
}

int publish(const std::string &topic, const std::string &payload) override;
int publish(mqtt::const_message_ptr msg) override;
Expand Down Expand Up @@ -160,7 +178,10 @@ int client_impl::disconnect()
return 0;
}

void client_impl::connection_lost(const std::string &cause) { spdlog::error("connection lost: {}", cause); }
void client_impl::connection_lost(const std::string &cause)
{
spdlog::error("connection lost: {}", cause);
}

int client_impl::publish(const std::string &topic, const std::string &payload)
{
Expand Down Expand Up @@ -445,42 +466,75 @@ client::client(const std::string &addr, int port, std::optional<std::string> id)

client::~client() = default;

int client::connect() { return _impl->connect(); }
int client::connect()
{
return _impl->connect();
}

int client::disconnect() { return _impl->disconnect(); }
int client::disconnect()
{
return _impl->disconnect();
}

bool client::is_connected() const { return _impl->is_connected(); }
bool client::is_connected() const
{
return _impl->is_connected();
}

const std::string &client::get_addr() const { return _impl->get_addr(); }
const std::string &client::get_addr() const
{
return _impl->get_addr();
}

const std::string &client::get_id() const { return _impl->get_id(); }
const std::string &client::get_id() const
{
return _impl->get_id();
}

int client::get_port() const { return _impl->get_port(); }
int client::get_port() const
{
return _impl->get_port();
}

void client::set_connection_timeout(unsigned int timeout) { _impl->set_connection_timeout(timeout); }
void client::set_connection_timeout(unsigned int timeout)
{
_impl->set_connection_timeout(timeout);
}

unsigned int client::get_connection_timeout() const { return _impl->get_connection_timeout(); }
unsigned int client::get_connection_timeout() const
{
return _impl->get_connection_timeout();
}

device::s_ptr client::register_device(const std::string &group, const std::string &name, unsigned int timeout_ms)
{
return _impl->register_device(group, name, timeout_ms);
}

int client::register_devices(unsigned int timeout_ms) { return _impl->register_devices(timeout_ms); }
int client::register_devices(unsigned int timeout_ms)
{
return _impl->register_devices(timeout_ms);
}

device::s_ptr client::get_device(const std::string &group, const std::string &name) const
{
return _impl->get_device(group, name);
}

std::vector<device::s_ptr> client::get_devices() const { return _impl->get_devices(); }
std::vector<device::s_ptr> client::get_devices() const
{
return _impl->get_devices();
}

std::vector<device::s_ptr> client::get_devices_in_group(const std::string &group) const
{
return _impl->get_devices_in_group(group);
}

std::set<std::string> client::get_groups() const { return _impl->get_groups(); }
std::set<std::string> client::get_groups() const
{
return _impl->get_groups();
}

itf_base::s_ptr client::get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
Expand Down
5 changes: 4 additions & 1 deletion source/core/core.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ void pza::core::set_log_level(pza::core::log_level level)
spdlog::set_level(static_cast<spdlog::level::level_enum>(level));
}

pza::core::log_level pza::core::get_log_level() { return static_cast<pza::core::log_level>(spdlog::get_level()); }
pza::core::log_level pza::core::get_log_level()
{
return static_cast<pza::core::log_level>(spdlog::get_level());
}
10 changes: 8 additions & 2 deletions source/core/interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ struct itf_impl {
itf_impl(itf_impl &&) = delete;
itf_impl &operator=(itf_impl &&) = delete;

const std::string &get_name() const { return info.name; }
const std::string &get_type() const { return info.type; }
const std::string &get_name() const
{
return info.name;
}
const std::string &get_type() const
{
return info.type;
}

void register_attributes(const std::list<attribute::s_ptr> &attribute_list);

Expand Down
50 changes: 40 additions & 10 deletions source/interfaces/bps_chan_ctrl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,55 @@ int bps_chan_ctrl::set_current(double amps)
return _current->set_field<double>("value", amps);
}

int bps_chan_ctrl::set_enable(bool enable) { return _enable->set_field<bool>("value", enable); }
int bps_chan_ctrl::set_enable(bool enable)
{
return _enable->set_field<bool>("value", enable);
}

bool bps_chan_ctrl::get_enable() { return _enable->get_field<bool>("value"); }
bool bps_chan_ctrl::get_enable()
{
return _enable->get_field<bool>("value");
}

double bps_chan_ctrl::get_min_voltage() { return _voltage->get_field<double>("min"); }
double bps_chan_ctrl::get_min_voltage()
{
return _voltage->get_field<double>("min");
}

double bps_chan_ctrl::get_max_voltage() { return _voltage->get_field<double>("max"); }
double bps_chan_ctrl::get_max_voltage()
{
return _voltage->get_field<double>("max");
}

double bps_chan_ctrl::get_min_current() { return _current->get_field<double>("min"); }
double bps_chan_ctrl::get_min_current()
{
return _current->get_field<double>("min");
}

double bps_chan_ctrl::get_max_current() { return _current->get_field<double>("max"); }
double bps_chan_ctrl::get_max_current()
{
return _current->get_field<double>("max");
}

double bps_chan_ctrl::get_preset_voltage() { return _voltage->get_field<double>("value"); }
double bps_chan_ctrl::get_preset_voltage()
{
return _voltage->get_field<double>("value");
}

double bps_chan_ctrl::get_preset_current() { return _current->get_field<double>("value"); }
double bps_chan_ctrl::get_preset_current()
{
return _current->get_field<double>("value");
}

unsigned int bps_chan_ctrl::get_num_decimals_voltage() { return _voltage->get_field<unsigned int>("decimals"); }
unsigned int bps_chan_ctrl::get_num_decimals_voltage()
{
return _voltage->get_field<unsigned int>("decimals");
}

unsigned int bps_chan_ctrl::get_num_decimals_current() { return _current->get_field<unsigned int>("decimals"); }
unsigned int bps_chan_ctrl::get_num_decimals_current()
{
return _current->get_field<unsigned int>("decimals");
}

void bps_chan_ctrl::register_enable_callback(const std::function<void()> &callback)
{
Expand Down
15 changes: 12 additions & 3 deletions source/interfaces/device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ device::device(mqtt_service *mqtt, itf_info &info)

device::~device() = default;

const std::string &device::get_model() const { return _identity->get_field<std::string>("model"); }
const std::string &device::get_model() const
{
return _identity->get_field<std::string>("model");
}

const std::string &device::get_manufacturer() const { return _identity->get_field<std::string>("manufacturer"); }
const std::string &device::get_manufacturer() const
{
return _identity->get_field<std::string>("manufacturer");
}

const std::string &device::get_family() const { return _identity->get_field<std::string>("family"); }
const std::string &device::get_family() const
{
return _identity->get_field<std::string>("family");
}
5 changes: 4 additions & 1 deletion source/interfaces/meter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ meter::meter(mqtt_service *mqtt, itf_info &info)

meter::~meter() = default;

double meter::get_measure() { return _impl->get_attribute("measure").get_field<double>("value"); }
double meter::get_measure()
{
return _impl->get_attribute("measure").get_field<double>("value");
}

void meter::register_measure_callback(const std::function<void()> &callback)
{
Expand Down
25 changes: 20 additions & 5 deletions source/utils/json_attribute.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ int json_attribute::parse(const std::string &payload)
return 0;
}

int json_attribute::get_string(const std::string &key, std::string &str) { return get(key, str); }
int json_attribute::get_string(const std::string &key, std::string &str)
{
return get(key, str);
}

int json_attribute::get_int(const std::string &key, int &i) { return get(key, i); }
int json_attribute::get_int(const std::string &key, int &i)
{
return get(key, i);
}

int json_attribute::get_unsigned_int(const std::string &key, unsigned &u) { return get(key, u); }
int json_attribute::get_unsigned_int(const std::string &key, unsigned &u)
{
return get(key, u);
}

int json_attribute::get_double(const std::string &key, double &f) { return get(key, f); }
int json_attribute::get_double(const std::string &key, double &f)
{
return get(key, f);
}

int json_attribute::get_bool(const std::string &key, bool &b) { return get(key, b); }
int json_attribute::get_bool(const std::string &key, bool &b)
{
return get(key, b);
}
22 changes: 17 additions & 5 deletions source/utils/topic.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ public:
topic &operator=(topic &&) = delete;
~topic() = default;

[[nodiscard]] bool is_valid() const { return _is_valid; }

[[nodiscard]] std::string get_topic() const { return _topic; }
[[nodiscard]] std::string get_group() const { return _list[1]; }
[[nodiscard]] std::string get_device_name() const { return _list[2]; }
[[nodiscard]] bool is_valid() const
{
return _is_valid;
}

[[nodiscard]] std::string get_topic() const
{
return _topic;
}
[[nodiscard]] std::string get_group() const
{
return _list[1];
}
[[nodiscard]] std::string get_device_name() const
{
return _list[2];
}

static std::string regexify_topic(const std::string &topic);
static bool topic_matches(const std::string &str, const std::string &fnmatchPattern);
Expand Down

0 comments on commit 1d083e2

Please sign in to comment.