Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More device control #57

Closed
msillano opened this issue Apr 21, 2021 · 4 comments · Fixed by #63
Closed

More device control #57

msillano opened this issue Apr 21, 2021 · 4 comments · Fixed by #63
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@msillano
Copy link

msillano commented Apr 21, 2021

Production scenario:

  • Many tuya devices (20+), each device uses a node-red-contrib-tuya-smart-device.
  • Some devices are permanently in use (thermometer sensor, fire alarm, door sensors, etc.).
  • Others are in use only for short periods (power strip, humidifier, TV ... etc.) mainly because they are turned off.

The problem

  • Too many connection retries, too many debug pad messages, too many resources used.
  • The node-red 'disable' node feature works, but need a 'Deploy'. Not a solution at run-time.
  • see ISUUE# 35
  • The support-timeout ISSUE#41 is a good help (thanks vinodsr), but it is static, i.e. cannot be changed at run-time.
    It is better to have a fast (2-10 s) timeout when the device is working (to reconnect it soon in case of any problem) and a very slow (60-600 s) timeout when the device is OFF (so we polling it, and auto-reconnect when turned on).

The proposed solution

  1. A new command findTimeout, to set dynamically the findTimeout, in millisecond.
    Syntax: msg ={"findTimeout": 10000}.
    It can be sent alone or with standard SET/GET/MULTIPLE/SCHEMA commands.

Example: look at IR #1, default timeout 10 s.

