How to use pson_array to set a property

Hello, I am trying to set a property as an array of strings.

pson data;
pson update;
pson_array ids = data["ids"];
ids.add(SRB_State.lastEpcCode.c_str());
update["ids"] = ids;
thing.set_property("containers", update, false);

This causes a whole lot of error output, not sure what I’m doing wrong or where I can get documentation about this, I’ve been searching around for info about using PSON in c++ as well as Thinger docs but they don’t talk about working with arrays.

I’m not a c++ developer so forgive if I’m missing something obvious :smiley:

Hi @reusables-official

The correct way to work with pson_array and set a property as its value would be something like this:

pson data;
pson_array& array = data;
array.add("value1");
array.add("value2");

thing.set_property("containers", data, false);

With this example the property would just be an array of strings, without a key associated to the array. But this could also be done by setting instead pson_array& array = data["key"].

So in your case I believe this would be the correct code:

pson data;

pson_array& ids = data["ids"];
ids.add(SRB_State.lastEpcCode.c_str());

thing.set_property("containers", data, false);

Let us know if this solution works for you :slight_smile:

1 Like

Hey @jaimebs ! Thanks for the response, I was able to get it compiling and have just confirmed that the data is set to the property as expected. Thanks so much! I think the issue was around my using two different pson objects, so I simplified to only set data. Here is my code in case someone else wants to look in future:

void updateThinger(void* params) {
  for(;;) {
    // creating a pson to store the property values from thinger platform
    pson data;
    #ifdef NETWORK_MODE_WIFI
      wifithing.get_property("containers", data);
    #elif defined(NETWORK_MODE_LTE)
      gsmthing.get_property("containers", data);
    #endif
    pson_array& ids = data["ids"];
    ids.add(SRB_State.lastEpcCode.c_str());
    unsigned int currentCount = data["count"];
    unsigned int newCount = 1 + currentCount;
    data["count"] = newCount;
    data["lastEpc"] = SRB_State.lastEpcCode.c_str();
    #ifdef NETWORK_MODE_WIFI
      wifithing.set_property("containers", data, false);
    #elif defined(NETWORK_MODE_LTE)
      gsmthing.set_property("containers", data, true);
    #endif
    vTaskDelete(NULL);
  };
};

Not sure if this is on the roadmap, but it would be sweet if we could access arrays from dashboard functions. At the moment, it seems like I can only pass in array members as the source value for a dashboard function, but being able to iterate over an array for processing would unlock some cool possibilities.

@jaimebs Actually noticing that my code above is only working with regards the ids array. The count value is not incrementing and the lastEpcCode is also not updating. I have placed logs in my code to confirm that both newCount and SRB_State.lastEpcCode.c_str() are the values that I expect them to be, but in the Thinger dashboard under properties, only the ids array is being modified. Any ideas?

Is there any where to get documentation about pson? The documentation for setting properties in thinger is also very sparse and hard to find, and seems to assume that values are not computed from old values. The comment about more information being available in the codification section of the docs is misleading as well as there is nothing further there, unless I’ve completely overlooked it.

OK to close the loop here, it seems that by setting everything to the pson type properties are not being written correctly:

unsigned int currentCount = data["count"];
const unsigned int newCount = 1 + currentCount;
pson total = newCount;
pson lastEpc = SRB_State.lastEpcCode.c_str();
data["count"] = total;
data["lastEpc"] = lastEpc;
wifithing.set_property("containers", data, false);