Skip to content

Commit

Permalink
Merge pull request #43 from henols/master
Browse files Browse the repository at this point in the history
Deals with csv and json payload in a smarter way
  • Loading branch information
knolleary committed Apr 16, 2014
2 parents f18b14c + 9808e8a commit afa46ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
17 changes: 5 additions & 12 deletions io/emoncms/88-emoncms.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,20 @@
<input type="text" id="node-input-emonServer">
</div>
<div class="form-row">
<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>
<label for="node-input-nodegroup"><i class="icon-tag"></i> Node</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-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>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>
<p>Emoncms post.</p>
<p>The <b>msg.payload</b> can contain either a comma separated list of name value pairs ex. name:value,... or a comma separated list of values ex. 1,2,.. .
<p>If Node is left blank <b>msg.nodegrpup</b> will used.</p>
<p>Insertion time can be manipulated by setting <b>msg.time</b>.</p>
</script>

<script type="text/javascript">
Expand All @@ -48,7 +43,6 @@
defaults: {
name: {value:"Emoncms"},
emonServer: {type:"emoncms-server", required:true},
topic: {value:""},
nodegroup: {value:""}
},
inputs:1,
Expand Down Expand Up @@ -85,7 +79,6 @@
category: 'config',
defaults: {
server: {value:"http://localhost",required:true},
// apikey: {value:"",required:true},
name: {value:""}
},
label: function() {
Expand Down
16 changes: 11 additions & 5 deletions io/emoncms/88-emoncms.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,24 @@ function Emoncms(n) {
this.baseurl = sc.server;
this.apikey = sc.apikey;

this.topic = n.topic ||"";
this.nodegroup = n.nodegroup || "";
var node = this;
if (this.baseurl.substring(0,5) === "https") { var http = require("https"); }
else { var http = require("http"); }
this.on("input", function(msg) {

var topic = this.topic || msg.topic;
this.url = this.baseurl + '/input/post.json?';
if(msg.payload.indexOf(':') > -1){
this.url += 'json={' + msg.payload + '}';
} else {
this.url += 'csv='+msg.payload;
}
this.url += '&apikey='+this.apikey;
var nodegroup = this.nodegroup || msg.nodegroup;
this.url = this.baseurl + '/input/post.json?json={' + topic + ':' + msg.payload+'}&apikey='+this.apikey;
if(nodegroup != ""){
this.url += '&node='+nodegroup;
this.url += '&node=' + nodegroup;
}
if(typeof msg.time !== 'undefined'){
this.url += '&time=' + msg.time;
}
node.log("[emoncms] "+this.url);
http.get(this.url, function(res) {
Expand Down

0 comments on commit afa46ef

Please sign in to comment.