@@ -9,11 +9,40 @@ module.exports = function (RED) {
9
9
RED . nodes . createNode ( this , config ) ;
10
10
var node = this ;
11
11
12
+ var paytoqs = config . paytoqs || "ignore" ;
13
+
14
+ function _stringifyParams ( params ) {
15
+ var paramList = [ ] ;
16
+ for ( var [ key , value ] of Object . entries ( params ) ) {
17
+ var dataType = typeof value ;
18
+ if ( [ "string" , "number" ] . includes ( dataType ) ) {
19
+ paramList . push ( `${ key } =${ value } ` ) ;
20
+ }
21
+ }
22
+
23
+ return paramList . join ( "&" ) ;
24
+ }
25
+
26
+ function _appendQueryParams ( reqOpts , payload ) {
27
+ if ( typeof payload === "object" ) {
28
+ var newParams = _stringifyParams ( payload ) ;
29
+ if ( newParams && reqOpts . query !== "" ) {
30
+ newParams = "&" + newParams ;
31
+ }
32
+
33
+ reqOpts . query = reqOpts . query + newParams ;
34
+ } else {
35
+ throw new Error ( "Hallo" ) ;
36
+ }
37
+ }
38
+
12
39
function _constructPayload ( msg , contentFormat ) {
13
40
var payload = null ;
14
41
15
- if ( contentFormat === "text/plain" ) {
16
- payload = msg . payload ;
42
+ if ( ! msg . payload ) {
43
+ return null ;
44
+ } else if ( contentFormat === "text/plain" ) {
45
+ payload = msg . payload . toString ( ) ;
17
46
} else if ( contentFormat === "application/json" ) {
18
47
payload = JSON . stringify ( msg . payload ) ;
19
48
} else if ( contentFormat === "application/cbor" ) {
@@ -32,18 +61,26 @@ module.exports = function (RED) {
32
61
port : url . port ,
33
62
query : url . search . substring ( 1 ) ,
34
63
} ;
35
- reqOpts . method = (
36
- config . method ||
37
- msg . method ||
38
- "GET"
39
- ) . toUpperCase ( ) ;
40
- reqOpts . headers = { } ;
41
- reqOpts . headers [ "Content-Format" ] = config [ "content-format" ] ;
64
+ reqOpts . method = config . method . toUpperCase ( ) || "GET" ;
65
+ if ( config . method === "use" && msg . method != null ) {
66
+ reqOpts . method = msg . method . toUpperCase ( ) ;
67
+ }
68
+
69
+ reqOpts . headers = msg . headers ;
70
+ if ( reqOpts . headers == null ) {
71
+ reqOpts . headers = { } ;
72
+ }
73
+ if ( reqOpts . headers [ "Content-Format" ] == null ) {
74
+ reqOpts . headers [ "Content-Format" ] = "application/json" ;
75
+ }
42
76
reqOpts . multicast = config . multicast ;
43
77
reqOpts . multicastTimeout = config . multicastTimeout ;
44
78
45
79
function _onResponse ( res ) {
46
80
function _send ( payload ) {
81
+ if ( ! reqOpts . observe ) {
82
+ node . status ( { } ) ;
83
+ }
47
84
node . send (
48
85
Object . assign ( { } , msg , {
49
86
payload : payload ,
@@ -54,31 +91,38 @@ module.exports = function (RED) {
54
91
}
55
92
56
93
function _onResponseData ( data ) {
57
- if ( config [ "raw-buffer" ] ) {
94
+ var contentFormat = res . headers [ "Content-Format" ] ;
95
+ var configContentFormat = config [ "content-format" ] ;
96
+
97
+ if ( config [ "raw-buffer" ] === true || configContentFormat === "raw-buffer" ) {
58
98
_send ( data ) ;
59
- } else if ( res . headers [ "Content-Format" ] === "text/plain" ) {
99
+ } else if ( contentFormat === "text/plain" || configContentFormat === "text/plain" ) {
60
100
_send ( data . toString ( ) ) ;
61
- } else if (
62
- res . headers [ "Content-Format" ] === "application/json"
63
- ) {
101
+ } else if ( contentFormat . startsWith ( "application/" ) && contentFormat . includes ( "json" ) ) {
64
102
try {
65
103
_send ( JSON . parse ( data . toString ( ) ) ) ;
66
104
} catch ( error ) {
105
+ node . status ( {
106
+ fill : "red" ,
107
+ shape : "ring" ,
108
+ text : error . message ,
109
+ } ) ;
67
110
node . error ( error . message ) ;
68
111
}
69
- } else if (
70
- res . headers [ "Content-Format" ] === "application/cbor"
71
- ) {
72
- cbor . decodeAll ( data , function ( err , data ) {
73
- if ( err ) {
112
+ } else if ( contentFormat . startsWith ( "application/" ) && contentFormat . includes ( "cbor" ) ) {
113
+ cbor . decodeAll ( data , function ( error , data ) {
114
+ if ( error ) {
115
+ node . error ( error . message ) ;
116
+ node . status ( {
117
+ fill : "red" ,
118
+ shape : "ring" ,
119
+ text : error . message ,
120
+ } ) ;
74
121
return false ;
75
122
}
76
123
_send ( data [ 0 ] ) ;
77
124
} ) ;
78
- } else if (
79
- res . headers [ "Content-Format" ] ===
80
- "application/link-format"
81
- ) {
125
+ } else if ( contentFormat === "application/link-format" ) {
82
126
_send ( linkFormat . parse ( data . toString ( ) ) ) ;
83
127
} else {
84
128
_send ( data . toString ( ) ) ;
@@ -87,40 +131,70 @@ module.exports = function (RED) {
87
131
88
132
res . on ( "data" , _onResponseData ) ;
89
133
90
- if ( reqOpts . observe ) {
134
+ if ( reqOpts . observe === true ) {
135
+ node . status ( {
136
+ fill : "blue" ,
137
+ shape : "dot" ,
138
+ text : "coapRequest.status.observing" ,
139
+ } ) ;
91
140
node . stream = res ;
92
141
}
93
142
}
94
143
95
- var payload = _constructPayload ( msg , config [ "content-format" ] ) ;
144
+ var payload ;
145
+
146
+ if ( reqOpts . method !== "GET" ) {
147
+ payload = _constructPayload ( msg , reqOpts . headers [ "Content-Format" ] ) ;
148
+ } else if ( paytoqs === "query" ) {
149
+ try {
150
+ _appendQueryParams ( reqOpts , msg . payload ) ;
151
+ } catch ( error ) {
152
+ node . error ( "Coap request: Invalid payload format!" ) ;
153
+ return ;
154
+ }
155
+ }
96
156
97
157
if ( config . observe === true ) {
98
158
reqOpts . observe = "1" ;
99
159
} else {
100
160
delete reqOpts . observe ;
101
161
}
102
162
103
- //TODO: should revisit this block
163
+ // TODO: should revisit this block
104
164
if ( node . stream ) {
105
165
node . stream . close ( ) ;
106
166
}
107
167
108
168
var req = coap . request ( reqOpts ) ;
109
169
req . on ( "response" , _onResponse ) ;
110
- req . on ( "error" , function ( err ) {
170
+ req . on ( "error" , function ( error ) {
171
+ node . status ( {
172
+ fill : "red" ,
173
+ shape : "ring" ,
174
+ text : error . message ,
175
+ } ) ;
111
176
node . log ( "client error" ) ;
112
- node . log ( err ) ;
177
+ node . log ( error . message ) ;
113
178
} ) ;
114
179
115
- if ( payload ) {
180
+ if ( payload != null ) {
116
181
req . write ( payload ) ;
117
182
}
118
183
req . end ( ) ;
119
184
}
120
185
121
186
this . on ( "input" , function ( msg ) {
187
+ node . status ( {
188
+ fill : "blue" ,
189
+ shape : "dot" ,
190
+ text : "coapRequest.status.requesting" ,
191
+ } ) ;
122
192
_makeRequest ( msg ) ;
123
193
} ) ;
194
+
195
+ this . on ( "close" , function ( ) {
196
+ node . status ( { } ) ;
197
+ } ) ;
124
198
}
125
199
RED . nodes . registerType ( "coap request" , CoapRequestNode ) ;
126
200
} ;
0 commit comments