mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Merge pull request #29 from henols/master
Emoncms update gui hidden API key
This commit is contained in:
commit
66598a66b1
@ -23,21 +23,22 @@
|
||||
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
|
||||
<input type="text" id="node-input-topic" placeholder="">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-nodegroup"><i class="icon-tag"></i> Node Group</label>
|
||||
<input type="text" id="node-input-nodegroup" placeholder="">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Emoncms">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-nodegroup"><i class="icon-tag"></i> Node</label>
|
||||
<input type="text" id="node-input-nodegroup" placeholder="">
|
||||
</div>
|
||||
<div class="form-tips">If Topic is left blank the output Topic is the same as the input Topic.</div>
|
||||
<div class="form-tips">Topic is not mandatory, if Topic is left blank <b>msg.topic</b> will used. Topic overrides <b>msg.topic</b></br>
|
||||
Node Group (numeric) is not mandatory, if Node Group is left blank <b>msg.nodegrpup</b> will used. Node Group overrides <b>msg.nodegroup</b></div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="emoncms">
|
||||
<p>Performs post to Emoncms.</p>
|
||||
<p>The Topic is not mandatory, if Topic is left blank <b>msg.topic</b> will used.</p>
|
||||
<p>The Node is not mandatory, (numberic)</p>
|
||||
<p>Topic is not mandatory, if Topic is left blank <b>msg.topic</b> will used. Topic overrides <b>msg.topic</b></p>
|
||||
<p>Node Group (numeric) is not mandatory, if Node Group is left blank <b>msg.nodegrpup</b> will used. Node overrides <b>msg.nodegrpup</b></p>
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
@ -84,11 +85,36 @@
|
||||
category: 'config',
|
||||
defaults: {
|
||||
server: {value:"http://localhost",required:true},
|
||||
apikey: {value:"",required:true},
|
||||
// apikey: {value:"",required:true},
|
||||
name: {value:""}
|
||||
},
|
||||
label: function() {
|
||||
return this.name||this.server;
|
||||
},
|
||||
oneditprepare: function() {
|
||||
$.getJSON('emoncms-server/'+this.id,function(data) {
|
||||
if (data.apikey) {
|
||||
$('#node-config-input-apikey').val(data.apikey);
|
||||
}
|
||||
});
|
||||
},
|
||||
oneditsave: function() {
|
||||
var newApikey = $('#node-config-input-apikey').val();
|
||||
var credentials = {};
|
||||
credentials.apikey = newApikey;
|
||||
$.ajax({
|
||||
url: 'emoncms-server/'+this.id,
|
||||
type: 'POST',
|
||||
data: credentials,
|
||||
success:function(result){}
|
||||
});
|
||||
},
|
||||
ondelete: function() {
|
||||
$.ajax({
|
||||
url: 'emoncms-server/'+this.id,
|
||||
type: 'DELETE',
|
||||
success: function(result) {}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
@ -19,11 +19,50 @@ var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
function EmoncmsServerNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.server = n.server;
|
||||
this.apikey = n.apikey;
|
||||
this.name = n.name;
|
||||
var credentials = RED.nodes.getCredentials(n.id);
|
||||
if (credentials) {
|
||||
this.apikey = credentials.apikey;
|
||||
}
|
||||
|
||||
}
|
||||
RED.nodes.registerType("emoncms-server",EmoncmsServerNode);
|
||||
|
||||
var querystring = require('querystring');
|
||||
|
||||
RED.app.get('/emoncms-server/:id',function(req,res) {
|
||||
var credentials = RED.nodes.getCredentials(req.params.id);
|
||||
if (credentials) {
|
||||
res.send(JSON.stringify({apikey:credentials.apikey}));
|
||||
} else {
|
||||
res.send(JSON.stringify({}));
|
||||
}
|
||||
});
|
||||
|
||||
RED.app.delete('/emoncms-server/:id',function(req,res) {
|
||||
RED.nodes.deleteCredentials(req.params.id);
|
||||
res.send(200);
|
||||
});
|
||||
|
||||
RED.app.post('/emoncms-server/:id',function(req,res) {
|
||||
|
||||
var body = "";
|
||||
req.on('data', function(chunk) {
|
||||
body+=chunk;
|
||||
});
|
||||
req.on('end', function(){
|
||||
var newCreds = querystring.parse(body);
|
||||
var credentials = RED.nodes.getCredentials(req.params.id)||{};
|
||||
if (newCreds.apikey == null || newCreds.apikey == "") {
|
||||
delete credentials.apikey;
|
||||
} else {
|
||||
credentials.apikey = newCreds.apikey;
|
||||
}
|
||||
RED.nodes.addCredentials(req.params.id,credentials);
|
||||
res.send(200);
|
||||
});
|
||||
});
|
||||
|
||||
function Emoncms(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.emonServer = n.emonServer;
|
||||
@ -40,9 +79,10 @@ function Emoncms(n) {
|
||||
this.on("input", function(msg) {
|
||||
|
||||
var topic = this.topic || msg.topic;
|
||||
var nodegroup = this.nodegroup || msg.nodegroup;
|
||||
this.url = this.baseurl + '/input/post.json?json={' + topic + ':' + msg.payload+'}&apikey='+this.apikey;
|
||||
if(this.nodegroup != ""){
|
||||
this.url += '&node='+this.nodegroup;
|
||||
if(nodegroup != ""){
|
||||
this.url += '&node='+nodegroup;
|
||||
}
|
||||
node.log("[emoncms] "+this.url);
|
||||
http.get(this.url, function(res) {
|
||||
|
Loading…
Reference in New Issue
Block a user