Skip to content

Commit 7cdaaf5

Browse files
VieshothVieshoth NavaratnamStiv-workisak-jacobssonftor-work
committed
devicedatahub examples API update
Change-Id: Ie42e6f3fa7b62fdab440b44c124de712330f12ad Co-authored-by: Vieshoth Navaratnam <vieshotn@axis.com> Co-authored-by: Stiv Abdullwahed <stiva@axis.com> Co-authored-by: Isak Jakobsson <isakj@axis.com> Co-authored-by: Fernando Esquirio Torres <fernando.torres@axis.com>
1 parent 57c2534 commit 7cdaaf5

17 files changed

Lines changed: 253 additions & 220 deletions

File tree

.github/workflows/device-data-hub.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
axis-os: ["12.10.68"]
16+
axis-os: ["12.11.72"]
1717
arch: ["armv7hf", "aarch64"]
1818
env:
1919
EXREPO: acap-native-examples

device-data-hub/acap-communication/object-consumer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG ARCH=armv7hf
2-
ARG VERSION=12.10.0
2+
ARG VERSION=12.11.0-rc.1
33
ARG UBUNTU_VERSION=24.04
44
ARG REPO=axisecp
55
ARG SDK=acap-native-sdk

device-data-hub/acap-communication/object-consumer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The application creates a Device Data Hub client and connects to the service. Th
3636
### Subscribing to a topic
3737

3838
The application creates a `DHSubscriber` to subscribe to *acap.object_detector*.
39-
The functions of `DHSubscriberListener` listener struct handles incoming data by logging it.
39+
A data callback registered with `dh_subscriber_set_data_callback` handles incoming data by logging it.
4040

4141
> [!NOTE]
4242
> You can subscribe to topics that don't exist yet. Data will arrive once the topic is created and
@@ -57,7 +57,7 @@ To be able to connect to Device Data Hub, enable Device Data Hub in the manifest
5757

