xsnippet logotype

xsnippet

#362676, Untitled [ Python 3 ]

by Guest
 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
import RPi.GPIO as GPIO
import yaml
import argparse

from metis import motion, camera, led, telegram


def parse_command_line():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '-c', '--conf', default='config.yaml',
        help='set path to the config file')

    arguments = parser.parse_args()

    return arguments

if __name__ == '__main__':
    with open(parse_command_line().conf, 'r') as stream:
        config = yaml.load(stream)
    print(config)

    GPIO.setmode(GPIO.BCM)

    detector = motion.MotionDetector(config['sensor_pin'])
    recorder = camera.CamRecorder()
    telega = telegram.Telegram(config['telegram_token'], config['recipient_id'])
    motion_led = led.Led(config['led_motion_pin'])
    no_motion_led = led.Led(config['led_no_motion_pin'], True)

    detector.start.connect(lambda sender: motion_led.on(), weak=False)
    detector.start.connect(lambda sender: no_motion_led.off(), weak=False)
    detector.start.connect(lambda sender: telega.send_message('Intruder detected'), weak=False)
    detector.start.connect(lambda sender: recorder.record(), weak=False)

    detector.end.connect(lambda sender: motion_led.off(), weak=False)
    detector.end.connect(lambda sender: no_motion_led.on(), weak=False)
    detector.end.connect(lambda sender: recorder.stop(), weak=False)

    recorder.end.connect(lambda sender, filename: print(filename, 'recorded'), weak=False)
    recorder.end.connect(lambda sender, filename: telega.send_doc(filename), weak=False)

    try:
        detector.run()
    except (KeyboardInterrupt, SystemExit):
        print('Exiting')
    finally:
        motion_led.off()
        no_motion_led.off()
        GPIO.cleanup()