Giter Club home page Giter Club logo

flatsiedatsie / webthings-network-presence-detection Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 9.0 22.16 MB

Devices on the local network can be added as a 'thing' in the Candle Controller / WebThings Gateway. Automations can then respond to their presence. For example, turn on the lights at night when your mobile phone connects to the wireless network.

License: MIT License

Python 70.85% Shell 1.49% CSS 6.08% JavaScript 20.55% HTML 1.04%
webthings webthings-adapter presence-detection privacy network candlesmarthome

webthings-network-presence-detection's Introduction

Webthings network presence detection

Using this add-on, devices on your local network can be added as a 'thing' in the Candle controller. Automations can then respond to their presence. For example, turn on the lights at night when your mobile phone connects to the wireless network. Or turn of the heater when none of your phones or laptops are on the network.

A screenshot of the user interface

A screenshot of a presence detection thing2

Features

  • You can change how many minutes a device must 'disappear' from the network before it is marked as being away. The add-on scans your selected things continously. When you press the (+) button on the things page, and also once every hour, it does a deep scan to find new devices on your network.
  • If the human-readable name of a device is not known, then it will try to look-up the device manufacturer, and use that as a name. The add-on does not connect to the internet to do this. It is done locally, on the gateway, so your privacy is protected.
  • This add-on uses 'ping', 'arping' and 'arp' to scan the local network.

Limitations

A thing is linked to a mac-address of a device. Nowadays some devices, like iphones, change their mac-address once in a while to make tracking difficult. This is a great privacy feature, but it may also hinder this add-ons ability to track (i)phone presence.

Versions

  • Versions before 0.1.0 continously ran deep-scans. This was very processor intensive.
  • Since version 0.1.0 it only scans the devices you added to your things page. The add-on now has a much higher scan rate, and can respond to devices re-connecting to the network in just a few seconds.
  • Version 1.0 added some new privacy features
  • Version 2.0 is a big overhaul for Candle 2.0, which includes avahi scanning and can ignore Candle devices (enabled by default), and enabled additional privacy protection (no longer uses mac address as part of devices IDs)
  • Version 2.1 adds a UI

Thanks to

Michael Stegeman (formerly of the Mozilla Foundation) helped a lot in getting this add-on to work optimally. https://github.com/mrstegeman

webthings-network-presence-detection's People

Contributors

flatsiedatsie avatar madb1lly avatar mrsteakhouse avatar mrstegeman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

webthings-network-presence-detection's Issues

Time window

Hello, the time window setting is not been adhere to. I have set the time window to 5 minutes before the device is marked offline?
gateway1

But the device is still showing active after 22 mins last seen even thou there is no IP in the details ?
gateway2

Just turned on The WIFI on the device and now is showing active with the IP address in details and last seen 0 mins ago.
Gateway3

High CPU usage

When this add-on is enabled, a python3 process very quickly jumps up to 100% CPU on my raspberry pi 4. I'm listening for the presence of just one device on my network. When I disable this add-on, the process disappears. Seems like an awful lot of load on my processor to occasionally poll for the presence of a single device on my network. What information can I provide to help track down the issue?

print debug messages

When error occure it is very hard to get information about the nature of the error.
It appears, that there are debug log messages, but they are commented out. Another property could enable those debug messages for further information.

Functions missing in util.py

2019-04-30 10:42:34.957 ERROR  : network-presence-detection: Traceback (most recent call last):
2019-04-30 10:42:34.958 ERROR  : network-presence-detection:   File "/Users/mstegeman/.mozilla-iot/addons/network-presence-detection-adapter/main.py", line 12, in <module>
2019-04-30 10:42:34.958 ERROR  : network-presence-detection:     from pkg.presence_adapter import presenceAdapter  # noqa
2019-04-30 10:42:34.958 ERROR  : network-presence-detection:   File "/Users/mstegeman/workspace/adapters/network-presence-detection/pkg/presence_adapter.py", line 14, in <module>
2019-04-30 10:42:34.958 ERROR  : network-presence-detection:     from .util import validip, clamp
2019-04-30 10:42:34.958 ERROR  : network-presence-detection: ImportError: cannot import name 'validip' from 'pkg.util' (/Users/mstegeman/workspace/adapters/network-presence-detection/pkg/util.py)

