In Angularjs I need to display a list of datapoints {name:"", type:"", value:"",...} that I retrieve from the database of the Cumulocity cloud. Also I need the datapoints to update whenever their value changes in the backend. The datapoint name and type are known beforehand, but the value I have to retrieve separately and update after it changes in the backend.
function init(){
var promiseDatapointNameType = c8yDevices.detail(deviceId);
promiseDatapointNameType.then(c8yBase.getResData)
.then(createDatapointObjectStack)
.then(getDatapointObjectValues) // Problems start here
.then(resolvedObjectsToView);
};
I am trying to accomplish this through an array of promised objects, where each object is the datapoint's {name and type} that were retrieve earlier and then I request the latest value for each of the datapoint Objects in the stack from the backend.
function getDatapointObjectValues(datapointStack){
return datapointStack.map(function(dpObject){
return getLatestMeasurement(dpObject);
});
}
Therefore I call the function getLatestMeasurement on each object in the datapointStack, that fires a request to the Cumulocity database and returns a promise that returns the latest value for the datapoint Object when resolved. Additionally the realtime option is set, that updates this promised value in "realtime" on value change.
// For a given filter, function returns a promise of the latest
function getLatestMeasurement(dpObject){
var filter = {
device: deviceId,
fragment: "CloudControl Parameters",
series: dpObject.name
};
var realtime = true;
return c8yMeasurements.latest(filter, realtime).then(c8yBase.getResData).then(function (responseValue) {
var jsonPathToDp = filter.fragment + "." + filter.series + ".value";
var latestValueDB = getObjectValue(responseValue, jsonPathToDp);
dpObject.valueLatest = latestValueDB;
return dpObject;
});
};
If any of the promises in the array of promised objects resolves, I want to update the corresponding Datapoint Object in the view so that the new value gets displayed.
function resolvedObjectsToView(dpStackValues){
$q.race(dpStackValues).then(function(dpRaceObject){
vm.device.datapointStack.push(dpRaceObject);
});
}
My current state is that all the datapoints get displayed, however they do not update their values if they change in the backend. Here I got stuck as I don't know if reusing a promise after it resolved in an array is practicable and also how to accomplish it with an alternative way. If I use race only a single datapoint is shown in the list, if I use $q.all they are all shown but in both cases no value updates.
So my questions are:
How do i reuse resolved promised Objects if a value in the backend changes? And how do I update an objects value in the view if the associated promised object in an array resolves?