Alarms and Conditions
Overview
OPC UA Part 9 defines a standard Method with a fixed NodeId on ConditionType / AlarmConditionType. The SDK wraps it as a C API with clear parameters.
| Method | NodeId | Input Parameters |
|---|---|---|
| Acknowledge | i=9111 | (eventId: ByteString, comment: LocalizedText) |
| Confirm | i=9113 | (eventId: ByteString, comment: LocalizedText) |
| AddComment | i=9029 | (eventId: ByteString, comment: LocalizedText) |
| Enable | i=2803 | — |
| Disable | i=2805 | — |
| ConditionRefresh | i=3875 | (subscriptionId: IntegerId) |
| ConditionRefresh2 | i=12912 | (subscriptionId: IntegerId, monitoredItemId: IntegerId) |
| OneShotShelve | i=9211 | — |
| TimedShelve | i=9213 | (shelvingTime: Duration, milliseconds) |
| Unshelve | i=9215 | — |
The table above lists the Part 9 standard methods and their input parameters; the code below C API wraps them into semantically clear functions, so callers do not need to manually concatenate NodeId and Variant[].
API
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| DarraUa_Alarm_Acknowledge(session, condition_id, event_id_bytes, event_id_len, comment) | Alarm | Write | User Known |
| DarraUa_Alarm_Confirm(session, condition_id, event_id_bytes, event_id_len, comment) | Alarm | Write | The issue has been resolved on-site |
| DarraUa_Alarm_AddComment(session, condition_id, event_id_bytes, event_id_len, comment) | Alarm | Write | Add a note |
| DarraUa_Alarm_EnableCondition(session, condition_id) | Alarm | Write | Enable |
| DarraUa_Alarm_DisableCondition(session, condition_id) | Alarm | Write | Disable |
| DarraUa_Alarm_ConditionRefresh(session, subscription_id) | Alarm | Write | Reissue All Activation Alarms |
| DarraUa_Alarm_ConditionRefresh2(session, subscription_id, monitored_item_id) | Alarm | Write | Specify only MonitoredItem |
| DarraUa_Alarm_OneShotShelve(session, shelving_state_id) | Alarm | Write | One-Time Blocking |
| DarraUa_Alarm_TimedShelve(session, shelving_state_id, duration_ms) | Alarm | Write | Temporary Mute |
| DarraUa_Alarm_Unshelve(session, shelving_state_id) | Alarm | Write | Unblock |
DarraUa_Status DarraUa_Alarm_Acknowledge(
DarraUa_SessionHandle session,
const char* condition_id,
const uint8_t* event_id_bytes,
size_t event_id_len,
const char* comment);
DarraUa_Status DarraUa_Alarm_ConditionRefresh(
DarraUa_SessionHandle session,
uint32_t subscription_id);
DarraUa_Status DarraUa_Alarm_TimedShelve(
DarraUa_SessionHandle session,
const char* shelving_state_id,
double duration_ms);
| Parameters | Default | Note |
|---|---|---|
| session | — | Session Handle (DarraUa_SessionHandle) |
| condition_id | — | Alarm Condition Example NodeId String (taken from the event field ConditionId ) |
| event_id_bytes / event_id_len | — | Event ID Byte Stream (ByteString) |
| comment | NULL is treated as an empty string | Comment (LocalizedText.text); may be an empty string |
| subscription_id | — | Server-side Subscription Id |
| monitored_item_id | — | Server-side MonitoredItem Id |
| shelving_state_id | — | The "ShelvingState NodeId" subnode of an alarm instance typically takes the form of "{conditionId}.ShelvingState" |
| duration_ms | — | Shelving duration in milliseconds, subject to the server's MaxTimeShelved constraint |
Returns all DarraUa_Status, without discarding any.
Code Example
#include <opcua.h>
#include <opcua_alarm.h>
#include <opcua_event.h>
#include <stdio.h>
/* ---- 1) Event Subscription (opcua_event.h Actual Callback) ---- */
static void on_event(DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_Event* ev,
void* user_context)
{
(void)sub; (void)mi; (void)user_context;
if (!ev) return;
/* DarraUa_Event There are only 5 fields: severity / message / source_name / time / event_type */
printf("[event] severity=%u src=%s msg=%s\n",
(unsigned)ev->severity,
ev->source_name ? ev->source_name : "",
ev->message ? ev->message : "");
}
/* ---- 2) Alarm Operations (condition_id / Provided event_id by the business side) ---- */
static DarraUa_Status acknowledge_alarm(DarraUa_SessionHandle session,
DarraUa_SubscriptionHandle sub,
const char* condition_id,
const uint8_t* event_id,
size_t event_id_len)
{
/* a. Have the server resend the current activation alert (once when the alert panel is first opened or after a reconnection) */
DarraUa_Alarm_ConditionRefresh(session, (uint32_t)sub);
/* b. The user clicks "Confirm" */
DarraUa_Status rc = DarraUa_Alarm_Acknowledge(session,
condition_id, event_id, event_id_len, "操作员已知");
if (rc != UA_STATUS_GOOD)
fprintf(stderr, "Acknowledge 失败 0x%08X\n", (unsigned)rc);
/* c. On-site processing (Acknowledge = Acknowledged, Confirm = Processed; some types support only one of these) */
DarraUa_Alarm_Confirm(session, condition_id, event_id, event_id_len, "现场已处理");
/* d. Temporarily mute for 30 seconds */
char shelving_id[256];
snprintf(shelving_id, sizeof(shelving_id), "%s.ShelvingState", condition_id);
DarraUa_Alarm_TimedShelve(session, shelving_id, 30000.0);
return rc;
}
int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://server:4840";
/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */
DarraUa_SubscriptionConfig sub_cfg;
DarraUa_SubscriptionConfig_Init(&sub_cfg);
sub_cfg.publishing_interval_ms = 500.0;
sub_cfg.publishing_enabled = 1;
DarraUa_SubscriptionHandle sub = UA_INVALID_SUBSCRIPTION_HANDLE;
DarraUa_Subscription_Create(session, &sub_cfg, &sub);
/* Create an Event on Server object i=2253 MonitoredItem */
DarraUa_MonitoredItemHandle ev_mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Subscription_AddEventItem(sub, "i=2253", on_event, NULL, &ev_mi);
/* pump event (or DarraUa_Session_StartAutoPublish start a background thread) */
for (int i = 0; i < 10; ++i) DarraUa_Session_Publish(session, 1000);
/* Upon receiving an event, event_id perform the alerting operation saved on the business side: condition_id / */
/* acknowledge_alarm(session, sub, cond_id, ev_id, ev_id_len); */
DarraUa_Subscription_Delete(sub);
DarraUa_Session_Disconnect(session);
DarraUa_Session_Close(session);
return 0;
}
EventId / ConditionId Where is it from?: These two values are required input parameters for the Part 9 alarm method. Currently, the C event subscription is fixed to return only 4 SelectClause fields (Severity / Message / SourceName / Time; see
opcua_event.h), and there is no EventId / ConditionId field inDarraUa_Event; therefore, the SDK event callback cannot provide these two values; they must be provided by the business side (for example, retrieved from its own event logs or upper-level systems) and passed toDarraUa_Alarm_*.
Error Handling
DarraUa_Alarm_* Returns all ( DarraUa_Status i.e., uint32_t), without discarding any.
| Return Value | Meaning |
|---|---|
UA_STATUS_GOOD (0x00000000) | Success |
UA_STATUS_INVALID_ARGUMENT (0x8F000004) | Invalid input parameters (invalid session / condition_id is empty, etc.) |
OPC UA Bad* Server-side code | Server-side pass-through, with a numeric DarraUa_Status return value |
Typical Server Status Codes (Part 4, Appendix A: Names; Side C includes opcua_status.h only a subset of commonly used codes; the following three are not defined as macros and are compared by their numerical values):
| Status Code Name | Meaning |
|---|---|
| BadEventIdUnknown | EventId Does not match this condition |
| BadConditionAlreadyEnabled | The condition has been enabled |
| BadConditionAlreadyShelved | The alarm has been disabled |
TimedShelve Returns when the time exceeds the server's MaxTimeShelved ( BadShelvingTimeOutOfRange which is also a value passed through from the server).
Best Practices
- Must be called after the first subscription
DarraUa_Alarm_ConditionRefreshto have the server resend the current activation alarm - First "Acknowledge" (acknowledged), then "Confirm" (processed)—two separate steps
DarraUa_Alarm_TimedShelveIt'sduration_msmilliseconds, not seconds.- Do not acknowledge (ack) synchronously in event callbacks; push to a queue for background processing.
condition_id/event_id_bytesAll of themBorrow Pointers: They do SDK not take ownership of memory and do not free it for you; if the business logic allocates memory itself, it is responsible for freeing it.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| Acknowledge | acknowledge | acknowledge | Acknowledge | acknowledge | DarraUa_Alarm_Acknowledge |
| Confirm | confirm | confirm | Confirm | confirm | DarraUa_Alarm_Confirm |
| ConditionRefresh | condition_refresh | conditionRefresh | ConditionRefresh | condition_refresh | DarraUa_Alarm_ConditionRefresh |