Skip to content

Commit

Permalink
minor clean up,
Browse files Browse the repository at this point in the history
avoid saving namespaces of locations to config.json,
bugfix:
- missing devices in namespaces
- incorrect id naming in room-device related preparation
  • Loading branch information
Niels Roche committed Jan 5, 2016
1 parent 6ace483 commit e997df8
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions classes/AutomationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ AutomationController.prototype.init = function () {

self.loadModules(function () {
self.emit("core.init");

// necessary?
/*self.devices.on('change', function (device) {
ws.push({
type: "me.z-wave.devices.location_update",
data: JSON.stringify(device.toJSON())
});
pushNamespaces(device, false);
});*/

// update namespaces if device title has changed
self.devices.on('change:metrics:title', function (device) {
Expand Down Expand Up @@ -149,10 +140,22 @@ AutomationController.prototype.setDefaultLang = function (lang) {
};

AutomationController.prototype.saveConfig = function () {

// do clean up of location namespaces
cleanupLocations = function (locations) {
var newLoc = [];

locations.forEach(function(loc){
newLoc.push(_.omit(loc, 'namespaces'));
});

return newLoc;
};

var cfgObject = {
"controller": this.config,
"vdevInfo": this.vdevInfo,
"locations": this.locations,
"locations": cleanupLocations(this.locations),
"profiles": this.profiles,
"instances": this.instances,
"modules_categories": this.modules_categories
Expand Down Expand Up @@ -1285,6 +1288,13 @@ AutomationController.prototype.generateNamespaces = function (callback, device,

paramEntry = paramEntry? paramEntry : {};

// create 'none' entry to get an object with array of 'none probetype' entries
if (_.isArray(paramEntry)){
paramEntry = {
none: paramEntry
};
}

// check for CC sub type and add device namespaces
if(cutType.length > 1){

Expand Down Expand Up @@ -1319,14 +1329,21 @@ AutomationController.prototype.generateNamespaces = function (callback, device,
// add entries to type entries
paramEntry = paramEntry? paramEntry : [];

if (!_.isArray(paramEntry) && Object.keys(paramEntry).length < 1){
paramEntry = [];
// create 'none' entry to get an object with array of 'none probetype' entries
if (_.isArray(paramEntry)){
paramEntry = {
none: paramEntry
};
} else if(!_.isArray(paramEntry) && !paramEntry['none']){
paramEntry['none'] = paramEntry;
}

paramEntry = addRemoveEntry(paramEntry);
paramEntry.none = addRemoveEntry(paramEntry.none);
}

} else if (_.isArray(paramEntry)){
paramEntry = addRemoveEntry(paramEntry);
}
// delete 'none' entry if it exists as a single entry
if (!_.isArray(paramEntry) && Object.keys(paramEntry).length === 1 && paramEntry['none']) {
paramEntry = paramEntry['none'];
}

// set namespaces
Expand Down Expand Up @@ -1628,8 +1645,7 @@ AutomationController.prototype.replaceNamespaceFilters = function (moduleMeta) {
k.forEach(function(key) {
if(key === 'devicesByRoom') {
dSRoom['enum'].forEach(function(roomId, index) {
var cnt = index + 1,
locNspc = [],
var locNspc = [],
nspc =[];

location = self.getLocation(self.locations, roomId);
Expand All @@ -1641,9 +1657,9 @@ AutomationController.prototype.replaceNamespaceFilters = function (moduleMeta) {
dSDevByRoom['enum'] = nspc && nspc.length > 0? nspc: [langFile.no_devices_found];
dSDevByRoom['dependencies'] = "room";

newObj['devicesByRoom_' + cnt] = _.clone(dSDevByRoom);
if (newObj['devicesByRoom_' + cnt]['title']) {
newObj['devicesByRoom_' + cnt]['title'] = newObj['devicesByRoom_' + cnt]['title'] + '_' + cnt;
newObj['devicesByRoom_' + roomId] = _.clone(dSDevByRoom);
if (newObj['devicesByRoom_' + roomId]['title']) {
newObj['devicesByRoom_' + roomId]['title'] = newObj['devicesByRoom_' + roomId]['title'] + '_' + roomId;
}
});
} else {
Expand Down Expand Up @@ -1682,23 +1698,24 @@ AutomationController.prototype.replaceNamespaceFilters = function (moduleMeta) {
if(key === 'devicesByRoom') {
dSRoom['optionLabels'].forEach(function(roomName, index) {

var cnt = index + 1,
locNspc = [],
nspc = [];
var locNspc = [],
nspc = [],
locationId;

location = self.locations.filter(function(location){ return location.title === roomName });
locationId = location[0]? location[0].id : location.id;

if (location[0]) {
nspc = self.getListNamespaces(path, location[0].namespaces);
}

dSDevByRoom['optionLabels'] = nspc && nspc.length > 0? nspc: [langFile.no_devices_found];
dSDevByRoom['dependencies'] = { "room" : location[0].id };
dSDevByRoom['dependencies'] = { "room" : locationId };

newObj['devicesByRoom_' + cnt] = _.clone(dSDevByRoom);
newObj['devicesByRoom_' + locationId] = _.clone(dSDevByRoom);

if (newObj['devicesByRoom_' + cnt]['label']) {
newObj['devicesByRoom_' + cnt]['label'] = newObj['devicesByRoom_' + cnt]['label'] + '_' + cnt;
if (newObj['devicesByRoom_' + locationId]['label']) {
newObj['devicesByRoom_' + locationId]['label'] = newObj['devicesByRoom_' + locationId]['label'] + '_' + locationId;
}
});
} else {
Expand Down Expand Up @@ -1789,7 +1806,6 @@ AutomationController.prototype.replaceNamespaceFilters = function (moduleMeta) {
path = flr.substring(id[0].length + 1).replace(/:/gi, '.');

// get namespaces
//self.generateNamespaces();
nspc = self.getListNamespaces(path, self.namespaces);
if (nspc) {
namespaces = namespaces.concat(nspc);
Expand Down

0 comments on commit e997df8

Please sign in to comment.