#include <chrono>
#include <iostream>
#include <thread>
int main() {
sub.setSubscriptionParameters(subscriptionParameters);
sub.setPublishingMode(true);
auto mon = sub.subscribeDataChange(
std::cout
<< "Data change notification:\n"
<<
"- attribute id: " <<
static_cast<int>(item.
getAttributeId()) <<
"\n";
std::cout <<
"Current server time (UTC): " << dt.
format(
"%H:%M:%S") << std::endl;
}
);
mon.setMonitoringParameters(monitoringParameters);
});
while (true) {
try {
client.
connect(
"opc.tcp://localhost:4840");
std::cout <<
"Error: " << e.
what() <<
"\nRetry to connect in 3 seconds\n";
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
}
Exception for bad status codes from open62541 UA_STATUSCODE_*.
const char * what() const noexcept override
void disconnect()
Disconnect and close the connection to the server.
void connect(std::string_view endpointUrl)
Connect to the selected server.
void onSessionActivated(StateCallback callback)
Set a state callback that will be called after the session is activated.
void run()
Run the client's main loop by. This method will block until Client::stop is called.
Subscription< Client > createSubscription(const SubscriptionParameters ¶meters={})
Create a subscription to monitor data changes and events.
UA_DataValue wrapper class.
Variant & getValue() &noexcept
Get value.
UA_DateTime wrapper class.
std::string format(std::string_view format, bool localtime=false) const
Convert to string with given format (same format codes as strftime).
High-level monitored item class.
uint32_t subscriptionId() const noexcept
Get the server-assigned identifier of the underlying subscription.
uint32_t monitoredItemId() const noexcept
Get the server-assigned identifier of this monitored item.
const NodeId & getNodeId()
Get the monitored NodeId.
AttributeId getAttributeId()
Get the monitored AttributeId.
std::string toString() const
Encode NodeId as a string like ns=1;s=SomeNode.
T getScalarCopy() const
Get copy of scalar value with given template type.
@ Server_ServerStatus_CurrentTime
@ Value
The most recent value of the variable that the server has.
Extended monitoring parameters with default values from open62541.
double samplingInterval
Interval in milliseconds that defines the fastest rate at which the MonitoredItem should be accessed ...
Subscription parameters with default values from open62541.
double publishingInterval
Cyclic interval in milliseconds that the subscription is requested to return notifications.