Skip to content

Commit 0d7b252

Browse files
authored
Merge pull request #169 from vinodsr/bugfix/node-red-crash
Better error handling and fixes node-red crash
2 parents a82a2d9 + 4002616 commit 0d7b252

File tree

5 files changed

+415
-203
lines changed

5 files changed

+415
-203
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 5.3.3
4+
5+
- Better error handling and fixes node-red crash
6+
37
## 5.3.2
48

59
- Fixes handling of TuyaApi crash

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-tuya-smart-device",
3-
"version": "5.3.2",
3+
"version": "5.3.3",
44
"description": "A node-red module to interact with the tuya smart devices",
55
"repository": "https://github.com/vinodsr/node-red-contrib-tuya-smart-device",
66
"main": "index.js",

src/tuya-smart-device-generic.js

Lines changed: 149 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -81,89 +81,173 @@ module.exports = function (RED) {
8181
});
8282
});
8383
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+
);
101134
}
102135
});
103136

104137
if (shouldSubscribeRefreshData) {
105138
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+
}
122172
});
123173
}
124174

125175
if (shouldSubscribeData) {
126176
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+
}
143210
});
144211
}
145212
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+
{
157241
context: {
158-
message: e,
242+
message: error,
159243
deviceVirtualId: msg.payload.deviceVirtualId,
160244
deviceKey: msg.payload.deviceKey,
161245
deviceIp: msg.payload.deviceIp,
246+
requestID: requestID,
162247
},
163-
});
164-
node.logger.error(`[${requestID}] Cannot find the device`);
165-
//setTimeout(findDevice, 1000);
166-
});
248+
}
249+
);
250+
}
167251
};
168252
findDevice();
169253
} catch (error) {

0 commit comments

Comments
 (0)