diff --git a/.clang-format b/.clang-format index 05d0b9a..c76ac62 100644 --- a/.clang-format +++ b/.clang-format @@ -4,11 +4,11 @@ TabWidth: 8 UseTab: Always BreakBeforeBraces: Linux AllowShortIfStatementsOnASingleLine: false -AllowShortFunctionsOnASingleLine: false +AllowShortFunctionsOnASingleLine: true AllowShortLoopsOnASingleLine: false AllowShortBlocksOnASingleLine: false IndentCaseLabels: false -ColumnLimit: 80 +ColumnLimit: 120 AccessModifierOffset: -8 NamespaceIndentation: None PointerAlignment: Right @@ -16,3 +16,4 @@ AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true FixNamespaceComments: false AlignAfterOpenBracket: Align +PackConstructorInitializers: Never diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bb31710..fad3aaf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,29 +7,46 @@ jobs: runs-on: ubuntu-latest container: image: ghcr.io/panduza/pzacxx-build-img:latest - + options: --user 1001 + strategy: + matrix: + os: [linux] + compiler: [gcc] + build_type: [Debug] + shared: ["True", "False"] + exclude: + - os: linux + compiler: mingw + - os: win + compiler: gcc + - os: win + compiler: clang steps: - name: Checkout uses: actions/checkout@v4 - if: ${{ env.ACT }} name: Hack container for local development - run: - pacman -S --noconfirm nodejs + run: pacman -S --noconfirm nodejs - - name: Cache Conan - id: cache-conan - uses: actions/cache@v3 + - name: "cache" + uses: actions/cache@v2 with: - path: ~/.conan2 - key: conan-cache + path: ~/.conan2/p + key: conan-cache-${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.build_type }}-${{ matrix.shared }} - - name: - if: steps.cache-conan.outputs.cache-hit != 'true' - run: - conan install -pr:b ./conan_profiles/linux_gcc -pr:h ./conan_profiles/linux_gcc --build=missing -s build_type=Debug -o shared=False . - + - name: "Install" + run: + conan install -pr:b ./conan_profiles/linux_gcc -pr:h ./conan_profiles/${{ matrix.os }}_${{ matrix.compiler }} --build=missing -s build_type=${{ matrix.build_type }} -o shared=${{ matrix.shared }} . - - name: Install dependencies and build (Debug, Static) + - name: "Build" run: | - echo "Building Debug, Static" + if [ "${{ matrix.shared }}" = "True" ]; then + lib_type="shared" + else + lib_type="static" + fi + ls -l build + echo "build folder: ./build/${{ matrix.os }}-${{ matrix.compiler }}-$lib_type" + cmake --preset ${{ matrix.os }}-${{ matrix.compiler }}-$lib_type + cmake --build ./build/${{ matrix.os }}-${{ matrix.compiler }}-$lib_type --config ${{ matrix.build_type }} diff --git a/examples/bps.cxx b/examples/bps.cxx index ac44cbf..62c09bd 100644 --- a/examples/bps.cxx +++ b/examples/bps.cxx @@ -19,22 +19,19 @@ int main() if (cli->register_devices() < 0) return -1; - auto vm = cli->get_interface( - "default", "Panduza_FakeBps", "channel", 0, "vm"); + auto vm = cli->get_interface("default", "Panduza_FakeBps", "channel", 0, "vm"); if (!vm) { spdlog::error("voltmeter not found"); return -1; } - auto ctrl = cli->get_interface( - "default", "Panduza_FakeBps", "channel", 0, "ctrl"); + auto ctrl = cli->get_interface("default", "Panduza_FakeBps", "channel", 0, "ctrl"); if (!ctrl) { spdlog::error("ctrl not found"); return -1; } - auto am = cli->get_interface( - "default", "Panduza_FakeBps", "channel", 0, "am"); + auto am = cli->get_interface("default", "Panduza_FakeBps", "channel", 0, "am"); if (!am) { spdlog::error("ammeter not found"); return -1; @@ -44,11 +41,9 @@ int main() ctrl->set_voltage(3.3); ctrl->set_current(0.1); - vm->register_measure_callback( - [&]() { spdlog::info("voltage: {}", vm->get_measure()); }); + vm->register_measure_callback([&]() { spdlog::info("voltage: {}", vm->get_measure()); }); - am->register_measure_callback( - [&]() { spdlog::info("current: {}", am->get_measure()); }); + am->register_measure_callback([&]() { spdlog::info("current: {}", am->get_measure()); }); while (true) ; diff --git a/include/pza/core/client.hxx b/include/pza/core/client.hxx index 675f0d8..b1bf9f9 100644 --- a/include/pza/core/client.hxx +++ b/include/pza/core/client.hxx @@ -18,8 +18,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit client(const std::string &addr, int port, - std::optional id = std::nullopt); + explicit client(const std::string &addr, int port, std::optional id = std::nullopt); client(const client &) = delete; client &operator=(const client &) = delete; client(client &&) = delete; @@ -37,50 +36,40 @@ public: void set_connection_timeout(unsigned int timeout_ms); [[nodiscard]] unsigned int get_connection_timeout() const; - device::s_ptr - register_device(const std::string &group, const std::string &name, - unsigned int timeout_ms = device_timeout_default); + device::s_ptr register_device(const std::string &group, const std::string &name, + unsigned int timeout_ms = device_timeout_default); int register_devices(unsigned int timeout_ms = devices_timeout_default); - [[nodiscard]] device::s_ptr get_device(const std::string &group, - const std::string &name) const; + [[nodiscard]] device::s_ptr get_device(const std::string &group, const std::string &name) const; [[nodiscard]] std::vector get_devices() const; - [[nodiscard]] std::vector - get_devices_in_group(const std::string &group) const; + [[nodiscard]] std::vector get_devices_in_group(const std::string &group) const; [[nodiscard]] std::set get_groups() const; - [[nodiscard]] itf_base::s_ptr - get_interface(const std::string &group, const std::string &name, - const std::string &interface_group, unsigned int idx, - const std::string &interface_name) const; - [[nodiscard]] itf_base::s_ptr - get_interface(const std::string &group, const std::string &name, - const std::string &interface_name) const; + [[nodiscard]] itf_base::s_ptr get_interface(const std::string &group, const std::string &name, + const std::string &interface_group, unsigned int idx, + const std::string &interface_name) const; + [[nodiscard]] itf_base::s_ptr get_interface(const std::string &group, const std::string &name, + const std::string &interface_name) const; template - [[nodiscard]] std::shared_ptr - get_interface(const std::string &group, const std::string &name, - const std::string &interface_group, unsigned int idx, - const std::string &interface_name) const + [[nodiscard]] std::shared_ptr get_interface(const std::string &group, const std::string &name, + const std::string &interface_group, unsigned int idx, + const std::string &interface_name) const { - return std::dynamic_pointer_cast(get_interface( - group, name, interface_group, idx, interface_name)); + return std::dynamic_pointer_cast(get_interface(group, name, interface_group, idx, interface_name)); } template - [[nodiscard]] std::shared_ptr - get_interface(const std::string &group, const std::string &name, - const std::string &interface_name) const + [[nodiscard]] std::shared_ptr get_interface(const std::string &group, const std::string &name, + const std::string &interface_name) const { - return std::dynamic_pointer_cast( - get_interface(group, name, interface_name)); + return std::dynamic_pointer_cast(get_interface(group, name, interface_name)); } private: static constexpr unsigned int platforms_timeout_default = 500; static constexpr unsigned int device_timeout_default = 500; - static constexpr unsigned int devices_timeout_default = - device_timeout_default * 5; + static constexpr unsigned int devices_timeout_default = device_timeout_default * 5; std::unique_ptr _impl; }; diff --git a/include/pza/core/device.hxx b/include/pza/core/device.hxx index 5e3f7c0..9033006 100644 --- a/include/pza/core/device.hxx +++ b/include/pza/core/device.hxx @@ -20,7 +20,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit device(mqtt_service &mqtt, struct device_info &info); + explicit device(mqtt_service *mqtt, struct device_info &info); device(const device &) = delete; device &operator=(const device &) = delete; device(device &&) = delete; @@ -35,32 +35,26 @@ public: [[nodiscard]] unsigned int get_number_of_interfaces() const; - [[nodiscard]] itf_base::s_ptr - get_interface(const std::string &name) const; + [[nodiscard]] itf_base::s_ptr get_interface(const std::string &name) const; - template - std::shared_ptr get_interface(const std::string &name) const + template std::shared_ptr get_interface(const std::string &name) const { return std::dynamic_pointer_cast(get_interface(name)); } - [[nodiscard]] itf_base::s_ptr - get_interface(const std::string &interface_group, unsigned int idx, - const std::string &name) const; + [[nodiscard]] itf_base::s_ptr get_interface(const std::string &interface_group, unsigned int idx, + const std::string &name) const; template - std::shared_ptr get_interface(const std::string &interface_group, - unsigned int idx, + std::shared_ptr get_interface(const std::string &interface_group, unsigned int idx, const std::string &name) const { - return std::dynamic_pointer_cast( - get_interface(interface_group, idx, name)); + return std::dynamic_pointer_cast(get_interface(interface_group, idx, name)); } [[nodiscard]] std::vector get_interface_names() const; - [[nodiscard]] unsigned int - get_interface_group_count(const std::string &group) const; + [[nodiscard]] unsigned int get_interface_group_count(const std::string &group) const; private: std::unique_ptr _impl; diff --git a/include/pza/core/interface.hxx b/include/pza/core/interface.hxx index e46ea94..6ffec6b 100644 --- a/include/pza/core/interface.hxx +++ b/include/pza/core/interface.hxx @@ -36,7 +36,7 @@ public: [[nodiscard]] const std::string &get_type() const; protected: - explicit itf_base(mqtt_service &mqtt, itf_info &info); + explicit itf_base(mqtt_service *mqtt, itf_info &info); std::unique_ptr _impl; }; diff --git a/include/pza/interfaces/ammeter.hxx b/include/pza/interfaces/ammeter.hxx index 3490a3d..246367e 100644 --- a/include/pza/interfaces/ammeter.hxx +++ b/include/pza/interfaces/ammeter.hxx @@ -11,7 +11,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit ammeter(mqtt_service &mqtt, itf_info &info); + explicit ammeter(mqtt_service *mqtt, itf_info &info); ammeter(const ammeter &) = delete; ammeter(ammeter &&) = delete; ammeter &operator=(const ammeter &) = delete; diff --git a/include/pza/interfaces/bps_chan_ctrl.hxx b/include/pza/interfaces/bps_chan_ctrl.hxx index 3181c39..3347164 100644 --- a/include/pza/interfaces/bps_chan_ctrl.hxx +++ b/include/pza/interfaces/bps_chan_ctrl.hxx @@ -11,7 +11,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit bps_chan_ctrl(mqtt_service &mqtt, itf_info &info); + 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; diff --git a/include/pza/interfaces/device.hxx b/include/pza/interfaces/device.hxx index 270724e..2bc3df1 100644 --- a/include/pza/interfaces/device.hxx +++ b/include/pza/interfaces/device.hxx @@ -11,7 +11,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit device(mqtt_service &mqtt, itf_info &info); + explicit device(mqtt_service *mqtt, itf_info &info); device(const device &) = delete; device(device &&) = delete; device &operator=(const device &) = delete; diff --git a/include/pza/interfaces/meter.hxx b/include/pza/interfaces/meter.hxx index d6b5431..b17dff0 100644 --- a/include/pza/interfaces/meter.hxx +++ b/include/pza/interfaces/meter.hxx @@ -15,7 +15,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit meter(mqtt_service &mqtt, itf_info &info); + explicit meter(mqtt_service *mqtt, itf_info &info); meter(const meter &) = delete; meter(meter &&) = delete; meter &operator=(const meter &) = delete; diff --git a/include/pza/interfaces/platform.hxx b/include/pza/interfaces/platform.hxx index 997bdc6..a3c906a 100644 --- a/include/pza/interfaces/platform.hxx +++ b/include/pza/interfaces/platform.hxx @@ -11,7 +11,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit platform(mqtt_service &mqtt, itf_info &info); + explicit platform(mqtt_service *mqtt, itf_info &info); platform(const platform &) = delete; platform(platform &&) = delete; platform &operator=(const platform &) = delete; diff --git a/include/pza/interfaces/voltmeter.hxx b/include/pza/interfaces/voltmeter.hxx index 81724c7..5295ad6 100644 --- a/include/pza/interfaces/voltmeter.hxx +++ b/include/pza/interfaces/voltmeter.hxx @@ -11,7 +11,7 @@ public: using u_ptr = std::unique_ptr; using w_ptr = std::weak_ptr; - explicit voltmeter(mqtt_service &mqtt, itf_info &info); + explicit voltmeter(mqtt_service *mqtt, itf_info &info); voltmeter(const voltmeter &) = delete; voltmeter(voltmeter &&) = delete; voltmeter &operator=(const voltmeter &) = delete; diff --git a/source/core/attribute.cxx b/source/core/attribute.cxx index 4c666c8..ae80a2e 100644 --- a/source/core/attribute.cxx +++ b/source/core/attribute.cxx @@ -1,6 +1,7 @@ #include "attribute.hxx" -attribute::attribute(std::string name) : _name(std::move(name)) +attribute::attribute(std::string name) + : _name(std::move(name)) { } @@ -30,8 +31,7 @@ void attribute::on_message(mqtt::const_message_ptr msg) } else if (std::holds_alternative(type)) { _set_field(json, name); } else { - spdlog::error( - "attribute::on_message: unknown field type"); + spdlog::error("attribute::on_message: unknown field type"); } } @@ -42,15 +42,11 @@ void attribute::on_message(mqtt::const_message_ptr msg) } } -void attribute::register_callback(const std::function &cb) -{ - _callbacks.push_back(cb); -} +void attribute::register_callback(const std::function &cb) { _callbacks.push_back(cb); } void attribute::remove_callback(const std::function &cb) { _callbacks.remove_if([&](const std::function &f) { - return f.target_type() == cb.target_type() && - f.target() == cb.target(); + return f.target_type() == cb.target_type() && f.target() == cb.target(); }); } diff --git a/source/core/attribute.hxx b/source/core/attribute.hxx index 783c268..c34a4be 100644 --- a/source/core/attribute.hxx +++ b/source/core/attribute.hxx @@ -23,15 +23,9 @@ public: attribute &operator=(attribute &&) = delete; ~attribute(); - const std::string &get_name() const - { - return _name; - } + const std::string &get_name() const { return _name; } - template void register_field(const std::string &name) - { - _fields[name] = T(); - } + template void register_field(const std::string &name) { _fields[name] = T(); } template const T &get_field(const std::string &name) { @@ -43,8 +37,7 @@ public: } } - template - int set_field(const std::string &field, const T &val) + template int set_field(const std::string &field, const T &val) { nlohmann::json data; std::mutex mtx; @@ -57,9 +50,8 @@ public: return -1; } - if (_cv.wait_for(lock, std::chrono::seconds(3), [&]() { - return std::get(_fields[field]) == val; - }) == false) { + if (_cv.wait_for(lock, std::chrono::seconds(3), [&]() { return std::get(_fields[field]) == val; }) == + false) { spdlog::error("attribute::set: timed out waiting for " "value to be set"); return -1; @@ -72,24 +64,16 @@ public: void register_callback(const std::function &cb); void remove_callback(const std::function &cb); - void set_msg_callback( - const std::function &cb) - { - _msg_cb = cb; - } + void set_msg_callback(const std::function &cb) { _msg_cb = cb; } private: - using field_types = - std::variant; + using field_types = std::variant; - template - void _set_field(json_attribute &json, const std::string &name) + template void _set_field(json_attribute &json, const std::string &name) { T val; if (json.get(name, val) < 0) { - spdlog::error( - "attribute::on_message: failed to get field {}", - name); + spdlog::error("attribute::on_message: failed to get field {}", name); return; } std::get(_fields[name]) = val; diff --git a/source/core/client.cxx b/source/core/client.cxx index f451650..80a549b 100644 --- a/source/core/client.cxx +++ b/source/core/client.cxx @@ -31,66 +31,38 @@ struct device_info { }; struct client_impl : mqtt_service { - explicit client_impl(const std::string &addr, int port, - std::optional id = std::nullopt); + explicit client_impl(const std::string &addr, int port, std::optional id = std::nullopt); 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(const std::string &topic, const std::string &payload) override; int publish(mqtt::const_message_ptr msg) override; - int subscribe( - const std::string &topic, - const std::function &cb) override; + int subscribe(const std::string &topic, const std::function &cb) override; int unsubscribe(const std::string &topic) override; void connection_lost(const std::string &cause); void message_arrived(mqtt::const_message_ptr msg); int scan_platforms(unsigned int timeout_ms); - int scan_device(const std::string &group, const std::string &name, - unsigned int timeout_ms); + int scan_device(const std::string &group, const std::string &name, unsigned int timeout_ms); int scan_devices(unsigned int timeout_ms); - device::s_ptr create_device(const std::string &group, - const std::string &name); - device::s_ptr register_device(const std::string &group, - const std::string &name, - unsigned int timeout_ms); + device::s_ptr create_device(const std::string &group, const std::string &name); + device::s_ptr register_device(const std::string &group, const std::string &name, unsigned int timeout_ms); int register_devices(unsigned int timeout_ms); - device::s_ptr get_device(const std::string &group, - const std::string &name) const; + device::s_ptr get_device(const std::string &group, const std::string &name) const; std::vector get_devices() const; - std::vector - get_devices_in_group(const std::string &group) const; + std::vector get_devices_in_group(const std::string &group) const; std::set get_groups() const; @@ -103,18 +75,16 @@ struct client_impl : mqtt_service { int _port; std::string _id; std::mutex _mtx; - std::unordered_map> - _listeners; + std::unordered_map> _listeners; std::unordered_map _devices; std::set _platforms_scanned; std::unordered_map _devices_scanned; unsigned int _device_count = 0; }; -client_impl::client_impl(const std::string &addr, int port, - std::optional id) - : _addr(addr), _port(port) +client_impl::client_impl(const std::string &addr, int port, std::optional id) + : _addr(addr), + _port(port) { std::string url = "tcp://" + addr + ":" + std::to_string(port); @@ -125,8 +95,7 @@ client_impl::client_impl(const std::string &addr, int port, std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, range); _id = "pza_" + std::to_string(dis(gen)); - spdlog::warn("no client id provided, using generated id: {}", - _id); + spdlog::warn("no client id provided, using generated id: {}", _id); } else _id = id.value(); @@ -145,16 +114,12 @@ client_impl::client_impl(const std::string &addr, int port, } }; - auto connection_lost = [&](const std::string &cause) { - spdlog::error("connection lost: {}", cause); - }; + auto connection_lost = [&](const std::string &cause) { spdlog::error("connection lost: {}", cause); }; on_device_info = [&](mqtt::const_message_ptr msg) { - std::string base_topic = msg->get_topic().substr( - 4, msg->get_topic().find("/device/atts/info") - 4); + std::string base_topic = msg->get_topic().substr(4, msg->get_topic().find("/device/atts/info") - 4); - spdlog::trace("received device info: {} {}", msg->get_topic(), - msg->get_payload_str()); + spdlog::trace("received device info: {} {}", msg->get_topic(), msg->get_payload_str()); _devices_scanned.emplace(base_topic, msg->get_payload_str()); }; @@ -174,8 +139,7 @@ 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) { + if (_paho_client->connect(connOpts)->wait_for(std::chrono::milliseconds(_conn_timeout)) == false) { spdlog::error("failed to connect to client: {}", _id); return -1; } @@ -188,8 +152,7 @@ int client_impl::disconnect() { spdlog::debug("Attempting to disconnect from {}...", _addr); - if (_paho_client->disconnect()->wait_for( - std::chrono::milliseconds(_conn_timeout)) == false) { + if (_paho_client->disconnect()->wait_for(std::chrono::milliseconds(_conn_timeout)) == false) { spdlog::error("failed to disconnect from client: {}", _id); return -1; } @@ -197,10 +160,7 @@ 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) { @@ -209,28 +169,22 @@ 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) { + if (_paho_client->publish(msg)->wait_for(std::chrono::milliseconds(msg_timeout_default_ms)) == false) { spdlog::error("failed to publish from client: {}", _id); return -1; } - spdlog::trace("published message {} to {}", msg->get_payload_str(), - msg->get_topic()); + spdlog::trace("published message {} to {}", msg->get_payload_str(), msg->get_topic()); return 0; } -int client_impl::subscribe( - const std::string &topic, - const std::function &cb) +int client_impl::subscribe(const std::string &topic, const std::function &cb) { std::string t; 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); + 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); _listeners.erase(t); return -1; } @@ -242,11 +196,8 @@ 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); + if (_paho_client->unsubscribe(topic)->wait_for(std::chrono::seconds(_conn_timeout)) == false) { + spdlog::error("failed to unsubscribe from topic: {} on client {}", topic, _id); return -1; } spdlog::trace("unsubscribed from topic: {}", topic); @@ -278,7 +229,7 @@ void client_impl::message_arrived(mqtt::const_message_ptr msg) int client_impl::scan_platforms(unsigned int timeout_ms) { - scanner scanner(*this); + scanner scanner(this); _platforms_scanned.clear(); @@ -290,8 +241,7 @@ int client_impl::scan_platforms(unsigned int timeout_ms) json_attribute json("info"); if (json.parse(payload) < 0) { - spdlog::error("failed to parse platform info: {}", - payload); + spdlog::error("failed to parse platform info: {}", payload); return; } @@ -299,8 +249,7 @@ int client_impl::scan_platforms(unsigned int timeout_ms) spdlog::error("failed to parse type info: {}", payload); return; } - if (type != "platform" || - _platforms_scanned.find(topic) != _platforms_scanned.end()) + if (type != "platform" || _platforms_scanned.find(topic) != _platforms_scanned.end()) return; _platforms_scanned.insert(topic); @@ -308,8 +257,7 @@ int client_impl::scan_platforms(unsigned int timeout_ms) spdlog::trace("received platform info: {}", payload); if (json.get_unsigned_int("number_of_devices", val) < 0) { - spdlog::error("failed to parse platform info: {}", - payload); + spdlog::error("failed to parse platform info: {}", payload); return; } _device_count += val; @@ -318,8 +266,7 @@ int client_impl::scan_platforms(unsigned int timeout_ms) scanner.set_scan_timeout_ms(timeout_ms) .set_message_callback(on_platform_info) .set_condition_callback([&]() { - std::this_thread::sleep_for( - std::chrono::milliseconds(platforms_timeout_ms)); + std::this_thread::sleep_for(std::chrono::milliseconds(platforms_timeout_ms)); return true; }) .set_publisher(mqtt::make_message("pza", "p")) @@ -335,31 +282,27 @@ int client_impl::scan_platforms(unsigned int timeout_ms) spdlog::error("No devices found on scanned platforms"); return -1; } - spdlog::info("Found {} platform{} with {} devices", - _platforms_scanned.size(), + spdlog::info("Found {} platform{} with {} devices", _platforms_scanned.size(), _platforms_scanned.size() > 1 ? "s" : "", _device_count); return 0; } int client_impl::scan_devices(unsigned int timeout_ms) { - scanner scanner(*this); + scanner scanner(this); _devices_scanned.clear(); auto on_device_info = [&](mqtt::const_message_ptr msg) { - std::string base_topic = msg->get_topic().substr( - 4, msg->get_topic().find("/device/atts/info") - 4); + std::string base_topic = msg->get_topic().substr(4, msg->get_topic().find("/device/atts/info") - 4); - spdlog::trace("received device info: {} {}", msg->get_topic(), - msg->get_payload_str()); + spdlog::trace("received device info: {} {}", msg->get_topic(), msg->get_payload_str()); _devices_scanned.emplace(base_topic, msg->get_payload_str()); }; scanner.set_scan_timeout_ms(timeout_ms) .set_message_callback(on_device_info) - .set_condition_callback( - [&]() { return (_device_count == _devices_scanned.size()); }) + .set_condition_callback([&]() { return (_device_count == _devices_scanned.size()); }) .set_publisher(mqtt::make_message("pza", "d")) .set_subscription_topic("pza/+/+/device/atts/info"); @@ -370,18 +313,16 @@ int client_impl::scan_devices(unsigned int timeout_ms) return 0; } -int client_impl::scan_device(const std::string &group, const std::string &name, - unsigned int timeout_ms) +int client_impl::scan_device(const std::string &group, const std::string &name, unsigned int timeout_ms) { - scanner scanner(*this); + scanner scanner(this); auto combined = group + "/" + name; _devices.erase(combined); scanner.set_scan_timeout_ms(timeout_ms) .set_message_callback(on_device_info) - .set_condition_callback( - [&]() { return _devices_scanned.count(combined) > 0; }) + .set_condition_callback([&]() { return _devices_scanned.count(combined) > 0; }) .set_publisher(mqtt::make_message("pza", combined)) .set_subscription_topic("pza/" + combined + "/device/atts/info"); @@ -392,8 +333,7 @@ int client_impl::scan_device(const std::string &group, const std::string &name, return 0; } -device::s_ptr client_impl::create_device(const std::string &group, - const std::string &name) +device::s_ptr client_impl::create_device(const std::string &group, const std::string &name) { device::s_ptr dev; json_attribute json("info"); @@ -403,16 +343,12 @@ device::s_ptr client_impl::create_device(const std::string &group, auto elem = _devices_scanned.at(combined); if (json.parse(elem) < 0) { - spdlog::error("failed to parse device info for device {}", - combined); + spdlog::error("failed to parse device info for device {}", combined); return nullptr; } - if (json.get_unsigned_int("number_of_interfaces", - info.number_of_interfaces) < 0) { - spdlog::error( - "failed to parse number of interfaces for device {}", - combined); + if (json.get_unsigned_int("number_of_interfaces", info.number_of_interfaces) < 0) { + spdlog::error("failed to parse number of interfaces for device {}", combined); return nullptr; } @@ -420,7 +356,7 @@ device::s_ptr client_impl::create_device(const std::string &group, info.name = name; try { - dev = std::make_shared(*this, info); + dev = std::make_shared(this, info); } catch (const std::exception &exc) { spdlog::error("failed to create device: {}", exc.what()); return nullptr; @@ -429,9 +365,7 @@ device::s_ptr client_impl::create_device(const std::string &group, return dev; } -device::s_ptr client_impl::register_device(const std::string &group, - const std::string &name, - unsigned int timeout_ms) +device::s_ptr client_impl::register_device(const std::string &group, const std::string &name, unsigned int timeout_ms) { if (scan_device(group, name, timeout_ms) < 0) { spdlog::error("failed to scan device {}", name); @@ -464,8 +398,7 @@ int client_impl::register_devices(unsigned int timeout_ms) return ret; } -device::s_ptr client_impl::get_device(const std::string &group, - const std::string &name) const +device::s_ptr client_impl::get_device(const std::string &group, const std::string &name) const { auto combined = group + "/" + name; @@ -484,8 +417,7 @@ std::vector client_impl::get_devices() const return ret; } -std::vector -client_impl::get_devices_in_group(const std::string &group) const +std::vector client_impl::get_devices_in_group(const std::string &group) const { std::vector ret; @@ -513,84 +445,45 @@ client::client(const std::string &addr, int port, std::optional 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) +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 +device::s_ptr client::get_device(const std::string &group, const std::string &name) const { return _impl->get_device(group, name); } -std::vector client::get_devices() const -{ - return _impl->get_devices(); -} +std::vector client::get_devices() const { return _impl->get_devices(); } -std::vector -client::get_devices_in_group(const std::string &group) const +std::vector client::get_devices_in_group(const std::string &group) const { return _impl->get_devices_in_group(group); } -std::set client::get_groups() const -{ - return _impl->get_groups(); -} +std::set 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, +itf_base::s_ptr client::get_interface(const std::string &group, const std::string &name, + const std::string &interface_group, unsigned int idx, const std::string &interface_name) const { auto dev = _impl->get_device(group, name); @@ -599,8 +492,7 @@ itf_base::s_ptr client::get_interface(const std::string &group, return dev->get_interface(interface_group, idx, interface_name); } -itf_base::s_ptr client::get_interface(const std::string &group, - const std::string &name, +itf_base::s_ptr client::get_interface(const std::string &group, const std::string &name, const std::string &interface_name) const { auto dev = _impl->get_device(group, name); diff --git a/source/core/core.cxx b/source/core/core.cxx index db49325..9102dff 100644 --- a/source/core/core.cxx +++ b/source/core/core.cxx @@ -5,7 +5,4 @@ void pza::core::set_log_level(pza::core::log_level level) spdlog::set_level(static_cast(level)); } -pza::core::log_level get_log_level() -{ - return static_cast(spdlog::get_level()); -} +pza::core::log_level get_log_level() { return static_cast(spdlog::get_level()); } diff --git a/source/core/device.cxx b/source/core/device.cxx index d278c32..620d1b8 100644 --- a/source/core/device.cxx +++ b/source/core/device.cxx @@ -32,8 +32,7 @@ struct device_info { static constexpr unsigned int interfaces_timeout = 1000; struct device_impl { - explicit device_impl(mqtt_service &mqtt, - const struct device_info &info); + explicit device_impl(mqtt_service *mqtt, const struct device_info &info); device_impl(const device_impl &) = delete; device_impl(device_impl &&) = delete; device_impl &operator=(const device_impl &) = delete; @@ -42,15 +41,11 @@ struct device_impl { void on_interface_info(mqtt::const_message_ptr msg); - unsigned int get_number_of_interfaces() const - { - return interfaces.size(); - } + unsigned int get_number_of_interfaces() const { return interfaces.size(); } itf_base::s_ptr find_interface(const std::string &name) const; itf_base::s_ptr get_interface(const std::string &name) const; - itf_base::s_ptr get_interface(const std::string &interface_group, - unsigned int idx, + itf_base::s_ptr get_interface(const std::string &interface_group, unsigned int idx, const std::string &name) const; std::vector get_interface_names() const; @@ -63,19 +58,16 @@ struct device_impl { itf::device::s_ptr device_interface = nullptr; }; -device_impl::device_impl(mqtt_service &mqtt, const struct device_info &info) +device_impl::device_impl(mqtt_service *mqtt, const struct device_info &info) : info(info) { scanner scanner(mqtt); auto on_interface_info = [&](mqtt::const_message_ptr msg) { - std::string base_topic = msg->get_topic().substr( - 0, msg->get_topic().find("/atts/info")); - std::string itf_name = - base_topic.substr(base_topic.find_last_of('/') + 1); + std::string base_topic = msg->get_topic().substr(0, msg->get_topic().find("/atts/info")); + std::string itf_name = base_topic.substr(base_topic.find_last_of('/') + 1); - spdlog::trace("received interface info: {} {}", - msg->get_topic(), msg->get_payload_str()); + spdlog::trace("received interface info: {} {}", msg->get_topic(), msg->get_payload_str()); interfaces_scanned[itf_name] = msg->get_payload_str(); }; @@ -83,19 +75,14 @@ device_impl::device_impl(mqtt_service &mqtt, const struct device_info &info) scanner.set_scan_timeout_ms(interfaces_timeout) .set_message_callback(on_interface_info) .set_condition_callback([&]() { - return (info.number_of_interfaces && - (info.number_of_interfaces == - interfaces_scanned.size())); + return (info.number_of_interfaces && (info.number_of_interfaces == interfaces_scanned.size())); }) - .set_publisher( - mqtt::make_message("pza", info.group + "/" + info.name)) - .set_subscription_topic("pza/" + info.group + "/" + info.name + - "/+/atts/info"); + .set_publisher(mqtt::make_message("pza", info.group + "/" + info.name)) + .set_subscription_topic("pza/" + info.group + "/" + info.name + "/+/atts/info"); if (scanner.run() < 0) - spdlog::error( - "timed out waiting for interfaces, expected {} got {}", - info.number_of_interfaces, interfaces_scanned.size()); + spdlog::error("timed out waiting for interfaces, expected {} got {}", info.number_of_interfaces, + interfaces_scanned.size()); } device_impl::~device_impl() = default; @@ -111,17 +98,12 @@ itf_base::s_ptr device_impl::find_interface(const std::string &name) const return it->second; } -itf_base::s_ptr device_impl::get_interface(const std::string &name) const -{ - return find_interface(name); -} +itf_base::s_ptr device_impl::get_interface(const std::string &name) const { return find_interface(name); } -itf_base::s_ptr device_impl::get_interface(const std::string &interface_group, - unsigned int idx, +itf_base::s_ptr device_impl::get_interface(const std::string &interface_group, unsigned int idx, const std::string &name) const { - std::string full_name = - ":" + interface_group + "_" + std::to_string(idx) + ":_" + name; + std::string full_name = ":" + interface_group + "_" + std::to_string(idx) + ":_" + name; return find_interface(full_name); } @@ -136,8 +118,7 @@ std::vector device_impl::get_interface_names() const return names; } -unsigned int -device_impl::get_interface_group_count(const std::string &group) const +unsigned int device_impl::get_interface_group_count(const std::string &group) const { std::regex pattern(":(" + group + ")_([0-9]+):_.*"); std::smatch matches; @@ -151,7 +132,7 @@ device_impl::get_interface_group_count(const std::string &group) const return unique_ids.size(); } -device::device(mqtt_service &mqtt, struct device_info &info) +device::device(mqtt_service *mqtt, struct device_info &info) : _impl(std::make_unique(mqtt, info)) { json_attribute json("info"); @@ -160,35 +141,27 @@ device::device(mqtt_service &mqtt, struct device_info &info) std::string type; if (json.parse(itf.second) < 0) { - spdlog::error( - "failed to parse attribute info for interface {}", - itf.first); + spdlog::error("failed to parse attribute info for interface {}", itf.first); continue; } if (json.get_string("type", type) < 0) { - spdlog::error("failed to get type for interface {}", - itf.first); + spdlog::error("failed to get type for interface {}", itf.first); continue; } - auto itf_ptr = interface_factory::create_interface( - mqtt, info.group, info.name, itf.first, type); + auto itf_ptr = interface_factory::create_interface(mqtt, info.group, info.name, itf.first, type); if (itf_ptr == nullptr) { - spdlog::error( - "failed to create interface {} of type {}", - itf.first, type); + spdlog::error("failed to create interface {} of type {}", itf.first, type); continue; } _impl->interfaces[itf.first] = itf_ptr; } if (_impl->interfaces.find("device") != _impl->interfaces.end()) { - _impl->device_interface = std::static_pointer_cast( - _impl->interfaces["device"]); + _impl->device_interface = std::static_pointer_cast(_impl->interfaces["device"]); _impl->info.family = _impl->device_interface->get_family(); - _impl->info.manufacturer = - _impl->device_interface->get_manufacturer(); + _impl->info.manufacturer = _impl->device_interface->get_manufacturer(); _impl->info.model = _impl->device_interface->get_model(); } else { throw std::runtime_error("device interface not found"); @@ -197,52 +170,27 @@ device::device(mqtt_service &mqtt, struct device_info &info) device::~device() = default; -const std::string &device::get_name() const -{ - return _impl->info.name; -} +const std::string &device::get_name() const { return _impl->info.name; } -const std::string &device::get_group() const -{ - return _impl->info.group; -} +const std::string &device::get_group() const { return _impl->info.group; } -const std::string &device::get_model() const -{ - return _impl->info.model; -} +const std::string &device::get_model() const { return _impl->info.model; } -const std::string &device::get_manufacturer() const -{ - return _impl->info.manufacturer; -} +const std::string &device::get_manufacturer() const { return _impl->info.manufacturer; } -const std::string &device::get_family() const -{ - return _impl->info.family; -} +const std::string &device::get_family() const { return _impl->info.family; } -unsigned int device::get_number_of_interfaces() const -{ - return _impl->get_number_of_interfaces(); -} +unsigned int device::get_number_of_interfaces() const { return _impl->get_number_of_interfaces(); } -itf_base::s_ptr device::get_interface(const std::string &name) const -{ - return _impl->get_interface(name); -} +itf_base::s_ptr device::get_interface(const std::string &name) const { return _impl->get_interface(name); } -itf_base::s_ptr device::get_interface(const std::string &interface_group, - unsigned int idx, +itf_base::s_ptr device::get_interface(const std::string &interface_group, unsigned int idx, const std::string &name) const { return _impl->get_interface(interface_group, idx, name); } -std::vector device::get_interface_names() const -{ - return _impl->get_interface_names(); -} +std::vector device::get_interface_names() const { return _impl->get_interface_names(); } unsigned int device::get_interface_group_count(const std::string &group) const { diff --git a/source/core/interface.cxx b/source/core/interface.cxx index 0ffe721..529114c 100644 --- a/source/core/interface.cxx +++ b/source/core/interface.cxx @@ -14,10 +14,11 @@ using namespace pza; 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_cmd(topic_base + "/cmds/set"), mqtt(mqtt) +itf_impl::itf_impl(mqtt_service *mqtt, itf_info &info) + : info(info), + topic_base("pza/" + info.group + "/" + info.device_name + "/" + info.name), + topic_cmd(topic_base + "/cmds/set"), + mqtt(mqtt) { } @@ -27,12 +28,11 @@ itf_impl::~itf_impl() auto att = elem.second.get(); std::string att_topic = topic_base + "/atts/" + att->get_name(); - mqtt.unsubscribe(att_topic); + mqtt->unsubscribe(att_topic); } } -void itf_impl::register_attributes( - const std::list &attribute_list) +void itf_impl::register_attributes(const std::list &attribute_list) { std::mutex mtx; std::unique_lock lock(mtx); @@ -44,26 +44,24 @@ void itf_impl::register_attributes( std::string att_topic = topic_base + "/atts/" + att_name; attributes[att_name] = att; - mqtt.subscribe(att_topic, [&](mqtt::const_message_ptr msg) { + mqtt->subscribe(att_topic, [&](mqtt::const_message_ptr msg) { att->on_message(msg); count++; cv.notify_one(); }); } - if (cv.wait_for(lock, std::chrono::seconds(attributes_timeout), [&]() { - return (count == attributes.size()); - }) == false) { + if (cv.wait_for(lock, std::chrono::seconds(attributes_timeout), + [&]() { return (count == attributes.size()); }) == false) { throw std::runtime_error("timed out waiting for attributes"); } auto send_message = [&](const nlohmann::json &data) { - return mqtt.publish(mqtt::make_message(topic_cmd, data.dump())); + return mqtt->publish(mqtt::make_message(topic_cmd, data.dump())); }; auto on_message = [&](mqtt::const_message_ptr msg) { - nlohmann::json data = - nlohmann::json::parse(msg->get_payload_str()); + nlohmann::json data = nlohmann::json::parse(msg->get_payload_str()); std::string att_name = data.begin().key(); auto it = attributes.find(att_name); @@ -80,33 +78,24 @@ void itf_impl::register_attributes( std::string att_topic = topic_base + "/atts/" + att->get_name(); att->set_msg_callback(send_message); - mqtt.subscribe(att_topic, on_message); + mqtt->subscribe(att_topic, on_message); } } -attribute &itf_impl::get_attribute(const std::string &name) -{ - return *attributes[name]; -} +attribute &itf_impl::get_attribute(const std::string &name) { return *attributes[name]; } int itf_impl::send_message(const nlohmann::json &data) { - return mqtt.publish(mqtt::make_message(topic_cmd, data.dump())); + return mqtt->publish(mqtt::make_message(topic_cmd, data.dump())); } -itf_base::itf_base(mqtt_service &mqtt, itf_info &info) +itf_base::itf_base(mqtt_service *mqtt, itf_info &info) : _impl(std::make_unique(mqtt, info)) { } itf_base::~itf_base() = default; -const std::string &itf_base::get_name() const -{ - return _impl->get_name(); -} +const std::string &itf_base::get_name() const { return _impl->get_name(); } -const std::string &itf_base::get_type() const -{ - return _impl->get_type(); -} +const std::string &itf_base::get_type() const { return _impl->get_type(); } diff --git a/source/core/interface.hxx b/source/core/interface.hxx index 14f3518..d530c81 100644 --- a/source/core/interface.hxx +++ b/source/core/interface.hxx @@ -18,7 +18,7 @@ struct itf_info { }; struct itf_impl { - explicit itf_impl(mqtt_service &mqtt, itf_info &info); + explicit itf_impl(mqtt_service *mqtt, itf_info &info); virtual ~itf_impl(); itf_impl(const itf_impl &) = delete; @@ -26,17 +26,10 @@ 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_list); + void register_attributes(const std::list &attribute_list); attribute &get_attribute(const std::string &name); @@ -45,6 +38,6 @@ struct itf_impl { itf_info info; std::string topic_base; std::string topic_cmd; - mqtt_service &mqtt; + mqtt_service *mqtt; std::unordered_map attributes; }; diff --git a/source/core/interface_factory.cxx b/source/core/interface_factory.cxx index 8a8c2a9..dfb0a14 100644 --- a/source/core/interface_factory.cxx +++ b/source/core/interface_factory.cxx @@ -8,24 +8,21 @@ #include #include -template -static itf_base::s_ptr allocate_interface(mqtt_service &mqtt, itf_info &info) +template static itf_base::s_ptr allocate_interface(mqtt_service *mqtt, itf_info &info) { return std::make_shared(mqtt, info); } -static const std::unordered_map - factory_map = {{"device", allocate_interface}, - {"platform", allocate_interface}, - {"ammeter", allocate_interface}, - {"voltmeter", allocate_interface}, - {"bpc", allocate_interface}}; - -itf_base::s_ptr interface_factory::create_interface( - mqtt_service &mqtt, const std::string &group, - const std::string &device_name, const std::string &name, - const std::string &type) +static const std::unordered_map factory_map = { + {"device", allocate_interface}, + {"platform", allocate_interface}, + {"ammeter", allocate_interface}, + {"voltmeter", allocate_interface}, + {"bpc", allocate_interface}}; + +itf_base::s_ptr interface_factory::create_interface(mqtt_service *mqtt, const std::string &group, + const std::string &device_name, const std::string &name, + const std::string &type) { itf_info info; diff --git a/source/core/interface_factory.hxx b/source/core/interface_factory.hxx index e896f5f..ac8b1c7 100644 --- a/source/core/interface_factory.hxx +++ b/source/core/interface_factory.hxx @@ -13,11 +13,8 @@ using namespace pza; namespace interface_factory { -using factory_function = - std::function; +using factory_function = std::function; -itf_base::s_ptr create_interface(mqtt_service &mqtt, const std::string &group, - const std::string &device_name, - const std::string &name, - const std::string &type); -}; \ No newline at end of file +itf_base::s_ptr create_interface(mqtt_service *mqtt, const std::string &group, const std::string &device_name, + const std::string &name, const std::string &type); +}; diff --git a/source/core/mqtt_service.hxx b/source/core/mqtt_service.hxx index f271cb3..ccf6b5a 100644 --- a/source/core/mqtt_service.hxx +++ b/source/core/mqtt_service.hxx @@ -8,6 +8,8 @@ class mqtt_service { public: + using s_ptr = std::shared_ptr; + mqtt_service() = default; mqtt_service(const mqtt_service &) = delete; mqtt_service &operator=(const mqtt_service &) = delete; @@ -15,11 +17,8 @@ public: mqtt_service &operator=(mqtt_service &&) = delete; virtual ~mqtt_service() = default; - virtual int publish(const std::string &topic, - const std::string &payload) = 0; + virtual int publish(const std::string &topic, const std::string &payload) = 0; virtual int publish(mqtt::const_message_ptr msg) = 0; - virtual int - subscribe(const std::string &topic, - const std::function &cb) = 0; + virtual int subscribe(const std::string &topic, const std::function &cb) = 0; virtual int unsubscribe(const std::string &topic) = 0; }; diff --git a/source/core/scanner.cxx b/source/core/scanner.cxx index ed253f6..517aee9 100644 --- a/source/core/scanner.cxx +++ b/source/core/scanner.cxx @@ -1,7 +1,9 @@ #include "scanner.hxx" -scanner::scanner(mqtt_service &mqtt) - : _mqtt(mqtt), _message_cb(nullptr), _condition_cb(nullptr), +scanner::scanner(mqtt_service *mqtt) + : _mqtt(mqtt), + _message_cb(nullptr), + _condition_cb(nullptr), _pub_msg(nullptr) { } @@ -11,8 +13,7 @@ int scanner::run() bool ret = false; std::unique_lock lock(_mtx); - if (_message_cb == nullptr || _condition_cb == nullptr || - _pub_msg == nullptr || _sub_topic.empty()) { + if (_message_cb == nullptr || _condition_cb == nullptr || _pub_msg == nullptr || _sub_topic.empty()) { spdlog::error("scanner not configured"); return -1; } @@ -22,10 +23,9 @@ int scanner::run() _cv.notify_one(); }; - _mqtt.subscribe(_sub_topic, _on_message); - _mqtt.publish(_pub_msg); - ret = _cv.wait_for(lock, std::chrono::milliseconds(_scan_timeout), - _condition_cb); - _mqtt.unsubscribe(_sub_topic); + _mqtt->subscribe(_sub_topic, _on_message); + _mqtt->publish(_pub_msg); + ret = _cv.wait_for(lock, std::chrono::milliseconds(_scan_timeout), _condition_cb); + _mqtt->unsubscribe(_sub_topic); return (ret == true) ? 0 : -1; } diff --git a/source/core/scanner.hxx b/source/core/scanner.hxx index 730d909..7306bb3 100644 --- a/source/core/scanner.hxx +++ b/source/core/scanner.hxx @@ -10,15 +10,14 @@ class scanner { public: - explicit scanner(mqtt_service &mqtt); + explicit scanner(mqtt_service *mqtt); scanner &set_scan_timeout_ms(unsigned int timeout) { _scan_timeout = timeout; return *this; } - scanner &set_message_callback( - const std::function &cb) + scanner &set_message_callback(const std::function &cb) { _message_cb = cb; return *this; @@ -43,7 +42,7 @@ public: private: static constexpr unsigned int _scan_timeout_default = 3; // in seconds unsigned int _scan_timeout = _scan_timeout_default; - mqtt_service &_mqtt; + mqtt_service *_mqtt; std::function _message_cb; std::function _condition_cb; std::condition_variable _cv; diff --git a/source/interfaces/ammeter.cxx b/source/interfaces/ammeter.cxx index 6ac0a49..5212eeb 100644 --- a/source/interfaces/ammeter.cxx +++ b/source/interfaces/ammeter.cxx @@ -4,7 +4,8 @@ using namespace pza::itf; -ammeter::ammeter(mqtt_service &mqtt, itf_info &info) : meter(mqtt, info) +ammeter::ammeter(mqtt_service *mqtt, itf_info &info) + : meter(mqtt, info) { } diff --git a/source/interfaces/bps_chan_ctrl.cxx b/source/interfaces/bps_chan_ctrl.cxx index af7ac87..d886e06 100644 --- a/source/interfaces/bps_chan_ctrl.cxx +++ b/source/interfaces/bps_chan_ctrl.cxx @@ -4,7 +4,7 @@ using namespace pza::itf; -bps_chan_ctrl::bps_chan_ctrl(mqtt_service &mqtt, itf_info &info) +bps_chan_ctrl::bps_chan_ctrl(mqtt_service *mqtt, itf_info &info) : itf_base(mqtt, info) { _enable = std::make_shared("enable"); @@ -56,64 +56,32 @@ int bps_chan_ctrl::set_current(double amps) return _current->set_field("value", amps); } -int bps_chan_ctrl::set_enable(bool enable) -{ - return _enable->set_field("value", enable); -} +int bps_chan_ctrl::set_enable(bool enable) { return _enable->set_field("value", enable); } -bool bps_chan_ctrl::get_enable() -{ - return _enable->get_field("value"); -} +bool bps_chan_ctrl::get_enable() { return _enable->get_field("value"); } -double bps_chan_ctrl::get_min_voltage() -{ - return _voltage->get_field("min"); -} +double bps_chan_ctrl::get_min_voltage() { return _voltage->get_field("min"); } -double bps_chan_ctrl::get_max_voltage() -{ - return _voltage->get_field("max"); -} +double bps_chan_ctrl::get_max_voltage() { return _voltage->get_field("max"); } -double bps_chan_ctrl::get_min_current() -{ - return _current->get_field("min"); -} +double bps_chan_ctrl::get_min_current() { return _current->get_field("min"); } -double bps_chan_ctrl::get_max_current() -{ - return _current->get_field("max"); -} +double bps_chan_ctrl::get_max_current() { return _current->get_field("max"); } -double bps_chan_ctrl::get_preset_voltage() -{ - return _voltage->get_field("value"); -} +double bps_chan_ctrl::get_preset_voltage() { return _voltage->get_field("value"); } -double bps_chan_ctrl::get_preset_current() -{ - return _current->get_field("value"); -} +double bps_chan_ctrl::get_preset_current() { return _current->get_field("value"); } -unsigned int bps_chan_ctrl::get_num_decimals_voltage() -{ - return _voltage->get_field("decimals"); -} +unsigned int bps_chan_ctrl::get_num_decimals_voltage() { return _voltage->get_field("decimals"); } -unsigned int bps_chan_ctrl::get_num_decimals_current() -{ - return _current->get_field("decimals"); -} +unsigned int bps_chan_ctrl::get_num_decimals_current() { return _current->get_field("decimals"); } -void bps_chan_ctrl::register_enable_callback( - const std::function &callback) +void bps_chan_ctrl::register_enable_callback(const std::function &callback) { _enable->register_callback(callback); } -void bps_chan_ctrl::remove_enable_callback( - const std::function &callback) +void bps_chan_ctrl::remove_enable_callback(const std::function &callback) { _enable->remove_callback(callback); } diff --git a/source/interfaces/device.cxx b/source/interfaces/device.cxx index 7ca061a..2aefdd5 100644 --- a/source/interfaces/device.cxx +++ b/source/interfaces/device.cxx @@ -4,7 +4,8 @@ using namespace pza::itf; -device::device(mqtt_service &mqtt, itf_info &info) : itf_base(mqtt, info) +device::device(mqtt_service *mqtt, itf_info &info) + : itf_base(mqtt, info) { _identity = std::make_shared("identity"); @@ -17,17 +18,8 @@ device::device(mqtt_service &mqtt, itf_info &info) : itf_base(mqtt, info) device::~device() = default; -const std::string &device::get_model() -{ - return _identity->get_field("model"); -} +const std::string &device::get_model() { return _identity->get_field("model"); } -const std::string &device::get_manufacturer() -{ - return _identity->get_field("manufacturer"); -} +const std::string &device::get_manufacturer() { return _identity->get_field("manufacturer"); } -const std::string &device::get_family() -{ - return _identity->get_field("family"); -} +const std::string &device::get_family() { return _identity->get_field("family"); } diff --git a/source/interfaces/meter.cxx b/source/interfaces/meter.cxx index e8b0ac1..cd7b808 100644 --- a/source/interfaces/meter.cxx +++ b/source/interfaces/meter.cxx @@ -4,7 +4,8 @@ using namespace pza::itf; -meter::meter(mqtt_service &mqtt, itf_info &info) : itf_base(mqtt, info) +meter::meter(mqtt_service *mqtt, itf_info &info) + : itf_base(mqtt, info) { _measure = std::make_shared("measure"); @@ -15,10 +16,7 @@ meter::meter(mqtt_service &mqtt, itf_info &info) : itf_base(mqtt, info) meter::~meter() = default; -double meter::get_measure() -{ - return _impl->get_attribute("measure").get_field("value"); -} +double meter::get_measure() { return _impl->get_attribute("measure").get_field("value"); } void meter::register_measure_callback(const std::function &callback) { diff --git a/source/interfaces/platform.cxx b/source/interfaces/platform.cxx index 4fc74cc..4dff8a3 100644 --- a/source/interfaces/platform.cxx +++ b/source/interfaces/platform.cxx @@ -4,7 +4,8 @@ using namespace pza::itf; -platform::platform(mqtt_service &mqtt, itf_info &info) : itf_base(mqtt, info) +platform::platform(mqtt_service *mqtt, itf_info &info) + : itf_base(mqtt, info) { } diff --git a/source/interfaces/voltmeter.cxx b/source/interfaces/voltmeter.cxx index bcfb645..f5c4141 100644 --- a/source/interfaces/voltmeter.cxx +++ b/source/interfaces/voltmeter.cxx @@ -4,7 +4,8 @@ using namespace pza::itf; -voltmeter::voltmeter(mqtt_service &mqtt, itf_info &info) : meter(mqtt, info) +voltmeter::voltmeter(mqtt_service *mqtt, itf_info &info) + : meter(mqtt, info) { } diff --git a/source/utils/json_attribute.cxx b/source/utils/json_attribute.cxx index 34d1239..4110571 100644 --- a/source/utils/json_attribute.cxx +++ b/source/utils/json_attribute.cxx @@ -16,27 +16,12 @@ 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); } diff --git a/source/utils/topic.cxx b/source/utils/topic.cxx index 1036501..8e089e8 100644 --- a/source/utils/topic.cxx +++ b/source/utils/topic.cxx @@ -1,6 +1,7 @@ #include "topic.hxx" -topic::topic(const std::string &topic) : _topic(topic) +topic::topic(const std::string &topic) + : _topic(topic) { std::stringstream strs(topic); std::string buf; @@ -31,8 +32,7 @@ std::string topic::regexify_topic(const std::string &topic) return t; } -bool topic::topic_matches(const std::string &str, - const std::string &fnmatchPattern) +bool topic::topic_matches(const std::string &str, const std::string &fnmatchPattern) { std::string regexPattern = _convertPattern(fnmatchPattern); std::regex pattern(regexPattern); diff --git a/source/utils/topic.hxx b/source/utils/topic.hxx index 4fcb29e..4632ff4 100644 --- a/source/utils/topic.hxx +++ b/source/utils/topic.hxx @@ -17,27 +17,14 @@ 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); + static bool topic_matches(const std::string &str, const std::string &fnmatchPattern); static std::string _convertPattern(const std::string &fnmatchPattern); private: