This repository has been archived by the owner on Dec 29, 2017. It is now read-only.
forked from vancetran/amazon-dash-rpi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
button.py
51 lines (41 loc) · 1.44 KB
/
button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/
# crontab -e
# @reboot source /PATH/TO/REPO/.venv/bin/activate && python /PATH/TO/REPO/button.py
# Exit daemon:
# find number: ps aux | grep "button.py"
# kill process: sudo kill 61604
import json
import logging
from daemonize import Daemonize
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import sniff
from scapy.all import ARP
import requests
import os
def arp_display(pkt):
mac = pkt[ARP].hwsrc
if mac in [button['address'] for button in config['buttons']]:
idx = [button['address'] for button in config['buttons']].index(mac)
button = config['buttons'][idx]
logging.info(button['name'])
r = requests.post(button['url'], json=button['body'], headers=button['headers'])
logging.info('status code: {}'.format(r.status_code))
def main():
sniff(prn=arp_display, filter='arp', store=0, count=0)
# create basepath
path = os.path.dirname(os.path.realpath(__file__))
# log events in log file
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.propagate = False
fh = logging.FileHandler(path + '/button.log', 'w')
# read config file
with open(path + '/config.json', mode='r') as data_file:
config = json.load(data_file)
# start sniffing
daemon = Daemonize(
app='dashbutton',
pid='{}/button.pid'.format(path),
action=main)
daemon.start()