Esempio n. 1
0
def trigger_alarm():
    """Sends alarm signal (either e-mail or siren)"""
    # Alarm delay then check again
    time.sleep(alarm_delay)
    # Check again if someone is home now
    if Device.objects.filter(is_home=True).count() > 0:
        return # cancel alarm

    l = Log(log_type='AL')
    l.save()

    try:
        Config.objects.get(config_type='ALARM', name='email', enabled=True)
        for c in Config.objects.filter(config_type='EMAIL', enabled=True):
            send_mail(
                'Doorguard ALARM',
                'Alarm Triggered!!!',
                settings.DEFAULT_FROM_EMAIL,
                [c.name],
                fail_silently=True
            )
    except ObjectDoesNotExist:
        pass # email sending disabled

    try:
        Config.objects.get(config_type='ALARM', name='sound', enabled=True)
        # do some crazy shitty sound action!!!
        GPIO.output(alarm_pin, 0) # start alarm...
        # just for some seconds to timestamp
        time.sleep(alarm_time)
        GPIO.output(alarm_pin, 1) # disable alarm
    except ObjectDoesNotExist:
        pass # sound action disabled
Esempio n. 2
0
def check_devices():
    """Checks which devices are at home"""
    while not stop_threads.isSet():
        devices = Device.objects.all()  # get all current devices
        for d in devices:
            for i in range(int(ping_retry)):
                ret = subprocess.call("ping -c 1 %s" % d.ip,
                    shell=True,
                    stdout=open('/dev/null', 'w'),
                    stderr=subprocess.STDOUT)
                if ret == 0:
                    break # break retries, if device is alive
                time.sleep(1)
            is_home = (ret == 0)
            if not d.is_home == is_home:
                d.is_home = is_home
                d.save()
                l = Log(device=d, status=is_home, log_type='DE', text='(checker)')
                l.save()
        time.sleep(device_check_wait)
Esempio n. 3
0
def check_motion():
    """Checks motions in the room with IR-detector"""
    GPIO.setup(motion_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

    while not stop_threads.isSet():
        curr_state = GPIO.input(motion_pin)
        if curr_state:
            l = Log(status=curr_state, log_type='MO')
            l.save()

#           could trigger also alarm here if no phone is present
#            if curr_state == 0 and Device.objects.filter(is_home=True).count() == 0:
                # Door opened and nobody at home!
#                alarm_thread = threading.Thread(target=trigger_alarm)
#                alarm_thread.daemon = True
#                alarm_thread.start()

 #           Device.objects.filter(is_home=True).order_by('-modified')

        time.sleep(motion_check_wait)
Esempio n. 4
0
def dhcp_pipe_reader():
    """Checks for new DHCP-registration of devices"""
    if not os.path.exists(pipe_name):
        os.mkfifo(pipe_name)

    while not stop_threads.isSet():
        with open(pipe_name, 'r') as pipe:
            line = pipe.readline()[:-1]   # get line except \n at end
            line = line.split(' ')        # split up by ' ' in chunks
            cmd = line[0]
            if cmd == 'old' or cmd == 'add':
                mac = line[1]
                ip = line[2]
                hostname = line[3]

                try:
                    d = Device.objects.get(ip=ip, is_home=False)
                    d.is_home = True
                    d.save()
                    l = Log(device=d, status=True, log_type='DE', text='(dhcp)')
                    l.save()
                except ObjectDoesNotExist:
                    pass
Esempio n. 5
0
def check_door():
    """Checks the door-state with magnetic switch"""
    GPIO.setup(check_pin, GPIO.IN)
    old_state = GPIO.input(check_pin)

    while not stop_threads.isSet():
        curr_state = GPIO.input(check_pin)
        if old_state != curr_state:
            # state of pin changed
            old_state = curr_state
            l = Log(status=curr_state, log_type='DO')
            l.save()

            if curr_state == 0 and Device.objects.filter(is_home=True).count() == 0:
                # Door opened and nobody at home!
                alarm_thread = threading.Thread(target=trigger_alarm)
                alarm_thread.daemon = True
                alarm_thread.start()

            if curr_state == 0:
                try:
                    c = Config.objects.get(config_type='ALARM', name='siren_test', enabled=True)
                    c.save()
                    GPIO.output(alarm_pin, 0) # start alarm...
                    # just for some seconds to timestamp
                    if c.value:
                        time.sleep(float(c.value))
                    else:
                        time.sleep(alarm_time)
                    GPIO.output(alarm_pin, 1) # disable alarm
                    c.enabled=False
                    c.save()
                except ObjectDoesNotExist:
                    pass # do nothing

#            Device.objects.filter(is_home=True).order_by('-modified')
        time.sleep(door_check_wait)