node-red-contrib-deconz
Advanced tools
Comparing version 0.7.4 to 0.8.0
280
deconz.js
@@ -35,5 +35,5 @@ var request = require('request'); | ||
if (controller && controller instanceof deConzServerNode) { | ||
controller.getItemsList(function (items) { | ||
controller.getItemsList(function (items, groups) { | ||
if (items) { | ||
res.json(items); | ||
res.json({items:items, groups:groups}); | ||
} else { | ||
@@ -124,2 +124,30 @@ res.status(404).end(); | ||
node.sendLastState = function () { | ||
if (typeof (config.device) == 'string' && config.device.length) { | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
if (deviceMeta) { | ||
devices[node.id] = deviceMeta.uniqueid; | ||
node.meta = deviceMeta; | ||
if (node.config.outputAtStartup) { | ||
setTimeout(function(){ | ||
node.sendState(deviceMeta); | ||
}, 1500); //we need this timeout after restart of node-red (homekit delays) | ||
} | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not set' | ||
}); | ||
} | ||
} | ||
//get server node | ||
@@ -129,28 +157,3 @@ node.server = RED.nodes.getNode(config.server); | ||
if (typeof (config.device) == 'string' && config.device.length) { | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
if (deviceMeta) { | ||
devices[node.id] = deviceMeta.uniqueid; | ||
node.meta = deviceMeta; | ||
if (node.config.outputAtStartup) { | ||
setTimeout(function(){ | ||
node.sendState(deviceMeta); | ||
}, 1500); //we need this timeout after restart of node-red (homekit delays) | ||
} | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not set' | ||
}); | ||
} | ||
node.sendLastState(); | ||
} | ||
@@ -337,60 +340,39 @@ RED.nodes.registerType("deconz-input", deConzItemIn); | ||
console.log('//send data to API'); | ||
// console.log(payload); | ||
//send data to API | ||
node.server.getDeviceMeta(function(deviceMeta){ | ||
if (deviceMeta) { | ||
var url = 'http://'+node.server.ip+':'+node.server.port+'/api/'+node.server.apikey+'/lights/'+deviceMeta.device_id+'/state'; | ||
var post = {}; | ||
if (node.commandType == 'object' || node.commandType == 'homekit') { | ||
post = payload; | ||
} else { | ||
if (command != 'on') post['on'] = true; | ||
if (command == 'bri') post['on'] = payload > 0 ? true : false; | ||
post[command] = payload; | ||
} | ||
if ((/group_/g).test(node.device)) { | ||
var url = 'http://' + node.server.ip + ':' + node.server.port + '/api/' + node.server.apikey + '/groups/' + ((node.device).split('group_').join('')) + '/action'; | ||
var post = {}; | ||
if (node.commandType == 'object' || node.commandType == 'homekit') { | ||
post = payload; | ||
} else { | ||
if (command != 'on') post['on'] = true; | ||
if (command == 'bri') post['on'] = payload > 0 ? true : false; | ||
post[command] = payload; | ||
} | ||
// post["on"] = true; | ||
node.log('Requesting url: '+url); | ||
console.log(post); | ||
request.put({ | ||
url: url, | ||
form: JSON.stringify(post) | ||
}, function(error, response, body){ | ||
if (body) { | ||
var response = JSON.parse(body)[0]; | ||
if ('success' in response) { | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "ok", | ||
}); | ||
} else if ('error' in response) { | ||
response.error.post = post; //add post data | ||
node.warn('deconz-out ERROR: '+response.error.description); | ||
node.warn(response.error); | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "error", | ||
}); | ||
} | ||
node.cleanTimer = setTimeout(function(){ | ||
node.status({}); //clean | ||
}, 3000); | ||
node.postData(url, post); | ||
} else { | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
if (deviceMeta) { | ||
var url = 'http://' + node.server.ip + ':' + node.server.port + '/api/' + node.server.apikey + '/lights/' + deviceMeta.device_id + '/state'; | ||
var post = {}; | ||
if (node.commandType == 'object' || node.commandType == 'homekit') { | ||
post = payload; | ||
} else { | ||
if (command != 'on') post['on'] = true; | ||
if (command == 'bri') post['on'] = payload > 0 ? true : false; | ||
post[command] = payload; | ||
} | ||
}); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
node.postData(url, post); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
} | ||
@@ -400,2 +382,3 @@ | ||
// /api/<apikey>/groups/<id>/action | ||
// /api/<apikey>/lights/<id>/state | ||
@@ -410,4 +393,38 @@ }); | ||
} | ||
node.postData = function(url, post) { | ||
node.log('Requesting url: '+url); | ||
console.log(post); | ||
request.put({ | ||
url: url, | ||
form: JSON.stringify(post) | ||
}, function(error, response, body){ | ||
if (body) { | ||
var response = JSON.parse(body)[0]; | ||
if ('success' in response) { | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "ok", | ||
}); | ||
} else if ('error' in response) { | ||
response.error.post = post; //add post data | ||
node.warn('deconz-out ERROR: '+response.error.description); | ||
node.warn(response.error); | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "error", | ||
}); | ||
} | ||
node.cleanTimer = setTimeout(function(){ | ||
node.status({}); //clean | ||
}, 3000); | ||
} | ||
}); | ||
} | ||
} | ||
RED.nodes.registerType("deconz-output", deConzOut); | ||
@@ -431,2 +448,7 @@ | ||
devices[node.id] = 'event'; | ||
node.sendLastState = function() { | ||
node.status({}); | ||
} | ||
} | ||
@@ -444,2 +466,3 @@ RED.nodes.registerType("deconz-event", deConzItemEvent); | ||
node.items = undefined; | ||
node.groups = undefined; | ||
node.items_list = undefined; | ||
@@ -493,2 +516,7 @@ node.discoverProcess = false; | ||
} | ||
if ("groups" in dataParsed) { | ||
node.groups = dataParsed.groups; | ||
// console.log(node.groups); | ||
} | ||
} | ||
@@ -571,3 +599,3 @@ | ||
node.items_list.push({ | ||
device_name: prop.name + ' : ' + prop.type, | ||
device_name: prop.name + ' (' + prop.type + ')', | ||
uniqueid: prop.uniqueid, | ||
@@ -578,3 +606,3 @@ meta: prop | ||
callback(node.items_list); | ||
callback(node.items_list, node.groups); | ||
return node.items_list; | ||
@@ -588,2 +616,5 @@ }, forceRefresh); | ||
node.connect = function () { | ||
connect(node, {host:node.ip, port:node.ws_port}); | ||
} | ||
// this.heartbeat = function() { | ||
@@ -614,2 +645,3 @@ // clearTimeout(node.pingTimeout); | ||
node.sendState = function (device) { | ||
@@ -659,2 +691,31 @@ if (device.state === undefined) { | ||
node.sendLastState = function() { | ||
if (typeof (config.device) == 'string' && config.device.length) { | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
if (deviceMeta) { | ||
devices[node.id] = deviceMeta.uniqueid; | ||
node.meta = deviceMeta; | ||
if (node.config.outputAtStartup) { | ||
setTimeout(function(){ | ||
node.sendState(deviceMeta); | ||
}, 1500); //we need this timeout after restart of node-red (homekit delays) | ||
} | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not set' | ||
}); | ||
} | ||
} | ||
//get server node | ||
@@ -664,38 +725,3 @@ node.server = RED.nodes.getNode(config.server); | ||
// //check if this device exists | ||
// node.server.getDeviceMeta(function(deviceMeta){ | ||
// if (!deviceMeta) { | ||
// node.status({ | ||
// fill: "red", | ||
// shape: "dot", | ||
// text: 'Device not found' | ||
// }); | ||
// } | ||
// }, config.device); | ||
if (typeof (config.device) == 'string' && config.device.length) { | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
if (deviceMeta) { | ||
devices[node.id] = deviceMeta.uniqueid; | ||
node.meta = deviceMeta; | ||
if (node.config.outputAtStartup) { | ||
setTimeout(function(){ | ||
node.sendState(deviceMeta); | ||
}, 1500); //we need this timeout after restart of node-red (homekit delays) | ||
} | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not set' | ||
}); | ||
} | ||
node.sendLastState(); | ||
} | ||
@@ -715,2 +741,9 @@ RED.nodes.registerType("deconz-battery", deConzItemBattery); | ||
for (var nodeId in devices) { | ||
var node = RED.nodes.getNode(nodeId); | ||
if (typeof(node.sendLastState) == 'function') { | ||
node.sendLastState(); | ||
} | ||
} | ||
}); | ||
@@ -754,2 +787,3 @@ | ||
if (node.type === "deconz-input") { | ||
// console.log(dataParsed); | ||
node.sendState(dataParsed); | ||
@@ -785,2 +819,6 @@ } | ||
} | ||
setTimeout(function(){ | ||
serverNode.connect(); | ||
}, 10000); | ||
}); | ||
@@ -787,0 +825,0 @@ |
@@ -34,3 +34,3 @@ { | ||
}, | ||
"version": "0.7.4" | ||
"version": "0.8.0" | ||
} |
@@ -54,3 +54,3 @@ RED.nodes.registerType('deconz-battery', { | ||
this.device_name = selectedOptions.text(); | ||
this.device_name = selectedOptions.text().replace(/ *\([^)]*\) */g, ""); | ||
} else { | ||
@@ -57,0 +57,0 @@ this.device_name = this.device = null; |
@@ -16,3 +16,4 @@ function deconz_gatewayScanner(nodeItem, selectedItemElementName, options = {}) { | ||
deviceType:false, | ||
batteryFilter:false | ||
batteryFilter:false, | ||
groups:false | ||
}, options); | ||
@@ -42,3 +43,17 @@ | ||
$.each(data, function(index, value) { | ||
if (options.groups && data.groups) { | ||
groupHtml = $('<optgroup/>', { label: "Groups" }); | ||
groupHtml.appendTo(selectedItemElement); | ||
$.each(data.groups, function(index, value) { | ||
$('<option value="group_' + value.id +'">' +value.name +' (lights: '+value.lights.length+')</option>').appendTo(groupHtml); | ||
}); | ||
groupHtml = $('<optgroup/>', { label: "Devices" }); | ||
groupHtml.appendTo(selectedItemElement); | ||
} | ||
$.each(data.items, function(index, value) { | ||
disabled = ''; | ||
@@ -84,2 +99,3 @@ nameSuffix = ''; | ||
// if (optgroup != value.device_type) { | ||
@@ -92,3 +108,5 @@ // groupHtml = $('<optgroup/>', { label: value.device_friendly_name}); | ||
// $('<option value="' + value.topic + '"'+(selected ? 'selected' : '')+'>' + value.control_name + '</option>').appendTo(groupHtml); | ||
$('<option '+disabled+' value="' + value.uniqueid +'">' +value.device_name + (nameSuffix?' ('+nameSuffix+')':'') +'</option>').appendTo(selectedItemElement);//.appendTo(groupHtml?groupHtml:selectedItemElement); | ||
var parentElement = (options.groups && data.groups.length)?groupHtml:selectedItemElement; | ||
$('<option '+disabled+' value="' + value.uniqueid +'">' +value.device_name + (nameSuffix?' ('+nameSuffix+')':'') +'</option>').appendTo(parentElement); | ||
}); | ||
@@ -95,0 +113,0 @@ |
@@ -79,3 +79,3 @@ RED.nodes.registerType('deconz-output', { | ||
deconz_getItemList(node.device, '#node-input-device', {allowEmpty:true, deviceType:'lights'}); | ||
deconz_getItemList(node.device, '#node-input-device', {allowEmpty:true, deviceType:'lights', groups:true}); | ||
@@ -98,3 +98,3 @@ $deviceInput.on('change', function(){ | ||
this.device_name = selectedOptions.text(); | ||
this.device_name = selectedOptions.text().replace(/ *\([^)]*\) */g, ""); | ||
} else { | ||
@@ -101,0 +101,0 @@ this.device_name = this.device = null; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
793543
2764