Create a Subscription (C)
OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Subscription_Create(
DarraUa_SessionHandle session,
const DarraUa_SubscriptionConfig* cfg,
DarraUa_SubscriptionHandle* out_sub);
Prerequisite Reading / Supplementary Materials
- Add MonitoredItem Please see Add MonitoredItem.
- DataChange Please see the callback field DataChange Event.
DarraUa_SubscriptionConfig Field
typedef struct {
double publishing_interval_ms; /* Default 500 */
uint32_t lifetime_count; /* Default 2400 */
uint32_t max_keep_alive_count; /* Default 10 */
uint32_t max_notifications; /* 0 = Infinite */
uint8_t priority; /* 0-255 */
int32_t publishing_enabled; /* 1 = Enabled */
} DarraUa_SubscriptionConfig;
OPCUA_API void OPCUA_CALL
DarraUa_SubscriptionConfig_Init(DarraUa_SubscriptionConfig* cfg);
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Publications | publishing_interval_ms | double | in | The minimum interval for the server to push data; the server may revise (adjust) this value. Default: 500 |
publishing_enabled | int32_t | in | 1 = Publishing enabled, 0 = Sampling only, no push | |
| Life Cycle | lifetime_count | uint32_t | in | The number of publish cycles PublishRequest after which a subscription is cleaned up if no messages are received; default is 2400 |
max_keep_alive_count | uint32_t | in | How often to send an empty KeepAlive when there is no new data; default is 10 | |
max_notifications | uint32_t | in | Maximum number of notifications that can be sent in a single publish; 0 = unlimited | |
| Scheduling | priority | uint8_t | in | Subscription priority 0–255 |
Be sure to DarraUa_SubscriptionConfig_Init(&cfg) set the default values first, then overwrite them.
Usage
DarraUa_SubscriptionConfig sc;
DarraUa_SubscriptionConfig_Init(&sc);
sc.publishing_interval_ms = 500.0;
sc.publishing_enabled = 1;
DarraUa_SubscriptionHandle sub = UA_INVALID_SUBSCRIPTION_HANDLE;
DarraUa_Status st = DarraUa_Subscription_Create(h, &sc, &sub);
if (!UA_STATUS_IS_GOOD(st) || sub == UA_INVALID_SUBSCRIPTION_HANDLE) {
fprintf(stderr, "Subscription_Create failed: %s\n", DarraUa_StatusName(st));
return -1;
}
Multiple Subscription Channels
If an app requires both 100 ms high-frequency subscriptions (fitness data) and 5000 ms low-frequency subscriptions (ambient temperature), create 2 separate subscription branches; do not combine them into a single one:
DarraUa_SubscriptionConfig fast_cfg;
DarraUa_SubscriptionConfig_Init(&fast_cfg);
fast_cfg.publishing_interval_ms = 100.0;
fast_cfg.publishing_enabled = 1;
DarraUa_SubscriptionConfig slow_cfg;
DarraUa_SubscriptionConfig_Init(&slow_cfg);
slow_cfg.publishing_interval_ms = 5000.0;
slow_cfg.publishing_enabled = 1;
DarraUa_SubscriptionHandle fast = 0, slow = 0;
DarraUa_Subscription_Create(h, &fast_cfg, &fast);
DarraUa_Subscription_Create(h, &slow_cfg, &slow);
DarraUa_MonitoredItemHandle mi_fast = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_MonitoredItemHandle mi_slow = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Subscription_AddNode(fast, "ns=2;s=AxisPosition", UA_ATTR_VALUE,
-1.0, on_change, NULL, &mi_fast);
DarraUa_Subscription_AddNode(slow, "ns=2;s=AmbientTemperature", UA_ATTR_VALUE,
-1.0, on_change, NULL, &mi_slow);
Each subscription has its own release schedule, and they do not affect one another.
Release
DarraUa_Subscription_Delete(sub);
After a subscription is deleted, all MIs are destroyed with it; the server sends a DeleteSubscription request to release resources.