1

In our application, when we create a new object via the API, we send SIM and GSM module-related information within the c8y_Mobile fragment. The object models an embedded device with limited capabilities, so we make use of the HTTPS API directly.

PUT /inventory/managedObjects/myid HTTP/1.1
Host: mytenant.cumulocity.com
Authorization: Basic ....
Content-Type: application/vnd.com.nsn.cumulocity.managedObject+json
Accept: application/vnd.com.nsn.cumulocity.managedObject+json

{
    "c8y_Mobile": {
        "imei": 1234567890123456,
        "imsi": 23456789011234567890,
        "iccid": 01234567890123456789
        ... 
    }
}

The managed object shows the new fragment as expected:

   ...
   "c8y_IsDevice": {},
   "c8y_Mobile": {
        "imei": 1234567890123456,
        "imsi": 23456789011234567890,
        "iccid": 01234567890123456789
        ... 
    },
    ...

When a user changes the SIM card on the embedded unit, IMSI and ICCID properties should be updated within the managedObject c8y_Mobile fragment. But if we send only those properties the whole fragment is overridden:

PUT /inventory/managedObjects/myid HTTP/1.1
Host: mytenant.cumulocity.com
Authorization: Basic ....
Content-Type: application/vnd.com.nsn.cumulocity.managedObject+json
Accept: application/vnd.com.nsn.cumulocity.managedObject+json

{
    "c8y_Mobile": {
        "imsi": 23456789011234567890,
        "iccid": 01234567890123456789
    }
}

So the managed object shows this:

   ...
   "c8y_IsDevice": {},
   "c8y_Mobile": {
        "imsi": 23456789011234567890,
        "iccid": 01234567890123456789
    },
    ...

Please note that the imei property and others have been lost and are not longer present in the managed object.

In order to save data and minimise transactions I would like to know if there is a way to update fragments without having to send all the desired properties again.

I've tried to use HTTP POST instead of PUT, but that gives me a method not allowed error, as stated in the documentation.

mikelfo
  • 15
  • 7

1 Answers1

1

There is no direct way to do that (but a workaround). In general when you do a PUT on any object it will be merged only on the root level of the JSON meaning if your PUT contains c8y_Mobile it will replace the current c8y_Mobile (regardless of what is contained).

Here is what you can do:

First you invent so new fragments that you use as a temporary fragment:

PUT /inventory/managedObjects/myid HTTP/1.1
Host: mytenant.cumulocity.com
Authorization: Basic ....
Content-Type: application/vnd.com.nsn.cumulocity.managedObject+json
Accept: application/vnd.com.nsn.cumulocity.managedObject+json

{
    "c8y_Mobile_imsi": "23456789011234567890",
    "c8y_Mobile_iccid": "01234567890123456789"
}

Additionally you create an event processing rule that when you update for example "c8y_Mobile_imsi" it will merge this value into the existing c8y_Mobile fragment (preserving the other sub-fragments).

Important:

You either send the PUT as transient (so these values are not persisted in the device object) or your rule removes the temporary fragment immediately (in the same update operation like the merge with c8y_Mobile). This is important because in CEP you do not know which fragment was updated when you listen to ManagedObjectUpdated. So if you would keep the temporary fragment in the device object the rule would trigger in an endless loop (which would lead to an automatic undeploy of the rule).

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • Yes I expected something like this behaviour when using PUT. I will try that workaround thanks! – mikelfo Feb 22 '17 at 21:30
  • I tried the event rule and then removing the fragment at the same time as the update and worked wonderfully. I will probably use it in the future.But this time, in order to keep my application valid for multiple use cases I will keep track of the complete c8y_Mobile fragment and send all properties each time. – mikelfo Feb 23 '17 at 12:42