codetheweb / tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.
MIT License
2.05k stars 337 forks source link

Unhandled 'error' event #382

Open bb165source opened 3 years ago

bb165source commented 3 years ago

I keep running into a Unhandled 'error' event each time I seem to run a get or set command.

I have successfully managed to retrieve the keys and the Virtual ID after configuring it with tuya-cli wizard. but then when I run DEBUG=* tuya-cli get --id xxx --key yyy --ip 192.168.2.X --protocol-version 3.3 --all

 TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.2.X... +19ms
  TuyAPI Socket connected. +53ms
  TuyAPI GET Payload: +4ms
  TuyAPI {
  TuyAPI   gwId: 'xxx',
  TuyAPI   devId: 'xxx',
  TuyAPI   t: 'zzz',
  TuyAPI   dps: {},
  TuyAPI   uid: 'xxx'
  TuyAPI } +0ms
  TuyAPI GET Payload: +40ms
  TuyAPI {
  TuyAPI   gwId: 'xxx',
  TuyAPI   devId: 'xxx',
  TuyAPI   t: 'zzz',
  TuyAPI   dps: {},
  TuyAPI   uid: 'xxx'
  TuyAPI } +1ms
  TuyAPI Received data: 000055aa000000010000000a0000002c0000000174dcd8742e1a111e4c3813eff5cc79603802e61d8d669ee45311ad10613dc1ad68531c3e0000aa55 +12ms
  TuyAPI Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')
  TuyAPI     at TuyaDevice.emit (events.js:303:17)
  TuyAPI     at Socket.<anonymous> (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:319:22)
  TuyAPI     at Socket.emit (events.js:314:20)
  TuyAPI     at addChunk (_stream_readable.js:297:12)
  TuyAPI     at readableAddChunk (_stream_readable.js:272:9)
  TuyAPI     at Socket.Readable.push (_stream_readable.js:213:10)
  TuyAPI     at TCP.onStreamRead (internal/stream_base_commons.js:188:23) +14ms
events.js:291
      throw er; // Unhandled 'error' event
      ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')
    at TuyaDevice.emit (events.js:303:17)
    at Socket.<anonymous> (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:319:22)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on TuyaDevice instance at:
    at Socket.<anonymous> (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:335:18)
    at Socket.emit (events.js:314:20)
    [... lines matching original stack trace ...]
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 'json obj data unvalid'
}

If I run tuya-cli set --id xxx --key yyy --ip 192.168.2.X --protocol-version 3.3 --dps 1 --set true (or false)

I still get the same error as above, but funnily my plug switches on (or off).

I'm not sure where I am going wrong.

llevering commented 3 years ago

There is a new 'feature' in the latest Tuya release it seems... The local api (or at least the endpoint we most use) is not available until you've had contact with the Tuya app. If you open the app interact with the device and then close the app (maybe even do 'Force stop' of the app as it stays sometimes running in the background) and then try to connect again, is the use then maybe resolved (if this is the root cause of the exception)?

bb165source commented 3 years ago

There is a new 'feature' in the latest Tuya release it seems... The local api (or at least the endpoint we most use) is not available until you've had contact with the Tuya app. If you open the app interact with the device and then close the app (maybe even do 'Force stop' of the app as it stays sometimes running in the background) and then try to connect again, is the use then maybe resolved (if this is the root cause of the exception)?

Thank for your suggestion. I tried doing the above but I'm experiencing the same error. I tried rolling back to a version of Smart Life app from October.

Do you have any other suggestions?

llevering commented 3 years ago

No sorry this is the only issue I've run into when using this api. Probably a different root cause then.

bb165source commented 3 years ago

Thanks.

Maybe @codetheweb can help?

codetheweb commented 3 years ago

Have you tried using the 3.1 protocol?

Also, what kind of device do you have?

bb165source commented 3 years ago

With protocol 3.1, I get a 'parse data error'.

The device I am using is a LSPA7 UK Smart Socket.

codetheweb commented 3 years ago

Hmm.

It's possible that this is related.

Could you try the event-based example in the README and see if you have the same issue?

bb165source commented 3 years ago

So I copied the example and edited in my id and key and saved the file as async.js file. Then I ran 'node async.js'. I then go the following error:

C:\Users\User\Downloads\PortableGit\node-v14.15.1-win-x86>node async.js
(node:8336) UnhandledPromiseRejectionWarning: Error: find() timed out. Is the device powered on and the ID or IP correct?
    at C:\Users\User\Downloads\PortableGit\node-v14.15.1-win-x86\node_modules\tuyapi\index.js:665:13
    at Timeout._onTimeout (C:\Users\User\Downloads\PortableGit\node-v14.15.1-win-x86\node_modules\tuyapi\node_modules\p-timeout\index.js:25:13)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:8336) 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: 1)
