From c449cd6a2c57881ffa1c10c806da9f662413e186 Mon Sep 17 00:00:00 2001 From: FelixTing Date: Wed, 19 Jul 2023 12:35:56 +0000 Subject: [PATCH] fix: Read actual status code after a POST device API call Also update the discovery example to v3. Signed-off-by: FelixTing --- src/c/config.h | 2 -- src/c/examples/discovery/README.md | 10 +++++----- src/c/examples/discovery/watcher1.json | 2 +- src/c/examples/discovery/watcher2.json | 2 +- src/c/metadata.c | 16 +++++++++++++++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/c/config.h b/src/c/config.h index 0d4b5deca..86c8df47e 100644 --- a/src/c/config.h +++ b/src/c/config.h @@ -13,8 +13,6 @@ #include "rest-server.h" #include "map.h" -#include - #define EX_METRIC_EVSENT 0x1 #define EX_METRIC_RDGSENT 0x2 #define EX_METRIC_RDCMDS 0x4 diff --git a/src/c/examples/discovery/README.md b/src/c/examples/discovery/README.md index 21059c74c..16b27f460 100644 --- a/src/c/examples/discovery/README.md +++ b/src/c/examples/discovery/README.md @@ -10,12 +10,12 @@ provisioning of devices. The environment variable CSDK_DIR should be set to a directory containing the C SDK include files and libraries. -Set LD_LIBRARY_PATH to $CSDK_DIR/lib +Set LD_LIBRARY_PATH to $CSDK_DIR/lib:/opt/iotech/iot/1.5/lib ### Building ``` -gcc -I$CSDK_DIR/include -L$CSDK_DIR/lib -o device-template template.c -lcsdk +gcc -I$CSDK_DIR/include -I/opt/iotech/iot/1.5/include -L$CSDK_DIR/lib -L/opt/iotech/iot/1.5/lib -o device-template template.c -lcsdk -liot ``` ### Device Profile @@ -39,7 +39,7 @@ Discovery/Interval to 0), discovery may be forced by calling the discovery endpoint manually: ``` -curl -X POST 0:59999/api/v2/discovery +curl -X POST 0:59999/api/v3/discovery ``` Initially, none of the discovered devices will be added to EdgeX, but by @@ -47,8 +47,8 @@ using appropriate Provision Watchers they can be accepted. To upload the supplied Provision Watchers to core-metadata: ``` -curl -X POST -d@watcher1.json 0:59881/api/v2/provisionwatcher -curl -X POST -d@watcher2.json 0:59881/api/v2/provisionwatcher +curl -X POST -d@watcher1.json 0:59881/api/v3/provisionwatcher +curl -X POST -d@watcher2.json 0:59881/api/v3/provisionwatcher ``` The Provision Watchers each match one of the discovered devices. They work by diff --git a/src/c/examples/discovery/watcher1.json b/src/c/examples/discovery/watcher1.json index 14a233e76..8a9a2223f 100644 --- a/src/c/examples/discovery/watcher1.json +++ b/src/c/examples/discovery/watcher1.json @@ -1 +1 @@ -[{"apiVersion":"v2","provisionWatcher":{"apiVersion":"v2", "name":"watcher1", "identifiers":{"MAC":"00-05-1B-A1-99-[0-9A-Fa-f][0-9A-Fa-f]"}, "blockingIdentifiers":{"MAC":["00-05-1B-A1-99-99"]},"profileName":"TemplateSensor","serviceName":"device-template","adminState":"UNLOCKED"}}] +[{"apiVersion":"v2","provisionWatcher":{"apiVersion":"v2","name":"watcher1","identifiers":{"MAC":"00-05-1B-A1-99-[0-9A-Fa-f][0-9A-Fa-f]"},"blockingIdentifiers":{"MAC":["00-05-1B-A1-99-99"]},"serviceName":"device-template","adminState":"UNLOCKED","discoveredDevice":{"profileName":"TemplateSensor","serviceName":"device-template","adminState":"UNLOCKED","properties":{"DeviceNameTemplate":{"valueReplace":true,"template":"device-name-{{MAC}}"}}}}}] diff --git a/src/c/examples/discovery/watcher2.json b/src/c/examples/discovery/watcher2.json index a3be002f2..6d6b11deb 100644 --- a/src/c/examples/discovery/watcher2.json +++ b/src/c/examples/discovery/watcher2.json @@ -1 +1 @@ -[{"apiVersion":"v2","provisionWatcher":{"apiVersion":"v2", "name":"watcher2", "identifiers":{"HTTP":"10\\.0\\.0\\.[0-9]*"}, "blockingIdentifiers":{"HTTP":["10.0.0.1","10.0.0.255"]},"profileName":"TemplateSensor","serviceName":"device-template","adminstate":"UNLOCKED"}}] +[{"apiVersion":"v2","provisionWatcher":{"apiVersion":"v2","name":"watcher2","identifiers":{"HTTP":"10\\.0\\.0\\.[0-9]*"},"blockingIdentifiers":{"HTTP":["10.0.0.1","10.0.0.255"]},"profileName":"TemplateSensor","serviceName":"device-template","adminstate":"UNLOCKED","discoveredDevice":{"profileName":"TemplateSensor","serviceName":"device-template","adminState":"UNLOCKED","properties":{"DeviceNameTemplate":{"valueReplace":true,"template":"device-name-{{HTTP}}"}}}}}] diff --git a/src/c/metadata.c b/src/c/metadata.c index 72ae58507..3003bb77b 100644 --- a/src/c/metadata.c +++ b/src/c/metadata.c @@ -540,7 +540,21 @@ void edgex_metadata_client_add_or_modify_device iot_data_t *jwt_data = edgex_secrets_request_jwt (secretprovider); ctx.jwt_token = iot_data_string(jwt_data); - if (edgex_http_post (lc, &ctx, url, json, edgex_http_write_cb, &err) == 409) + edgex_http_post (lc, &ctx, url, json, edgex_http_write_cb, &err); + + uint statusCode = 0; + JSON_Value *val = json_parse_string(ctx.buff); + JSON_Array *resps = json_value_get_array (val); + size_t nresps = json_array_get_count (resps); + if (nresps) + { + JSON_Object *obj = json_array_get_object (resps, 0); + statusCode = json_object_get_uint (obj, "statusCode"); + + } + json_value_free(val); + + if (statusCode == 409) { if (edgex_metadata_client_check_device (lc, endpoints, secretprovider, name)) {