skydiver / ewelink-api

eWeLink API for JavaScript
https://www.npmjs.com/package/ewelink-api
MIT License
268 stars 108 forks source link

Online Offline state ? #66

Open foulek57 opened 4 years ago

foulek57 commented 4 years ago

Hi,

I want to check if a device are online before i send a toggle cmd.

Any idea to check it ?

Thanks.

ttz642 commented 4 years ago

Is that for local connection or via the server?

For local send a ping, for via the cloud read the status of the device.

ttz642 commented 4 years ago

You can scan your local network:

$ avahi-browse -d local _ewelink._tcp --resolve -tp | grep -P '^\+'
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local

wait a few seconds for every device to reply:

$ avahi-browse -d local _ewelink._tcp --resolve -tp | grep -P '^\+'
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local
+;wlp2s0;IPv4;eWeLink_1000XXXXXX;_ewelink._tcp;local

If you need the IP address of each device:

$ avahi-browse -d local _ewelink._tcp --resolve -tp | pcre2grep --om-separator='|'  -o1 -o2 -i  'local;eWeLink_(1000[0-9a-f]{6}).local;(192.168.1\..+);8081;'
1000XXXXXX|192.168.1.202
1000XXXXXX|192.168.1.220
1000XXXXXX|192.168.1.227
1000XXXXXX|192.168.1.237
1000XXXXXX|192.168.1.226
1000XXXXXX|192.168.1.233
1000XXXXXX|192.168.1.249
1000XXXXXX|192.168.1.238
foulek57 commented 4 years ago

Hi, It is for could connection, I want to check if the device a online, but from the socket directly (with socket.send()) Without an await, the getDevice do not work with the socket.

Want to do in this

` const socket = await connection.openWebSocket(async data => { // data is the message from eWeLink console.log(data)

socket.send(payload); }); `

But I do know what I need to send as payload

ttz642 commented 4 years ago

Hi, It is for could connection, I want to check if the device a online, but from the socket directly (with socket.send()) Without an await, the getDevice do not work with the socket.

Want to do in this

` const socket = await connection.openWebSocket(async data => { // data is the message from eWeLink console.log(data)

socket.send(payload); }); `

This opens a web socket to the eweLink cloud for listening for events, it does not connect directly to the device and so success only confirms you've connected to the cloud.