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.

118 lines
4.4 KiB

#include "OPCUAClient.h"
#include <iostream>
#include <vector>
#include <stack>
#include <iomanip>
// Helper structure for navigation history (back button)
struct HistoryStep {
uint16_t nsIndex;
uint32_t numericId;
std::string name;
};
int main(int argc, char* argv[]) {
OPCUAClient opcClient;
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <Server-URL>" << std::endl;
return 1;
}
std::string endpoint = argv[1];
std::cout << "=== OPC UA Console Browser ===" << std::endl;
std::cout << "Connecting to " << endpoint << "..." << std::endl;
if (!opcClient.Connect(endpoint)) {
std::cerr << "Connection failed!" << std::endl;
return -1;
}
// Starting point: Objects folder (NS 0, ID 85)
uint16_t currentNs = 0;
uint32_t currentId = 85;
std::string currentName = "Root/Objects";
std::stack<HistoryStep> history;
while (true) {
std::cout << "\n--------------------------------------------------" << std::endl;
std::cout << "Current path: " << currentName << " (NS:" << currentNs << ", ID:" << currentId << ")" << std::endl;
std::cout << "--------------------------------------------------" << std::endl;
// List child nodes
auto nodes = opcClient.GenList(currentNs, currentId);
if (nodes.empty()) {
std::cout << "[No child nodes found or access denied]" << std::endl;
} else {
for (size_t i = 0; i < nodes.size(); ++i) {
std::string typeStr = (nodes[i].nodeClass == UA_NODECLASS_VARIABLE) ? "[VAR]" : "[FOLDER]";
std::cout << std::right << std::setw(5) << i + 1 << ") " << std::setw(9) << typeStr << " " << std::left << std::setw(32) << nodes[i].name << std::setw(8) << " (NS:" << nodes[i].nsIndex << ", ID:";
if (nodes[i].isStringId) {
std::cout << "s=" << nodes[i].stringId;
} else {
std::cout << "i=" << nodes[i].numericId;
}
std::cout << ")";
// If it is a variable, show the value immediately
if (nodes[i].nodeClass == UA_NODECLASS_VARIABLE && !nodes[i].isStringId) {
std::cout << " -> Value: " << opcClient.ReadNumeric(nodes[i].nsIndex, nodes[i].numericId);
} else if (nodes[i].nodeClass == UA_NODECLASS_VARIABLE && nodes[i].isStringId) {
std::cout << " -> Value: " << opcClient.ReadString(nodes[i].nsIndex, nodes[i].stringId);
}
std::cout << std::endl;
}
}
// Navigation options
std::cout << "\nOptions: [number] = select/open | 0 = back | -1 = exit" << std::endl;
std::cout << "Input: ";
int choice;
if (!(std::cin >> choice)) {
std::cin.clear();
std::cin.ignore(10000, '\n');
continue;
}
if (choice == -1) {
break;
} else if (choice == 0) {
if (!history.empty()) {
auto prev = history.top();
history.pop();
currentNs = prev.nsIndex;
currentId = prev.numericId;
currentName = prev.name;
} else {
std::cout << "Already at the top level!" << std::endl;
}
} else if (choice > 0 && choice <= static_cast<int>(nodes.size())) {
auto selectedNode = nodes[choice - 1];
if (selectedNode.nodeClass == UA_NODECLASS_VARIABLE) {
std::cout << "\n>>> Variable '" << selectedNode.name << "' selected. <<<" << std::endl;
// The value has already been printed in the list
} else {
// Go into the folder (only for numeric IDs to keep the example simple)
if (!selectedNode.isStringId) {
history.push({currentNs, currentId, currentName});
currentNs = selectedNode.nsIndex;
currentId = selectedNode.numericId;
currentName = currentName + "/" + selectedNode.name;
} else {
std::cout << "Navigation into string-based folder IDs is skipped in this demo." << std::endl;
}
}
} else {
std::cout << "Invalid selection!" << std::endl;
}
}
opcClient.Close();
std::cout << "Browser closed." << std::endl;
return 0;
}