TuyaAPI / cli

🔧 A CLI for Tuya devices
MIT License
261 stars 44 forks source link

Socket Error When Using Get Command #84

Closed loaxley closed 3 years ago

loaxley commented 3 years ago

Hi, i'm unable to successfully run the tuya-cli get command. I've seen a couple similar issues, and i've read through all relevant issues, but nothing helped me resolve my issue. The device in question is Ihttps://www.aliexpress.com/item/4001045079168.html?. I was able to get the Key & ID using the Tuya Developer/IOT method and tuya-cli wizard. I've double checked, and my Key & ID are still active and accurate.

I've also pinged the device, and everything is nominal: PING 192.168.1.29 (192.168.1.29): 56 data bytes 64 bytes from 192.168.1.29: icmp_seq=0 ttl=255 time=96.999 ms 64 bytes from 192.168.1.29: icmp_seq=1 ttl=255 time=46.313 ms 64 bytes from 192.168.1.29: icmp_seq=2 ttl=255 time=9.810 ms 64 bytes from 192.168.1.29: icmp_seq=3 ttl=255 time=21.714 ms 64 bytes from 192.168.1.29: icmp_seq=4 ttl=255 time=11.614 ms 64 bytes from 192.168.1.29: icmp_seq=5 ttl=255 time=9.481 ms 64 bytes from 192.168.1.29: icmp_seq=6 ttl=255 time=25.220 ms 64 bytes from 192.168.1.29: icmp_seq=7 ttl=255 time=9.805 ms 64 bytes from 192.168.1.29: icmp_seq=8 ttl=255 time=13.514 ms 64 bytes from 192.168.1.29: icmp_seq=9 ttl=255 time=12.034 ms 64 bytes from 192.168.1.29: icmp_seq=10 ttl=255 time=11.396 ms

Additionally, I ran nmap on the device's IP: nmap -p 6668 192.168.1.29 Starting Nmap 7.70 ( https://nmap.org ) at 2020-08-31 03:48 PDT Nmap scan report for 192.168.1.29 Host is up (0.53s latency).

PORT STATE SERVICE 6668/tcp open irc

Nmap done: 1 IP address (1 host up) scanned in 2.18 seconds

Here's the command i'm running. Note that I also tried forcing --protocol-version 3.0, 3.1, 3.2, 3.3 and 3.4 (I don't even know if all of these exist, I just wanted to try every possibility). The "x"s below are redacted, i'm using my actual ID and Key.

tuya-cli get --id REDACTED --key REDACTED --ip 192.168.1.29 --all

And here's the result:

(node:22824) UnhandledPromiseRejectionWarning: Error: Error from socket at Socket. (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:315:20) at errorOrDestroy (internal/streams/destroy.js:108:12) at onwriteError (_stream_writable.js:418:5) at onwrite (_stream_writable.js:445:5) at doWrite (_stream_writable.js:399:11) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () (node:22824) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 12) (node:22824) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:22824) UnhandledPromiseRejectionWarning: Error: Error from socket at Socket. (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:315:20) at errorOrDestroy (internal/streams/destroy.js:108:12) at onwriteError (_stream_writable.js:418:5) at onwrite (_stream_writable.js:445:5) at doWrite (_stream_writable.js:399:11) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () (node:22824) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 14)

And here's the debug command (again, i've redacted the id and key): DEBUG=* tuya-cli get --id REDACTED --key REDACTED --ip 192.168.1.29 --all

And the debug output (ID redacted): TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 192.168.1.29... +6ms TuyAPI Socket connected. +278ms TuyAPI GET Payload: +1ms TuyAPI { TuyAPI gwId: 'REDACTED', TuyAPI devId: 'REDACTED', TuyAPI t: 'REDACTED', TuyAPI dps: {}, TuyAPI uid: 'REDACTED' TuyAPI } +1ms TuyAPI GET Payload: +9ms TuyAPI { TuyAPI gwId: 'REDACTED', TuyAPI devId: 'REDACTED', TuyAPI t: 'REDACTED', TuyAPI dps: {}, TuyAPI uid: '31xxxxxxxxxxxxxxxdf8' TuyAPI } +1ms TuyAPI Error event from socket. 192.168.1.29 Error: write ECONNRESET at afterWriteDispatched (internal/stream_base_commons.js:154:25) at writeGeneric (internal/stream_base_commons.js:145:3) at Socket._writeGeneric (net.js:786:11) at Socket._write (net.js:798:8) at doWrite (_stream_writable.js:403:12) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) { errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'write' } +2ms TuyAPI Socket closed: 192.168.1.29 +4ms TuyAPI Error event from socket. 192.168.1.29 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) at RetryOperation._fn (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-retry/index.js:41:18) at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/retry/lib/retry_operation.js:81:10) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) { code: 'ERR_STREAM_DESTROYED' } +1s TuyAPI Error event from socket. 192.168.1.29 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) at RetryOperation._fn (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-retry/index.js:41:18) at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/retry/lib/retry_operation.js:81:10) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) { code: 'ERR_STREAM_DESTROYED' } +2s TuyAPI Error event from socket. 192.168.1.29 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) at RetryOperation._fn (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-retry/index.js:41:18) at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/retry/lib/retry_operation.js:81:10) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) { code: 'ERR_STREAM_DESTROYED' } +4s TuyAPI Error event from socket. 192.168.1.29 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) at RetryOperation._fn (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-retry/index.js:41:18) at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/retry/lib/retry_operation.js:81:10) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) { code: 'ERR_STREAM_DESTROYED' } +8s TuyAPI Error event from socket. 192.168.1.29 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () at pRetry.retries (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:239:14) at RetryOperation._fn (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-retry/index.js:41:18) at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/retry/lib/retry_operation.js:81:10) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) { code: 'ERR_STREAM_DESTROYED' } +16s (node:22853) UnhandledPromiseRejectionWarning: Error: Error from socket at Socket. (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:315:20) at errorOrDestroy (internal/streams/destroy.js:108:12) at onwriteError (_stream_writable.js:418:5) at onwrite (_stream_writable.js:445:5) at doWrite (_stream_writable.js:399:11) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:242:23 at new Promise () (node:22853) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 7) (node:22853) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm a total loss here. Any help would be much appreciated.

