# Online status not getting updating in the device managed object

**URL:** <https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [May 2, 2024, 11:48am UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347 "2024-05-02T11:48:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanbp](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohanbp](https://community.cumulocity.com/u/mohanbp)\
**Post date:** [May 2, 2024, 11:48am UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/1 "2024-05-02T11:48:04Z")

</div>

We are facing the issue, where we are not getting online statues for a device even though the device is getting the measurements every minute. We are using cumulocity sdk to send the measurements to the deviceas shown below:

```
		MeasurementRepresentation createMr = new MeasurementRepresentation();
		createMr.set("measurement",xyz);
		createMr.setSource(xxx);
		createMr.setType(xxx);
		createMr.setDateTime(dateTime);
		measurementApi.create(createMr);

```

If we use RestApi it is working fine. We had posted the question `https://community.cumulocity.com/t/device-availability-status-not-changing-when-measurments-created-using-sdk-api/3883/2`  
and got few replies.  
One was removing the X-Cumulocity-Application-Key header from the request and sending the empty PUT requests to update the status manually. For the first one, we don’t have sdk support to remove the X-Cumulocity-Application-Key header and second I tried id did not work. Since it is an immediate issue which we have to fulfill the client request, can some body provide a solution for this one

---

<div class="post-metadata">

**Author:** ![Philipp\_Emmel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/philipp_emmel/32/19455_2.png) [@Philipp\_Emmel](https://community.cumulocity.com/u/Philipp_Emmel)\
**Post date:** [May 2, 2024, 12:04pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/2 "2024-05-02T12:04:56Z")

</div>

As explained in your last question, the microservice SDK provides means to remove the X-Cumulocity-Application-Key header. It just doesn’t work through the MeasurementApi class itself but through the context.

> [@Exclude a particular header in MeasurementApi or InventoryApi which creating or updating](https://community.cumulocity.com/t/exclude-a-particular-header-in-measurementapi-or-inventoryapi-which-creating-or-updating/2304/2):
>
> Most relevant headers including the X-Cumulocity-Application-Key header are controlled through the context. So in order to change or remove a header you’ll have to create a new context and run your request within it. The context is managed through com.cumulocity.microservice.context.ContextService\<C\> I don’t have a concrete example that I can share but here is a basic approach that you can take: Inject ContextService\<MicroserviceCredentials\> into your bean Get the current context from it usin…

---

<div class="post-metadata">

**Author:** ![Christian\_Guether1](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/christian_guether1/32/14848_2.png) [@Christian\_Guether1](https://community.cumulocity.com/u/Christian_Guether1)\
**Post date:** [May 2, 2024, 12:28pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/3 "2024-05-02T12:28:43Z")

</div>

As Philipp has mentioned this can be solved by using the Microservice SDK and updating the context in which you execute the creation of a new measurement:

```auto
return microserviceSubscriptionsService.callForTenant(tenantId, () -> {
	// remove application header from context
	final MicroserviceCredentials clonedContext = new MicroserviceCredentials(
			contextService.getContext().getTenant(),
			contextService.getContext().getUsername(), contextService.getContext().getPassword(),
			contextService.getContext().getOAuthAccessToken(), contextService.getContext().getXsrfToken(),
			contextService.getContext().getTfaToken(), null);
	
	return contextService.callWithinContext(clonedContext, () -> {
		try {
			return measurementApi.create(measurementRepresentation);
		} catch (SDKException e) {
			e.printStackTrace();
		}
	
		return null;
	});
});

```

Make sure to inject `MicroserviceSubscriptionsService microserviceSubscriptionsService` and `ContextService<MicroserviceCredentials> contextService`.

  

* * *

Christian Guether

[Software AG Professional Services](https://www.softwareag.com/en_corporate/resources/services/professional-services.html)

[![](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/c/3/c3a93a2618e4708f51be40fe2e142061f7c4edc7.jpeg)](https://www.softwareag.com/en_corporate/resources/services/fast-track-services.html)

---

<div class="post-metadata">

**Author:** ![mohanbp](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohanbp](https://community.cumulocity.com/u/mohanbp)\
**Post date:** [May 6, 2024, 3:51pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/4 "2024-05-06T15:51:26Z")

</div>

Hi @Christian_Guether , I have tried the above code by creating a different context to insert the MeasurementRepresentation. But it did not work.

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [May 6, 2024, 4:00pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/5 "2024-05-06T16:00:04Z")

</div>

Can you share your code block? For me it is working like expected.

---

<div class="post-metadata">

**Author:** ![mohanbp](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohanbp](https://community.cumulocity.com/u/mohanbp)\
**Post date:** [May 6, 2024, 5:56pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/6 "2024-05-06T17:56:58Z")

</div>

Hi @Stefan_Witschel

Please find the method below that I am using to insert the MeasurementRepresentation

```auto
    private void insertMeasurement() {
       microserviceSubscriptionsService.callForTenant(contextService.getContext().getTenant(), () -> {
            final MicroserviceCredentials clonedContext = new MicroserviceCredentials(contextService.getContext().getTenant(), contextService.getContext().getUsername(), contextService.getContext().getPassword(), contextService.getContext().getOAuthAccessToken(), contextService.getContext().getXsrfToken(), contextService.getContext().getTfaToken(), null);

            contextService.callWithinContext(clonedContext, () -> {
                try {
                    MeasurementRepresentation measurementToInjest = // createMeasurementRepresentation
                    MeasurementRepresentation mrCreated = measurementApi.create(measurementToInjest);
                } catch (SDKException e) {
                   log.error("The current exception is "+e.getStackTrace());
                }
                return null;
            });
            return null;
        });
    }

```

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [May 8, 2024, 1:01pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/7 "2024-05-08T13:01:51Z")

</div>

As you don’t return anything you don’t need to run `callForTenant`. Instead replace it with `runForTenant` and remove the return part.

Everything else looks fine.  
Did you set the required Interval on your device? Otherwise the connection status will not be updated.

---

<div class="post-metadata">

**Author:** ![Philipp\_Emmel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/philipp_emmel/32/19455_2.png) [@Philipp\_Emmel](https://community.cumulocity.com/u/Philipp_Emmel)\
**Post date:** [May 13, 2024, 11:01am UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/8 "2024-05-13T11:01:09Z")

</div>

For various reasons the context may be cached (see: [cumulocity-clients-java/microservice/context/src/main/java/com/cumulocity/microservice/context/annotation/EnableContextSupportConfiguration.java at develop · Cumulocity-IoT/cumulocity-clients-java · GitHub](https://github.com/Cumulocity-IoT/cumulocity-clients-java/blob/develop/microservice/context/src/main/java/com/cumulocity/microservice/context/annotation/EnableContextSupportConfiguration.java#L45))

In this we don’t not use the app key as part of the combined key for retrieving said context again. This may lead to the same context being returned again without the changes made. In order to circumvent this behaviour you can also reset the XSRF token to a value of your choice (ex: “PLACEHOLDER”) since it is not relevant when sending requests from a microservice to Cumulocity within the same environment.

The resulting code will then look like this:

```auto
private MicroserviceCredentials createContextWithoutApiKey(MicroserviceCredentials source) {
  return new MicroserviceCredentials(
    source.getTenant(),
    source.getUsername(),
    source.getPassword(),
    source.getOAuthAccessToken(),
    "PLACEHOLDER", //added to replace context, check: com.cumulocity.microservice.context.annotation.EnableContextSupportConfiguration.contextScopeConfigurer
    source.getTfaToken(),
    null
  );
}

```

Then you can make use of it in your measurement sending implementation like this:

```auto
        MicroserviceCredentials noAppKeyContext = createContextWithoutApiKey(contextService.getContext());
        contextService.callWithinContext(noAppKeyContext, () -> measurementApi.create(createMr));

```

---

<div class="post-metadata">

**Author:** ![mohanbp](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohanbp](https://community.cumulocity.com/u/mohanbp)\
**Post date:** [May 13, 2024, 2:49pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/9 "2024-05-13T14:49:43Z")

</div>

Thanks @Philipp_Emmel , Will do these changes in my code as well

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [November 9, 2024, 2:49pm UTC](https://community.cumulocity.com/t/online-status-not-getting-updating-in-the-device-managed-object/1347/10 "2024-11-09T14:49:57Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
