Skip to content

Commit

Permalink
Sparkplug proto (#119)
Browse files Browse the repository at this point in the history
* cleanup vcpkg.json

* add protobuf for sparkplug_b

* Committing clang-format changes

* Added some data to existing example

* Committing clang-format changes

---------

Co-authored-by: Ómar Högni Guðmarsson <[email protected]>
Co-authored-by: omarhogni <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2023
1 parent 726f7b0 commit 14581d5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory(confman)
add_subdirectory(ipc)
add_subdirectory(soem_interface)
add_subdirectory(operation_mode)
add_subdirectory(mqtt-sparkplug)

add_library(tfc INTERFACE)
add_library(tfc::tfc ALIAS tfc)
Expand Down
1 change: 1 addition & 0 deletions libs/mqtt-sparkplug/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sparkplug_b
32 changes: 32 additions & 0 deletions libs/mqtt-sparkplug/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.24)
project(mqtt-sparkplug)

find_package(protobuf CONFIG REQUIRED)

include(FetchContent)
FetchContent_Declare(
sparkplug_proto
GIT_REPOSITORY https://github.com/eclipse/tahu.git
GIT_TAG v1.0.4
)
FetchContent_Populate(sparkplug_proto)

file(
COPY
${sparkplug_proto_SOURCE_DIR}/sparkplug_b/sparkplug_b.proto
DESTINATION
${CMAKE_CURRENT_SOURCE_DIR}/sparkplug_b/
)

add_library(sparkplug_proto
${CMAKE_CURRENT_SOURCE_DIR}/sparkplug_b/sparkplug_b.proto
)
add_library(tfc::sparkplug::proto ALIAS sparkplug_proto)
target_link_libraries(sparkplug_proto
PUBLIC
protobuf::libprotobuf
)
target_include_directories(sparkplug_proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate(TARGET sparkplug_proto LANGUAGE cpp)

add_subdirectory(example)
7 changes: 7 additions & 0 deletions libs/mqtt-sparkplug/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

add_executable(mqtt-sparkplug-example mqtt-sparkplug-example.cpp)

target_link_libraries(mqtt-sparkplug-example PUBLIC tfc::sparkplug::proto)



51 changes: 51 additions & 0 deletions libs/mqtt-sparkplug/example/mqtt-sparkplug-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

// specification https://sparkplug.eclipse.org/specification/version/3.0/documents/sparkplug-specification-3.0.0.pdf

#include <sparkplug_b/sparkplug_b.pb.h>
#include <filesystem>

using org::eclipse::tahu::protobuf::Payload;
using org::eclipse::tahu::protobuf::Payload_Metric;

using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::system_clock;

int main() {
[[maybe_unused]] Payload_Metric metric{};
[[maybe_unused]] Payload p{};
metric.set_name("test");
metric.set_timestamp(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());

std::cout << metric.name() << std::endl;

std::cout << "--------Sparkplug b DATA-----------" << std::endl;

std::vector<uint8_t> data = { 8, 240, 163, 235, 168, 148, 49, 18, 46, 10, 20, 78, 111, 100, 101, 95, 82, 101, 100,
47, 115, 111, 109, 101, 95, 115, 116, 114, 105, 110, 103, 24, 240, 163, 235, 168, 148, 49,
32, 12, 122, 13, 73, 115, 32, 116, 104, 105, 115, 32, 108, 105, 102, 101, 63, 24, 6 };
p.ParseFromArray(data.data(), data.size());
p.PrintDebugString();

std::cout << "--------Sparkplug b Nbirth-----------" << std::endl;

data = { 8, 134, 165, 128, 171, 148, 49, 18, 26, 10, 20, 78, 111, 100, 101, 32, 67,
111, 110, 116, 114, 111, 108, 47, 82, 101, 98, 105, 114, 116, 104, 32, 11, 112,
0, 18, 11, 10, 5, 98, 100, 83, 101, 113, 32, 8, 88, 0, 24, 0 };
p.ParseFromArray(data.data(), data.size());
p.PrintDebugString();

std::cout << "--------Sparkplug b Nbirth #2-----------" << std::endl;
data = { 8, 177, 192, 243, 202, 148, 49, 18, 26, 10, 20, 78, 111, 100, 101, 95, 82, 101, 100, 47, 115, 111,
109, 101, 95, 110, 117, 109, 98, 101, 114, 32, 3, 56, 1, 18, 26, 10, 20, 78, 111, 100, 101, 95,
82, 101, 100, 47, 115, 111, 109, 101, 95, 115, 116, 114, 105, 110, 103, 32, 12, 56, 1, 24, 1 };
p.ParseFromArray(data.data(), data.size());
p.PrintDebugString();

std::cout << "-------------------" << std::endl;
for (auto& k : p.metrics()) {
std::cout << k.name() << std::endl;
}
std::cout << "-------------------" << std::endl;
return 0;
}
11 changes: 5 additions & 6 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@
"host": true
},
"azmq",
"bext-sml",
"bext-ut",
"boost-url",
"boost-asio",
"boost-program-options",
"boost-stacktrace",
"boost-beast",
"date",
"fmt",
"glaze",
"gtest",
"spdlog",
"magic-enum",
"mp-units",
"bext-sml",
"soem",
{
"name": "libgpiod",
"features": ["cxx-bindings"]
},
"magic-enum",
"mp-units",
"sdbusplus",
"date"
"protobuf"
]
}

0 comments on commit 14581d5

Please sign in to comment.