알람 및 조건
개요
OPC UA Part 9 ConditionType / AlarmConditionType 에서 고정된 NodeId 표준 메서드가 정의되어 있습니다.SDK 매개변수가 명확한 형태로 캡슐화되어 있습니다 C API.
| Method | NodeId | 입력 매개변수 |
|---|---|---|
| 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, 밀리초) |
| Unshelve | i=9215 | — |
위 표는 Part 9 표준 메서드와 그 입력 매개변수를 보여줍니다. 아래에서는 C API가 이를 의미론적으로 명확한 함수로 묶어두었으므로, 호출하는 쪽에서는 NodeId와 Variant[]를 수동으로 조합할 필요가 없습니다.
API
| 함수 | 분류 | 읽기 및 쓰기 | 설명 |
|---|---|---|---|
| DarraUa_Alarm_Acknowledge(session, condition_id, event_id_bytes, event_id_len, comment) | 알람 | 쓰기 | 사용자가 이미 알고 있음 |
| DarraUa_Alarm_Confirm(session, condition_id, event_id_bytes, event_id_len, comment) | 알람 | 쓰기 | 현장 조치가 완료되었습니다. |
| DarraUa_Alarm_AddComment(session, condition_id, event_id_bytes, event_id_len, comment) | 알람 | 쓰기 | 메모 추가 |
| DarraUa_Alarm_EnableCondition(session, condition_id) | 알람 | 쓰기 | 활성화 |
| DarraUa_Alarm_DisableCondition(session, condition_id) | 알람 | 쓰기 | 비활성화 |
| DarraUa_Alarm_ConditionRefresh(session, subscription_id) | 알람 | 쓰기 | 전체 재발급 활성화 알람 |
| DarraUa_Alarm_ConditionRefresh2(session, subscription_id, monitored_item_id) | 알람 | 쓰기 | 지정만 MonitoredItem |
| DarraUa_Alarm_OneShotShelve(session, shelving_state_id) | 알람 | 쓰기 | 일회성 차폐 |
| DarraUa_Alarm_TimedShelve(session, shelving_state_id, duration_ms) | 알람 | 쓰기 | 일시적 차단 |
| DarraUa_Alarm_Unshelve(session, shelving_state_id) | 알람 | 쓰기 | 차단 해제 |
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);
| 매개변수 | 기본값 | 설명 |
|---|---|---|
| session | — | 세션 핸들 (DarraUa_SessionHandle) |
| condition_id | — | 알람 조건 예시 NodeId 문자열 (이벤트 ConditionId 필드에서 가져옴) |
| event_id_bytes / event_id_len | — | 이벤트 ID 바이트 스트림 (ByteString) |
| comment | NULL은 빈 문자열로 간주됩니다. | 주석 (LocalizedText.text), 빈 문자열일 수 있음 |
| subscription_id | — | 서버 측 Subscription Id |
| monitored_item_id | — | 서버 측 MonitoredItem Id |
| shelving_state_id | — | 알람 인스턴스의 ShelvingState 하위 NodeId노드는 일반적으로 다음과 같은 형식을 띱니다. "{conditionId}.ShelvingState" |
| duration_ms | — | 보류 시간(밀리초), server MaxTimeShelved에 따라 제한됨 |
모두 반환하며 DarraUa_Status, 예외를 발생시키지 않습니다.
코드 예시
#include <opcua.h>
#include <opcua_alarm.h>
#include <opcua_event.h>
#include <stdio.h>
/* ---- 1) 이벤트 구독 (opcua_event.h 의 실제 콜백) ---- */
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 5개의 필드만 있습니다: 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) 경보 처리 (condition_id / event_id 업무 측에서 제공) ---- */
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. 서버 측에서 현재 활성화된 경보를 다시 전송하도록 한다 (경보 패널을 처음 열 때 / 재연결 후 한 번 호출) */
DarraUa_Alarm_ConditionRefresh(session, (uint32_t)sub);
/* b. 사용자가 "확인"을 클릭하면 */
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. 현장 처리 (Acknowledge=확인됨, Confirm=처리됨, 일부 유형은 둘 중 하나만 지원됨) */
DarraUa_Alarm_Confirm(session, condition_id, event_id, event_id_len, "现场已处理");
/* d. 30초 동안 일시적으로 차단 */
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 + 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);
/* 서버 객체 i=2253에 이벤트 생성 MonitoredItem */
DarraUa_MonitoredItemHandle ev_mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Subscription_AddEventItem(sub, "i=2253", on_event, NULL, &ev_mi);
/* pump 이벤트 (또는 백그라운드 DarraUa_Session_StartAutoPublish 스레드 시작) */
for (int i = 0; i < 10; ++i) DarraUa_Session_Publish(session, 1000);
/* 이벤트를 수신하면, 업무 측에 저장된 condition_id / event_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 어디서 왔나요?: 이 두 값은 Part 9 알람 메서드의 필수 입력 매개변수입니다. 현재 C 이벤트 구독은 고정적으로 4개 SelectClause (Severity / Message / SourceName / Time, 참조
opcua_event.h)만 처리하며,DarraUa_Event여기에는 EventId / ConditionId 필드가 포함되어 있지 않으므로, 본 SDK 의 이벤트 콜백은줄 수 없다이 두 값은; 비즈니스 측에서 직접 준비하여(예: 자체 이벤트 로그/상위 시스템에서 가져와) 에 전달해야 합니다DarraUa_Alarm_*.
오류 처리
DarraUa_Alarm_* 모두 반환 ( DarraUa_Status 즉, uint32_t), 예외를 발생시키지 않음.
| 반환값 | 의미 |
|---|---|
UA_STATUS_GOOD (0x00000000) | 성공 |
UA_STATUS_INVALID_ARGUMENT (0x8F000004) | 입력 매개변수 오류 (세션 무효 / condition_id가 비어 있음 등) |
OPC UA Bad* 서버 코드 | 서버 측 패스스루, 수치로 DarraUa_Status 반환 |
대표적인 서버 상태 코드 (Part 4 부록 A 명칭; C 측에서는 자주 사용되는 하위 집합만 opcua_status.h 수록하며, 다음 세 가지는 매크로로 정의되지 않았으므로 수치에 따라 대조함):
| 상태 코드 이름 | 의미 |
|---|---|
| BadEventIdUnknown | EventId 해당 조건과 일치하지 않습니다 |
| BadConditionAlreadyEnabled | 조건이 활성화되었습니다 |
| BadConditionAlreadyShelved | 알람이 비활성화되었습니다 |
TimedShelve 서버의 MaxTimeShelved를 초과할 때 반환됩니다 ( BadShelvingTimeOutOfRange 이 값 역시 서버 측에서 그대로 전달된 값입니다).
모범 사례
- 첫 구독 후 반드시 설정해야 하며
DarraUa_Alarm_ConditionRefresh, 서버 측에서 현재 활성화 알람을 재발송하도록 합니다. - Acknowledge(확인) 후 Confirm(처리 완료), 두 단계를 따로 진행
DarraUa_Alarm_TimedShelve는 밀리초이지duration_ms, 초가 아닙니다- 이벤트 콜백 내에서 ack를 동기화하지 말고, 큐에 밀어 넣어 백그라운드에서 처리하십시오.
condition_id/event_id_bytes모두차용포인터는 소유권을 인수하지 SDK 않으며, 대신 해제해 주지도 않습니다. 비즈니스 측에서 직접 할당한 메모리인 경우, 비즈니스 측에서 해제해야 합니다.
다국어 대조
| 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 |