Option to reduce frequency of polling

Hi @flatsiedatsie,
First - nice addon, thanks :)

Second - my logs are now 99.99 etc % made up from this addon checking for the presence of the devices I asked it check for on the network. Can you add an option to set the minimum checking period? For my purposes at present every minute would be easily frequent enough.

I don't know exactly what the network, memory or CPU load from this high frequency checking is, but I'm sure it's not free. Note that I'm running the gateway on a RPi B, not an RPi2, 3 or 4, so I probably notice resource usage more than others.

Cheers :)

More info on discovered devices

Can your add-on provide more info when a device is discovered, such as the MAC address? It's hard to identify cell phones, for example, when the only identification is the manufacturer. Thanks.

nmblookup is not available by default on Raspberry Pi

As of today, I'm seeing very large numbers of errors in the log, of the form:

2019-12-11 23:10:07.423 ERROR : network-presence-detection: /bin/sh: 1: nmblookup: not found

This program appears to be part of Samba and is not part of the default install on Raspberry Pi.

I did not see this in the logs yesterday before the new 0.1.1 version was auto-installed today. The new version appears to have fixed the issue where existing Presence items were working but it was not possible to add new Presence items.

2019-12-11 13:27:34.874 INFO : Checking for add-on updates...
2019-12-11 13:27:36.514 INFO : Fetching add-on https://s3-us-west-2.amazonaws.com/mozilla-gateway-addons/network-presence-detection-adapter-0.1.1.tgz as /tmp/f7CUQs/network-presence-detection-adapter.tar.gz
2019-12-11 13:27:38.595 INFO : Expanding add-on /tmp/f7CUQs/network-presence-detection-adapter.tar.gz
2019-12-11 13:27:38.870 INFO : Unloading PresenceAdapter
2019-12-11 13:27:39.590 INFO : network-presence-detection: Config loaded ok
2019-12-11 13:27:39.591 INFO : network-presence-detection:
2019-12-11 13:27:39.592 INFO : network-presence-detection: + DEVICE init: Charles Phone
2019-12-11 13:27:39.593 INFO : network-presence-detection: + PROPERTY init: details
2019-12-11 13:27:39.594 INFO : network-presence-detection: -value: 192.168.x.x
2019-12-11 13:27:39.595 INFO : network-presence-detection: + DEVICE.ADD_INTEGER_CHILD with id: minutes_ago
2019-12-11 13:27:39.597 INFO : network-presence-detection: + PROPERTY init: minutes_ago
2019-12-11 13:27:39.598 INFO : network-presence-detection: -value: 0
2019-12-11 13:27:39.599 INFO : network-presence-detection: + DEVICE.ADD_BOOLEAN_CHILD with id: recently1
2019-12-11 13:27:39.600 INFO : network-presence-detection: + PROPERTY init: recently1
2019-12-11 13:27:39.601 INFO : network-presence-detection: -value: True
2019-12-11 13:27:39.602 INFO : network-presence-detection: -All properties: {'details': {'label': 'Details', 'type': 'string', 'readOnly': True}, 'minutes_ago': {'label': 'Minutes ago last seen', 'type': 'integer', 'readOnly': True}, 'recently1': {'@type': 'BooleanProperty', 'label': 'Recently spotted', 'type': 'boolean', 'readOnly': True}}
2019-12-11 13:27:39.603 INFO : network-presence-detection: Saving updated list of found devices to json file
2019-12-11 13:27:39.604 INFO : network-presence-detection: Saving updated list of found devices to json file
2019-12-11 13:27:39.605 INFO : network-presence-detection: Saving updated list of found devices to json file
2019-12-11 13:27:39.606 INFO : network-presence-detection: Saving updated list of found devices to json file