5858
```json
5959
"resources": {
60-
"deviceDataHub_beta2": {
60+
"deviceDataHub": {
6161
"enabled": true
6262
}
6363
}

device-data-hub/acap-communication/object-consumer/app/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"schemaVersion": "2.0.0",
2+
"schemaVersion": "2.2.0",
33
"acapPackageConf": {
44
"setup": {
55
"appName": "object_consumer",
@@ -16,7 +16,7 @@
1616
}
1717
},
1818
"resources": {
19-
"deviceDataHub_beta2": {
19+
"deviceDataHub": {
2020
"enabled": true
2121
}
2222
}

device-data-hub/acap-communication/object-consumer/app/object_consumer.c

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <datahub/client.h>
1212
#include <datahub/subscriber.h>
1313

14-
#define TOPIC_NAME "com.example.object_detector"
14+
#define TOPIC_NAME "com.example.objectdetector"
1515

1616
// Global state
1717
static volatile sig_atomic_t keep_running = true;
@@ -20,36 +20,36 @@ static DHSubscriber* data_subscriber = NULL;
2020

2121
// Forward declarations
2222
static void cleanup_resources(void);
23-
static bool handle_client_error(DHClientError* err, const char* context);
23+
static bool handle_client_error(DHError* err, const char* context);
2424
static bool initialize_client(void);
2525
static bool setup_subscription(const char* topics[], unsigned int topics_count);
2626
static void signal_handler(int sig);
27-
static void on_data_received(DHTopicSample* sample, void* user_data);
27+
static void on_data_received(const DHTopicSample* sample, void* user_data);
2828

2929
// error handling
30-
static bool handle_client_error(DHClientError* err, const char* context) {
30+
static bool handle_client_error(DHError* err, const char* context) {
3131
if (err) {
32-
syslog(LOG_ERR, "Error in %s: %s\n", context, dh_client_error_to_string(err));
33-
dh_client_destroy_error(err);
32+
syslog(LOG_ERR, "Error in %s: %s\n", context, dh_error_to_string(err));
33+
dh_error_destroy(err);
3434
return true;
3535
}
3636
return false;
3737
}
3838

3939
// Initialize client and connect
4040
static bool initialize_client(void) {
41-
DHClientOptions* opts = dh_client_options_create();
42-
dh_client_options_set_log_level(opts, DH_LOG_INFO);
43-
dh_client_options_set_log_target(opts, DH_LOG_TARGET_CONSOLE);
44-
45-
client = dh_client_create("Client for object_consumer", opts);
46-
dh_client_options_destroy(opts);
47-
if (!client) {
48-
syslog(LOG_ERR, "Failed to create client");
41+
DHError* conn_cb_err = NULL;
42+
client = dh_client_create("Client for object_consumer", &conn_cb_err);
43+
if (!client && !handle_client_error(conn_cb_err, "create client")) {
4944
return false;
5045
}
5146

52-
DHClientError* err = NULL;
47+
conn_cb_err = NULL;
48+
if (!dh_client_set_logging(client, DH_LOG_INFO, DH_LOG_TARGET_CONSOLE, &conn_cb_err)) {
49+
handle_client_error(conn_cb_err, "set logging");
50+
}
51+
52+
DHError* err = NULL;
5353
dh_client_connect(client, &err);
5454
if (handle_client_error(err, "client connect")) {
5555
return false;
@@ -59,10 +59,10 @@ static bool initialize_client(void) {
5959
}
6060

6161
// Data received callback
62-
static void on_data_received(DHTopicSample* sample, void* user_data) {
62+
static void on_data_received(const DHTopicSample* sample, void* user_data) {
6363
syslog(LOG_INFO, "User data: %s\n", (const char*)user_data);
64-
const DHTopicData* topic_data = dh_topic_sample_get_topic_data(sample);
65-
const char* data = dh_topic_data_get_json_str(topic_data);
64+
const DHTopicData* topic_data = dh_topic_sample_get_data(sample);
65+
const char* data = dh_topic_data_get_json_data(topic_data);
6666
if (data) {
6767
syslog(LOG_INFO, "Received Object Detection data: %s\n", data);
6868
}
@@ -71,48 +71,62 @@ static void on_data_received(DHTopicSample* sample, void* user_data) {
7171
// Subscribe to a topic
7272
static bool setup_subscription(const char* topics[], unsigned int topics_count) {
7373
// Create subscriber
74-
DHClientError* err = NULL;
74+
DHError* err = NULL;
7575
data_subscriber =
7676
dh_client_create_subscriber(client, "Data subscriber for object-consumer", &err);
7777
if (handle_client_error(err, "create subscriber")) {
7878
return false;
7979
}
8080

81-
// Set up listener callbacks
82-
DHSubscriberListener* sub_listener =
83-
dh_subscriber_listener_create(NULL, NULL, on_data_received, "object_consumer_data");
84-
if (!sub_listener) {
85-
syslog(LOG_ERR, "Failed to create subscriber listener");
81+
// Register data callback
82+
dh_subscriber_set_data_callback(data_subscriber,
83+
on_data_received,
84+
"object_consumer_data",
85+
&err);
86+
if (handle_client_error(err, "set data callback")) {
87+
return false;
88+
}
89+
90+
// Build subscription filter
91+
DHFilter* filter = dh_filter_create();
92+
if (!filter) {
93+
syslog(LOG_ERR, "Failed to create filter");
8694
return false;
8795
}
8896

89-
dh_subscriber_set_listener(data_subscriber, sub_listener, NULL);
90-
dh_subscriber_listener_destroy(sub_listener);
97+
// Add topic names to filter
98+
for (unsigned int i = 0; i < topics_count; i++) {
99+
dh_filter_add_topic_name(filter, topics[i], &err);
100+
if (handle_client_error(err, "add topic name to filter")) {
101+
dh_filter_destroy(filter);
102+
return false;
103+
}
104+
}
105+
106+
// Create subscription options and add filter
107+
DHSubscribeOptions* options = dh_subscribe_options_create();
108+
if (!options) {
109+
syslog(LOG_ERR, "Failed to create subscription options");
110+
dh_filter_destroy(filter);
111+
return false;
112+
}
91113

92-
// Subscribe to topic
93-
DHSubscriptionConfig* updates = dh_subscription_config_create();
94-
if (!updates) {
95-
syslog(LOG_ERR, "Failed to create subscription update config");
114+
dh_subscribe_options_add_filter(options, filter, &err);
115+
dh_filter_destroy(filter);
116+
if (handle_client_error(err, "add filter to options")) {
117+
dh_subscribe_options_destroy(options);
96118
return false;
97119
}
98-
dh_subscription_config_set(updates, false, false, true);
99-
100-
err = NULL;
101-
dh_subscriber_subscribe(data_subscriber,
102-
topics,
103-
topics_count, // topic_count
104-
NULL, // instance_keys
105-
0, // instance_key_count
106-
NULL, // content_filter
107-
false, // get_history
108-
updates,
109-
&err);
120+
121+
dh_subscribe_options_set_enable_data_updates(options, true);
122+
123+
// Subscribe with the above configured options
124+
dh_subscriber_subscribe(data_subscriber, options, &err);
125+
dh_subscribe_options_destroy(options);
110126
if (handle_client_error(err, "subscribe to topic")) {
111127
return false;
112128
}
113129

114-
dh_subscription_config_destroy(updates);
115-
116130
return true;
117131
}
118132

@@ -124,7 +138,7 @@ static void cleanup_resources(void) {
124138
}
125139

126140
if (client) {
127-
DHClientError* err = NULL;
141+
DHError* err = NULL;
128142
dh_client_disconnect(client, &err);
129143
handle_client_error(err, "client disconnect");
130144
dh_client_destroy(client);

device-data-hub/acap-communication/object-detector/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG ARCH=armv7hf
2-
ARG VERSION=12.10.0
2+
ARG VERSION=12.11.0-rc.1
33
ARG UBUNTU_VERSION=24.04
44
ARG REPO=axisecp
55
ARG SDK=acap-native-sdk

device-data-hub/acap-communication/object-detector/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ description, and data schema. The schema specifies that data is a JSON object wi
4646

4747
### Publishing data
4848

49-
The application creates a topic data writer with a `on_consumer_match_update` to receive notifications
49+
The application creates a topic data writer and registers an `on_consumer_match_update` callback
50+
using `dh_writer_set_consumer_match_update_callback` to receive notifications
5051
when subscribers connect or disconnect. When `DH_CONSUMER_MATCH` is received, consumers
5152
exist; when `DH_CONSUMER_NO_MATCH` is received, there are no subscribers.
5253
Data is only written when matching consumers exist, and must conform to the topic's data schema.
@@ -76,7 +77,7 @@ the manifest that handles Device Data Hub:
7677

7778
```json
7879
"resources": {
79-
"deviceDataHub_beta2": {
80+
"deviceDataHub": {
8081
"enabled": true,
8182
"accessControlList": [
8283
{

device-data-hub/acap-communication/object-detector/app/manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"schemaVersion": "2.0.0",
2+
"schemaVersion": "2.2.0",
33
"acapPackageConf": {
44
"setup": {
55
"appName": "object_detector",
@@ -16,12 +16,12 @@
1616
}
1717
},
1818
"resources": {
19-
"deviceDataHub_beta2": {
19+
"deviceDataHub": {
2020
"enabled": true,
2121
"accessControlList": [
2222
{
2323
"topics": [
24-
"com.example.object_detector"
24+
"com.example.objectdetector"
2525
],
2626
"usernames": [
2727
"acap-object_consumer"
@@ -32,7 +32,7 @@
3232
},
3333
{
3434
"topics": [
35-
"com.example.object_detector"
35+
"com.example.objectdetector"
3636
],
3737
"usernames": [
3838
"acap-object_detector"

0 commit comments

Comments
 (0)