I have created an Angular service for my component to extract a list object measurements from my Cumulocity Inventory using the InventoryService. The service needs to extract one of two types of objects based on the following SmartREST template:
To do so, I have created a method that uses an instance of InventoryService to call the list() method to extract the relevant measurements from a given date range.
Next, I am using a Subject of type IModeUsage from my custom Angular service to subscribe to the response stream; however, I am unsure how I am supposed to proceed from here.
From what I glimpsed usingVS code’s intellisense, the response.data is a structure of IManagedObjects, so I am not exactly sure what the Subject is subscribing to. Is it subscribing to a single IManagedObject or to the entire data structure?
Also, how would I go about transferring the data from one of the IManagedObject to an instance of IModeUsage if the data comes as a data structure?
it sounds like you are mixing up different things here…
I’m assuming you are creating a measurement via your SmartREST template. If so, you should use the MeasurementService instead of the InventoryService as these are two completely different APIs.
Also it sounds like you are unable to test the code you are developing against an actual tenant? Are you missing a device sending the data from the SmartREST template? If so, you could define a Simulator that sends this data to your Cumulocity tenant.
Transforming the data from one data structure (IManagedObject or actually IMeasurement) to another one (IModeUsage) is pretty basic JavaScript/TypeScript knowledge that we do not plan to teach in this forum. If you do not know how the objects you are going to receive via API look like, why do you not just take a look at the responses you are receiving from the API (e.g. via Postman) or via the services above?
My next challenge, however, is extracting the information from each element in the array.
The method returns a Promise<IResultList<IMeasurement> object but whenever I try to sequence through each array element I am unable to access any of the properties.
private getTemperatureMeasurement(startDate: string, endDate: string):void{
this.liveDashboardService
.getTemperatureMeasurements(startDate, endDate)
.then((result)=> {
for(let element in result.data){
console.log(element[0]);
}
})
}
private getTemperatureMeasurement(startDate: string, endDate: string):void{
this.liveDashboardService
.getTemperatureMeasurements(startDate, endDate)
.then((result)=> {
var len: number = result.data.length;
console.log(len);
const fragment = 'c8y_Temperature';
const series = 'T';
for(let i = 0; i < len; i++){
const measurement = result.data[i];
const valueUnitTuple = measurement[fragment][series];
console.log(measurement.time, valueUnitTuple.value, valueUnitTuple.unit);
}
})
}
If you need help in writing this kind of code, I would highly recommend to get in touch with our Professional Services to get some support.
This forum is at least not meant to teach writing basic JavaScript code.
You were asking how to retrieve the value from the data structure and tristan posted an working example. If this is not answering your question, what is your question?
I agree with Tristan, that we cannot answer basic typescript/javascript questions here. When developing Web Application you should have basic knowledge about that.
I would also suggest that you have a deeper look in our data structure: Cumulocity IoT - OpenAPI Specification
This will help to understand what is meant with fragments, series and values in measurements and how navigate in one document.