21 Apr 12:31:08 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
21 Apr 12:31:08 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 12:31:08 - [info] [tuya-smart-device:BLE MESH(SIG)Gateway] initiating the find command
21 Apr 12:31:08 - [info] [tuya-smart-device:power strip] initiating the find command
21 Apr 12:31:08 - [info] [tuya-smart-device:meter] initiating the find command
21 Apr 12:31:10 - [info] [tuya-smart-device:meter] Connected to device! 4864416*****
21 Apr 12:31:11 - [info] [tuya-smart-device:power strip] Connected to device! 36136661*****
21 Apr 12:31:13 - [info] [tuya-smart-device:BLE MESH(SIG)Gateway] Connected to device! bf0b2ef30ff*****
*21 Apr 12:31:18 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:31:18 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 12:31:28 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:31:48 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:32:07 - [info] [tuya-smart-device:BLE MESH(SIG)Gateway] Disconnected from tuyaDevice.
......  // IR #1: default every 10+10s:
21 Apr 12:33:49 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:33:59 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:34:09 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:34:19 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:34:29 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:34:39 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:34:49 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 12:34:49 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
21 Apr 12:34:54 - [info] [tuya-smart-device:meter] Data from device  [event:dp-refresh]: {"devId":"486441603c*****","dps":{"18":58},"t":1619001294}
...... // slow findTimeout. 40+10
21 Apr 12:37:30 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
21 Apr 12:37:40 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
==> 21 Apr 12:37:48 - [info] [tuya-smart-device:Smart IR #1] Updated findTimeout. New value:40000    
21 Apr 12:37:50 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:38:00 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:38:14 - [info] [tuya-smart-device:meter] Data from device  [event:dp-refresh]: {"devId":"486441603c*****","dps":{"20":2306},"t":1619001494}
21 Apr 12:38:29 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 12:38:39 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 12:38:40 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:38:50 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:39:12 - [info] [tuya-smart-device:meter] Data from device  [event:dp-refresh]: {"devId":"486441603c*****","dps":{"20":2317},"t":1619001552}
21 Apr 12:39:41 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:39:51 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:40:19 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 12:40:29 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 12:40:31 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:40:41 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
.........  // fast findTimeout. 5+10
21 Apr 12:43:11 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
==> 21 Apr 12:43:22 - [info] [tuya-smart-device:Smart IR #1] Updated findTimeout. New value:5000
21 Apr 12:43:51 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
21 Apr 12:43:59 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 12:43:59 - [info] [tuya-smart-device:smart umidifier] initiating the find command
*21 Apr 12:44:01 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:44:06 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
21 Apr 12:44:09 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
*21 Apr 12:44:16 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:44:21 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 12:44:31 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 12:44:36 - [info] [tuya-smart-device:Smart IR #1] initiating the find command

Here the code used: tuya-smart-device.js, line 43

    node.on('input', function (msg) {

            if ((msg.findTimeout !== undefined) &&
            (!isNaN(msg.findTimeout)) &&
            (msg.findTimeout > 0)){
                node.findTimeout = ~~msg.findTimeout;
                node.log("Updated findTimeout. New value:" + node.findTimeout);
        }

  1. A new command standby, to hibernate the device. It is not an alternative to findTimeout,. It uses a user command to set in a 'suspended' state the device, with a good saving of resources.
    Syntax: msg ={"standby": true|false}.
    It can be sent alone or with standard SET/GET/MULTIPLE/SCHEMA commands.

Example: look at IR #1, default timeout 10+10 s.

21 Apr 13:16:55 - [info] [tuya-smart-device:meter] Connected to device! 486441603c61*****
21 Apr 13:16:56 - [info] [tuya-smart-device:power strip] Connected to device! 3613666124a*****
*21 Apr 13:17:04 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 13:17:04 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 13:17:14 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 13:17:24 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 13:17:34 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
==>21 Apr 13:17:44 - [info] [tuya-smart-device:Smart IR #1] not retrying the find as shouldTryReconnect = false
21 Apr 13:18:44 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 13:18:54 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 13:20:34 - [info] [tuya-smart-device:Smart IR #3] initiating the find command
21 Apr 13:20:44 - [info] [tuya-smart-device:Smart IR #3] Cannot find the device, re-trying...
21 Apr 13:20:44 - [info] [tuya-smart-device:smart umidifier] Cannot find the device, re-trying...
==> 21 Apr 13:21:01 - [info] [tuya-smart-device:Smart IR #1] End standby: re-trying...
21 Apr 13:21:11 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 13:21:21 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 13:21:31 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 13:21:50 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 13:22:00 - [info] [tuya-smart-device:Smart IR #1] initiating the find command
*21 Apr 13:22:10 - [info] [tuya-smart-device:Smart IR #1] Cannot find the device, re-trying...
21 Apr 13:22:20 - [info] [tuya-smart-device:Smart IR #1] initiating the find command

Here the code used: tuya-smart-device.js, after the 'findTimeout ' code:

             if ((msg.standby !== undefined) &&
            (typeof msg.standby === 'boolean')) {
            shouldTryReconnect = !msg.standby;
            if (tuyaDevice.isConnected()){
               if ( msg.standby){
                    tuyaDevice.disconnect();
                    node.log("Received standby: disconnecting...");
                    clearTimeout(findTimeoutHandler);
                    }
            } else {
               if (!msg.standby){
                   node.log("End standby: re-trying...");
                   findTimeoutHandler = setTimeout(findDevice, node.findTimeout);
                   }
            }
      }
// a test is  required around the old operation code:

   if (msg.payload !== undefined) {
             let operation = msg.payload.operation || 'SET';
             .....
            }

(updated: now standby, if the device is connected, does disconnect() )

The proposed enhancements are backward compatible with ver 4.0.1

I'm using this code on my project, for now without problems.

Best regards
m.s.

@vinodsr vinodsr self-assigned this Apr 23, 2021
@vinodsr vinodsr added the enhancement New feature or request label Apr 23, 2021
@vinodsr vinodsr added this to the 5.0.0 milestone Apr 23, 2021
@vinodsr
Copy link
Owner

vinodsr commented Apr 23, 2021

Cool 👍 .. Nice suggestions.

Added this to 5.0 milestone. (May -31 )

@vinodsr
Copy link
Owner

vinodsr commented May 16, 2021

@msillano I am planning to make changes which gives you better control on the tuya-smart-device-node.

  • Input using an operation CONTROL to set the values dynamically.
  • A new output to give updates on the state of the client.. (CONNECT, DISCONNECT etc )

I am checking if it is possible for a node to update the configuration . ? so that if you send a control message with a new findTimeout , it is also updated in the node 's config. Next time when we open the node using the editor, we can see the udpated value.

This helps in easy debugging. ( else inside the runtime it will have a different value compared to those in the editor)

For generic node , since we are not maintaining the values at the node properties this is not an issue.

Do you have any idea on how to update the node's properties dynamically. ?

@vinodsr
Copy link
Owner

vinodsr commented May 30, 2021

@msillano I had released a new version 4.1.0 with some more control on state and timeouts. . It will be good if you can do a test run

@msillano
Copy link
Author

Ok, asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants