Deals with csv and json payload in a smarter way and manipulation

insertion time
This commit is contained in:
henols 2014-03-20 14:51:16 +01:00
parent 8a60d1f918
commit e6f90f9b8d
2 changed files with 13 additions and 33 deletions

View File

@ -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>
@ -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>

View File

@ -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;
@ -72,7 +71,6 @@ 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;
@ -80,11 +78,11 @@ function Emoncms(n) {
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;