1
1
/**
2
2
* Copyright 2013 Henrik Olsson [email protected]
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
5
* use this file except in compliance with the License. You may obtain a copy of
6
6
* the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
12
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -22,8 +22,8 @@ function EmoncmsServerNode(n) {
22
22
this . name = n . name ;
23
23
var credentials = RED . nodes . getCredentials ( n . id ) ;
24
24
if ( credentials ) {
25
- this . apikey = credentials . apikey ;
26
- }
25
+ this . apikey = credentials . apikey ;
26
+ }
27
27
28
28
}
29
29
RED . nodes . registerType ( "emoncms-server" , EmoncmsServerNode ) ;
@@ -64,48 +64,48 @@ RED.app.post('/emoncms-server/:id',function(req,res) {
64
64
} ) ;
65
65
66
66
function Emoncms ( n ) {
67
- RED . nodes . createNode ( this , n ) ;
67
+ RED . nodes . createNode ( this , n ) ;
68
68
this . emonServer = n . emonServer ;
69
69
var sc = RED . nodes . getNode ( this . emonServer ) ;
70
70
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 ) {
73
80
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
+ } ) ;
109
109
}
110
110
111
111
RED . nodes . registerType ( "emoncms" , Emoncms ) ;
0 commit comments