TuyaAPI / cli

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

tuya-cli set not working Error event from socket. Error: write EPIPE #80

Closed cdprince closed 3 years ago

cdprince commented 3 years ago

This does not seem 100% repeatable I have just updated my npm and apt-get upgrade then updated tuyaapi/cli tuya-cli -V 1.13.3 $ npm -v 5.8.0

I ran this command yesterday many times and it worked fine turning on and off a power strip socket, except for consecutive identical commands (https://github.com/iRayanKhan/homebridge-tuya/issues/116)

$ DEBUG=* tuya-cli set --ip '10.0.0.212' --id 73328546dc4f22eb3795 --key 153e749667XXXXXX --dps 1 --set 1 TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 10.0.0.212... +11ms TuyAPI Socket connected. +147ms TuyAPI GET Payload: +2ms TuyAPI { gwId: '73328546dc4f22eb3795', TuyAPI devId: '73328546dc4f22eb3795', TuyAPI t: '1595467267', TuyAPI dps: {}, TuyAPI uid: '73328546dc4f22eb3795' } +1ms TuyAPI SET Payload: +19ms TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI gwId: '73328546dc4f22eb3795', TuyAPI uid: '', TuyAPI t: 1595467267, TuyAPI dps: { '1': 1 } } +1ms TuyAPI Error event from socket. 10.0.0.212 { Error: write EPIPE at WriteWrap.afterWrite [as oncomplete] (net.js:789:14) errno: 'EPIPE', code: 'EPIPE', syscall: 'write' } +11ms events.js:174 throw er; // Unhandled 'error' event ^

Error: Error from socket at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:198:13) at errorOrDestroy (internal/streams/destroy.js:107:12) at onwriteError (_stream_writable.js:436:5) at onwrite (_stream_writable.js:461:5) at _destroy (internal/streams/destroy.js:49:7) at Socket._destroy (net.js:614:3) at Socket.destroy (internal/streams/destroy.js:37:8) at WriteWrap.afterWrite [as oncomplete] (net.js:791:10) Emitted 'error' event at: at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:16) at Socket.emit (events.js:198:13) [... lines matching original stack trace ...] at WriteWrap.afterWrite [as oncomplete] (net.js:791:10)

I tried this syntax, just in case there was a change "--set True", did not work $ DEBUG=* tuya-cli set --ip '10.0.0.212' --id 73328546dc4f22eb3795 --key 153e749667XXXXXX --dps 1 --set True TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 10.0.0.212... +10ms TuyAPI Socket connected. +160ms TuyAPI GET Payload: +2ms TuyAPI { gwId: '73328546dc4f22eb3795', TuyAPI devId: '73328546dc4f22eb3795', TuyAPI t: '1595467301', TuyAPI dps: {}, TuyAPI uid: '73328546dc4f22eb3795' } +0ms TuyAPI SET Payload: +20ms TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI gwId: '73328546dc4f22eb3795', TuyAPI uid: '', TuyAPI t: 1595467300, TuyAPI dps: { '1': true } } +0ms TuyAPI Error event from socket. 10.0.0.212 { Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' } +120ms events.js:174 throw er; // Unhandled 'error' event ^

Error: Error from socket at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:198:13) at emitErrorNT (internal/streams/destroy.js:91:8) at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:16) at Socket.emit (events.js:198:13) [... lines matching original stack trace ...] at process._tickCallback (internal/process/next_tick.js:63:19)

