11/**
22 * Copyright 2013 Henrik Olsson [email protected] 3- *
3+ *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
55 * use this file except in compliance with the License. You may obtain a copy of
66 * the License at
7- *
7+ *
88 * http://www.apache.org/licenses/LICENSE-2.0
9- *
9+ *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -22,8 +22,8 @@ function EmoncmsServerNode(n) {
2222 this . name = n . name ;
2323 var credentials = RED . nodes . getCredentials ( n . id ) ;
2424 if ( credentials ) {
25- this . apikey = credentials . apikey ;
26- }
25+ this . apikey = credentials . apikey ;
26+ }
2727
2828}
2929RED . nodes . registerType ( "emoncms-server" , EmoncmsServerNode ) ;
@@ -64,48 +64,48 @@ RED.app.post('/emoncms-server/:id',function(req,res) {
6464} ) ;
6565
6666function Emoncms ( n ) {
67- RED . nodes . createNode ( this , n ) ;
67+ RED . nodes . createNode ( this , n ) ;
6868 this . emonServer = n . emonServer ;
6969 var sc = RED . nodes . getNode ( this . emonServer ) ;
7070
71- this . baseurl = sc . server ;
72- this . apikey = sc . apikey ;
71+ this . baseurl = sc . server ;
72+ this . apikey = sc . apikey ;
73+
74+ this . topic = n . topic || "" ;
75+ this . nodegroup = n . nodegroup || "" ;
76+ var node = this ;
77+ if ( this . baseurl . substring ( 0 , 5 ) === "https" ) { var http = require ( "https" ) ; }
78+ else { var http = require ( "http" ) ; }
79+ this . on ( "input" , function ( msg ) {
7380
74- this . topic = n . topic || "" ;
75- this . nodegroup = n . nodegroup || "" ;
76- var node = this ;
77- if ( this . baseurl . substring ( 0 , 5 ) === "https" ) { var http = require ( "https" ) ; }
78- else { var http = require ( "http" ) ; }
79- this . on ( "input" , function ( msg ) {
80-
81- var topic = this . topic || msg . topic ;
82- var nodegroup = this . nodegroup || msg . nodegroup ;
83- this . url = this . baseurl + '/input/post.json?json={' + topic + ':' + msg . payload + '}&apikey=' + this . apikey ;
84- if ( nodegroup != "" ) {
85- this . url += '&node=' + nodegroup ;
86- }
87- node . log ( "[emoncms] " + this . url ) ;
88- http . get ( this . url , function ( res ) {
89- node . log ( "Http response: " + res . statusCode ) ;
90- msg . rc = res . statusCode ;
91- msg . payload = "" ;
92- if ( ( msg . rc != 200 ) && ( msg . rc != 404 ) ) {
93- node . send ( msg ) ;
94- }
95- res . setEncoding ( 'utf8' ) ;
96- res . on ( 'data' , function ( chunk ) {
97- msg . payload += chunk ;
98- } ) ;
99- res . on ( 'end' , function ( ) {
100- node . send ( msg ) ;
101- } ) ;
102- } ) . on ( 'error' , function ( e ) {
103- // node.error(e);
104- msg . rc = 503 ;
105- msg . payload = e ;
106- node . send ( msg ) ;
107- } ) ;
108- } ) ;
81+ var topic = this . topic || msg . topic ;
82+ var nodegroup = this . nodegroup || msg . nodegroup ;
83+ this . url = this . baseurl + '/input/post.json?json={' + topic + ':' + msg . payload + '}&apikey=' + this . apikey ;
84+ if ( nodegroup != "" ) {
85+ this . url += '&node=' + nodegroup ;
86+ }
87+ node . log ( "[emoncms] " + this . url ) ;
88+ http . get ( this . url , function ( res ) {
89+ node . log ( "Http response: " + res . statusCode ) ;
90+ msg . rc = res . statusCode ;
91+ msg . payload = "" ;
92+ if ( ( msg . rc != 200 ) && ( msg . rc != 404 ) ) {
93+ node . send ( msg ) ;
94+ }
95+ res . setEncoding ( 'utf8' ) ;
96+ res . on ( 'data' , function ( chunk ) {
97+ msg . payload += chunk ;
98+ } ) ;
99+ res . on ( 'end' , function ( ) {
100+ node . send ( msg ) ;
101+ } ) ;
102+ } ) . on ( 'error' , function ( e ) {
103+ // node.error(e);
104+ msg . rc = 503 ;
105+ msg . payload = e ;
106+ node . send ( msg ) ;
107+ } ) ;
108+ } ) ;
109109}
110110
111111RED . nodes . registerType ( "emoncms" , Emoncms ) ;
0 commit comments