Secondary issue: that last line above was repeated over 100 times in the log (perhaps exactly 253 times?)

Network Presence Add-on failing to start 0.11.0

Hello, I recently upgraded my webthings Gateway to try some new features like this add-on. I installed it and attempted to add a new device. But no new device pops up in 'add device'. It also looks like when I enable and disable the add-on no separate process is started and in the logs I only see the below.
2020-09-22 08:20:03.238 INFO : Killing network-presence-detection-adapter plugin. 2020-09-22 08:20:06.214 INFO : Loading add-on: network-presence-detection-adapter

I am using python3 version 3.5, is this version too old? Additionally is there a log output somewhere so I can troubleshoot this?

Very unstable detection

Hey.

First of all - this plugin is a must have! if only it behaved more stable. It varies a lot in when it "detects" a device. Could you maybe describe exactly how often it actually looks for devices?

My usecase: I want it to be able to turn off the lights when i leave a building, and then turn on the lights when I enter.

I have defined two rules:

  1. turn off lights, when it has been inactive in more than 1 minute.
  2. turn on lights, when inactive in less that 1 minute.

Even with the device constantly online, "last detected" shows up to 2 minutes in "last seen" when testing (offlining the wifi device on purpuse). When online, the device is in fact constantly online (no wifi sleeping etc).

The "last seen" switches to 99999 when the device status is "inactive" - even though it has been offline for just two minutes - a bit odd...?

Could you make it (ar)ping the devices much more often? it would increase reliability a lot :-)

Also - a bluetooth edition of this would be REALLY cool - but thats another thing ;) If I can figure out how, I will make it!

new error message in Gateway logs

I just noticed over 1000 instances of this message in the gateway log
2020-08-06 22:16:47.782 INFO : network-presence-detection-adapter: Could not add recently spotted property'>' not supported between instances of 'NoneType' and 'int'
2020-08-06 22:16:47.783 INFO : network-presence-detection-adapter: Could not add recently spotted property'>' not supported between instances of 'NoneType' and 'int'
2020-08-06 22:16:47.784 INFO : network-presence-detection-adapter: Could not add recently spotted property'>' not supported between instances of 'NoneType' and 'int'
2020-08-06 22:16:47.784 INFO : network-presence-detection-adapter: Could not add recently spotted property'>' not supported between instances of 'NoneType' and 'int'

Can't detect our smartphones anymore

Hi @flatsiedatsie,

I don't know exactly when this happened but during the last month or so the add-on can no longer detect our smartphones. Neither of our smartphones has had an update in the past few months and neither has the ADSL modem/router AFAIK. The Gateway is connected by LAN and the presence detector can see other Wifi devices no problem.

What can I do to get this working again?

Thanks ๐Ÿ™‚

Error /bin/sh arp : not found

I am getting the following error in the gateway, please help me to solve this problem
network-presence-detection: arp: not found

I am running using the following command, arp I am binding from the host machine(its not present in the container). This is working from the inside container and also working from python3 subprocess.

docker run \
-d
--rm
-v /usr/sbin/arp:/usr/sbin/arp
-v /path/to/shared/data:/home/node/.mozilla-iot
--net=host
--name mozilla-iot-gateway
mozillaiot/gateway:latest

#Testing

docker exec -it mozilla-iot-gateway sh
arp ## yes its working
python3 -c "import subprocess; subprocess.run('arp')" #working

"Target IP" unclear

The description under Target IP reads

Can be used to override the add-on to target a specific IP address range. For example, paste in 192.168.8.10 if you want to scan 192.168.8.2 through 192.168.8.254

