node-red-contrib-deconz
Advanced tools
Comparing version 0.4.0 to 0.5.0
297
deconz.js
@@ -65,22 +65,17 @@ var request = require('request'); | ||
RED.httpAdmin.get(NODE_PATH + 'gwscanner', function (req, res) { | ||
// var ip = require("ip"); | ||
// console.log ( ip.address() ); | ||
RED.httpAdmin.get(NODE_PATH + 'getDeviceMeta', function (req, res) { | ||
var config = req.query; | ||
var controller = RED.nodes.getNode(config.controllerID); | ||
var forceRefresh = config.forceRefresh ? ['1', 'yes', 'true'].includes(config.forceRefresh.toLowerCase()) : false; | ||
var uniqueid = config.uniqueid; | ||
var portscanner = require('portscanner'); | ||
if (controller && controller instanceof deConzServerNode) { | ||
controller.getDeviceMeta(function (meta) { | ||
if (meta) { | ||
res.json(meta); | ||
} else { | ||
res.status(404).end(); | ||
} | ||
}, uniqueid); | ||
} else { | ||
res.status(404).end(); | ||
} | ||
// 127.0.0.1 is the default hostname; not required to provide | ||
portscanner.findAPortNotInUse([80], '127.0.0.1').then(port => { | ||
console.log(`Port ${port} is available!`); | ||
// Now start your service on this port... | ||
}); | ||
}); | ||
//*************** Input Node *************** | ||
@@ -90,5 +85,7 @@ function deConzItemIn(config) { | ||
var node = this; | ||
node.config = config; | ||
node.config = config; | ||
//get server node | ||
node.server = RED.nodes.getNode(config.server); | ||
if (!node.server) return status_no_server(node); | ||
@@ -110,15 +107,4 @@ //check if this device exists | ||
devices[node.id] = deviceMeta.uniqueid; | ||
node.meta = deviceMeta; | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: (config.state in node.meta.state) ? (node.meta.state[config.state] ? node.meta.state[config.state] : '') : "connected", | ||
}); | ||
node.send({ | ||
payload: (config.state in node.meta.state) ? node.meta.state[config.state] : node.meta.state, | ||
meta: deviceMeta, | ||
}); | ||
node.sendState(deviceMeta); | ||
} else { | ||
@@ -140,2 +126,17 @@ node.status({ | ||
this.sendState = function (device) { | ||
//status | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: (node.config.state in device.state) ? device.state[node.config.state] : "connected" | ||
}); | ||
//outputs | ||
node.send([ | ||
{payload: (node.config.state in device.state) ? device.state[node.config.state] : device.state, payload_raw: device.state}, | ||
format_to_homekit(device) | ||
]); | ||
}; | ||
} | ||
@@ -152,3 +153,6 @@ RED.nodes.registerType("deconz-input", deConzItemIn); | ||
node.cleanTimer = null; | ||
//get server node | ||
node.server = RED.nodes.getNode(config.server); | ||
if (!node.server) return status_no_server(node); | ||
@@ -221,3 +225,7 @@ | ||
node.device = config.device; | ||
//get server node | ||
node.server = RED.nodes.getNode(config.server); | ||
if (!node.server) return status_no_server(node); | ||
node.payload = config.payload; | ||
@@ -263,2 +271,4 @@ node.payloadType = config.payloadType; | ||
} | ||
case 'object': | ||
case 'homekit': | ||
case 'msg': | ||
@@ -295,2 +305,3 @@ case 'num': | ||
case 'json': | ||
case 'alert': | ||
@@ -304,2 +315,6 @@ case 'effect': | ||
case 'homekit': | ||
payload = format_from_homekit(message, payload); | ||
break; | ||
case 'str': | ||
@@ -312,2 +327,10 @@ default: { | ||
//empty payload, stop | ||
if (payload === null) { | ||
return false; | ||
} | ||
console.log('//send data to API'); | ||
// console.log(payload); | ||
//send data to API | ||
node.server.getDeviceMeta(function(deviceMeta){ | ||
@@ -317,5 +340,9 @@ if (deviceMeta) { | ||
var post = {}; | ||
if (command != 'on') post['on'] = true; | ||
if (command == 'bri') post['on'] = payload>0?true:false;; | ||
post[command] = payload; | ||
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; | ||
} | ||
@@ -325,2 +352,3 @@ | ||
node.log('Requesting url: '+url); | ||
console.log(post); | ||
@@ -394,6 +422,5 @@ request.put({ | ||
node.apikey = n.apikey; | ||
node.pingTimeout = undefined; | ||
this.discoverDevices = function (callback, forceRefresh = false) { | ||
@@ -485,5 +512,15 @@ if (forceRefresh || node.items === undefined) { | ||
// this.heartbeat = function() { | ||
// clearTimeout(node.pingTimeout); | ||
// | ||
// // Use `WebSocket#terminate()` and not `WebSocket#close()`. Delay should be | ||
// // equal to the interval at which your server sends out pings plus a | ||
// // conservative assumption of the latency. | ||
// node.pingTimeout = setTimeout(() => { | ||
// this.terminate(); | ||
// }, 15000 + 1000); | ||
// } | ||
// this.discoverDevices(node); | ||
connect({host:node.ip, port:node.ws_port}); | ||
connect(node, {host:node.ip, port:node.ws_port}); | ||
} | ||
@@ -494,7 +531,7 @@ | ||
function connect(config) { | ||
function connect(serverNode, config) { | ||
const WebSocket = require('ws'); | ||
const ws = new WebSocket('ws://' + config.host + ':' + config.port); | ||
ws.on('open', function open() { | ||
@@ -506,4 +543,3 @@ console.log('Connected to WebSocket'); | ||
ws.on('error', function(err) { | ||
// need to get both the statusCode and the reason phrase | ||
console.log(err); | ||
serverNode.warn('deCONZ error: '+err); | ||
}); | ||
@@ -517,19 +553,12 @@ | ||
if (dataParsed.uniqueid === item) { | ||
var node = RED.nodes.getNode(nodeId); | ||
if (node && node.type === "deconz-input") { | ||
var serverNode = RED.nodes.getNode(node.server.id); | ||
serverNode.items[dataParsed.uniqueid].state = dataParsed.state; //set last state | ||
//update server items db | ||
var serverNode = RED.nodes.getNode(node.server.id); | ||
serverNode.items[dataParsed.uniqueid].state = dataParsed.state; | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: (node.config.state in dataParsed.state) ? dataParsed.state[node.config.state] : "connected" | ||
}); | ||
node.send({ | ||
payload: (node.config.state in dataParsed.state) ? dataParsed.state[node.config.state] : dataParsed.state, | ||
event: dataParsed | ||
}); | ||
if (node && node.type === "deconz-input") { | ||
node.sendState(dataParsed); | ||
} | ||
@@ -542,7 +571,165 @@ } | ||
ws.on('close', function close() { | ||
console.log('disconnected'); | ||
clearTimeout(serverNode.pingTimeout); | ||
// setTimeout(connect(serverNode, config), 15000); | ||
serverNode.warn('deCONZ WebSocket closed'); | ||
for (var nodeId in devices) { | ||
var node = RED.nodes.getNode(nodeId); | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'disconnected' | ||
}); | ||
} | ||
}); | ||
// ws.on('open', serverNode.heartbeat); | ||
// ws.on('ping', serverNode.heartbeat); | ||
} | ||
function disconnect(config) {} | ||
function format_to_homekit(device) { | ||
var state = device.state; | ||
var msg = {}; | ||
// console.log(device.state); | ||
var characteristic = {}; | ||
if (state !== undefined){ | ||
// if (device.device_type === 'sensors') { | ||
// switch (device.type) { | ||
// case "ZHATemperature": | ||
// characteristic.CurrentTemperature = state.temperature/100; | ||
// break; | ||
// case "ZHAHumidity": | ||
// characteristic.CurrentRelativeHumidity = state.humidity/100; | ||
// break; | ||
// case "ZHALightLevel": | ||
// // characteristic.CurrentRelativeHumidity = state.humidity; | ||
// break; | ||
// case "ZHAPresence": | ||
// // characteristic.CurrentRelativeHumidity = state.humidity; | ||
// break; | ||
// case "ZHAOpenClose": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "ZHASwitch": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPLightlevel": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPHumidity": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPTemperature": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPPresence": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPOpenClose": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPSwitch": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPGenericStatus": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "CLIPGenericFlag": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// case "Daylight": | ||
// // characteristic.ContactSensorState = state.humidity; | ||
// break; | ||
// } | ||
// } | ||
if (state['temperature'] !== undefined){ | ||
characteristic.CurrentTemperature = state.temperature/100; | ||
} | ||
if (state['humidity'] !== undefined){ | ||
characteristic.CurrentRelativeHumidity = state.humidity/100; | ||
} | ||
// if (state['lightlevel'] !== undefined){ | ||
// characteristic.CurrentAmbientLightLevel = state.lightlevel; | ||
// } | ||
if (state['open'] !== undefined){ | ||
characteristic.ContactSensorState = !state.open; | ||
} | ||
if (state['vibration'] !== undefined){ | ||
characteristic.ContactSensorState = !state.vibration; | ||
} | ||
if (state['on'] !== undefined){ | ||
characteristic.On = state.on; | ||
} | ||
if (state['bri'] !== undefined){ | ||
characteristic.Brightness = state.bri/2.55 | ||
} | ||
if (state['hue'] !== undefined){ | ||
characteristic.Hue = state.hue/182; | ||
} | ||
if (state['sat'] !== undefined){ | ||
characteristic.Saturation = state.sat/2.55 | ||
} | ||
if (state['ct'] !== undefined){ | ||
characteristic.ColorTemperature = state.ct; | ||
if (state.ct < 140) characteristic.ColorTemperature = 140; | ||
else if (state.ct > 500) characteristic.ColorTemperature = 500; | ||
} | ||
} | ||
msg.payload = characteristic; | ||
return msg; | ||
} | ||
function format_from_homekit(message, payload) { | ||
if (message.hap.context === undefined) { | ||
return null; | ||
} | ||
var msg = {}; | ||
if (payload.On !== undefined) { | ||
msg['on'] = payload.On; | ||
} else if (payload.Brightness !== undefined) { | ||
msg['bri'] = payload.Brightness*2.55; | ||
msg['on'] = payload.Brightness>0?true:false; | ||
} else if (payload.Hue !== undefined) { | ||
msg['hue'] = payload.Hue*182; | ||
msg['on'] = true; | ||
} else if (payload.Saturation !== undefined) { | ||
msg['sat'] = payload.Saturation*2.55; | ||
msg['on'] = true; | ||
} else if (payload.ColorTemperature !== undefined) { | ||
msg['ct'] = payload.ColorTemperature; | ||
msg['on'] = true; | ||
} | ||
return msg; | ||
} | ||
function status_no_server(node) { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: 'Server node error' | ||
}); | ||
return false; | ||
} | ||
} |
@@ -11,4 +11,4 @@ { | ||
"bootstrap-multiselect": "^0.9.13-1", | ||
"eventsource": "^0.2.1", | ||
"request": "^2.81.0" | ||
"request": "latest", | ||
"portscanner": "latest" | ||
}, | ||
@@ -34,3 +34,3 @@ "description": "deCONZ connectivity nodes for node-red", | ||
}, | ||
"version": "0.4.0" | ||
"version": "0.5.0" | ||
} |
@@ -1,12 +0,6 @@ | ||
function deconz_getDeviceMeta(uniqueid) { | ||
var deServerElement = $('#node-input-server'); | ||
var serverNode = RED.nodes.node(deServerElement.val()); | ||
$.getJSON('/deconz/getDeviceMeta', { | ||
controllerID: serverNode.id, | ||
uniqueid:uniqueid, | ||
forceRefresh: refresh | ||
}).done(function (data, textStatus, jqXHR) { | ||
return data; | ||
}); | ||
function deconz_gatewayScanner(nodeItem, selectedItemElementName, options = {}) { | ||
$.getJSON('/deconz/gwscanner', {}) | ||
.done(function (data, textStatus, jqXHR) { | ||
console.log(data); | ||
}).fail(function (jqXHR, textStatus, errorThrown) {}); | ||
} | ||
@@ -36,5 +30,2 @@ | ||
try { | ||
if (options.allowEmpty) { | ||
@@ -127,4 +118,2 @@ selectedItemElement.html('<option value="">--Select device</option>'); | ||
// Initialize bootstrap multiselect form | ||
@@ -145,3 +134,2 @@ selectedItemElement.multiselect({ | ||
// Initial call to populate item list | ||
@@ -160,4 +148,2 @@ deconz_updateItemList(RED.nodes.node(deServerElement.val()), selectedItemElement, selectedItemElement.val() || nodeItem, false); | ||
function deconz_getItemStateList(nodeItem, selectedItemElementName, options = {}) { | ||
@@ -255,3 +241,3 @@ | ||
function deconz_initSettings(callback) { | ||
function deconz_initSettings(callback, inputSettings) { | ||
var settings = { | ||
@@ -265,4 +251,8 @@ name:false, | ||
$.get("https://dresden-light.appspot.com/discover", function( data ) {}).done(function(data) { | ||
if (!data.length) { | ||
alert( "Can't discover your device, enter settings manually" ); | ||
return false; | ||
} | ||
$.get("https://dresden-light.appspot.com/discover", function( data ) {}).done(function(data) { | ||
settings.name = data[0].name; | ||
@@ -272,2 +262,4 @@ settings.ip = data[0].internalipaddress; | ||
// deconz_getApiKey(callback, settings.ip, settings.port); | ||
$.ajax({ | ||
@@ -324,1 +316,51 @@ type: "POST", | ||
} | ||
function deconz_getApiKey(callback, ip, port) { | ||
$.ajax({ | ||
type: "POST", | ||
dataType: 'json', | ||
url: 'http://'+settings.ip+':'+settings.port+'/api', | ||
data: JSON.stringify({"devicetype":"Node-red"}), | ||
success: function(response){ | ||
var resp = response[0]; | ||
if ('success' in resp) { | ||
settings.apikey = resp.success.username; | ||
$.ajax({ | ||
type: "GET", | ||
dataType: 'json', | ||
url: 'http://'+settings.ip+':'+settings.port+'/api/'+settings.apikey+'/config', | ||
success: function(response){ | ||
if ('websocketport' in response) { | ||
settings.ws_port = response.websocketport; | ||
} | ||
}, | ||
error: function (err) { | ||
var response = (JSON.parse(err.responseText)); | ||
var resp = response[0]; | ||
if ('error' in resp) { | ||
alert(resp.error.description); | ||
} | ||
}, | ||
complete: function() { | ||
callback(settings); | ||
return settings; | ||
} | ||
}); | ||
} | ||
}, | ||
error: function (err) { | ||
var response = (JSON.parse(err.responseText)); | ||
var resp = response[0]; | ||
if ('error' in resp) { | ||
alert(resp.error.description); | ||
} | ||
callback(settings); | ||
return settings; | ||
}, | ||
complete: function() { | ||
} | ||
}); | ||
} |
@@ -24,3 +24,3 @@ RED.nodes.registerType('deconz-input', { | ||
inputs: 0, | ||
outputs: 1, | ||
outputs: 2, | ||
outputLabels: ["event"], | ||
@@ -27,0 +27,0 @@ paletteLabel: 'in', |
@@ -39,3 +39,3 @@ RED.nodes.registerType('deconz-output', { | ||
label: function() { | ||
var label = 'deconz-get'; | ||
var label = 'deconz-out'; | ||
if (this.name) { | ||
@@ -58,6 +58,6 @@ label = this.name; | ||
icon: 'icons/node-red-contrib-deconz/icon-color.png', | ||
options: ['on', 'bri', 'hue', 'sat', 'ct', 'xy', 'alert', 'effect', 'colorloopspeed', 'transitiontime'] | ||
options: ['on', 'bri', 'hue', 'sat', 'ct', 'xy', 'alert', 'effect', 'colorloopspeed', 'transitiontime', 'json', 'homekit'] | ||
}; | ||
$('#node-input-command').typedInput({ | ||
types: [deConzTypes, 'str', 'msg'], | ||
types: [deConzTypes, {value:'homekit',label:'homekit',icon: 'icons/node-red-contrib-deconz/homekit-logo.png',options:['homekit']}, 'str', 'msg', {value:'object',label:'object',options:['json']}], | ||
default: 'msg', | ||
@@ -64,0 +64,0 @@ value: 'topic', |
@@ -33,2 +33,11 @@ RED.nodes.registerType('deconz-server', { | ||
$refreshBtn.on('click', function(){ | ||
// | ||
// deconz_gatewayScanner(); | ||
// return false; | ||
var currentSettings = { | ||
name:$('#node-config-input-name').val(), | ||
ip:$('#node-config-input-ip').val(), | ||
port:$('#node-config-input-port').val(), | ||
}; | ||
deconz_initSettings(function(settings){ | ||
@@ -40,3 +49,3 @@ if (settings.name) $('#node-config-input-name').val(settings.name); | ||
if (settings.ws_port) $('#node-config-input-ws_port').val(settings.ws_port); | ||
}); | ||
}, currentSettings); | ||
@@ -43,0 +52,0 @@ }); |
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
773279
42
2396
+ Addedportscanner@latest
+ Addedasync@2.6.4(transitive)
+ Addedis-number-like@1.0.8(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash.isfinite@3.3.2(transitive)
+ Addedportscanner@2.2.0(transitive)
- Removedeventsource@^0.2.1
- Removedeventsource@0.2.3(transitive)
- Removedoriginal@1.0.2(transitive)
- Removedquerystringify@2.2.0(transitive)
- Removedrequires-port@1.0.0(transitive)
- Removedurl-parse@1.5.10(transitive)
Updatedrequest@latest