node-red-contrib-denon
Advanced tools
Comparing version 0.1.0 to 0.1.1
67
denon.js
@@ -26,4 +26,4 @@ /** | ||
/** | ||
* Initialize an denonjs socket, calling the handler function | ||
* when successfully connected, passing it the denonjs connection | ||
* Initialize an denon_telnet socket, calling the handler function | ||
* when successfully connected, passing it the denon_telnet connection | ||
*/ | ||
@@ -37,8 +37,12 @@ this.initializeDenonConnection = function (handler) { | ||
if (handler && (typeof handler === 'function')) { | ||
if (node.denon.connection) | ||
if (node.denon.connection && node.denon.connected) | ||
handler(node.denon); | ||
else | ||
else { | ||
if (node.denon.connection && !node.denon.connected) | ||
node.denon.connect(); | ||
node.denon.on('connected', function () { | ||
handler(node.denon); | ||
}); | ||
} | ||
} | ||
@@ -53,2 +57,3 @@ return node.denon; | ||
}); | ||
node.denon.connect(); | ||
if (handler && (typeof handler === 'function')) { | ||
@@ -59,3 +64,2 @@ node.denon.on('connected', function () { | ||
} | ||
node.denon.connect(); | ||
DEBUG && RED.comms.publish("debug", { | ||
@@ -69,3 +73,3 @@ name: node.name, | ||
this.on("close", function () { | ||
node.log('disconnecting from denonjs server at ' + config.host + ':' + config.port); | ||
node.log('disconnecting from denon device at ' + config.host + ':' + config.port); | ||
node.denon && node.denon.disconnect && node.denon.disconnect(); | ||
@@ -101,2 +105,3 @@ node.denon = null; | ||
var payload = msg.payload; | ||
console.log('------------- ' + JSON.stringify(msg) + ''); | ||
if (typeof(msg.payload) === "object") { | ||
@@ -120,5 +125,5 @@ payload = msg.payload; | ||
if (msg.topic) { | ||
if(payload.value===null || payload.value===undefined) | ||
if (payload.value === null || payload.value === undefined) | ||
payload.value = payload.cmd; | ||
payload.cmd = msg.topic.toString(); | ||
payload = {cmd: msg.topic.toString(), value: payload.value}; | ||
} | ||
@@ -184,5 +189,2 @@ | ||
DEBUG && RED.comms.publish("debug", {name: node.name, msg: 'send data[' + JSON.stringify(data) + ']'}); | ||
//node.log('send data[' + data + ']'); | ||
// init a new one-off connection from the effectively singleton DenonController | ||
// there seems to be no way to reuse the outgoing conn in adreek/node-denonjs | ||
controllerNode.initializeDenonConnection(function (fsm) { | ||
@@ -192,8 +194,25 @@ try { | ||
data.cmd = data.cmd || data.method; | ||
data.args = data.args || data.params; | ||
fsm.connection.run(data.cmd, data.args).then(function () { | ||
callback && callback(err); | ||
}, function (err) { | ||
callback && callback(err); | ||
}); | ||
data.value = data.value || data.params; | ||
switch (data.cmd.toLowerCase()) { | ||
case 'setvolumedb': | ||
fsm.connection.setVolumeDb(parseFloat(data.value), function (error, response) { | ||
if (!callback) | ||
return; | ||
if (error) | ||
callback && callback(error, response); | ||
else | ||
callback(response); | ||
}); | ||
break; | ||
default: | ||
fsm.connection.send(data.cmd, function (error, response) { | ||
if (!callback) | ||
return; | ||
if (error) | ||
callback && callback(error, response); | ||
else | ||
callback(response); | ||
}); | ||
} | ||
} | ||
@@ -242,14 +261,2 @@ catch (err) { | ||
function bindNotificationListeners(connection) { | ||
function getListenerForNotification(notification) { | ||
return function (data) { | ||
node.receiveNotification(notification, data); | ||
} | ||
} | ||
Object.keys(connection.schema.schema.notifications).forEach(function (method) { | ||
connection.schema.schema.notifications[method](getListenerForNotification(method)); | ||
}); | ||
} | ||
node.receiveNotification = function (notification, data) { | ||
@@ -270,4 +277,2 @@ DEBUG && RED.comms.publish("debug", { | ||
controllerNode.initializeDenonConnection(function (fsm) { | ||
bindNotificationListeners(fsm.connection); | ||
if (fsm.connected) | ||
@@ -274,0 +279,0 @@ nodeStatusConnected(); |
@@ -11,3 +11,3 @@ ~function (undefined) { | ||
PING_TIMEOUT: options.pingTimeout || options['ping-timeout'] || 5000, | ||
PING_INTERVAL: options.pingInterval || options['ping-interval'] || 10000, | ||
PING_INTERVAL: options.pingInterval || options['ping-interval'] || 20000, | ||
RECONNECT_INTERVAL: options.reconnectInterval || options['reconnect-interval'] || 5000, | ||
@@ -144,3 +144,3 @@ initialize: function (options) { | ||
this.emit('disconnected'); | ||
this.connection && this.connection.close(); | ||
this.connection && this.connection.getConnection().destroy(); | ||
this.connection = null; | ||
@@ -147,0 +147,0 @@ this.transition('uninitialized'); |
{ | ||
"name": "node-red-contrib-denon", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Node-RED nodes for communicating with a Denon AVR.", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
707416
9
403