#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.getDecodedData());
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.
getScalar<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.
UA_Variant wrapper class.
bool isType(const UA_DataType *dataType) const noexcept
Check if the variant type is equal to the provided data type.
Span< T > getArray()
Get array with given template type (only native or wrapper types).
bool isArray() const noexcept
Check if the variant is an array.
T & getScalar() &
Get reference to scalar value with given template type (only native or wrapper types).
bool isScalar() const noexcept
Check if the variant is a scalar.
void * data() noexcept
Get pointer to the underlying data.
Span(Container &) -> Span< typename Container::value_type >