(node:8336) [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 not sure if the above is the correct way of running the script.

LoikMaksim commented 3 years ago

I had same problem

 TuyAPI Received data: 000055aa000000010000000a0000002c00000001023a6a79770961b8                                                                                                                                                                                                                                             35e7d3821d9bb1d2c13d3dc9a4f2fbcbbff8884f794397e787254f990000aa55 +5ms
  TuyAPI Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')

One of my devices give me error. (moes socket)

I had try async and await reguest - no matter;

api.find();

api.connect()

//if try get properties api.get()

uyAPI Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')
  TuyAPI     at TuyaDevice.emit (events.js:182:17)
  TuyAPI     at Socket.client.on.data (/usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:319:22)
  TuyAPI     at Socket.emit (events.js:193:13)
  TuyAPI     at addChunk (_stream_readable.js:295:12)
  TuyAPI     at readableAddChunk (_stream_readable.js:276:11)
  TuyAPI     at Socket.Readable.push (_stream_readable.js:231:10)
  TuyAPI     at TCP.onStreamRead (internal/stream_base_commons.js:154:17) +0ms
events.js:170
      throw er; // Unhandled 'error' event

api.disconnect()

bb165source commented 3 years ago

I seem to be getting a new error now and running a set command @codetheweb :

Set succeeded.
node:internal/process/promises:225
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('Timeout waiting for status response from device id: xxx')
    at new NodeError (node:internal/errors:278:15)
    at TuyaDevice.emit (node:events:365:17)
    at /usr/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:257:12
    at Timeout._onTimeout (/usr/lib/node_modules/@tuyapi/cli/node_modules/p-timeout/index.js:25:13)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 'Timeout waiting for status response from device id: xxx'
}
codetheweb commented 3 years ago

Please post the full debug output.

My guess is that another device (phone, server, etc.) is trying to control it at the same time.

grzes13 commented 3 years ago

I'm having the same issue and as for @bb165source command switches the device on/off successfully:

$ DEBUG=* tuya-cli set --ip xx.xx.xx.148 --id xxx-id --key xxx-key --dps 1 --protocol-version 3.3 --set false
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to xx.xx.xx.148... +11ms
  TuyAPI Socket connected. +14ms
  TuyAPI GET Payload: +3ms
  TuyAPI { gwId: 'xxx-id',
  TuyAPI   devId: 'xxx-id',
  TuyAPI   t: '1609880929',
  TuyAPI   dps: {},
  TuyAPI   uid: 'xxx-id' } +0ms
  TuyAPI SET Payload: +22ms
  TuyAPI { devId: 'xxx-id',
  TuyAPI   gwId: 'xxx-id',
  TuyAPI   uid: '',
  TuyAPI   t: 1609880929,
  TuyAPI   dps: { '1': false } } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000002c00000001bf8f82d0c7144458a9bbee282bdf2c01fe8090e18c7e8ee241d3b00afa0f37ccca60f24e0000aa55 +7ms
  TuyAPI Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')
  TuyAPI     at TuyaDevice.emit (events.js:187:17)
  TuyAPI     at Socket.client.on.data (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:319:22)
  TuyAPI     at Socket.emit (events.js:198:13)
  TuyAPI     at addChunk (_stream_readable.js:288:12)
  TuyAPI     at readableAddChunk (_stream_readable.js:269:11)
  TuyAPI     at Socket.Readable.push (_stream_readable.js:224:10)
  TuyAPI     at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) +5ms
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('json obj data unvalid')
    at TuyaDevice.emit (events.js:187:17)
    at Socket.client.on.data (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:319:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
Emitted 'error' event at:
    at Socket.client.on.data (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:335:18)
    at Socket.emit (events.js:198:13)
    [... lines matching original stack trace ...]
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

Running the event-based example results in error below:

Connected to device!
Data from device: json obj data unvalid
xtest.js:31
  console.log(`Boolean status of default property: ${data.dps['1']}.`);
                                                             ^

TypeError: Cannot read property '1' of undefined
    at TuyaDevice.device.on.data (/xxx/test.js:31:62)
    at TuyaDevice.emit (events.js:198:13)
    at TuyaDevice._packetHandler (/xxx/node_modules/tuyapi/index.js:489:10)
    at packets.forEach.packet (/xxx/node_modules/tuyapi/index.js:382:43)
    at Array.forEach (<anonymous>)
    at Socket.client.on.data (/xxx/node_modules/tuyapi/index.js:378:19)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)

After setting the issueGetOnConnect: false it returns no data:

Connected to device!
Disconnected from device
tuya-cli --version
1.13.4

I've also tested the synchronous version and it seems to work beside gathering the the schema with "device.get({schema: true}).then(data => console.log(data))" which results in: json obj data unvalid but it is possible to access the properties directly:

  await device.get({dps: 0}).then(status => console.log(status))
  await device.get({dps: 18}).then(status => console.log(status))
  await device.get({dps: 19}).then(status => console.log(status))
  await device.get({dps: 20}).then(status => console.log(status))
codetheweb commented 3 years ago

This sounds very similar to the behavior described here: https://github.com/codetheweb/tuyapi/issues/389.

axtux commented 3 years ago

I have the same problem if I set the IP of the device. If I do not set it, tuyapi waits for the UDP broadcast and then everything is fine.

I tried on a local Wi-Fi with no internet and no other clients than the tuya device and I have the same error so I can tell you that nothing else is accessing the device at the same time.

codetheweb commented 3 years ago

I have the same problem if I set the IP of the device. If I do not set it, tuyapi waits for the UDP broadcast and then everything is fine.

I tried on a local Wi-Fi with no internet and no other clients than the tuya device and I have the same error so I can tell you that nothing else is accessing the device at the same time.

This likely means that you're setting the wrong version in the constructor / omitting it entierly. Try both version: 3.1 and version: 3.3 in the constructor and see if that works. Please open a new issue if you continue to have issues.