@@ -81,89 +81,173 @@ module.exports = function (RED) {
81
81
} ) ;
82
82
} ) ;
83
83
tuyaDevice . on ( 'connected' , ( ) => {
84
- node . logger . log (
85
- `[${ requestID } ] Connected to device! ${ msg . payload . deviceVirtualId } `
86
- ) ;
87
- setStatusConnected ( ) ;
88
- switch ( operation ) {
89
- case 'SET' :
90
- node . logger . debug (
91
- `[${ requestID } ] sending command SET : ${ JSON . stringify (
92
- msg . payload . payload
93
- ) } `
94
- ) ;
95
- tuyaDevice . set ( msg . payload . payload ) ;
96
- break ;
97
- default :
98
- node . logger . error (
99
- `[${ requestID } ] Invalid operation ${ operation } `
100
- ) ;
84
+ try {
85
+ node . logger . log (
86
+ `[${ requestID } ] Connected to device! ${ msg . payload . deviceVirtualId } `
87
+ ) ;
88
+ setStatusConnected ( ) ;
89
+
90
+ switch ( operation ) {
91
+ case 'SET' :
92
+ node . logger . debug (
93
+ `[${ requestID } ] sending command SET : ${ JSON . stringify (
94
+ msg . payload . payload
95
+ ) } `
96
+ ) ;
97
+ tuyaDevice . set ( msg . payload . payload ) . catch ( ( error ) => {
98
+ setStatusOnError (
99
+ error . message ,
100
+ requestID ,
101
+ 'Uncaught error : ' + error . message ,
102
+ {
103
+ context : {
104
+ message : error ,
105
+ deviceVirtualId : msg . payload . deviceVirtualId ,
106
+ deviceKey : msg . payload . deviceKey ,
107
+ deviceIp : msg . payload . deviceIp ,
108
+ requestID : requestID ,
109
+ } ,
110
+ }
111
+ ) ;
112
+ } ) ;
113
+ break ;
114
+ default :
115
+ node . logger . error (
116
+ `[${ requestID } ] Invalid operation ${ operation } `
117
+ ) ;
118
+ }
119
+ } catch ( error ) {
120
+ setStatusOnError (
121
+ error . message ,
122
+ requestID ,
123
+ 'Uncaught error : ' + error . message ,
124
+ {
125
+ context : {
126
+ message : error ,
127
+ deviceVirtualId : msg . payload . deviceVirtualId ,
128
+ deviceKey : msg . payload . deviceKey ,
129
+ deviceIp : msg . payload . deviceIp ,
130
+ requestID : requestID ,
131
+ } ,
132
+ }
133
+ ) ;
101
134
}
102
135
} ) ;
103
136
104
137
if ( shouldSubscribeRefreshData ) {
105
138
tuyaDevice . on ( 'dp-refresh' , ( data ) => {
106
- node . logger . debug (
107
- `[${ requestID } ] Data from device [event:dp-refresh]: ${ JSON . stringify (
108
- data
109
- ) } `
110
- ) ;
111
- tuyaDevice . disconnect ( ) ;
112
- node . send ( {
113
- payload : {
114
- data : data ,
115
- deviceVirtualId : msg . payload . deviceVirtualId ,
116
- deviceKey : msg . payload . deviceKey ,
117
- deviceName : msg . payload . deviceName ,
118
- deviceIp : msg . payload . deviceIp ,
119
- requestID : requestID ,
120
- } ,
121
- } ) ;
139
+ try {
140
+ node . logger . debug (
141
+ `[${ requestID } ] Data from device [event:dp-refresh]: ${ JSON . stringify (
142
+ data
143
+ ) } `
144
+ ) ;
145
+ tuyaDevice . disconnect ( ) ;
146
+ node . send ( {
147
+ payload : {
148
+ data : data ,
149
+ deviceVirtualId : msg . payload . deviceVirtualId ,
150
+ deviceKey : msg . payload . deviceKey ,
151
+ deviceName : msg . payload . deviceName ,
152
+ deviceIp : msg . payload . deviceIp ,
153
+ requestID : requestID ,
154
+ } ,
155
+ } ) ;
156
+ } catch ( error ) {
157
+ setStatusOnError (
158
+ error . message ,
159
+ requestID ,
160
+ 'Uncaught error : ' + error . message ,
161
+ {
162
+ context : {
163
+ message : error ,
164
+ deviceVirtualId : msg . payload . deviceVirtualId ,
165
+ deviceKey : msg . payload . deviceKey ,
166
+ deviceIp : msg . payload . deviceIp ,
167
+ requestID : requestID ,
168
+ } ,
169
+ }
170
+ ) ;
171
+ }
122
172
} ) ;
123
173
}
124
174
125
175
if ( shouldSubscribeData ) {
126
176
tuyaDevice . on ( 'data' , ( data ) => {
127
- node . logger . debug (
128
- `[${ requestID } ] Data from device [event:data]: ${ JSON . stringify (
129
- data
130
- ) } `
131
- ) ;
132
- tuyaDevice . disconnect ( ) ;
133
- node . send ( {
134
- payload : {
135
- data : data ,
136
- deviceVirtualId : msg . payload . deviceVirtualId ,
137
- deviceKey : msg . payload . deviceKey ,
138
- deviceName : msg . payload . deviceName ,
139
- deviceIp : msg . payload . deviceIp ,
140
- requestID : requestID ,
141
- } ,
142
- } ) ;
177
+ try {
178
+ node . logger . debug (
179
+ `[${ requestID } ] Data from device [event:data]: ${ JSON . stringify (
180
+ data
181
+ ) } `
182
+ ) ;
183
+ tuyaDevice . disconnect ( ) ;
184
+ node . send ( {
185
+ payload : {
186
+ data : data ,
187
+ deviceVirtualId : msg . payload . deviceVirtualId ,
188
+ deviceKey : msg . payload . deviceKey ,
189
+ deviceName : msg . payload . deviceName ,
190
+ deviceIp : msg . payload . deviceIp ,
191
+ requestID : requestID ,
192
+ } ,
193
+ } ) ;
194
+ } catch ( error ) {
195
+ setStatusOnError (
196
+ error . message ,
197
+ requestID ,
198
+ 'Uncaught error : ' + error . message ,
199
+ {
200
+ context : {
201
+ message : error ,
202
+ deviceVirtualId : msg . payload . deviceVirtualId ,
203
+ deviceKey : msg . payload . deviceKey ,
204
+ deviceIp : msg . payload . deviceIp ,
205
+ requestID : requestID ,
206
+ } ,
207
+ }
208
+ ) ;
209
+ }
143
210
} ) ;
144
211
}
145
212
let findDevice = ( ) => {
146
- setStatusConnecting ( ) ;
147
- node . logger . debug ( `[${ requestID } ] initiating the find command` ) ;
148
- tuyaDevice
149
- . find ( { } )
150
- . then ( ( ) => {
151
- // Connect to device
152
- tuyaDevice . connect ( ) ;
153
- } )
154
- . catch ( ( e ) => {
155
- // We need to retry
156
- setStatusOnError ( e . message , requestID , "Can't find device" , {
213
+ try {
214
+ setStatusConnecting ( ) ;
215
+ node . logger . debug ( `[${ requestID } ] initiating the find command` ) ;
216
+ tuyaDevice
217
+ . find ( { } )
218
+ . then ( ( ) => {
219
+ // Connect to device
220
+ tuyaDevice . connect ( ) ;
221
+ } )
222
+ . catch ( ( e ) => {
223
+ // We need to retry
224
+ setStatusOnError ( e . message , requestID , "Can't find device" , {
225
+ context : {
226
+ message : e ,
227
+ deviceVirtualId : msg . payload . deviceVirtualId ,
228
+ deviceKey : msg . payload . deviceKey ,
229
+ deviceIp : msg . payload . deviceIp ,
230
+ } ,
231
+ } ) ;
232
+ node . logger . error ( `[${ requestID } ] Cannot find the device` ) ;
233
+ //setTimeout(findDevice, 1000);
234
+ } ) ;
235
+ } catch ( error ) {
236
+ setStatusOnError (
237
+ error . message ,
238
+ requestID ,
239
+ 'Uncaught error : ' + error . message ,
240
+ {
157
241
context : {
158
- message : e ,
242
+ message : error ,
159
243
deviceVirtualId : msg . payload . deviceVirtualId ,
160
244
deviceKey : msg . payload . deviceKey ,
161
245
deviceIp : msg . payload . deviceIp ,
246
+ requestID : requestID ,
162
247
} ,
163
- } ) ;
164
- node . logger . error ( `[${ requestID } ] Cannot find the device` ) ;
165
- //setTimeout(findDevice, 1000);
166
- } ) ;
248
+ }
249
+ ) ;
250
+ }
167
251
} ;
168
252
findDevice ( ) ;
169
253
} catch ( error ) {
0 commit comments