codetheweb / tuyapi

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

Frequent errors #17

Closed unparagoned closed 6 years ago

unparagoned commented 6 years ago

I'm getting the following quite frequently, using the basic toggle example provided. I've tried to figure out what is going on but I simply don't get js and how it works.

(node:6521) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it. (node:6521) 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.

edit: https://github.com/unparagoned/njsTuya/issues/1

Mostly the toggle works but getting the state afterword is a problem. What I've done is just rework myscript so it doesn't ask for the state again and it seems to be working fine. I just add some code around it and assume if a return result is true then it's changed state, rather than performing a second get call.

Edit: I get the errors now and then with the modified code but it's much less frequent.

codetheweb commented 6 years ago

I just published an update that allows you to log some useful information to the console. Update tuyapi, then run your script with DEBUG=* node script.js.

Post the output here, and maybe we can figure out what's going on.

binary-person commented 6 years ago

Same problem, I just used

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  process.exit(1);
});

in my main code to intentionally exit the program and used forever-monitor:

var forever = require('forever-monitor');
var child = new (forever.Monitor)('main.js');

child.on('watch:restart', function(info) {
    console.error('Restaring script because ' + info.file + ' changed');
});

child.on('restart', function() {
    console.error('Forever restarting script for ' + child.times + ' time');
});

child.on('exit:code', function(code) {
    console.error('Forever detected script exited with code ' + code);
});
child.start();

to keep it going. Just an idea, hope it helps. -- Simon

unparagoned commented 6 years ago

@codetheweb

[01:24:46] openhabian@debra:/etc/openhab2/scripts$ DEBUG=* node njstuya.js -ip 192.168.2.81 -id 012001865ccf7f598277 -key 00cfs888v58f12a TOGGLE
  TuyAPI Device(s):  +0ms
  TuyAPI [ { id: '01200***5ccf7f598277',
  TuyAPI     key: '00cfs***2v58f12a',
  TuyAPI     type: 'outlet',
  TuyAPI     uid: '',
  TuyAPI     port: 6668,
  TuyAPI     version: 3.1,
  TuyAPI     cipher:
  TuyAPI      { algorithm: [Object],
  TuyAPI        mode: [Object],
  TuyAPI        blockSize: 16,
  TuyAPI        _finish: false,
  TuyAPI        _input: null,
  TuyAPI        output: null,
  TuyAPI        _op: [Function],
  TuyAPI        _decrypt: false } } ] +2ms
  TuyAPI Received UDP message. +444ms
  TuyAPI Parsing this data to JSON:  000055aa00000000000000000000009e000000007b226970223a223139322e3136382e322e3831222c2267774964223a223031323030313836356363663766353938323737222c22616374697665223a322c226162696c697479223a302c226d6f6465223a302c22656e6372797074223a747275652c2270726f647563744b6579223a2271484c4e417a6d7252644139454d586c222c2276657273696f6e223a22332e31227dabd0b1910000aa55 +1ms
  TuyAPI Parsing this data to JSON:  000055aa00000000000000000000009e000000007b226970223a223139322e3136382e322e3831222c2267774964223a223031323030313836356363663766353938323737222c22616374697665223a322c226162696c697479223a302c226d6f6465223a302c22656e6372797074223a747275652c2270726f647563744b6579223a2271484c4e417a6d7252644139454d586c222c2276657273696f6e223a22332e31227dabd0b1910000aa55 +0ms
  TuyAPI Received UDP message. +2s
  TuyAPI Parsing this data to JSON:  000055aa00000000000000000000009f000000007b226970223a223139322e3136382e322e313435222c2267774964223a223031323030313836356363663766353937633162222c22616374697665223a322c226162696c697479223a302c226d6f6465223a302c22656e6372797074223a747275652c2270726f647563744b6579223a2271484c4e417a6d7252644139454d586c222c2276657273696f6e223a22332e31227dd494f9ea0000aa55 +0ms
  TuyAPI Payload:  +1ms
  TuyAPI { gwId: '01200***ccf7f598277', devId: '01200***5ccf7f598277' } +0ms
  TuyAPI Sending this data:  000055aa000000000000000a000000467b2267774964223a223031323030313836356363663766353938323737222c226465764964223a223031323030313836356363663766353938323737227d000000000000aa55 +1ms
  TuyAPI Received data back. +10ms
  TuyAPI Parsing this data to JSON:  000055aa000000000000000a0000003e000000007b226465764964223a223031323030313836356363663766353938323737222c22647073223a7b2231223a66616c73657d7df2b3e53c0000aa55 +0ms
  TuyAPI Payload:  +1ms
  TuyAPI { devId: '01200***ccf7f598277',
  TuyAPI   uid: '',
  TuyAPI   t: '1517189326',
  TuyAPI   dps: { '1': true } } +0ms
  TuyAPI Sending this data:  000055aa000000000000000700000087332e3137313962333433386237353063326436686b777a37724a553247356a45684365466935374a4a424c596131683444674668786f546172324d7a4d5438447130624869446a714e4c4565537138486e56344f79683756506f666644667244597851627947366c467648524c44646949764f494e53575a38676f6146553d000000000000aa55 +3ms
(node:7225) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it.
(node:7225) 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.
codetheweb commented 6 years ago

Weird. Everything looks fine. (Just a note: my setup works with ~95% reliability.) I'm sorry but there's not much I can help you with. Are you sure nothing else is trying to control it?

unparagoned commented 6 years ago

I'm pretty sure I don't have any other apps open and tried closing everything on the phone. But I'm playing about with virtually every home automation ecosystem, I've seen some bump into each other by mistake. What would the best way be to see everything talking to the switch? Can I simply fire up wireshark or would I need to set up some kind of proxy gateway to connect the switch through to monitor everything?

codetheweb commented 6 years ago

If every possible device that could be controlling it is connected via WiFi, it should be possible to monitor connections to it with Wireshark (although I'm not 100% sure on this).

I'm currently in the process of adding a retry function if net.connect() returns with a ECONNRESET error. Hopefully this will reduce errors for you.

unparagoned commented 6 years ago

I've made a few changes so not sure what has fixed it. I've got locking and some small time delays to prevent the script being run to close to a previous run. Errors are infrequent now, and when I come into contact with one I just rerun the command.

I'm closing this since I'm having difficulty even creating the error anymore.