open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
custom_datatypes/server_custom_datatypes.cpp
#include "custom_datatypes.hpp"
int main() {
opcua::Server server;
// Get custom type definitions from common header
const auto& dataTypePoint = getPointDataType();
const auto& dataTypeMeasurements = getMeasurementsDataType();
const auto& dataTypeOpt = getOptDataType();
const auto& dataTypeUni = getUniDataType();
const auto& dataTypeColor = getColorDataType();
// Provide custom data type definitions to server
dataTypePoint,
dataTypeMeasurements,
dataTypeOpt,
dataTypeUni,
dataTypeColor,
});
// Add data type nodes
opcua::Node structureDataTypeNode(server, opcua::DataTypeId::Structure);
structureDataTypeNode.addDataType(dataTypePoint.getTypeId(), "PointDataType");
structureDataTypeNode.addDataType(dataTypeMeasurements.getTypeId(), "MeasurementsDataType");
structureDataTypeNode.addDataType(dataTypeOpt.getTypeId(), "OptDataType");
structureDataTypeNode.addDataType(dataTypeUni.getTypeId(), "UniDataType");
opcua::Node enumerationDataTypeNode(server, opcua::DataTypeId::Enumeration);
enumerationDataTypeNode.addDataType(dataTypeColor.getTypeId(), "Color")
{0, 0}, // auto-generate node id
"EnumValues",
.setArrayDimensions({0})
{0, {"", "Red"}, {}},
{1, {"", "Green"}, {}},
{2, {"", "Yellow"}, {}},
})
)
// Add variable type nodes (optional)
opcua::Node baseDataVariableTypeNode(server, opcua::VariableTypeId::BaseDataVariableType);
auto variableTypePointNode = baseDataVariableTypeNode.addVariableType(
{1, 4243},
"PointType",
.setDataType(dataTypePoint.getTypeId())
.setValueScalar(Point{1, 2, 3}, dataTypePoint)
);
auto variableTypeMeasurementNode = baseDataVariableTypeNode.addVariableType(
{1, 4444},
"MeasurementsType",
.setDataType(dataTypeMeasurements.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(Measurements{}, dataTypeMeasurements)
);
auto variableTypeOptNode = baseDataVariableTypeNode.addVariableType(
{1, 4645},
"OptType",
.setDataType(dataTypeOpt.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(Opt{}, dataTypeOpt)
);
auto variableTypeUniNode = baseDataVariableTypeNode.addVariableType(
{1, 4846},
"UniType",
.setDataType(dataTypeUni.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(Uni{}, dataTypeUni)
);
// Add variable nodes with some values
const Point point{3.0, 4.0, 5.0};
objectsNode.addVariable(
{1, "Point"},
"Point",
.setDataType(dataTypePoint.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(point, dataTypePoint),
variableTypePointNode.id()
);
const std::vector<Point> pointVec{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}};
objectsNode.addVariable(
{1, "PointVec"},
"PointVec",
.setDataType(dataTypePoint.getTypeId())
.setArrayDimensions({0}) // single dimension but unknown in size
.setValueArray(pointVec, dataTypePoint),
variableTypePointNode.id()
);
std::vector<float> measurementsValues{19.1F, 20.2F, 19.7F};
const Measurements measurements{
opcua::String("Test description"),
measurementsValues.size(),
measurementsValues.data(),
};
objectsNode.addVariable(
{1, "Measurements"},
"Measurements",
.setDataType(dataTypeMeasurements.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(measurements, dataTypeMeasurements),
variableTypeMeasurementNode.id()
);
float optC = 10.10F;
const Opt opt{3, nullptr, &optC};
objectsNode.addVariable(
{1, "Opt"},
"Opt",
.setDataType(dataTypeOpt.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(opt, dataTypeOpt),
variableTypeOptNode.id()
);
Uni uni{};
uni.switchField = UniSwitch::OptionB;
uni.fields.optionB = UA_STRING_STATIC("test string"); // NOLINT
objectsNode.addVariable(
{1, "Uni"},
"Uni",
.setDataType(dataTypeUni.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(uni, dataTypeUni),
variableTypeUniNode.id()
);
objectsNode.addVariable(
{1, "Color"},
"Color",
.setDataType(dataTypeColor.getTypeId())
.setValueRank(opcua::ValueRank::Scalar)
.setValueScalar(Color::Green, dataTypeColor)
);
server.run();
}
UA_EnumValueType wrapper class.
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
Node & addModellingRule(ModellingRule rule)
Add modelling rule.
Definition node.hpp:494
Node addProperty(const NodeId &id, std::string_view browseName, const VariableAttributes &attributes={})
Add property.
Definition node.hpp:196
Node addDataType(const NodeId &id, std::string_view browseName, const DataTypeAttributes &attributes={}, const NodeId &referenceType=ReferenceTypeId::HasSubtype)
Add data type.
Definition node.hpp:396
Node addVariableType(const NodeId &id, std::string_view browseName, const VariableTypeAttributes &attributes={}, const NodeId &variableType=VariableTypeId::BaseDataVariableType, const NodeId &referenceType=ReferenceTypeId::HasSubtype)
Add variable type.
Definition node.hpp:319
Node addVariable(const NodeId &id, std::string_view browseName, const VariableAttributes &attributes={}, const NodeId &variableType=VariableTypeId::BaseDataVariableType, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add variable.
Definition node.hpp:156
High-level server class.
Definition server.hpp:132
void setCustomDataTypes(Span< const DataType > dataTypes)
Set custom data types.
void run()
Run the server's main loop. This method will block until Server::stop is called.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
UA_VariableAttributes wrapper class.
auto & setDataType(const NodeId &dataType)
UA_VariableAttributes wrapper class.
auto & setDataType(const NodeId &dataType)
#define UA_STRING_STATIC(CHARS)