Python plugin: Presence detection from wireless router

Python and python framework
User avatar
Innovator
Posts: 12
Joined: Tuesday 20 February 2018 13:33
Target OS: NAS (Synology & others)
Domoticz version: 8924
Location: Amsterdam
Contact:

Re: Python plugin: Presence detection from wireless router

Post by Innovator » Wednesday 13 June 2018 20:09

EscApe wrote:
Tuesday 12 June 2018 22:18
@escape
If setting up key based access for the user running Domoticz on the Synology (root??) is not an option you can change a small piece of code in the plugin.
it's root

Look for (will be around line 81 in the version i'm uploading in a few minutes):

Code: Select all

cmd =[self.sshbin, "-o", "ConnectTimeout=" + str(sshtimeout), user+"@"+host, routerscript]
And change it to (with whatever path you are using):

Code: Select all

cmd =[self.sshbin, "-i", "/volume1/path/to/sshfiles/privatekeyfile", "-o", "ConnectTimeout=" + str(sshtimeout), user+"@"+host, routerscript]
I have nothing changed use version 0.4.1
You can experiment from the synology command line to confirm it is working: ssh -i <keyfile-path> usernameonrouter@routerip
ssh keyfile is correct working

I have added the wlanconfig command and ath* interface names to the latest version of the plugin. Will upload it in a few minutes. If anything goes wrong please include some debug logging of the plugin starting up. The part you included only shows that the polling failed, but the reason is a failure while initializing the plugin.

At this moment the plugin create no devices. The domticz log shows only
2018-06-13 19:49:53.064 (iDetect Wifi presence detection) Initialized version 0.4.1, author 'ESCape'
2018-06-13 19:49:53.064 (iDetect Wifi presence detection) Entering work loop.
no errors, debug = on
Version 0.4.1. is unfortunately not working
Synology DS918+ /DS412+
ASUS BRT-AC828, RT-AC86U, RT-AC68U

EscApe
Posts: 103
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Wednesday 13 June 2018 20:29

There have been some changes that require extra attention when upgrading from an earlier version.

In this order:

Make sure the ioverride.zip file (new since 0.4) is in you plugin directory (besides plugin.py and the orther zip files)
Restart domoticz! (Otherwise settings page is not updated)
go to the hardware settings page for the plugin and set the new parameters (and save)

devros
Posts: 148
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Friday 15 June 2018 10:20

Thanks now it all works (almost) without errors
sometimes i have in log timeout (few times in one log)
should i try modify script to make longer timeouts ?

Code: Select all

2018-06-15 10:03:57.215 Error: (presence) SSH subprocess failed with error: Command '['sshpass', '-p', 'XXXXX', 'ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=1', 'ubnt@XXXXX', "export PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH;wlanconfig ath0 list | grep '^..:..:..:..:..:.. ' | cut -d' ' -f1;wlanconfig eth0 list | grep '^..:..:..:..:..:.. ' | cut -d' ' -f1;exit"]' timed out after 2 seconds
2018-06-15 10:03:57.216 Error: (presence) Failed to poll present phones from router
2018-06-15 10:03:57.216 Error: (presence) No list of connected WLAN devices from router

EscApe
Posts: 103
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Friday 15 June 2018 16:46

@devros

Glad it's working! :D

Since the plugin runs an external program (ssh) it will block the plugin system whilst ssh is running. From the wiki: "Plugins are single threaded so the whole plugin system will wait." That's why i kept the timeouts short. If the router, network or Domoticz system is very busy it might fail, but at least it will not block the plugin system for long.

If the errors are not that frequent you can just ignore them. It only means you miss one poll cycle and presence/absence will be detected again on the next poll.

The timeouts relevant are:
sshtimeout: time it may take ssh to establish the connection to the router (shorter than alltimeout because real connection issues are most likely to last longer, so you want to skip the poll as soon as possible)
alltimeout: time the entire ssh command may take on the host system (a failsafe in case of unforeseen problems that can not be detected as quick as a connection issue. An example would be a working ssh connection, but hanging command on the router)

You can experiment with the timeouts. If your poll interval is more than 30 second i see no harm in increasing the (alltimeout) of the subprocess to 3-5 seconds. Worst case it will block your plugin system for 10% or even 16,6% of the time, but only if the entire command takes that long for some reason (very busy Domoticz host or router?). The sshtimeout is a different story. If your router is somehow unreachable or very slow to respond (down, rebooting, disconnected) the plugin might wait the full amount of the timeout every single time it tries to poll. On the other hand if it only polls every 30 seconds it will probably do no real harm if you set the sshtimeout to 2-3 seconds.

I poll every 10 seconds, so short timeouts are crucial to prevent the plugin from blocking the plugin system for most of the time. It is all a question of balance. On my dedicated raspberry pi i get very very few errors. The ones that did occur were related to the domoticz host being very busy (compiling domoticz ;) ) or the router rebooting. If everything is functioning normally the whole ssh command will be processed within tenths of a second... at least on my setup.

devros
Posts: 148
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Sunday 17 June 2018 20:30

EscApe wrote:
Friday 15 June 2018 16:46
@devros

Glad it's working! :D

Thanks for full info about timeouts :), but noticed that its not working (sorry) with new wlanconfig on my ubitiqui router...
wlanconfig eth0 list returns 'Invalid argument' (propablly disabled)

dunno why, previous (cat /proc/net/arp)
worked great and fast, can you add switch to use ARP for scaning please :)

Code: Select all

command: ['sshpass', '-p', 'xxx', 'ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=1', 'xxx@192.168.3.1', "export PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH;wlanconfig ath0 list | grep '^..:..:..:..:..:.. ' | cut -d' ' -f1;wlanconfig eth0 list | grep '^..:..:..:..:..:.. ' | cut -d' ' -f1;exit"]
2018-06-17 20:19:49.615 (presence) Returncode ssh command: 0
2018-06-17 20:19:49.615 (presence) Output from router: b'80:2a:a8:a4:e8:11\n'
2018-06-17 20:19:49.615 (presence) Considered absent: xxx
2018-06-17 20:19:49.616 (presence) Considered absent: xxxx

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests