Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host is unreachable, report error #224

Closed
subzero79 opened this issue Jul 8, 2019 · 7 comments
Closed

Host is unreachable, report error #224

subzero79 opened this issue Jul 8, 2019 · 7 comments

Comments

@subzero79
Copy link

subzero79 commented Jul 8, 2019

Hi there, just wanted to thank you for this library. There is a nodered plugin that uses this library but looks like the creator is not interested in updating to the latest tuyapi. I though it would just be to pass the version in the constructor and change the package version. But didn't work for me, so at the moment i am using the library by importing tuyapi into the global context which works good for me now.
Describe the bug
this is not like a bug but more like a question on the underlying net connect method. To use this tuya sockets i have them set up as mqtt switch in homeassistant, and all commanding and state report is done via nodered using mqtt in-out. To mark the devices as available or unavailable i use a 3 min interval ping to each device. To report the current state of the plugs i use a 10 sec interval query this time using the tuyapi module in a function node. I though to take out the ping out of the flow and just rely in tuyapi but seems like when host is unreachable there is no error report. I am correct to assume the net.connect method will not report any errors if the host is down? just will report on connections errors (sudden disconnect, refused, etc), the docs are not very specific on which errors

https://nodejs.org/api/net.html#net_event_connect

image

This is the code to check the status i use

const TuyAPI = global.get('tuyapi');
devObj = msg.payload

const device = new TuyAPI({
  id: devObj.devId,
  key: devObj.localKey,
  ip: devObj.ip,
  version: 3.3
});

device.connect();

device.on('connected', () => {
    node.status({fill:"green",shape:"dot",text:"connected"});
});

device.on('disconnected', () => {
    node.status({fill:"red",shape:"ring",text:"disconnected"});
});

device.on('error', error => {
  node.log('Error!', error);
});

let stateHasChanged = false;

device.on('data', data => {
    node.log('Data from device:', data);
    data.dps.power = data.dps['19'].toString()
    data.dps.state = data.dps['1']
    data.devObj = devObj
    delete data.dps['19']
    delete data.dps['1']
    node.send([data,null]);
    device.disconnect()

});

setTimeout(() => { device.disconnect(); }, 1000);

node.on('close', function() {
    device.disconnect() // tidy up any async code here - shutdown connections and so on.
});

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Debug Output
Post the output of your program/app/script when run with the DEBUG environment variable set to *. Example: DEBUG=* node test.js. Copy the output and paste it below (in between the code fences):


Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [Debian]
  • OS Version [9]
  • Node Version [v10.16.0]

Additional context
Add any other context about the problem here.

@codetheweb
Copy link
Owner

Correct me if I"m wrong, but I think the root of your confusion lies in thinking that TuyAPI exposes a native NodeJS socket. Although the interface is similar, TuyAPI does not expose the full underlying socket and the function signatures between the NodeJS Net class and TuyAPI are not the same.

A few things:

  • If a device is offline, running await device.connect() should throw an error (I think, haven't actually tested this recently).
  • You can call device.isConnected() to get the current connection status
  • You should be awaiting the initial device.connect(). So change that line to await device.connect().

@subzero79
Copy link
Author

subzero79 commented Jul 10, 2019

thanks for the answer. Yes i know tuyapi doesn't expose any socket, is because there is a error listener i would expect it to report on host unreachable but since net docs are not clear that when host is down it would raise error, neither then tuyapi would do report it.

Thanks again for your suggestion i'll test them, i did miss out reading the isConnected function

@codetheweb
Copy link
Owner

Got it, let me know if you have any other questions.

And if you discover anything inconsistent in your testing between real-world behavior and the TuyAPI docs, please let me know so I can update them.

@subzero79
Copy link
Author

I took some time to read node-red docs to create an exclusive node for this. Very basic at the moment just by using the asynchronous example on the README, i simply couldn't figure it out from node-red-constrib-tuya-smart.

So one thing i noticed, i have to disconnect the device on re-deploy otherwise it won't connect again due so the single connection limit. I do it like this, which works

		this.on('close', function() {
			if (device.isConnected()) {
				// node.warn("NR re-deploy, disconnecting")
				device.disconnect();
			}
		});

now....when the device is taken off from mains while connected, i would expect a disconnect event or error but nothing comes through. This can be important as it can be used to mark the device as offline or unavailable (mqtt switch in homeassistant for example). The event gets fired when i re-deploy, which means device.disconnect() gets called and a new connection attempt is made timing out as it should.

Back to the original post could this be a limitation on the net library or can it be handled by tuyapi?

@subzero79
Copy link
Author

Just an update i left the example running on DEBUG=* and i can see the keep alive ping pong, so i when i plug off the device i get a disconnect event about 100 pings later, that's about 16 minutes

DEBUG=* node mqtt.js 
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 10.10.4.81... +6ms
subscribed to presence!
  TuyAPI Socket connected. +73ms
Connected to device!
  TuyAPI GET Payload: +1ms
  TuyAPI { gwId: '82180707c44f33a6743a', devId: '82180707c44f33a6743a' } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000009c00000000c4e8dd3684830606c0687cb3dd8880b28aec23485ab3419f3575f0891ff8ee6fb50238b0f26b16b03b7ec7b2934de093924e0e208e96f2836c1db164c8fdcbba71cb8e9b56f6c1106dfd3ccfa35bc2c8572fa2bf2faf95a09179e6dde03f6267bf0cb281bab7c9ed15234436dcfe24676a03b5f39d8313d719f8343754e74ffa6705aa726b4e1a8a5c8ccff5b83e5fc91b7b81ad0000aa55 +50ms
  TuyAPI Parsed: +2ms
  TuyAPI {
  TuyAPI   payload: {
  TuyAPI     devId: '82180707c44f33a6743a',
  TuyAPI     dps: {
  TuyAPI       '1': false,
  TuyAPI       '9': 0,
  TuyAPI       '18': 0,
  TuyAPI       '19': 0,
  TuyAPI       '20': 2459,
  TuyAPI       '21': 1,
  TuyAPI       '22': 616,
  TuyAPI       '23': 30909,
  TuyAPI       '24': 18099,
  TuyAPI       '25': 1152
  TuyAPI     }
  TuyAPI   },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1
  TuyAPI } +1ms
Data from device: {
  devId: '82180707c44f33a6743a',
  dps: {
    '1': false,
    '9': 0,
    '18': 0,
    '19': 0,
    '20': 2459,
    '21': 1,
    '22': 616,
    '23': 30909,
    '24': 18099,
    '25': 1152
  }
}
Boolean status of default property: false.
false
topic:presence/state false
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +27ms
  TuyAPI Parsed: +1ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 10.10.4.81 +0ms
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +53ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 10.10.4.81 +1ms
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +82ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 10.10.4.81 +0ms
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Pinging 10.10.4.81 +10s
  TuyAPI Error event from socket. 10.10.4.81 Error: read ETIMEDOUT
    at TCP.onStreamRead (internal/stream_base_commons.js:183:27) {
  errno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'read'
} +2s
Error! Error: Error from socket
    at Socket.<anonymous> (/home/ricardo/Documents/develop/scratch/node_modules/tuyapi/index.js:323:30)
    at Socket.emit (events.js:203:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
  TuyAPI Socket closed: 10.10.4.81 +3ms
Disconnected from device.

@kueblc
Copy link
Collaborator

kueblc commented Aug 8, 2019

Heartbeat disconnect is being discussed for v6.0.0 #169 (comment)

@subzero79
Copy link
Author

subzero79 commented Aug 8, 2019

Thanks very much. I'll subscribe to the post for updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants