Skip to content

Commit

Permalink
Merge pull request #48 from vinodsr/feature/add-support-for-catch-node
Browse files Browse the repository at this point in the history
Added support for catch node
  • Loading branch information
vinodsr authored Mar 5, 2021
2 parents 0bb1f08 + 7f35243 commit c2dc571
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.1.0

- Added support for catch node. If there a catch node in the flow, the tuya smart device node will not throw any error to the debug window. Implementing [#47](https://github.com/vinodsr/node-red-contrib-tuya-smart-device/issues/47)

## 3.0.2

- Fixes [#43](https://github.com/vinodsr/node-red-contrib-tuya-smart-device/issues/43)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-tuya-smart-device",
"version": "3.0.2",
"version": "3.1.0",
"description": "A node-red module to interact with the tuya smart devices",
"repository": "https://github.com/vinodsr/node-red-contrib-tuya-smart-device",
"main": "index.js",
Expand Down
9 changes: 7 additions & 2 deletions src/tuya-smart-device-generic.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
category: 'smarthome',
color: '#C7E9C0',
defaults: {

name: { value: "", required: false },
},
inputs: 1,
outputs: 1,
icon: "bridge.svg",
label: function () {
return this.deviceName || "tuya smart device generic";
return this.name || "tuya smart device generic";
}
});
</script>

<script type="text/html" data-template-name="tuya-smart-device-generic">
<div>
Pass the device key and virual id as payload paramters. Refer help for more details.
<hr/>
<div class="form-row">
<label style="width:100%" for="node-input-name" ><i class="fa fa-id-card-o"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</div>
</script>

Expand Down
28 changes: 23 additions & 5 deletions src/tuya-smart-device-generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = function (RED) {
function TuyaSmartDeviceSelfNode(config) {
RED.nodes.createNode(this, config);
let node = this;
this.name = config.name;
this.operations = []
node.on('input', function (msg) {
let operation = msg.payload.operation || 'SET';
Expand Down Expand Up @@ -35,7 +36,15 @@ module.exports = function (RED) {

tuyaDevice.on('error', error => {
node.operations.pop();
setStatusOnError(error, requestID);
setStatusOnError(error, requestID, 'Error', {
context: {
message: error,
deviceVirtualId: msg.payload.deviceVirtualId,
deviceKey: msg.payload.deviceKey,
deviceIp: msg.payload.deviceIp,
requestID: requestID
}
});
});
tuyaDevice.on('connected', () => {
node.log(`[${requestID}] Connected to device! ${msg.payload.deviceVirtualId}`);
Expand All @@ -58,6 +67,8 @@ module.exports = function (RED) {
deviceVirtualId: msg.payload.deviceVirtualId,
deviceKey: msg.payload.deviceKey,
deviceName: msg.payload.deviceName,
deviceIp: msg.payload.deviceIp,
requestID: requestID
}
});
});
Expand All @@ -69,7 +80,14 @@ module.exports = function (RED) {
tuyaDevice.connect();
}).catch((e) => {
// We need to retry
setStatusOnError(e, requestID, "Can't find device");
setStatusOnError(e.message, requestID, "Can't find device", {
context: {
message: e,
deviceVirtualId: msg.payload.deviceVirtualId,
deviceKey: msg.payload.deviceKey,
deviceIp: msg.payload.deviceIp,
}
});
node.log(`[${requestID}] Cannot find the device`);
//setTimeout(findDevice, 1000);
});
Expand All @@ -80,9 +98,9 @@ module.exports = function (RED) {
let setStatusConnecting = function () { return node.status({ fill: "yellow", shape: "ring", text: "connecting" }); };
let setStatusConnected = function () { return node.status({ fill: "green", shape: "ring", text: "connected" }); };
let setStatusDisconnected = function () { return node.status({ fill: "red", shape: "ring", text: "disconnected" }); };
var setStatusOnError = function (e, requestID, message = "error") {
node.error(`[${requestID}] An error had occured ${e} ${JSON.stringify(e)}`);
return node.status({ fill: "red", shape: "ring", text: message });
var setStatusOnError = function (errorText, requestID, errorShortText = "error", data) {
node.error(`[${requestID}] An error had occured : ${errorText}`, data);
return node.status({ fill: "red", shape: "ring", text: errorShortText });
};

node.on('close', function () {
Expand Down
25 changes: 20 additions & 5 deletions src/tuya-smart-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (RED) {
let node = this;
let isConnected = false;
let shouldTryReconnect = true;
this.name = config.deviceName;
this.deviceName = config.deviceName;
this.deviceId = config.deviceId;
this.deviceIp = config.deviceIp;
Expand Down Expand Up @@ -40,9 +41,9 @@ module.exports = function (RED) {
let setStatusConnecting = function () { return node.status({ fill: "yellow", shape: "ring", text: "connecting" }); };
let setStatusConnected = function () { return node.status({ fill: "green", shape: "ring", text: "connected" }); };
let setStatusDisconnected = function () { return node.status({ fill: "red", shape: "ring", text: "disconnected" }); };
var setStatusOnError = function (e, message = "error") {
node.error(e, "An error had occured ");
return node.status({ fill: "red", shape: "ring", text: message });
var setStatusOnError = function (errorText, errorShortText = "error", data) {
node.error(errorText, data);
return node.status({ fill: "red", shape: "ring", text: errorShortText });
};
const connectionParams = {
id: node.deviceId,
Expand Down Expand Up @@ -89,7 +90,14 @@ module.exports = function (RED) {
});

tuyaDevice.on('error', error => {
setStatusOnError(error);
setStatusOnError(error, 'Error', {
context: {
message: error,
deviceVirtualId: node.deviceId,
deviceIp: node.deviceIp,
deviceKey: node.deviceKey
}
});
if (shouldTryReconnect) {
retryConnection();
}
Expand Down Expand Up @@ -124,7 +132,14 @@ module.exports = function (RED) {
connectDevice();
}).catch((e) => {
// We need to retry
setStatusOnError(e, "Can't find device");
setStatusOnError(e.message, "Can't find device", {
context: {
message: e,
deviceVirtualId: node.deviceId,
deviceIp: node.deviceIp,
deviceKey: node.deviceKey
}
});
if (shouldTryReconnect) {
node.log("Cannot find the device, re-trying...");
findTimeoutHandler = setTimeout(findDevice, node.findTimeout);
Expand Down

0 comments on commit c2dc571

Please sign in to comment.