Skip to content

Commit

Permalink
Deals with csv and json payload in a smarter way and manipulation
Browse files Browse the repository at this point in the history
insertion time
  • Loading branch information
henols committed Mar 20, 2014
1 parent 8a60d1f commit e6f90f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
36 changes: 9 additions & 27 deletions io/emoncms/88-emoncms.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,31 @@
<input type="text" id="node-input-emonServer">
</div>
<div class="form-row">
<label for="node-input-payloadType"><i class="icon-envelope"></i> Payload</label>
<select id="node-input-payloadType" style="width:125px !important">
<option value="csv">csv</option>
<option value="json">json</option>
</select>
</div>
<div class="form-row" id="node-input-row-topic">
<label for="node-input-topic"><i class="icon-tasks"></i> Key</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">If Payload is set to csv, <b>msg.payload</b> can be a comma separated values and the key will
be automatically generated by Emoncms, if Payload is set to json, <b>msg.payload</b> can only be a single value and
the key value can be manually or programmatically set.</br></br>
Key is not mandatory, if Key is left blank <b>msg.topic</b> will used. Key overrides <b>msg.topic</b></br></br>
<div class="form-tips">If <b>msg.payload</b> holds comma separated values (csv), Key and <b>msg.topic</b> is ignored and the key will
be automatically generated by Emoncms. If <b>msg.payload</b> holds a single value and Key and <b>msg.topic</b> is not specified, <b>msg.payload</b>
will be treated as a csv otherwise it will be treated as a json payload and the key value can be manually or programmatically set.</br></br>
Key is not mandatory, if Key is left blank <b>msg.topic</b> will used. Key overrides <b>msg.topic</b>, it will be ignored if <b>msg.payload</b> is holding csv</br></br>
Node (numeric) is not mandatory, if Node is left blank <b>msg.nodegrpup</b> will used. Node over rides <b>msg.nodegroup</b>.
</div>
</script>

<script type="text/x-red" data-help-name="emoncms">
<p>Performs post to Emoncms.</p>
<p>If Payload is set to csv, <b>msg.payload</b> can contain comma separated values and Key and <b>msg.topic</b> are ignored,
if Payload is set to json, <b>msg.payload</b> can only be a single value and Key or <b>msg.topic</b> must be specified</p>
<p>If Key is left blank <b>msg.topic</b> will used.</p>
<p>If <b>msg.payload</b> holds comma separated values (csv), Key and <b>msg.topic</b> is ignored.
If <b>msg.payload</b> holds a single value and Key and <b>msg.topic</b> is not specified, <b>msg.payload</b>
will be treated as a csv otherwise it will be treated as a json payload</p>
<p>If Key is left blank <b>msg.topic</b> will used, ignored if <b>msg.payload</b> holds csv.</p>
<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>
Expand All @@ -75,18 +69,6 @@
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-payloadType").change(function() {
var id = $("#node-input-payloadType option:selected").val();
if (id == "json") {
$("#node-input-row-topic").show();
} else {
$("#node-input-row-topic").hide();
}
});
$("#node-input-payloadType").val(this.payloadType);
$("#node-input-payloadType").change();
}
});
</script>
Expand Down
10 changes: 4 additions & 6 deletions io/emoncms/88-emoncms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function EmoncmsServerNode(n) {
RED.nodes.createNode(this,n);
this.server = n.server;
this.name = n.name;
this.payloadType = n.payloadType;
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.apikey = credentials.apikey;
Expand Down Expand Up @@ -72,19 +71,18 @@ function Emoncms(n) {
this.baseurl = sc.server;
this.apikey = sc.apikey;

this.payloadType = n.payloadType;
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) {
this.url = this.baseurl + '/input/post.json?';
if(this.payloadType == 'json'){
var topic = this.topic || msg.topic;
this.url += 'json={' + topic + ':' + msg.payload+'}';
} else {
var topic = this.topic || msg.topic;
if(msg.payload.indexOf(',') > -1 || topic.trim() == ''){
this.url += 'csv='+msg.payload;
} else {
this.url += 'json={' + topic + ':' + msg.payload+'}';
}
this.url += '&apikey='+this.apikey;
var nodegroup = this.nodegroup || msg.nodegroup;
Expand Down

0 comments on commit e6f90f9

Please sign in to comment.