codetheweb commented 3 years ago

It looks like the device is closing the connection. Can you double check that nothing else is potentially connecting to it (including having the app open in the background on your phone)?

loaxley commented 3 years ago

@codetheweb That must have been the issue. I closed out of the Tuya app and rebooted Home Assistant (which had a deleted Tuya integration, but which had not yet been restarted), and get now works. I'm ok marking this as closed, but before we do so, I do want to bring to your attention one peculiar thing I noticed.

The following command works quickly and repeatedly: tuya-cli get --id REDACTED --key REDACTED --all

The following command does not work ever: tuya-cli get --id REDACTED --key REDACTED --ip 192.168.1.29 --all

Here's the debug output when running get with the IP (and yes, I am 100% sure the IP is correct--when running the command without the IP, the payload includes the IP and it is 192.168.1.29): DEBUG=* tuya-cli get --id REDACTED --key REDACTED --ip 192.168.1.29 --all TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 192.168.1.29... +6ms TuyAPI Socket connected. +459ms TuyAPI GET Payload: +2ms TuyAPI { TuyAPI gwId: 'REDACTED', TuyAPI devId: 'REDACTED', TuyAPI t: 'REDACTED', TuyAPI dps: {}, TuyAPI uid: 'REDACTED' TuyAPI } +0ms TuyAPI GET Payload: +10ms TuyAPI { TuyAPI gwId: 'REDACTED', TuyAPI devId: 'REDACTED', TuyAPI t: 'REDACTED', TuyAPI dps: {}, TuyAPI uid: 'REDACTED' TuyAPI } +0ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +11ms TuyAPI Parsed: +5ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms TuyAPI Pong from 192.168.1.29 +2ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +24ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms TuyAPI Pong from 192.168.1.29 +1ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +24ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms TuyAPI Pong from 192.168.1.29 +1ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +226ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms TuyAPI Pong from 192.168.1.29 +1ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +629ms TuyAPI Parsed: +0ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms TuyAPI Pong from 192.168.1.29 +0ms TuyAPI Pinging 192.168.1.29 +9s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +144ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms TuyAPI Pong from 192.168.1.29 +0ms TuyAPI Pinging 192.168.1.29 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +13ms TuyAPI Parsed: +0ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms TuyAPI Pong from 192.168.1.29 +0ms

The last 4 lines repeat over and over and it never even seems to time out...an endless ping with no payload.

codetheweb commented 3 years ago

The only difference in those commands should be that if you omit the IP the version number is automatically set from the device broadcast. Did you try specifying the version number (3.1 or 3.3) along with the IP?

Nodi-Rubrum commented 3 years ago

I can't get GET command to work at all. Even if I provide correct information. I have device in phone app, works. But tuya-cli refuses to work. Same as above. The wizard will return the list of linked devices but other than that, nothing works.

codetheweb commented 3 years ago

@Nodi-Rubrum if you're providing the IP when attempting to control your device, have you tried both 3.1 and 3.3 protocols?

Nodi-Rubrum commented 3 years ago

Yes, I have tried 3.1 and 3.3. I have a few that use 3.1 and most use 3.3. Of the 4 devices I just got, 3 of the 4 work without issue. Do not get this odd, disconnection issue. Just 1 of the 4 does. The stable 3 use 3.3.

cwttdb70 commented 3 years ago

@Nodi-Rubrum can you run your get command appended with DEBUG=* and post the results?

codetheweb commented 3 years ago

@Nodi-Rubrum responding in #88.

@loaxley closing, feel free to reopen if necessary.