Skip to content

Commit

Permalink
Ready for 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wardru committed Oct 1, 2023
1 parent cba39da commit 034fd40
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 80 deletions.
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ find_package(GTest REQUIRED)
find_package(cppcheck REQUIRED)
find_package(magic_enum REQUIRED)

if (CMAKE_SYSTEM_NAME MATCHES "Windows" OR CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-allow-multiple-definition")
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-allow-multiple-definition")
set(CMAKE_CROSSCOMPILING_EMULATOR "wine")
endif()

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_library(${LIBRARY_NAME})

add_subdirectory(source)
#add_subdirectory(test)

option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_EXAMPLES "Build examples" OFF)
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

target_include_directories(${LIBRARY_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_BINARY_DIR}
)


target_link_libraries(${LIBRARY_NAME}
$<$<OR:$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>,$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>:PahoMqttCpp::paho-mqttpp3-static>
$<$<AND:$<BOOL:${BUILD_SHARED_LIBS}>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>>:PahoMqttCpp::paho-mqttpp3>
Expand All @@ -47,8 +51,7 @@ target_link_libraries(${LIBRARY_NAME}
magic_enum::magic_enum
)


set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
)
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PzaCxx(ConanFile):
}
default_options = {
"shared": True,
"build_examples": True
"build_examples": False
}
generators = "CMakeDeps", "CMakeToolchain", "virtualrunenv"
exports_sources = "CMakeLists.txt", "source/*", "version.h.in", "CHANGELOG.md", "test/*", "cmake/*", "examples/*", "LICENSE"
Expand Down
50 changes: 19 additions & 31 deletions examples/bps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

int main(void)
{
pza::core::set_log_level(pza::core::log_level::debug);
pza::core::set_log_level(pza::core::log_level::trace);

pza::client::ptr cli = std::make_shared<pza::client>("localhost", 1883);

Expand All @@ -17,38 +17,26 @@ int main(void)
if (cli->register_device(bps) == -1)
return -1;

for (size_t i = 0; i < bps->get_num_channels(); i++) {
for (size_t i = 0; i < 1; i++) {
auto bps_channel = bps->channel[i];

auto mdr = [&](double value) {
spdlog::info("Voltage: {}", value);
};

bps_channel->voltmeter.add_measure_callback(mdr);

bps_channel->voltmeter.remove_measure_callback(mdr);

auto lol = [&](double value) {
spdlog::info("double: {}", value);
};

bps_channel->voltmeter.add_measure_callback(lol);

bps_channel->voltmeter.remove_measure_callback(lol);

spdlog::info("Channel {}:", i);
bps_channel->ctrl.set_voltage(-7.3);
bps_channel->ctrl.set_current(1.0);
bps_channel->ctrl.set_enable(false);
spdlog::info(" Voltage: {}", bps_channel->voltmeter.get_measure());
spdlog::info(" Current: {}", bps_channel->ampermeter.get_measure());
spdlog::info(" Enabled: {}", bps_channel->ctrl.get_enable());
bps_channel->ctrl.set_enable(true);
bps_channel->ctrl.set_voltage(3.3);
bps_channel->ctrl.set_current(5.0);
spdlog::info(" Voltage: {}", bps_channel->voltmeter.get_measure());
spdlog::info(" Current: {}", bps_channel->ampermeter.get_measure());
spdlog::info(" Enabled: {}", bps_channel->ctrl.get_enable());
// spdlog::info("Channel {}:", i);
// bps_channel->ctrl.set_voltage(-7.3);
// bps_channel->ctrl.set_current(1.0);
// bps_channel->ctrl.set_enable(false);
// spdlog::info(" Voltage: {}", bps_channel->voltmeter.get_measure());
// spdlog::info(" Current: {}", bps_channel->ampermeter.get_measure());
// spdlog::info(" Enabled: {}", bps_channel->ctrl.get_enable());
// bps_channel->ctrl.set_enable(true);
// bps_channel->ctrl.set_voltage(3.3);
// bps_channel->ctrl.set_current(5.0);
// spdlog::info(" Voltage: {}", bps_channel->voltmeter.get_measure());
// spdlog::info(" Current: {}", bps_channel->ampermeter.get_measure());
// spdlog::info(" Enabled: {}", bps_channel->ctrl.get_enable());

//bps_channel->voltmeter.set_measure_polling_cycle(2);
//bps_channel->ampermeter.set_measure_polling_cycle(2);
bps_channel->ctrl.set_enable_polling_cycle(2);
}

spdlog::info("\n\nOK\n\n");
Expand Down
11 changes: 8 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
BUILD_DIR_LINUX="build"
BUILD_DIR_WINDOWS="buildwin"
BUILD_TYPE="Debug"
BUILD_EXAMPLES="False"
SHARED="True"
TARGET="Linux"
BUILD_DIR=$BUILD_DIR_LINUX
Expand All @@ -23,7 +24,7 @@ function install_deps {
fi
mkdir -p $BUILD_DIR
cd $BUILD_DIR
conan install .. -o shared=$SHARED -s build_type=$BUILD_TYPE --build=missing -pr:b ../conan_profiles/x86_64_Linux $EXTRA_CONAN_ARGS
conan install .. -o shared=$SHARED -o build_examples=$BUILD_EXAMPLES -s build_type=$BUILD_TYPE --build=missing -pr:b ../conan_profiles/x86_64_Linux $EXTRA_CONAN_ARGS
}

function build {
Expand All @@ -39,17 +40,18 @@ function clean {
}

function usage {
echo "Usage: $0 [-t <target>] [-r] [-s] [-d] [-c] [-h]"
echo "Usage: $0 [-t <target>] [-r] [-s] [-d] [-e] [-c] [-h]"
echo " -t Target platform (Windows or Linux). Default is Linux"
echo " -r Build in Release mode. Default is Debug"
echo " -s Build in Static mode. Default is Shared"
echo " -d Install dependencies"
echo " -e Build examples"
echo " -c Clean build directory"
echo " -h Display this help message"
}

# Parse command line arguments
while getopts "t:rscdbh" opt; do
while getopts "t:rscdebh" opt; do
case $opt in
t)
TARGET=$OPTARG
Expand All @@ -71,6 +73,9 @@ while getopts "t:rscdbh" opt; do
d)
DEPS="True"
;;
e)
BUILD_EXAMPLES="True"
;;
h)
usage
exit 0
Expand Down
22 changes: 17 additions & 5 deletions source/pza/core/attribute.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ namespace pza

