Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Further performance fixes & refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Henning Treu <henning.treu@telekom.de>
  • Loading branch information
Henning Treu committed Jun 27, 2017
1 parent 969b051 commit 2916387
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 151 deletions.
@@ -1,10 +1,9 @@
angular.module('PaperUI.controllers.control', []) //
.controller('ControlPageController', function($scope, $routeParams, $location, $timeout, itemRepository, thingTypeRepository, thingService, thingTypeService, channelTypeService, thingConfigService, imageService, util, thingRepository) {
.controller('ControlPageController', function($scope, $routeParams, $location, $timeout, $filter, itemRepository, thingTypeRepository, thingConfigService, imageService, util, thingRepository, channelTypeRepository) {
$scope.items = [];
$scope.selectedIndex = 0;
$scope.tabs = [];
$scope.things = [];
var thingTypes = [];

$scope.navigateTo = function(path) {
$location.path(path);
Expand All @@ -24,44 +23,48 @@ angular.module('PaperUI.controllers.control', []) //
$scope.selectedIndex = newIndex;
}

$scope.getThingsForTab = function(tabName) {
var tabThings = $scope.things.filter(function(thing) {
return thing.location === tabName;
})

return $filter('orderBy')(tabThings, 'label');
}

$scope.refresh = function() {
var index = $scope.selectedIndex;
itemRepository.getAll(function(items) {
$scope.items = items;
}, true);
getThings();
}
$scope.channelTypes = [];
$scope.thingTypes = [];
$scope.thingChannels = [];
$scope.isLoadComplete = false;
var thingList;

function getThings() {
$scope.things = [];
thingRepository.getAll().then(function(things) {
thingList = things;
$scope.isLoadComplete = false;
thingTypeRepository.getAll().then(function(thingTypes) {
$scope.thingTypes = thingTypes;
channelTypeService.getAll().$promise.then(function(channels) {
$scope.channelTypes = channels;
var thingTypesUsed = getUsedThingTypesUIDs(thingList);
var renderedThings = [];
thingRepository.getAll(null, true).then(function(things) {
thingTypeRepository.getAll(null, true).then(function(thingTypes) {
channelTypeRepository.getAll(null, true).then(function(channelTypes) {
var thingTypesUsed = getUsedThingTypesUIDs(things);
angular.forEach(thingTypesUsed, function(thingTypeUsed) {
thingTypeRepository.getOne(function(element) {
var thingType = thingTypes.filter(function(element) {
return element.UID === thingTypeUsed;
}, function(thingType) {
thingTypes.push(thingType);
angular.forEach(thingList, function(thing) {
if (thing.thingTypeUID === thingType.UID) {
renderThing(thing, thingType, $scope.channelTypes);
})[0];
if (thingType == null) {
return;
}
angular.forEach(things, function(thing) {
if (thing.thingTypeUID === thingType.UID) {
var renderedThing = renderThing(thing, thingType, channelTypes);
if (renderedThing && renderedThings.indexOf(renderedThing) == -1) {
renderedThings.push(renderedThing);
}
})
});
});
}).then(function() {
getTabs();
});
});

}
})
})
$scope.tabs = getTabs(renderedThings);
$scope.things = renderedThings;
})
})
});
}
function renderThing(thing, thingType, channelTypes) {
Expand All @@ -71,8 +74,8 @@ angular.module('PaperUI.controllers.control', []) //
return channel.linkedItems.length > 0;
});
});
if (thingHasChannels(thing) && $scope.things.indexOf(thing) == -1) {
$scope.things.push(thing);
if (thingHasChannels(thing)) {
return thing;
}
}
function thingHasChannels(thing) {
Expand All @@ -84,58 +87,47 @@ angular.module('PaperUI.controllers.control', []) //
return false;
}

function getTabs() {
if (!$scope.things) {
return;
function getTabs(things) {
if (!things) {
return [];
}
var renderedTabs = [];
var arr = [], otherTab = false;
for (var i = 0; i < $scope.things.length; i++) {
if ($scope.things[i].location && $scope.things[i].location.toUpperCase() != "OTHER") {
$scope.things[i].location = $scope.things[i].location.toUpperCase();
arr[$scope.things[i].location] = $scope.things[i].location;
angular.forEach(things, function(thing) {
if (thing.location && thing.location.toUpperCase() != "OTHER") {
thing.location = thing.location.toUpperCase();
arr[thing.location] = thing.location;
} else {
$scope.things[i].location = "OTHER";
thing.location = "OTHER";
otherTab = true;
}
})
for (value in arr) {
renderedTabs.push({
name : value
});
}
for ( var value in arr) {
if (!hasTab(value)) {
$scope.tabs.push({
name : value
});
}
}
if (otherTab && !hasTab("OTHER")) {
$scope.tabs.push({
if (otherTab) {
renderedTabs.push({
name : "OTHER"
});
}
$scope.isLoadComplete = true;
}

function hasTab(name) {
return $.grep($scope.tabs, function(tab) {
return tab.name == name;
}).length > 0;
return renderedTabs;
}

function getThingTypeLocal(thingTypeUID) {
var thingTypeComplete = $.grep(thingTypes, function(thingType) {
return thingType.UID == thingTypeUID;
function getUsedThingTypesUIDs(things) {
var thingTypeUIDs = new Set();
angular.forEach(things, function(thing) {
thingTypeUIDs.add(thing.thingTypeUID);
})

var result = [];
thingTypeUIDs.forEach(function(uid) {
result.push(uid);
});
return thingTypeComplete.length > 0 ? thingTypeComplete : null;
}

function getUsedThingTypesUIDs(things) {
var thingTypeUIDs = [];
if (things) {
for (var i = 0; i < things.length; i++) {
if (thingTypeUIDs.indexOf(things[i].thingTypeUID) == -1) {
thingTypeUIDs.push(things[i].thingTypeUID);
}
}
}
return thingTypeUIDs;
return result;
}

$scope.tabComparator = function(actual, expected) {
Expand Down
90 changes: 36 additions & 54 deletions extensions/ui/org.eclipse.smarthome.ui.paper/web-src/js/services.js
Expand Up @@ -524,25 +524,26 @@ angular.module('PaperUI.services', [ 'PaperUI.services.repositories', 'PaperUI.c
var thingChannels = [];
var includedChannels = [];
if (thingType && thingType.channelGroups && thingType.channelGroups.length > 0) {
for (var i = 0; i < thingType.channelGroups.length; i++) {
angular.forEach(thingType.channelGroups, function(channelGroup) {
var group = {};
group.name = thingType.channelGroups[i].label;
group.description = thingType.channelGroups[i].description;
group.channels = this.matchGroup(thing.channels, thingType.channelGroups[i].id);
group.name = channelGroup.label;
group.description = channelGroup.description;
group.channels = this.matchGroup(thing.channels, channelGroup.id);
includedChannels = includedChannels.concat(group.channels);
group.channels = advanced ? group.channels : this.filterAdvance(thingType, channelTypes, group.channels, false);
thingChannels.push(group);
}
})

var group = {
"name" : "Others",
"description" : "Other channels",
"channels" : []
};
for (var i = 0; i < thing.channels.length; i++) {
if (includedChannels.indexOf(thing.channels[i]) == -1) {
group.channels.push(thing.channels[i]);
angular.forEach(thing.channels, function(channel) {
if (includedChannels.indexOf(channel) == -1) {
group.channels.push(channel);
}
}
})
if (group.channels && group.channels.length > 0) {
thingChannels.push(group);
}
Expand All @@ -557,62 +558,43 @@ angular.module('PaperUI.services', [ 'PaperUI.services.repositories', 'PaperUI.c
},

filterAdvance : function(thingType, channelTypes, channels, advanced) {
var self = this;
self.thingType = thingType, self.channelTypes = channelTypes, self.channels = channels;
return $.grep(channels, function(channel, i) {
var channelType = self.getChannelTypeByUID(self.thingType, self.channelTypes, channel.channelTypeUID);
return channels.filter(function(channel) {
var channelType = this.getChannelTypeByUID(thingType, channelTypes, channel.channelTypeUID);
return channelType ? advanced == channelType.advanced : true;
});
}, this);
},
getChannelTypeByUID : function(thingType, channelTypes, channelUID) {
if (thingType) {
if (thingType.channels && thingType.channels.length > 0) {
var c, c_i, c_l;
for (c_i = 0, c_l = thingType.channels.length; c_i < c_l; ++c_i) {
c = thingType.channels[c_i];
if (c.typeUID == channelUID) {
return c;
}
var result = thingType.channels.filter(function(channel) {
return channel.typeUID === channelUID;
})
if (result.length > 0) {
return result[0];
}
}
if (thingType.channelGroups && thingType.channelGroups.length > 0) {
var c, c_i, c_l;
var cg, cg_i, cg_l;
for (cg_i = 0, cg_l = thingType.channelGroups.length; cg_i < cg_l; ++cg_i) {
cg = thingType.channelGroups[cg_i];
if (cg && cg.channels) {
for (c_i = 0, c_l = cg.channels.length; c_i < c_l; ++c_i) {
c = cg.channels[c_i];
if (c.typeUID == channelUID) {
return c;
}
angular.forEach(thingType.channelGroups, function(channelGroup) {
if (channelGroup && channelGroup.channels) {
var result = cg.channels.filter(function(channel) {
return channel.typeUID === channelUID;
})
if (result.length > 0) {
return result[0];
}
}
}
})
}
}
if (channelTypes) {
var c = {}, c_i, c_l;
for (c_i = 0, c_l = channelTypes.length; c_i < c_l; ++c_i) {
c = channelTypes[c_i];
if (c.UID == channelUID) {
return c;
}
}
return this.getChannelFromChannelTypes(channelTypes, channelUID);
}
return;
},
getChannelFromChannelTypes : function(channelTypes, channelUID) {
if (channelTypes) {
var c = {}, c_i, c_l;
for (c_i = 0, c_l = channelTypes.length; c_i < c_l; ++c_i) {
c = channelTypes[c_i];
if (c.UID == channelUID) {
return c;
}
}
}
return;
var result = channelTypes.filter(function(channelType) {
return channelType.UID === channelUID;
})
return result.length > 0 ? result[0] : null;
},
matchGroup : function(arr, id) {
var matched = [];
Expand All @@ -627,11 +609,11 @@ angular.module('PaperUI.services', [ 'PaperUI.services.repositories', 'PaperUI.c
return matched;
},
addTypeToChannels : function(groups, channelTypes) {
for (var g_i = 0; g_i < groups.length; g_i++) {
for (var c_i = 0; c_i < groups[g_i].channels.length; c_i++) {
groups[g_i].channels[c_i].channelType = this.getChannelFromChannelTypes(channelTypes, groups[g_i].channels[c_i].channelTypeUID);
}
}
angular.forEach(groups, function(group) {
angular.forEach(group.channels, function(channel) {
channel.channelType = this.getChannelFromChannelTypes(channelTypes, channel.channelTypeUID);
}, this)
}, this)
return groups;
}
}
Expand Down
Expand Up @@ -113,6 +113,9 @@ angular.module('PaperUI.services.repositories', []).factory('bindingRepository',
}).factory('thingTypeRepository', function($q, $rootScope, thingTypeService) {
$rootScope.data.thingTypes = [];
return new Repository($q, $rootScope, thingTypeService, 'thingTypes', true);
}).factory('channelTypeRepository', function($q, $rootScope, channelTypeService) {
$rootScope.data.channelTypes = [];
return new Repository($q, $rootScope, channelTypeService, 'channelTypes', true);
}).factory('discoveryResultRepository', function($q, $rootScope, inboxService, eventService) {
var repository = new Repository($q, $rootScope, inboxService, 'discoveryResults')
$rootScope.data.discoveryResults = [];
Expand Down

0 comments on commit 2916387

Please sign in to comment.