Skip to content

Commit

Permalink
Merge pull request #20 from mabunixda/dev
Browse files Browse the repository at this point in the history
fix ssdp location announcement
  • Loading branch information
mabunixda authored Jun 10, 2019
2 parents d033dc5 + 4c592ac commit 0c5d9db
Show file tree
Hide file tree
Showing 9 changed files with 3,890 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ cache:

before_install:
- npm install -g npm-check
- npm install -g mocha

install:
- npm install

script:
- grep console alexa/* | grep -v "no controller id found" || echo "no console logging found"
- npm-check
- echo "tests are still missing :("
- npm test

3 changes: 3 additions & 0 deletions alexa/alexa-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"use strict";

const alexa_controller = require('./alexa-home-controller');

module.exports = {
controllerNode: undefined,
controllerDynamic: true,
isDebug: process.env.DEBUG && process.env.DEBUG.indexOf("node-red-contrib-alexa-home") > 0 || false,
bri_default: process.env.BRI_DEFAULT || 126,
prefixUUID: "f6543a06-da50-11ba-8d8f-",
Expand Down
10 changes: 4 additions & 6 deletions alexa/alexa-home-controller.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<script type="text/x-red" data-template-name="alexa-home-controller">
<div class="form-row">
<label for="node-config-input-name">
<i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
No Configuration needed!
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('alexa-home-controller', {
category: 'alexa-home',
color: '#3FADB5',
defaults: {
name: {
value: ""
controllername: {
value: "Alexa Controller"
}
},
inputs: 0,
outputs: 0,
icon: "alexa-home.png",
label: function () {
return this.name || "Alexa Controller";
return this.controllername;
}
});
</script>
72 changes: 46 additions & 26 deletions alexa/alexa-home-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (RED) {

var Mustache = require('mustache'),
fs = require('fs'),
alexa_home = require('./alexa-helper.js');
alexa_home = require('./alexa-helper');

function findControllerNode() {

Expand All @@ -18,11 +18,13 @@ module.exports = function (RED) {

function getControllerNode(req, res) {

var node = findControllerNode();

if (alexa_home.controllerNode) {
return alexa_home.controllerNode;
}
var node = findControllerNode();
if (node != undefined) {
alexa_home.controllerDynamic = false;
alexa_home.controllerNode = node;
return node;
}
Expand Down Expand Up @@ -117,10 +119,21 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);

var node = this;
node.name = config.controllername;
node._commands = new Map();
alexa_home.controllerNode = node;

node.startSSDP(node.getHttpAddress());
var ssdpURIs = node.getHttpAddress();
var ssdpURI = undefined
if (ssdpURIs.length == 0) {
RED.log.error("Could not determine ssdp uri");
} else {
ssdpURI = ssdpURIs[0];
if (ssdpURIs.length > 1) {
RED.log.warn("More than 1 URI available - using 1st: " + ssdpURIs);
}
}
node.startSSDP(ssdpURI);

node.on('close', function (removed, doneFunction) {
node.server.stop()
Expand All @@ -132,37 +145,42 @@ module.exports = function (RED) {
});
node.setConnectionStatusMsg("green", "ok");

RED.log.info("Assigning alexa-home nodes to this controller");

RED.nodes.eachNode(function (n) {
if (n.type == "alexa-home") {
var x = RED.nodes.getNode(n.id);
if (x) {
if (x) {
node.registerCommand(x);
}
}
});
}

AlexaHomeController.prototype.getHttpAddress = function () {
var node = this;
var uiPort = RED.settings.uiPort || 1880;
if (process.env.ALEXA_IP) {
var publicIP = process.env.ALEXA_IP;
if (publicIP.indexOf(":") > 0) {
RED.log.debug("httpAddress using env.ALEXA_IP: " + publicIP);
RED.log.debug(this.name + " - httpAddress using env.ALEXA_IP: " + publicIP);
return publicIP;
}
RED.log.debug("httpAddress using env.ALEXA_IP and node-red uiPort: " + publicIP + ":" + RED.settings.uiPort);
return publicIP + ":" + RED.settings.uiPort;
RED.log.debug(this.name + " - httpAddress using env.ALEXA_IP and node-red uiPort: " + publicIP + ":" + uiPort);
return publicIP + ":" + uiPort;
}
if (RED.settings.uiHost && RED.settings.uiHost != "0.0.0.0") {
RED.log.debug("httpAddress using node-red settings: " + RED.settings.uiHost + ":" + RED.settings.uiPort);
return RED.settings.uiHost + ":" + RED.settings.uiPort;
RED.log.debug(this.name + " - httpAddress using node-red settings: " + RED.settings.uiHost + ":" + uiPort);
return RED.settings.uiHost + ":" +uiPort;
}
RED.log.debug("Determining httpAddress...")
RED.log.debug(node.name + " - Determining httpAddress...")
var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
var keys = Object.keys(ifaces);
var ssdpAddresses = [];
for (let k in keys) {
var alias = 0;

var ifname = keys[k];
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
Expand All @@ -172,18 +190,20 @@ module.exports = function (RED) {
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
if (alexa_home.isDebug) {
RED.log.debug(ifname + ':' + alias, iface.address);
RED.log.debug(node.name + " - " + ifname + ':' + alias, iface.address);
}
} else {
if (alexa_home.isDebug) {
RED.log.debug(ifname, iface.address);
RED.log.debug(node.name + " - " + ifname + "-" + iface.address);
}
RED.log.debug("httpAddress using interface address: " + iface.address + ":" + RED.settings.uiPort);
return iface.address + ":" + RED.settings.uiPort;
RED.log.debug(node.name + " - httpAddress using interface address: " + iface.address + ":" + uiPort);
ssdpAddresses.push(iface.address + ":" + uiPort);
}
++alias;
});
});
}

return ssdpAddresses;
}

AlexaHomeController.prototype.getDevices = function () {
Expand All @@ -209,7 +229,7 @@ module.exports = function (RED) {
AlexaHomeController.prototype.startSSDP = function (endpoint) {

var node = this;
node.log("alexa-home - Starting SSDP");
node.log(this.name + " - alexa-home - Starting SSDP");
var hueuuid = alexa_home.formatHueBridgeUUID(node.id);
const ssdp = require("node-ssdp").Server;
node.server = new ssdp({
Expand All @@ -220,11 +240,11 @@ module.exports = function (RED) {
node.server.reuseAddr = true;
node.server.addUSN('urn:schemas-upnp-org:device:basic:1')
node.server.start();
RED.log.debug("announcing: " + "http://" + endpoint + alexa_home.nodeSubPath + "/setup.xml");
RED.log.debug(this.name + " - announcing: " + "http://" + endpoint + alexa_home.nodeSubPath + "/setup.xml");
}

AlexaHomeController.prototype.handleSetup = function (request, response) {
RED.log.debug("Handling setup request");
RED.log.debug(this.name + " - Handling setup request");
var template = fs.readFileSync(__dirname + '/templates/setup.xml', 'utf8').toString();
var data = {
uuid: alexa_home.formatHueBridgeUUID(this.id),
Expand All @@ -239,7 +259,7 @@ module.exports = function (RED) {
}

AlexaHomeController.prototype.handleRegistration = function (request, response) {
RED.log.debug("Handling registration request");
RED.log.debug(this.name + " - Handling registration request");
var template = fs.readFileSync(__dirname + '/templates/registration.json', 'utf8').toString();
var data = {
username: alexa_home.HUE_USERNAME
Expand All @@ -255,7 +275,7 @@ module.exports = function (RED) {
}

AlexaHomeController.prototype.handleItemList = function (request, response) {
RED.log.debug("handling api item list request: " + request.params.itemType);
RED.log.debug(this.name + " - handling api item list request: " + request.params.itemType);
if (request.params.itemType !== "lights") {
response.status(404).end("");
return;
Expand All @@ -266,7 +286,7 @@ module.exports = function (RED) {
date: new Date().toISOString().split('.').shift()
}
var content = Mustache.render(template, data);
RED.log.debug("Sending all " + this._commands.size + " " + request.params.itemType + " json to " + request.connection.remoteAddress);
RED.log.debug(this.name + " - Sending all " + this._commands.size + " " + request.params.itemType + " json to " + request.connection.remoteAddress);
this.setConnectionStatusMsg("yellow", request.params.itemType + " list requested: " + this._commands.size);
response.set({
'Content-Type': 'application/json'
Expand All @@ -275,7 +295,7 @@ module.exports = function (RED) {
}

AlexaHomeController.prototype.handleApiCall = function (request, response) {
RED.log.debug("Hanlding API listing request");
RED.log.debug(this.name + " - Hanlding API listing request");
var responseTemplate = fs.readFileSync(__dirname + '/templates/response.json', 'utf8').toString();
var lights = fs.readFileSync(__dirname + '/templates/items/list.json', 'utf8').toString();
var data = {
Expand All @@ -287,7 +307,7 @@ module.exports = function (RED) {
var content = Mustache.render(responseTemplate, data, {
itemsTemplate: lights
});
RED.log.debug("Sending all " + this._commands.size + " lights information to " + request.connection.remoteAddress);
RED.log.debug(this.name + " - Sending all " + this._commands.size + " lights information to " + request.connection.remoteAddress);
this.setConnectionStatusMsg("yellow", "api requested");
response.set({
'Content-Type': 'application/json'
Expand Down
10 changes: 6 additions & 4 deletions alexa/alexa-home.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);

var node = this;
node.control = config.control;
node.name = config.devicename;
node.control = config.control;
if (config.devicetype) {
node.devicetype = config.devicetype;
} else {
Expand All @@ -31,9 +31,11 @@ module.exports = function (RED) {
done();
})

if (alexa_home.controllerNode) {
alexa_home.controllerNode.registerCommand(node);
return;
var controller = alexa_home.controllerNode;

if (controller) {
controller.registerCommand(node);
return;
}
RED.log.debug("No Alexa Home Controller available")
node.setConnectionStatusMsg("red", "No Alexa Home Controller available");
Expand Down
Loading

0 comments on commit 0c5d9db

Please sign in to comment.