node-red-contrib-deconz
Advanced tools
Comparing version 0.3.1 to 0.4.0
191
deconz.js
@@ -91,5 +91,16 @@ var request = require('request'); | ||
node.config = config; | ||
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 = RED.nodes.getNode(config.server); | ||
node.server.getDeviceMeta(function (deviceMeta) { | ||
@@ -139,2 +150,16 @@ if (deviceMeta) { | ||
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) { | ||
@@ -145,3 +170,3 @@ node.status({}); //clean | ||
clearTimeout(node.cleanTimer); | ||
node.server.getDeviceMeta(function(deviceMeta){ | ||
@@ -199,6 +224,147 @@ if (deviceMeta) { | ||
node.commandType = config.commandType; | ||
node.cleanTimer = null; | ||
//check if this device exists | ||
node.server.getDeviceMeta(function(deviceMeta){ | ||
if (!deviceMeta) { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
this.on('input', function (message) { | ||
}); | ||
if (typeof(config.device) == 'string' && config.device.length) { | ||
node.status({}); //clean | ||
this.on('input', function (message) { | ||
clearTimeout(node.cleanTimer); | ||
var payload; | ||
switch (node.payloadType) { | ||
case 'flow': | ||
case 'global': { | ||
RED.util.evaluateNodeProperty(node.payload, node.payloadType, this, message, function (error, result) { | ||
if (error) { | ||
node.error(error, message); | ||
} else { | ||
payload = result; | ||
} | ||
}); | ||
break; | ||
} | ||
case 'date': { | ||
payload = Date.now(); | ||
break; | ||
} | ||
case 'msg': | ||
case 'num': | ||
case 'str': | ||
default: { | ||
payload = message[node.payload]; | ||
break; | ||
} | ||
} | ||
var command; | ||
switch (node.commandType) { | ||
case 'msg': { | ||
command = message[node.command]; | ||
break; | ||
} | ||
case 'deconz_cmd': | ||
command = node.command; | ||
switch (command) { | ||
case 'on': | ||
payload = payload && payload != '0'?true:false; | ||
break; | ||
case 'bri': | ||
case 'hue': | ||
case 'sat': | ||
case 'ct': | ||
case 'colorloopspeed': | ||
case 'transitiontime': | ||
payload = parseInt(payload); | ||
break; | ||
case 'alert': | ||
case 'effect': | ||
default: { | ||
break; | ||
} | ||
} | ||
break; | ||
case 'str': | ||
default: { | ||
command = node.command; | ||
break; | ||
} | ||
} | ||
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 (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); | ||
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); | ||
} | ||
}); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not found' | ||
}); | ||
} | ||
}, config.device); | ||
// /api/<apikey>/lights/<id>/state | ||
}); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Device not set' | ||
}); | ||
} | ||
} | ||
@@ -246,10 +412,19 @@ | ||
if (dataParsed) { | ||
dataParsed = dataParsed.sensors; | ||
for (var index in dataParsed) { | ||
var prop = dataParsed[index]; | ||
for (var index in dataParsed.sensors) { | ||
var prop = dataParsed.sensors[index]; | ||
prop.device_type = 'sensors'; | ||
prop.device_id = parseInt(index); | ||
node.items[prop.uniqueid] = prop; | ||
} | ||
for (var index in dataParsed.lights) { | ||
var prop = dataParsed.lights[index]; | ||
prop.device_type = 'lights'; | ||
prop.device_id = parseInt(index); | ||
node.items[prop.uniqueid] = prop; | ||
} | ||
} | ||
// console.log(node.items); | ||
callback(node.items); | ||
@@ -256,0 +431,0 @@ return node.items; |
@@ -33,3 +33,3 @@ { | ||
}, | ||
"version": "0.3.1" | ||
"version": "0.4.0" | ||
} |
@@ -20,3 +20,4 @@ function deconz_getDeviceMeta(uniqueid) { | ||
refresh:false, | ||
allowEmpty:false | ||
allowEmpty:false, | ||
deviceType:false | ||
}, options); | ||
@@ -53,2 +54,5 @@ | ||
if (options.deviceType && options.deviceType != value.meta.device_type) { | ||
return true; | ||
} | ||
@@ -250,10 +254,15 @@ // selected = typeof(itemName) == 'string' && value.topic == itemName; | ||
function deconz_initSettings(callback) { | ||
var settings = { | ||
name:false, | ||
ip:false, | ||
port:false, | ||
apikey:false, | ||
ws_port:false | ||
}; | ||
$.get("https://dresden-light.appspot.com/discover", function( data ) {}).done(function(data) { | ||
var settings = { | ||
name:data[0].name, | ||
ip:data[0].internalipaddress, | ||
port:data[0].internalport, | ||
apikey:false, | ||
ws_port:false | ||
}; | ||
settings.name = data[0].name; | ||
settings.ip = data[0].internalipaddress; | ||
settings.port = data[0].internalport; | ||
@@ -260,0 +269,0 @@ $.ajax({ |
@@ -17,7 +17,10 @@ RED.nodes.registerType('deconz-output', { | ||
}, | ||
device_name: { | ||
value: null | ||
}, | ||
command: { | ||
value: '/on', | ||
value: 'on', | ||
}, | ||
commandType: { | ||
value: 'wb_cmd', | ||
value: 'deconz_cmd', | ||
}, | ||
@@ -37,8 +40,11 @@ payload: { | ||
label: function() { | ||
var label = 'deconz-output'; | ||
var label = 'deconz-get'; | ||
if (this.name) { | ||
label = this.name; | ||
} else if (typeof(this.device_name) == 'string' && this.device_name.length) { | ||
label = this.device_name; | ||
} else if (typeof(this.device) == 'string' && this.device.length) { | ||
label = this.device; | ||
} | ||
return label; | ||
@@ -49,10 +55,10 @@ }, | ||
var WbTypes = { | ||
value: 'wb_cmd', | ||
label: 'WB', | ||
icon: 'icons/node-red-contrib-deconz/deconz-color.png', | ||
options: ['/on'] | ||
var deConzTypes = { | ||
value: 'deconz_cmd', | ||
label: 'deCONZ', | ||
icon: 'icons/node-red-contrib-deconz/icon-color.png', | ||
options: ['on', 'bri', 'hue', 'sat', 'ct', 'xy', 'alert', 'effect', 'colorloopspeed', 'transitiontime'] | ||
}; | ||
$('#node-input-command').typedInput({ | ||
types: [WbTypes, 'str', 'msg'], | ||
types: [deConzTypes, 'str', 'msg'], | ||
default: 'msg', | ||
@@ -72,7 +78,29 @@ value: 'topic', | ||
setTimeout(function(){ | ||
deconz_getItemList(node.device, '#node-input-device', {disableReadonly:true, allowEmpty:true}); | ||
var $deviceInput = $('#node-input-device'); | ||
deconz_getItemList(node.device, '#node-input-device', {allowEmpty:true, deviceType:'lights'}); | ||
$deviceInput.on('change', function(){ | ||
deconz_getItemStateList(0, '#node-input-state'); | ||
}); | ||
setTimeout(function () { | ||
deconz_getItemStateList(node.state, '#node-input-state'); | ||
},100); | ||
}, 100); //we need small timeout, too fire change event for server select | ||
}, | ||
oneditsave: function () { | ||
var selectedOptions = $('#node-input-device option:selected'); | ||
if (selectedOptions) { | ||
this.device = selectedOptions.map(function () { | ||
return $(this).val(); | ||
}); | ||
this.device_name = selectedOptions.text(); | ||
} else { | ||
this.device_name = this.device = null; | ||
} | ||
} | ||
}); |
@@ -34,5 +34,5 @@ RED.nodes.registerType('deconz-server', { | ||
deconz_initSettings(function(settings){ | ||
$('#node-config-input-name').val(settings.name); | ||
$('#node-config-input-ip').val(settings.ip); | ||
$('#node-config-input-port').val(settings.port); | ||
if (settings.name) $('#node-config-input-name').val(settings.name); | ||
if (settings.ip) $('#node-config-input-ip').val(settings.ip); | ||
if (settings.port) $('#node-config-input-port').val(settings.port); | ||
if (settings.apikey) $('#node-config-input-apikey').val(settings.apikey); | ||
@@ -39,0 +39,0 @@ if (settings.ws_port) $('#node-config-input-ws_port').val(settings.ws_port); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
761579
41
2185