Skip to content

Commit

Permalink
Added refresh button to devices
Browse files Browse the repository at this point in the history
  • Loading branch information
hardillb committed Nov 10, 2016
1 parent 1478768 commit b7b35be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
78 changes: 48 additions & 30 deletions alexa.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@
<div class="form-row">
<label for="node-input-device"><i class="icon-tag"></i> Device</label>
<select id="node-input-device">

</select>
<a id="node-input-device-refresh" class="btn"><i class="fa fa-refresh"></i></a>
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tag"></i> Topic</label>
Expand All @@ -98,6 +99,17 @@
"extraInfo": {},
"value": 50
}</pre>
<p>This is a full list of the currently valid commands:</p>
<ul>
<li>TurnOnRequest</li>
<li>TurnOffRequest</li>
<li>SetPercentageRequest</li>
<li>IncrementPercentageRequest</li>
<li>DecrementPercentageRequest</li>
<li>SetTemperatureRequest</li>
<li>IncrementTemperatureRequest</li>
<li>DecrementTemperatureRequest</li>
</ul>
</script>

<script type="text/javascript">
Expand All @@ -121,51 +133,57 @@
},
oneditprepare: function(){
var node = this;

var getDevs = function (account){
$.getJSON('alexa-home/devices/' + account, function(data){
$('#node-input-device').find('option').remove().end();
for (d in data) {
$('<option/>',{
value: parseInt(data[d].applianceId),
text: data[d].friendlyName
}).appendTo('#node-input-device');
}
}).fail(function(){
console.log("problem getting devices");
});
if (node.device) {
$('#node-input-device').val(node.device);
}
}

if (node.conf) {
var account = $('#node-input-conf').val();
//console.log("account: ", account);
if (account != '_ADD_') {
$.getJSON('alexa-home/devices/' + account, function(data){
$('#node-input-device').find('option').remove().end();
for (d in data) {
$('<option/>',{
value: parseInt(data[d].applianceId),
text: data[d].friendlyName
}).appendTo('#node-input-device');
}
}).fail(function(){
console.log("problem getting devices");
});
if (node.device) {
$('#node-input-device').val(node.device);
}
getDevs(account);
}
}

$('#node-input-conf').change(function(){
var account = $('#node-input-conf').val();
//console.log("account changed: ", account);
if (account != '_ADD_') {
$.getJSON('alexa-home/devices/' + account, function(data){
$('#node-input-device').find('option').remove().end();
for (d in data) {
$('<option/>',{
value: parseInt(data[d].applianceId),
text: data[d].friendlyName
}).appendTo('#node-input-device');
}
}).fail(function(){
console.log("problem getting devices");
});
if (node.device) {
$('#node-input-device').val(node.device);
}
getDevs(account);
} else {
//console.log("new account");
$('#node-input-device').find('option').remove().end();
$('#node-input-device').val("");
}
})
});

$('#node-input-device-refresh').click(function(){
$('#node-input-device-refresh').addClass('disabled');
var account = $('#node-input-conf').val();
$.ajax({
url: 'alexa-home/refresh/' + account,
type: 'POST'
}).done(function(data){
setTimeout(function(){
getDevs(account);
$('#node-input-device-refresh').removeClass('disabled');
},3000);
});
});
}
});
</script>
17 changes: 16 additions & 1 deletion alexa.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,28 @@ module.exports = function(RED) {
};

RED.httpAdmin.post('/alexa-home/new-account',function(req,res){
console.log(req.body);
//console.log(req.body);
var username = req.body.user;
var password = req.body.pass;
var id = req.body.id;
getDevices(username,password,id);
});

RED.httpAdmin.post('/alexa-home/refresh/:id',function(req,res){
var id = req.params.id;
var conf = RED.nodes.getNode(id);
if (conf) {
var username = conf.username;
var password = conf.credentials.password;
getDevices(username,password,id);
res.status(200).send();
} else {
//not deployed yet
console.log("Can't refresh until deployed");
res.status(404).send();
}
});

RED.httpAdmin.get('/alexa-home/devices/:id',function(req,res){
if (devices[req.params.id]) {
res.send(devices[req.params.id]);
Expand Down

0 comments on commit b7b35be

Please sign in to comment.