explicit attribute(const std::string &name);

template<typename T>
void add_field(const std::string &name, bool readonly)
template <typename T>
void add_ro_field(const std::string &name)
{
field<T> field(name, readonly);
add_field<T>(name, access_mode::readonly);
}

field._callback = std::bind(&attribute::data_from_field, this, std::placeholders::_1);
_fields[name] = field;
template <typename T>
void add_rw_field(const std::string &name)
{
add_field<T>(name, access_mode::readwrite);
}

void on_message(const mqtt::const_message_ptr &message);
Expand All @@ -44,6 +47,15 @@ namespace pza

int data_from_field(const nlohmann::json &data);

template<typename T>
void add_field(const std::string &name, access_mode mode)
{
field<T> field(name, mode);

field._callback = std::bind(&attribute::data_from_field, this, std::placeholders::_1);
_fields[name] = field;
}

template<typename T>
void _assign_value(std::any &elem, const nlohmann::json &data)
{
Expand Down
10 changes: 4 additions & 6 deletions source/pza/core/client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int client::connect(void)
{
int ret;

spdlog::trace("connecting to {}...", _addr);
spdlog::debug("Attempting connection to {}...", _addr);

mqtt::connect_options connOpts;

Expand All @@ -55,7 +55,7 @@ int client::connect(void)

int client::disconnect(void)
{
spdlog::trace("disconnecting from {}...", _addr);
spdlog::debug("Attempting to disconnect from {}...", _addr);

try {
_paho_client->disconnect()->wait_for(std::chrono::seconds(CONN_TIMEOUT));
Expand Down Expand Up @@ -117,7 +117,6 @@ bool client::_topic_matches(const std::string &str, const std::string &fnmatchPa
return std::regex_match(str, pattern);
}


int client::_subscribe(const std::string &topic, const std::function<void(mqtt::const_message_ptr)> &cb)
{
std::string t;
Expand All @@ -135,7 +134,6 @@ int client::_subscribe(const std::string &topic, const std::function<void(mqtt::
}

spdlog::trace("subscribed to topic: {}", topic);

return 0;
}

Expand Down Expand Up @@ -226,7 +224,7 @@ int client::scan_devices(void)

if (ret == false) {
spdlog::error("timed out waiting for scan results");
spdlog::debug("_scan_device_count_expected = {}, got = {}", _scan_device_count_expected, _scan_device_results.size());
spdlog::debug("Expected {} devices, got {}", _scan_device_count_expected, _scan_device_results.size());
return -1;
}

Expand All @@ -249,7 +247,7 @@ int client::_scan_interfaces(std::unique_lock<std::mutex> &lock, const device::p
const std::string &scan_payload = _scan_device_results[device->_get_base_topic()];

if (json::get_unsigned_int(scan_payload, "info", "number_of_interfaces", _scan_itf_count_expected) == -1) {
spdlog::error("Unknown number of interface for device");
spdlog::error("Unknown number of interfaces for device {}", device->_get_base_topic());
return -1;
}

Expand Down
3 changes: 3 additions & 0 deletions source/pza/core/device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ int device::_set_identity(const std::string &payload)
return -1;
}

// Convert to lowercase
std::transform(family.begin(), family.end(), family.begin(), ::tolower);

if (family != get_family()) {
spdlog::error("Device is not compatible {} != {}", family, get_family());
return -1;
Expand Down
23 changes: 15 additions & 8 deletions source/pza/core/field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@

namespace pza
{
enum class access_mode
{
readonly,
readwrite
};

template <typename _type>
class field
{
public:
friend class attribute;

explicit field(const std::string &name, bool readonly)
: _name(name),
_readonly(readonly)
explicit field(const std::string &name, access_mode mode = access_mode::readonly)
: _value(_type()),
_name(name),
_mode(mode)
{
_setJsonType();
}
Expand Down Expand Up @@ -48,17 +55,17 @@ namespace pza

bool is_readonly(void) const
{
return _readonly;
return (_mode == access_mode::readonly);
}

using get_callback_type = std::function<void(_type)>;

void add_get_callback(const get_callback_type& callback)
void add_get_callback(const get_callback_type &callback)
{
_get_callbacks.push_back(std::make_shared<get_callback_type>(callback));
}

void remove_get_callback(const get_callback_type& callback)
void remove_get_callback(const get_callback_type &callback)
{
_get_callbacks.remove_if([&](const std::shared_ptr<get_callback_type>& ptr) {
return callback.target_type() == ptr->target_type();
Expand Down Expand Up @@ -107,11 +114,11 @@ namespace pza
}
}

_type _value = _type();
_type _value;
std::string _name;
nlohmann::json::value_t _json_type;
std::list<std::shared_ptr<get_callback_type>> _get_callbacks;
bool _readonly;
access_mode _mode;
std::function<int(const nlohmann::json &data)> _callback;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ namespace pza
{
class device;

class united_interface
class grouped_interface
{
public:
using ptr = std::shared_ptr<united_interface>;
using ptr = std::shared_ptr<grouped_interface>;

united_interface() = delete;
united_interface(const united_interface&) = delete;
united_interface(united_interface&&) = delete;
~united_interface() = delete;
grouped_interface() = delete;
grouped_interface(const grouped_interface&) = delete;
grouped_interface(grouped_interface&&) = delete;
~grouped_interface() = delete;

template <typename T>
static int register_interfaces(device *device, const std::string &name, const std::map<std::string, std::string> &map, std::vector<std::shared_ptr<T>> &channels)
{
int ret = 0;
size_t pos;
size_t chan_id;
size_t pos = 0;
int chan_id = -1;

for (auto const &elem : map) {
if (pza::string::starts_with(elem.first, ":" + name + "_") == true) {
Expand All @@ -35,8 +35,13 @@ namespace pza
}
}

if (chan_id == -1) {
spdlog::error("No {} channels found", name);
return -1;
}

channels.reserve(chan_id + 1);
for (size_t i = 0; i < chan_id + 1; i++) {
for (int i = 0; i < chan_id + 1; i++) {
channels.push_back(std::make_shared<T>(device, ":" + name + "_" + std::to_string(i) + ":"));
}

Expand Down
2 changes: 1 addition & 1 deletion source/pza/devices/bps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int bps::_register_interfaces(const std::map<std::string, std::string> &map)
{
int ret;

ret = united_interface::register_interfaces<bps_channel>(this, "channel", map, channel);
ret = grouped_interface::register_interfaces<bps_channel>(this, "channel", map, channel);
if (ret < 0)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion source/pza/devices/bps.hxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <pza/core/device.hxx>
#include <pza/core/united_interface.hxx>
#include <pza/core/grouped_interface.hxx>

#include <pza/interfaces/meter.hxx>
#include <pza/interfaces/bps_chan_ctrl.hxx>
Expand Down
Loading

0 comments on commit 034fd40

Please sign in to comment.