Decided to check repeatability for turning off a socket This looks like the error from (https://github.com/iRayanKhan/homebridge-tuya/issues/116) $ DEBUG=* tuya-cli set --ip '10.0.0.212' --id 73328546dc4f22eb3795 --key 153e74966XXXXXXX --dps 1 --set 0 TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 10.0.0.212... +10ms TuyAPI Socket connected. +300ms TuyAPI GET Payload: +2ms TuyAPI { gwId: '73328546dc4f22eb3795', TuyAPI devId: '73328546dc4f22eb3795', TuyAPI t: '1595468555', TuyAPI dps: {}, TuyAPI uid: '73328546dc4f22eb3795' } +0ms TuyAPI SET Payload: +20ms TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI gwId: '73328546dc4f22eb3795', TuyAPI uid: '', TuyAPI t: 1595468554, TuyAPI dps: { '1': 0 } } +1ms TuyAPI Received data: 000055aa000000010000000a00000064000000007b226465764964223a223733333238353436646334663232656233373935222c22647073223a7b2231223a66616c73652c2232223a747275652c2233223a747275652c2239223a302c223130223a302c223131223a307d7d9a0cfe410000aa55 +12ms TuyAPI Parsed: +4ms TuyAPI { payload: TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI dps: TuyAPI { '1': false, '2': true, '3': true, '9': 0, '10': 0, '11': 0 } }, TuyAPI leftover: false, TuyAPI commandByte: 10, TuyAPI sequenceN: 1 } +1ms TuyAPI Received data: 000055aa00000002000000070000000c0000000018cfc5da0000aa55 +14ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 7, sequenceN: 2 } +0ms TuyAPI Got SET ack. +1ms TuyAPI Pinging 10.0.0.212 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +2s TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms TuyAPI Pong from 10.0.0.212 +1ms TuyAPI Pinging 10.0.0.212 +8s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +16ms TuyAPI Parsed: +0ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms TuyAPI Pong from 10.0.0.212 +1ms ^C

Tried to check repeatability turning on a wifi power strip socket $ DEBUG=* tuya-cli set --ip '10.0.0.212' --id 73328546dc4f22eb3795 --key 153e749667097341 --dps 1 --set 1 TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 10.0.0.212... +11ms TuyAPI Socket connected. +28ms TuyAPI GET Payload: +3ms TuyAPI { gwId: '73328546dc4f22eb3795', TuyAPI devId: '73328546dc4f22eb3795', TuyAPI t: '1595468591', TuyAPI dps: {}, TuyAPI uid: '73328546dc4f22eb3795' } +0ms TuyAPI SET Payload: +19ms TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI gwId: '73328546dc4f22eb3795', TuyAPI uid: '', TuyAPI t: 1595468591, TuyAPI dps: { '1': 1 } } +1ms TuyAPI Received data: 000055aa000000010000000a00000064000000007b226465764964223a223733333238353436646334663232656233373935222c22647073223a7b2231223a66616c73652c2232223a747275652c2233223a747275652c2239223a302c223130223a302c223131223a307d7d9a0cfe410000aa55 +15ms TuyAPI Parsed: +7ms TuyAPI { payload: TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI dps: TuyAPI { '1': false, '2': true, '3': true, '9': 0, '10': 0, '11': 0 } }, TuyAPI leftover: false, TuyAPI commandByte: 10, TuyAPI sequenceN: 1 } +0ms TuyAPI Received data: 000055aa00000002000000070000000c0000000018cfc5da0000aa55 +12ms TuyAPI Parsed: +1ms TuyAPI { payload: false, leftover: false, commandByte: 7, sequenceN: 2 } +0ms TuyAPI Got SET ack. +1ms TuyAPI Received data: 000055aa00000000000000080000008b00000000332e313430633634646362646363313531386545444a74713576434e7a646c66632b2f627766477133474d4130684d7a4b4d56625a6f4262536d2f6b4a2b5367335a56436d2b562f62506348766b416c41347251616b43676535414d4c336a335273455466486f4c6334504d3967763677322b6858374775734a42556b733d2b1663320000aa55 +54ms TuyAPI Parsed: +1ms TuyAPI { payload: TuyAPI { devId: '73328546dc4f22eb3795', TuyAPI dps: { '1': true }, TuyAPI t: 1595468590, TuyAPI s: 7 }, TuyAPI leftover: false, TuyAPI commandByte: 8, TuyAPI sequenceN: 0 } +1ms TuyAPI Disconnect +2ms Set succeeded. TuyAPI Socket closed: 10.0.0.212 +8ms

Wait -- this worked, perhaps this is not going to be repeatable? Now I am running without error. I can no longer repeat the problem.

cdprince commented 3 years ago

I tried to see repeatablitiy and 1 in around 22 tries I get this error: events.js:174 throw er; // Unhandled 'error' event ^

Error: Error from socket at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:30) at Socket.emit (events.js:198:13) at errorOrDestroy (internal/streams/destroy.js:107:12) at onwriteError (_stream_writable.js:436:5) at onwrite (_stream_writable.js:461:5) at _destroy (internal/streams/destroy.js:49:7) at Socket._destroy (net.js:614:3) at Socket.destroy (internal/streams/destroy.js:37:8) at WriteWrap.afterWrite [as oncomplete] (net.js:791:10) Emitted 'error' event at: at Socket.client.on.err (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:350:16) at Socket.emit (events.js:198:13) [... lines matching original stack trace ...] at WriteWrap.afterWrite [as oncomplete] (net.js:791:10)

and when I get this error it takes a little bit to settle down the device, where I am much more likely to get consecutive errors.

codetheweb commented 3 years ago

Unfortunately I can't really help unless you provide a consistently reproducible example. "It happens every 22 tries" sounds more like a device issue than an issue with TuyAPI. Does this happen with multiple different types of devices, or just one?

I assume #79 is related, so I'm closing that for now.

cdprince commented 3 years ago

I do not think it is device related anymore. I can use the tuya smart app on my phone and control the device and I do not see any issues. But some of my associates see this a lot more frequently by orders of magnitude, so perhaps it is environment, or wifi related. I wonder if there is a tcp packet problem not handled. I do not have the time right now to track this down -- just a complete guess, is TCP reset handled?

codetheweb commented 3 years ago

It's possible the app controls the device via cloud services and/or has more robust retry mechanisms in place.

The 'close' event is handled for TCP sockets, so I assume RST is handled under that.

codetheweb commented 3 years ago

Closing because of inactivity.