This doesn't make sense to me. From looking at the code it appears this add-on is intended to scan ranges, but where is the logic in specifying a single IP if you are scanning an entire range?

Suggestion: Make wording clearer, e.g use CIDR notation or allow "start" and "end" of range to be scanned.

occasionally 'minutes ago last seen' jumps to an enormous value

I use the network presence presence detection to monitor whether my phone is at home/connected to the wi-fi and trigger some rules on various values. What I occasionally notice is best displayed in the graph from the Logs screen of the Gateway.
image
At the moment, the webthing looks like this
image

Extracting a relevant section from the logs database . Note that this is fairly long to demonstrate that most of the time, the numbers are good, just sometimes weird.
id,date,value
5,1588309855502,0.0
5,1588310545680,1.0
5,1588310611299,0.0
5,1588310676848,1.0
5,1588310709660,0.0
5,1588310861137,1.0
5,1588310910347,2.0
5,1588310975888,3.0
5,1588311041401,4.0
5,1588311074160,0.0
5,1588311374277,1.0
5,1588311407070,0.0
5,1588311472577,1.0
5,1588311538140,2.0
5,1588311587348,3.0
5,1588311652876,4.0
5,1588311669262,0.0
5,1588311819607,1.0
5,1588311868763,2.0
5,1588311917999,0.0
5,1588312091226,1.0
5,1588312107592,0.0
5,1588312610488,1.0
5,1588312643302,0.0
5,1588312744599,1.0
5,1588312759175,0.0
5,1588312835540,1.0
5,1588312873578,0.0
5,1588312949308,1.0
5,1588312987232,0.0
5,1588314577528,1.0
5,1588314626767,2.0
5,1588314681046,0.0
5,1588314746616,1.0
5,1588314779407,0.0
5,1588314844937,1.0
5,1588314894138,2.0
5,1588314959718,3.0
5,1588314976143,0.0
5,1588315068298,1.0
5,1588315117471,0.0
5,1588315265011,1.0
5,1588315281371,0.0
5,1588315384472,19831.0
5,1588315395499,0.0
5,1588315524147,1.0
5,1588315556900,0.0
5,1588315785031,1.0
5,1588315834220,2.0
5,1588315899741,3.0
5,1588315965230,4.0
5,1588316014391,5.0
5,1588316019531,0.0
5,1588316090121,1.0
5,1588316139280,2.0
5,1588316204740,3.0
5,1588316237480,0.0
5,1588316605100,1.0
5,1588316642916,0.0
5,1588316713480,1.0
5,1588316778980,2.0
5,1588316828090,3.0
5,1588316877211,0.0
5,1588317023400,1.0
5,1588317072550,2.0
5,1588317138065,3.0
5,1588317203630,4.0
5,1588317252780,0.0
5,1588317333580,1.0
5,1588317399100,2.0
5,1588317448180,0.0
5,1588317872870,1.0
5,1588317877916,0.0
5,1588318045481,1.0
5,1588318094581,2.0
5,1588318160091,3.0
5,1588318192811,0.0
5,1588318263451,1.0
5,1588318312530,2.0
5,1588318378020,3.0
5,1588318410750,0.0
5,1588319291401,1.0
5,1588319340560,2.0
5,1588319405990,3.0
5,1588319427402,0.0
5,1588319612730,1.0
5,1588319645451,0.0
5,1588319803011,1.0
5,1588319852110,2.0
5,1588319917590,3.0
5,1588319983050,4.0
5,1588319999430,0.0
5,1588320286801,1.0
5,1588320352291,0.0
5,1588320596810,1.0
5,1588320634611,0.0
5,1588320759430,1.0
5,1588320808520,2.0
5,1588320873960,3.0
5,1588320911788,0.0
5,1588321291950,1.0
5,1588321341060,0.0
5,1588321406611,1.0
5,1588321472070,2.0
5,1588321493551,0.0
5,1588321699030,1.0
5,1588321748190,2.0
5,1588321813700,0.0
5,1588321949711,1.0
5,1588322015200,2.0
5,1588322064260,3.0
5,1588322096990,0.0
5,1588322401680,1.0
5,1588322467141,2.0
5,1588322499851,0.0
5,1588322565330,1.0
5,1588322614450,2.0
5,1588322679920,3.0
5,1588322717689,0.0
5,1588322879320,1.0
5,1588322928440,2.0
5,1588322993900,3.0
5,1588323042980,0.0
5,1588323178920,1.0
5,1588323228060,2.0
5,1588323293530,3.0
5,1588323359040,4.0
5,1588323396877,0.0
5,1588323575956,1.0
5,1588323625061,2.0
5,1588323690561,0.0
5,1588323897391,1.0
5,1588323946551,2.0
5,1588324011991,3.0
5,1588324077561,0.0
5,1588324174581,1.0
5,1588324240111,2.0
5,1588324289261,3.0
5,1588324327086,0.0
5,1588324789731,1.0
5,1588324855271,2.0
5,1588324904461,3.0
5,1588324920851,0.0
5,1588325116351,1.0
5,1588325165496,2.0
5,1588325170725,0.0
5,1588325251511,1.0
5,1588325316981,2.0
5,1588325349731,0.0
5,1588325415211,1.0
5,1588325431581,0.0
5,1588325579040,1.0
5,1588325584633,0.0
5,1588325720730,1.0
5,1588325786201,2.0
5,1588325835300,3.0
5,1588325900790,4.0
5,1588325938674,0.0
5,1588326052310,1.0
5,1588326101430,2.0
5,1588326122892,0.0
5,1588326340860,1.0
5,1588326406417,2.0
5,1588326455517,0.0
5,1588326558872,1.0
5,1588326575230,0.0
5,1588326645900,1.0
5,1588326695050,2.0
5,1588326727770,0.0
5,1588326793260,1.0
5,1588326842400,2.0
5,1588326891530,0.0
5,1588327141002,1.0
5,1588327190197,2.0
5,1588327195254,0.0
5,1588327271051,1.0
5,1588327320231,2.0
5,1588327325280,0.0
5,1588327494211,1.0
5,1588327499301,0.0
5,1588327699781,1.0
5,1588327716191,0.0
5,1588327879893,1.0
5,1588327929111,2.0
5,1588327994608,3.0
5,1588328060140,4.0
5,1588328109271,5.0
5,1588328125660,0.0
5,1588328191138,1.0
5,1588328240290,2.0
5,1588328305756,3.0
5,1588328945669,20057.0
5,1588329011139,20058.0
5,1588329043890,0.0
5,1588329201470,1.0
5,1588329250590,2.0
5,1588329304801,0.0
5,1588329423319,1.0
5,1588329472424,2.0
5,1588329537930,3.0
5,1588329603410,4.0
5,1588329608530,0.0

arp command is only functional on Linux

Right now, the add-on uses sudo arp. That will work fine on Linux (where password-less sudo is enabled), but not elsewhere. For instance on macOS, that yields this:

$ sudo arp
usage: arp [-n] [-i interface] hostname
       arp [-n] [-i interface] [-l] -a
       arp -d hostname [pub] [ifscope interface]
       arp -d [-i interface] -a
       arp -s hostname ether_addr [temp] [reject] [blackhole] [pub [only]] [ifscope interface]
       arp -S hostname ether_addr [temp] [reject] [blackhole] [pub [only]] [ifscope interface]
       arp -f filename

Is there a platform-agnostic arp library for Python you could use instead? For instance, on Node we use local-devices.

Brute Force Scan Fails

Some devices I was monitoring stopped reporting presence any more despite having same IP, MAC etc.

I observed this following error:

network-presence-detection-adapter: Error doing brute force scan: dictionary changed size during iteration

Commenting line 197 in presence-adapter.py allowed the brute force scan to complete again and redetect my devices........ Not sure what this error relates to as I don't have time to get my head into the workings of this adapter.

# del self.previously_found[key]

Maybe someone can have a look at the flow? Might be a threading issue?

Exception in valid_ip

If there is exactly 3 dots in my hostname with domain name, the valid_ip() checking will fail.
For example, myhostname.lan.mydomain.com

2019-12-02 14:45:02.029 ERROR  : network-presence-detection: Exception in thread Thread-135:
2019-12-02 14:45:02.029 ERROR  : network-presence-detection: Traceback (most recent call last):
2019-12-02 14:45:02.030 ERROR  : network-presence-detection:   File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
2019-12-02 14:45:02.031 ERROR  : network-presence-detection:     self.run()
2019-12-02 14:45:02.031 ERROR  : network-presence-detection:   File "/usr/lib/python3.7/threading.py", line 865, in run
2019-12-02 14:45:02.032 ERROR  : network-presence-detection:     self._target(*self._args, **self._kwargs)
2019-12-02 14:45:02.032 ERROR  : network-presence-detection:   File "/home/pi/.mozilla-iot/addons/network-presence-detection-adapter/pkg/presence_adapter.py", line 260, in scan
2019-12-02 14:45:02.033 ERROR  : network-presence-detection:     if found_device_name == '?' or valid_ip(found_device_name):
2019-12-02 14:45:02.033 ERROR  : network-presence-detection:   File "/home/pi/.mozilla-iot/addons/network-presence-detection-adapter/pkg/util.py", line 10, in valid_ip
2019-12-02 14:45:02.034 ERROR  : network-presence-detection:     all(0 <= int(num) < 256 for num in ip.rstrip().split('.'))
2019-12-02 14:45:02.034 ERROR  : network-presence-detection:   File "/home/pi/.mozilla-iot/addons/network-presence-detection-adapter/pkg/util.py", line 10, in <genexpr>
2019-12-02 14:45:02.035 ERROR  : network-presence-detection:     all(0 <= int(num) < 256 for num in ip.rstrip().split('.'))
2019-12-02 14:45:02.036 ERROR  : network-presence-detection: ValueError: invalid literal for int() with base 10: 'myhostname'

Make default ip adress configurable

I'm running the gateway on a docker container on an odroid XU4. I was able do debug the code responsible for finding the local ip adress if the lan. Unfortunately the socket returns "0.0.0.0" and the adress gets set to "192.168.1.1". For pinging i need "192.168.0.1" and therefore no ping request are going through.

Please make the default adress configurable in the add-on settings page.
This is the line in the code.

Scan fails to run - _id is not defined

From my logs:

2019-12-07 19:52:37.024 INFO   : network-presence-detection: Config loaded ok
2019-12-07 19:52:37.025 INFO   : network-presence-detection: Error doing light arpa scan: name '_id' is not defined

Looks like line 138 of presence_adapter.py should be self.previously_found[str(key)] = {} rather than self.previously_found[str(_id)] = {}.

Device with Wake on LAN enabled is always active

Hi there,

First of all: Pretty great add-on! I really love how you use ARP instead of just pinging devices regularly.
However, my PC gets detected active all the time (all other devices work as expected). After a bit of trial and error, it seems to me that Wake on LAN is the problem (plugging out the LAN cable manually brings the presence detection thing to inactive state).
I guess this makes kind of sense. Anyway, it would be great if I could detect when my PC is offline. A ping is not successful when it is shutdown, so I think this could be used to detect activity of WoL-enabled devices?
Do you think it would be a good idea to add an option for regular ping polling instead of the current (pretty amazing!!!) approach to this add-on? Or should I rather aim for making a new add-on, or try to use some add-on for executing bash commands? Or am I probably just doing something completely stupid and this behaviour should not occur at all?๐Ÿ™ˆ

Regards,
Benedikt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.