Skip to content

Commit

Permalink
handle exceptions from dd-wrt presence detection
Browse files Browse the repository at this point in the history
  • Loading branch information
pawl committed Jul 11, 2017
1 parent 43c43db commit 3886600
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name='thief-snapshot',
version='0.1.2',
version='0.2.0',
description='Detects motion with a camera and sends snapshots using a '
'telegram chatbot if your phone is not on wifi.',
url='https://github.com/pawl/thief-snapshot',
Expand Down
7 changes: 7 additions & 0 deletions thief_snapshot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import logging

logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)
23 changes: 10 additions & 13 deletions thief_snapshot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@
is not on the network while motion is detected.
"""
import argparse
import logging
import time

import telegram
from configparser import ConfigParser

from thief_snapshot import cameras, presence_detection, motion_detection


logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
from thief_snapshot import (
cameras,
presence_detection,
motion_detection,
logger,
)
logger = logging.getLogger(__name__)

# iPhones will occasionally drop off the network and then re-appear
MAX_DEVICE_MISSING_COUNT = 180
Expand Down Expand Up @@ -107,29 +104,29 @@ def main():

while True:
presence_detected = presence_detector.is_presence_detected
logging.info('Presence detected? %s', presence_detected)
logger.info('Presence detected? %s', presence_detected)
if presence_detected:
device_missing_count = 0
else:
device_missing_count += 1
logging.info('Presence undetected count: %s', device_missing_count)
logger.info('Presence undetected count: %s', device_missing_count)

motion_detected = motion_detector.is_motion_detected
logging.info('Motion detected? %s', motion_detected)
logger.info('Motion detected? %s', motion_detected)

if (device_missing_count > MAX_DEVICE_MISSING_COUNT) and motion_detected:
for i in range(PHOTO_LIMIT):
bot.send_photo(
chat_id=settings['telegram']['chat_id'],
photo=camera.snapshot(),
)
logging.info('Waiting between snapshots...')
logger.info('Waiting between snapshots...')
time.sleep(1)

# give the telegram API a break
time.sleep(30)

logging.info('Waiting...')
logger.info('Waiting...')
time.sleep(1)


Expand Down
20 changes: 14 additions & 6 deletions thief_snapshot/presence_detection/_ddwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import requests

from thief_snapshot import logger
from thief_snapshot.presence_detection.base import BasePresenceDetector


Expand Down Expand Up @@ -32,13 +33,20 @@ def is_presence_detected(self):
Returns:
boolean -- whether the expected MAC address was detected
"""
url = 'http://{}:{}/Status_Wireless.live.asp'.format(self.ip, self.port)
response = requests.get(
url,
auth=(self.username, self.password),
timeout=4,
url = 'http://{}:{}/Status_Wireless.live.asp'.format(
self.ip,
self.port,
)
response.raise_for_status()
try:
response = requests.get(
url,
auth=(self.username, self.password),
timeout=4,
)
response.raise_for_status()
except requests.exceptions.RequestException:
logger.exception('Failed to get WIFI clients from DD-WRT router.')
return False

data = _parse_ddwrt_response(response.text)
if not data:
Expand Down

0 comments on commit 3886600

Please sign in to comment.