edgexfoundry / device-sdk-c

Owner: Device WG
Apache License 2.0
42 stars 42 forks source link

fix: edgex_device_read does not read labels #508

Closed FelixTing closed 9 months ago

FelixTing commented 9 months ago

fix: #482

If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/device-sdk-c/blob/main/.github/Contributing.md

PR Checklist

Please check if your PR fulfills the following requirements:

Testing Instructions

  1. Run the random generator example in debug mode and set a breakpoint at https://github.com/edgexfoundry/device-sdk-c/blob/6bdae90b977cdeecb957260a94da902cfcf10c29/src/c/callback3.c#L80
  2. Update device
    curl -X 'PATCH' \
    'http://localhost:59881/api/v3/device' \
    -d '[
    {
    "apiVersion": "v2",
    "device": {
      "name": "RandomDevice1",
      "labels": [
        "label1", "label2"
      ]
    }
    }
    ]'
  3. Check the device read by edgex_device_read in step 1
ych988 commented 9 months ago

I think use [devsdk_strings_new] directly will cause the order of the label array to be reversed from the original order. This way may not be perfect.

devsdk_strings *edgex_labels_read(const iot_data_t *obj)
{
  devsdk_strings *labels = NULL;
  const iot_data_t *ldata = iot_data_string_map_get (obj, "labels");
  if (ldata)
  {
    iot_data_vector_iter_t iter;
    iot_data_vector_iter (ldata, &iter);
    while (iot_data_vector_iter_next (&iter))
    {
      labels = devsdk_strings_new (iot_data_vector_iter_string (&iter), labels);    <--------- here
    }
  }
  return labels;
}

Reference: devman.c → devsdk_add_discovered_devices → devsdk_strings_new

FelixTing commented 9 months ago

@ych988 I see your point. Code is updated.

iain-anderson commented 9 months ago

You could iterate the vector backward something like

uint32_t i = iot_data_vector_size (ldata);
while (i)
{
   labels = devsdk_strings_new (iot_data_string (iot_data_vector_get (ldata, --i)));
}
SteveOss commented 9 months ago

There is a reverse iteration function: iot_data_vector_iter_prev