You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.8 KiB

#include "OPCUAClient.h"
OPCUAClient::OPCUAClient() : client(nullptr), isConnected(false) {
client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
}
OPCUAClient::~OPCUAClient() {
Close();
if (client) {
UA_Client_delete(client);
}
}
bool OPCUAClient::Connect(const std::string& endpointUrl) {
if (isConnected) return true;
UA_StatusCode retval = UA_Client_connect(client, endpointUrl.c_str());
if (retval != UA_STATUSCODE_GOOD) {
std::cerr << "Connection failed with status: " << UA_StatusCode_name(retval) << std::endl;
return false;
}
isConnected = true;
return true;
}
std::vector<OpcNodeInfo> OPCUAClient::GenList(uint16_t namespaceIndex, uint32_t numericNodeId) {
std::vector<OpcNodeInfo> nodeList;
if (!isConnected) {
std::cerr << "Not connected!" << std::endl;
return nodeList;
}
UA_BrowseRequest bReq;
UA_BrowseRequest_init(&bReq);
bReq.requestedMaxReferencesPerNode = 0;
bReq.nodesToBrowse = (UA_BrowseDescription*)UA_malloc(sizeof(UA_BrowseDescription));
bReq.nodesToBrowseSize = 1;
// The '&' was removed here to fix the compiler error
UA_BrowseDescription_init(bReq.nodesToBrowse);
bReq.nodesToBrowse->nodeId = UA_NODEID_NUMERIC(namespaceIndex, numericNodeId);
bReq.nodesToBrowse->resultMask = UA_BROWSERESULTMASK_ALL;
UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
if (bResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD) {
for (size_t i = 0; i < bResp.resultsSize; ++i) {
for (size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
UA_ReferenceDescription* ref = &bResp.results[i].references[j];
OpcNodeInfo info;
info.name = std::string((char*)ref->browseName.name.data, ref->browseName.name.length);
info.nsIndex = ref->nodeId.nodeId.namespaceIndex;
info.nodeClass = ref->nodeClass;
if (ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
info.numericId = ref->nodeId.nodeId.identifier.numeric;
info.isStringId = false;
} else if (ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
info.stringId = std::string((char*)ref->nodeId.nodeId.identifier.string.data, ref->nodeId.nodeId.identifier.string.length);
info.isStringId = true;
} else {
continue; // Skip other ID types in this example
}
nodeList.push_back(info);
}
}
}
UA_BrowseRequest_clear(&bReq);
UA_BrowseResponse_clear(&bResp);
return nodeList;
}
// Internal helper for converting UA_Variant to std::string
static std::string VariantToString(UA_Variant* value) {
if (!UA_Variant_isScalar(value)) {
return "(Array/non-scalar)";
}
if (value->type == &UA_TYPES[UA_TYPES_STRING]) {
UA_String* uaStr = (UA_String*)value->data;
return std::string((char*)uaStr->data, uaStr->length);
} else if (value->type == &UA_TYPES[UA_TYPES_INT32]) {
return std::to_string(*(UA_Int32*)value->data);
} else if (value->type == &UA_TYPES[UA_TYPES_DOUBLE]) {
return std::to_string(*(UA_Double*)value->data);
} else if (value->type == &UA_TYPES[UA_TYPES_BOOLEAN]) {
return *(UA_Boolean*)value->data ? "True" : "False";
}
return "(Data type not implemented)";
}
std::string OPCUAClient::ReadString(uint16_t namespaceIndex, const std::string& stringNodeId) {
if (!isConnected) return "Error: not connected";
UA_Variant value;
UA_Variant_init(&value);
UA_NodeId nodeId = UA_NODEID_STRING_ALLOC(namespaceIndex, stringNodeId.c_str());
UA_StatusCode retval = UA_Client_readValueAttribute(client, nodeId, &value);
UA_NodeId_clear(&nodeId);
std::string result = "";
if (retval == UA_STATUSCODE_GOOD) {
result = VariantToString(&value);
} else {
result = "Error (" + std::string(UA_StatusCode_name(retval)) + ")";
}
UA_Variant_clear(&value);
return result;
}
std::string OPCUAClient::ReadNumeric(uint16_t namespaceIndex, uint32_t numericNodeId) {
if (!isConnected) return "Error: not connected";
UA_Variant value;
UA_Variant_init(&value);
UA_NodeId nodeId = UA_NODEID_NUMERIC(namespaceIndex, numericNodeId);
UA_StatusCode retval = UA_Client_readValueAttribute(client, nodeId, &value);
std::string result = "";
if (retval == UA_STATUSCODE_GOOD) {
result = VariantToString(&value);
} else {
result = "Error (" + std::string(UA_StatusCode_name(retval)) + ")";
}
UA_Variant_clear(&value);
return result;
}
void OPCUAClient::Close() {
if (isConnected && client) {
UA_Client_disconnect(client);
isConnected = false;
}
}