vinodsr / node-red-contrib-tuya-smart-device

a node-red module for managing tuya smart devices
https://flows.nodered.org/node/node-red-contrib-tuya-smart-device
MIT License
47 stars 15 forks source link

More device control #57

Closed msillano closed 3 years ago

msillano commented 3 years ago

Production scenario:

The problem

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 commented 3 years ago

Cool 👍 .. Nice suggestions.

Added this to 5.0 milestone. (May -31 )

vinodsr commented 3 years ago

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

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 commented 3 years ago

@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 commented 3 years ago

Ok, asap.