Skip to content

Commit

Permalink
Generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
mwittig committed Jun 18, 2016
1 parent 778dcb0 commit dd0e146
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,20 @@ Schedules a given function which will not be called as long as it
* **Number** *delay* - delay in milliseconds
* **Function** *fn* - function to be called

## generateDeviceId([framework], prefix, framework, [lastId])

Generates a new device id which is not yet in use by another device

### Params:

* *[framework]* - the pimatic framework object.
* **String** *prefix* - a prefix string to be used as part of device id.
* **Integer** *framework* - the pimatic framework object.
* **String** *[lastId]* - the lastId returned by generateDeviceId

### Return:

* **String** the id generated or undefined if id could not be generated

<!-- End src\index.coffee -->

33 changes: 32 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ module.exports = function(env) {
return array;
}
output = {};
for (key = i = 0, ref = array.length; 0 <= ref ? i < ref : i > ref; key = 0 <= ref ? ++i : --i) {
for (key = i = 0, ref = array.length; i < ref; key = i += 1) {
output[array[key]] = array[key];
}
results = [];
Expand Down Expand Up @@ -271,6 +271,37 @@ module.exports = function(env) {
return fn.call(device);
}, delay);
return Promise.resolve();
},

/*
Generates a new device id which is not yet in use by another device
@param {*} [framework] - the pimatic framework object.
@param {String} prefix - a prefix string to be used as part of device id.
@param {Integer} framework - the pimatic framework object.
@param {String} [lastId] - the lastId returned by generateDeviceId
@returns {String} the id generated or undefined if id could not be generated
*/
generateDeviceId: function(framework, prefix, lastId) {
var i, m, matched, ref, result, start, x;
if (lastId == null) {
lastId = '';
}
start = 1;
if ((lastId != null) && lastId !== '') {
m = lastId.match(/.*-([0-9]+)$/);
if ((m != null) && m.length === 2) {
start = +m[1] + 1;
}
}
for (x = i = ref = start; i < 1000; x = i += 1) {
result = prefix + "-" + x;
matched = framework.deviceManager.devicesConfig.some(function(element, iterator) {
return element.id === result;
});
if (!matched) {
return result;
}
}
}
};
}
Expand Down

0 comments on commit dd0e146

Please sign in to comment.