#include <iostream>
#include <string>
#include "custom_datatypes.hpp"
int main() {
const auto& dataTypePoint = getPointDataType();
const auto& dataTypeMeasurements = getMeasurementsDataType();
const auto& dataTypeOpt = getOptDataType();
const auto& dataTypeUni = getUniDataType();
const auto& dataTypeColor = getColorDataType();
dataTypePoint,
dataTypeMeasurements,
dataTypeOpt,
dataTypeUni,
dataTypeColor,
});
client.
connect(
"opc.tcp://localhost:4840");
variant =
opcua::Node(client, {1,
"Point"}).readValue();
if (variant.
isType(dataTypePoint)) {
const auto* p =
static_cast<Point*
>(variant.
data());
std::cout << "Point:\n";
std::cout << "- x = " << p->x << "\n";
std::cout << "- y = " << p->y << "\n";
std::cout << "- z = " << p->z << "\n";
}
variant =
opcua::Node(client, {1,
"PointVec"}).readValue();
size_t i = 0;
const auto* p = static_cast<Point*>(extObj.decodedData());
std::cout << "PointVec[" << i++ << "]:\n";
std::cout << "- x = " << p->x << "\n";
std::cout << "- y = " << p->y << "\n";
std::cout << "- z = " << p->z << "\n";
}
}
variant =
opcua::Node(client, {1,
"Measurements"}).readValue();
if (variant.
isType(dataTypeMeasurements)) {
const auto* m =
static_cast<Measurements*
>(variant.
data());
std::cout << "Measurements:\n";
std::cout << "- description = " << m->description << "\n";
size_t i = 0;
for (
auto&& value :
opcua::Span(m->measurements, m->measurementsSize)) {
std::cout << "- measurements[" << i++ << "] = " << value << "\n";
}
}
auto formatOptional = [](const auto* ptr) {
return ptr == nullptr ? "NULL" : std::to_string(*ptr);
};
const auto* opt =
static_cast<Opt*
>(variant.
data());
std::cout << "Opt:\n";
std::cout << "- a = " << opt->a << "\n";
std::cout << "- b = " << formatOptional(opt->b) << "\n";
std::cout << "- c = " << formatOptional(opt->c) << "\n";
}
if (variant.
isType(dataTypeUni)) {
const auto* uni =
static_cast<Uni*
>(variant.
data());
std::cout << "Uni:\n";
std::cout << "- switchField = " << static_cast<int>(uni->switchField) << "\n";
if (uni->switchField == UniSwitch::OptionA) {
std::cout << "- optionA = " << uni->fields.optionA << "\n";
}
if (uni->switchField == UniSwitch::OptionB) {
std::cout <<
"- optionB = " <<
opcua::String(uni->fields.optionB) <<
"\n";
}
}
variant =
opcua::Node(client, {1,
"Color"}).readValue();
if (variant.
isType<int32_t>()) {
std::cout <<
"Color: " << variant.
scalar<int32_t>() <<
"\n";
}
}
void connect(std::string_view endpointUrl)
Connect to the selected server.
void setCustomDataTypes(Span< const DataType > dataTypes)
Set custom data types.
UA_ExtensionObject wrapper class.
High-level node class to access node attribute, browse and populate address space.
View to a contiguous sequence of objects, similar to std::span in C++20.
UA_Variant wrapper class.
Span< T > array()
Get reference to array with given template type (only native or wrapper types).
T & scalar() &
Get reference to scalar value with given template type (only native or wrapper types).
bool isType(const UA_DataType *type) const noexcept
Check if the variant type is equal to the provided data type.
bool isArray() const noexcept
Check if the variant is an array.
bool isScalar() const noexcept
Check if the variant is a scalar.
void * data() noexcept
Get pointer to the underlying data.