Skip to content

Commit

Permalink
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -212,9 +212,9 @@ lwClientControllers.controller('ClientDetailCtrl', [
var regUpdate = JSON.parse(msg.data);
$scope.client = regUpdate.registration;
if (regUpdate.update.objectLinks){
lwResources.buildResourceTree($scope.clientId, $scope.client.rootPath, $scope.client.objectLinks, function (objects){
lwResources.updateResourceTree($scope.clientId, $scope.objects, $scope.client.rootPath, regUpdate.update.objectLinks, function (objects){
$scope.objects = objects;
});
});
}
});
};
Original file line number Diff line number Diff line change
@@ -141,6 +141,61 @@ myModule.factory('lwResources',["$http", function($http) {
});
};

/**
* Update Resource Tree for the given rootPath and objectLinks
*/
var updateResourceTree = function(endpoint, tree, rootPath, objectLinks, callback) {
if (objectLinks.length == 0)
callback([]);

getObjectDefinitions(endpoint, function(objectDefs){

// add missing
for (var i = 0; i < objectLinks.length; i++) {

// remove root path from link
var link = objectLinks[i].url;
if(link.indexOf(rootPath) == 0) {
link.slice(rootPath.length);
}

// get list of resources (e.g. : [3] or [1,123]
var resourcepath = url2array(link);
var attributes = objectLinks[i].attributes;

switch (resourcepath.length) {
case 0:
// ignore empty path
break;
case 1:
// object
var object = addObject(tree, objectDefs, resourcepath[0],
attributes);
break;
case 2:
// instance
var object = addObject(tree, objectDefs, resourcepath[0], null);
addInstance(object, resourcepath[1], attributes);

break;
}
}

// remove extra object instances
result = tree.filter(function (object) {
// remove extra instances
object.instances = object.instances.filter(instance => objectLinks.find(link => link.url.startsWith(rootPath+object.id+"/"+instance.id)));
// filter object
return objectLinks.find(link => link.url.startsWith(rootPath+object.id));
});

// sort object
result.sort(function(o1,o2){return o1.id - o2.id});

callback(result);
});
};

/**
* add object with the given ID to resource tree if necessary and return it
*/
@@ -293,6 +348,7 @@ myModule.factory('lwResources',["$http", function($http) {
};

serviceInstance.buildResourceTree = buildResourceTree;
serviceInstance.updateResourceTree = updateResourceTree;
serviceInstance.findResource = findResource;
serviceInstance.findInstance = findInstance;
serviceInstance.addInstance = addInstance;

0 comments on commit b89c340

Please sign in to comment.