initial commit
idea from https://blog.serverdensity.com/how-to-monitor-cats/
This commit is contained in:
commit
9d5bd32c81
|
@ -0,0 +1,56 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import numpy
|
||||||
|
import subprocess
|
||||||
|
from bluepy.btle import Scanner, DefaultDelegate
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
devices = ( {'name': 'charol', 'addr': 'd3:01:00:00:6a:32', 'values': [] }, )
|
||||||
|
|
||||||
|
class ScanReport2SD(DefaultDelegate):
|
||||||
|
def handleDiscovery(self, dev, isNewDev, isNewData):
|
||||||
|
if dev.rssi < -128:
|
||||||
|
return
|
||||||
|
|
||||||
|
for d in devices:
|
||||||
|
# quick and dirty
|
||||||
|
try:
|
||||||
|
if d['addr'] == dev.addr:
|
||||||
|
d['values'].append(dev.rssi)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
|
def report():
|
||||||
|
data = {}
|
||||||
|
for d in devices:
|
||||||
|
list = d['values']
|
||||||
|
name = d['name']+'_litter'
|
||||||
|
if list == []:
|
||||||
|
data[name] = 0
|
||||||
|
print("DEBUG {} {}".format(name, list))
|
||||||
|
else:
|
||||||
|
# percentile85 has given good results to filter out cat/signal movement
|
||||||
|
percentile = numpy.percentile(numpy.array(list), 85)
|
||||||
|
# we reset values
|
||||||
|
d['values'] = []
|
||||||
|
# -60 is the rssi thershold we consider in place
|
||||||
|
if percentile < -60:
|
||||||
|
data[name] = 1
|
||||||
|
if percentile >= -60:
|
||||||
|
data[name] = 2
|
||||||
|
subprocess.run(["loginctl", "lock-session"])
|
||||||
|
print("DEBUG {} {} {}".format(name, percentile, list))
|
||||||
|
pprint(data)
|
||||||
|
f = open('/tmp/CatSpy.json', 'w')
|
||||||
|
f.write(json.dumps(data))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
scanner = Scanner().withDelegate(ScanReport2SD())
|
||||||
|
print ("Scanning for devices...")
|
||||||
|
while True:
|
||||||
|
scanner.scan(20)
|
||||||
|
report()
|
Loading…
Reference in New Issue