@@ -11,11 +11,40 @@ module.exports = function (RED) {
1111 RED . nodes . createNode ( this , config ) ;
1212 var node = this ;
1313
14+ var paytoqs = config . paytoqs || "ignore" ;
15+
16+ function _stringifyParams ( params ) {
17+ var paramList = [ ] ;
18+ for ( var [ key , value ] of Object . entries ( params ) ) {
19+ var dataType = typeof value ;
20+ if ( [ "string" , "number" ] . includes ( dataType ) ) {
21+ paramList . push ( `${ key } =${ value } ` ) ;
22+ }
23+ }
24+
25+ return paramList . join ( "&" ) ;
26+ }
27+
28+ function _appendQueryParams ( reqOpts , payload ) {
29+ if ( typeof payload === "object" ) {
30+ var newParams = _stringifyParams ( payload ) ;
31+ if ( newParams && reqOpts . query !== "" ) {
32+ newParams = "&" + newParams ;
33+ }
34+
35+ reqOpts . query = reqOpts . query + newParams ;
36+ } else {
37+ throw new Error ( "Hallo" ) ;
38+ }
39+ }
40+
1441 function _constructPayload ( msg , contentFormat ) {
1542 var payload = null ;
1643
17- if ( contentFormat === "text/plain" ) {
18- payload = msg . payload ;
44+ if ( ! msg . payload ) {
45+ return null ;
46+ } else if ( contentFormat === "text/plain" ) {
47+ payload = msg . payload . toString ( ) ;
1948 } else if ( contentFormat === "application/json" ) {
2049 payload = JSON . stringify ( msg . payload ) ;
2150 } else if ( contentFormat === "application/cbor" ) {
@@ -61,18 +90,26 @@ module.exports = function (RED) {
6190 port,
6291 query : url . search . substring ( 1 ) ,
6392 } ;
64- reqOpts . method = (
65- config . method ||
66- msg . method ||
67- "GET"
68- ) . toUpperCase ( ) ;
69- reqOpts . headers = { } ;
70- reqOpts . headers [ "Content-Format" ] = config [ "content-format" ] ;
93+ reqOpts . method = config . method . toUpperCase ( ) || "GET" ;
94+ if ( config . method === "use" && msg . method != null ) {
95+ reqOpts . method = msg . method . toUpperCase ( ) ;
96+ }
97+
98+ reqOpts . headers = msg . headers ;
99+ if ( reqOpts . headers == null ) {
100+ reqOpts . headers = { } ;
101+ }
102+ if ( reqOpts . headers [ "Content-Format" ] == null ) {
103+ reqOpts . headers [ "Content-Format" ] = "application/json" ;
104+ }
71105 reqOpts . multicast = config . multicast ;
72106 reqOpts . multicastTimeout = config . multicastTimeout ;
73107
74108 function _onResponse ( res ) {
75109 function _send ( payload ) {
110+ if ( ! reqOpts . observe ) {
111+ node . status ( { } ) ;
112+ }
76113 node . send (
77114 Object . assign ( { } , msg , {
78115 payload : payload ,
@@ -83,31 +120,38 @@ module.exports = function (RED) {
83120 }
84121
85122 function _onResponseData ( data ) {
86- if ( config [ "raw-buffer" ] ) {
123+ var contentFormat = res . headers [ "Content-Format" ] ;
124+ var configContentFormat = config [ "content-format" ] ;
125+
126+ if ( config [ "raw-buffer" ] === true || configContentFormat === "raw-buffer" ) {
87127 _send ( data ) ;
88- } else if ( res . headers [ "Content-Format" ] === "text/plain" ) {
128+ } else if ( contentFormat === "text/plain" || configContentFormat === "text/plain" ) {
89129 _send ( data . toString ( ) ) ;
90- } else if (
91- res . headers [ "Content-Format" ] === "application/json"
92- ) {
130+ } else if ( contentFormat . startsWith ( "application/" ) && contentFormat . includes ( "json" ) ) {
93131 try {
94132 _send ( JSON . parse ( data . toString ( ) ) ) ;
95133 } catch ( error ) {
134+ node . status ( {
135+ fill : "red" ,
136+ shape : "ring" ,
137+ text : error . message ,
138+ } ) ;
96139 node . error ( error . message ) ;
97140 }
98- } else if (
99- res . headers [ "Content-Format" ] === "application/cbor"
100- ) {
101- cbor . decodeAll ( data , function ( err , data ) {
102- if ( err ) {
141+ } else if ( contentFormat . startsWith ( "application/" ) && contentFormat . includes ( "cbor" ) ) {
142+ cbor . decodeAll ( data , function ( error , data ) {
143+ if ( error ) {
144+ node . error ( error . message ) ;
145+ node . status ( {
146+ fill : "red" ,
147+ shape : "ring" ,
148+ text : error . message ,
149+ } ) ;
103150 return false ;
104151 }
105152 _send ( data [ 0 ] ) ;
106153 } ) ;
107- } else if (
108- res . headers [ "Content-Format" ] ===
109- "application/link-format"
110- ) {
154+ } else if ( contentFormat === "application/link-format" ) {
111155 _send ( linkFormat . parse ( data . toString ( ) ) ) ;
112156 } else {
113157 _send ( data . toString ( ) ) ;
@@ -116,40 +160,70 @@ module.exports = function (RED) {
116160
117161 res . on ( "data" , _onResponseData ) ;
118162
119- if ( reqOpts . observe ) {
163+ if ( reqOpts . observe === true ) {
164+ node . status ( {
165+ fill : "blue" ,
166+ shape : "dot" ,
167+ text : "coapRequest.status.observing" ,
168+ } ) ;
120169 node . stream = res ;
121170 }
122171 }
123172
124- var payload = _constructPayload ( msg , config [ "content-format" ] ) ;
173+ var payload ;
174+
175+ if ( reqOpts . method !== "GET" ) {
176+ payload = _constructPayload ( msg , reqOpts . headers [ "Content-Format" ] ) ;
177+ } else if ( paytoqs === "query" ) {
178+ try {
179+ _appendQueryParams ( reqOpts , msg . payload ) ;
180+ } catch ( error ) {
181+ node . error ( "Coap request: Invalid payload format!" ) ;
182+ return ;
183+ }
184+ }
125185
126186 if ( config . observe === true ) {
127187 reqOpts . observe = true ;
128188 } else {
129189 delete reqOpts . observe ;
130190 }
131191
132- //TODO: should revisit this block
192+ // TODO: should revisit this block
133193 if ( node . stream ) {
134194 node . stream . close ( ) ;
135195 }
136196
137197 var req = coap . request ( reqOpts ) ;
138198 req . on ( "response" , _onResponse ) ;
139- req . on ( "error" , function ( err ) {
199+ req . on ( "error" , function ( error ) {
200+ node . status ( {
201+ fill : "red" ,
202+ shape : "ring" ,
203+ text : error . message ,
204+ } ) ;
140205 node . log ( "client error" ) ;
141- node . log ( err ) ;
206+ node . log ( error . message ) ;
142207 } ) ;
143208
144- if ( payload ) {
209+ if ( payload != null ) {
145210 req . write ( payload ) ;
146211 }
147212 req . end ( ) ;
148213 }
149214
150215 this . on ( "input" , function ( msg ) {
216+ node . status ( {
217+ fill : "blue" ,
218+ shape : "dot" ,
219+ text : "coapRequest.status.requesting" ,
220+ } ) ;
151221 _makeRequest ( msg ) ;
152222 } ) ;
223+
224+ this . on ( "close" , function ( ) {
225+ node . status ( { } ) ;
226+ } ) ;
153227 }
154228 RED . nodes . registerType ( "coap request" , CoapRequestNode ) ;
155229} ;
0 commit comments