Skip to content

Commit

Permalink
Rewrote the way messages passed to device nodes
Browse files Browse the repository at this point in the history
Also added a test for if the web site is down
  • Loading branch information
hardillb committed Nov 13, 2016
1 parent 5ea8b97 commit e03c134
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 32 deletions.
6 changes: 4 additions & 2 deletions alexa.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
credentials: {
password: {type:"password"}
},
color: 'gray',
color: '#D3D3D3',
label: function() {
return this.username;
},
Expand Down Expand Up @@ -88,6 +88,8 @@
You will also need to add the "Node-RED" skill to your Echo device</p>
<p>A full set of setup instrucations can be found
<a target="_blank" href="https://alexa-node-red.eu-gb.mybluemix.net/docs">here</a></p>
<p>The <i>Topic</i> field is optional and will be added to any messages sent from
the node.</p>
<p>The node outputs a message with a payload that looks something like this:</p>
<pre>{
"command": "TurnOnRequest",
Expand Down Expand Up @@ -123,7 +125,7 @@
},
outputs: 1,
inputs: 0,
color: 'gray',
color: '#D3D3D3',
icon: 'alexa.png',
label: function() {
return this.name || "Alexa Home";
Expand Down
107 changes: 78 additions & 29 deletions alexa.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,94 @@ module.exports = function(RED) {
this.username = n.username;
this.password = this.credentials.password;

this.users = {};

var node = this;

var options = {
username: node.username,
password: node.password
password: node.password,
servers:[
{
protocol: 'mqtts',
host: 'alexa-node-red.hardill.me.uk',
port: 8883
},
{
protocol: 'mqtt',
host: 'alexa-node-red.hardill.me.uk',
port: 1883
}
]
};

getDevices(node.username, node.password, node.id);

node.client = mqtt.connect('mqtt://alexa-node-red.hardill.me.uk', options);
this.connect = function() {
node.client = mqtt.connect('mqtts://alexa-node-red.hardill.me.uk:8883', options);
node.client.setMaxListeners(0);

node.client.on('connect', function() {
node.setStatus({text:'connected', shape:'dot', fill:'green'});
node.client.removeAllListeners('message');
node.client.subscribe(node.username + '/#');
node.client.on('message', function(topic, message){
var msg = JSON.parse(message.toString());
for (var id in node.users) {
if (node.users.hasOwnProperty(id)){
node.users[id].command(msg);
}
}
});
});

node.client.on('connect', function() {
node.emit('status',{text:'connected', shape:'dot', fill:'green'});
node.client.subscribe(node.username + '/#');
});
node.client.on('offline',function(){
node.setStatus({text: 'disconnected', shape: 'dot', fill:'red'});
});

node.client.on('offline',function(){
node.emit('status',{text: 'disconnected', shape: 'dot', fill:'red'});
});
node.client.on('reconnect', function(){
node.setStatus({text: 'reconnecting', shape: 'ring', fill:'red'});
});
}

node.client.on('reconnect', function(){
node.emit('status',{text: 'reconnecting', shape: 'ring', fill:'red'});
});
this.setStatus = function(status) {
for( var id in node.users) {
if (node.users.hasOwnProperty(id)) {
node.users[id].status(status);
}
}
}

node.client.on('message', function(topic, message){
// console.log(topic);
// console.log(message.toString());
var msg = JSON.parse(message.toString());
node.emit('alexa'+msg.payload.appliance.applianceId, msg);
});
this.register = function(deviceNode) {
node.users[deviceNode.id] = deviceNode;
if (Object.keys(node.users).length === 1) {
//connect
node.connect();
}
};

this.deregister = function(deviceNode, done) {
delete node.users[deviceNode.id];

if (Object.keys(node.users).length === 0) {
//disconnect
if (node.client && node.client.connected) {
node.client.end(done);
} else {
node.client.end();
done();
}
}

done();
};

this.on('close',function(){
node.client.end();
if (node.client && node.client.connected) {
node.client.end();
}
//node.removeAllListeners();
delete devices[node.id];
//delete devices[node.id];
});
};

Expand All @@ -80,7 +133,7 @@ module.exports = function(RED) {

var node = this;

function msgHandler(message){
node.command = function (message){
var msg ={
topic: node.topic || "",
payload: {
Expand Down Expand Up @@ -109,14 +162,10 @@ module.exports = function(RED) {
node.send(msg);
}

node.conf.on('alexa'+node.device, msgHandler);

node.conf.on('status',function(status){
node.status(status);
});
node.conf.register(node);

node.on('close', function(){
node.conf.removeListener(''+node.device,msgHandler);
node.on('close', function(done){
node.conf.deregister(node, done);
});

}
Expand All @@ -135,7 +184,7 @@ module.exports = function(RED) {
password: password
}
}, function(err, res, body){
if (!err) {
if (!err && res.statusCode == 200) {
var devs = JSON.parse(body);
//console.log(devs);
devices[id] = devs;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-alexa-home-skill",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e03c134

Please sign in to comment.