/
pir.py
55 lines (39 loc) · 1.1 KB
/
pir.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
52
53
54
55
from lib.power import Power
from lib.switch import Switch
from app import App
from lib.obj import Obj
import time
# import RPi.GPIO as GPIO
from gpio_emulator.EmulatorGUI import GPIO
settings = Obj()
settings.lastMotionEndTime = None
settings.maxIdleSeconds = 10
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
light = Power(4)
app = App()
Power(14).switchOn()
def is_idle(): return settings.lastMotionEndTime != None
def calcSecondsIdle(): return int(time.time() - settings.lastMotionEndTime)
def resetIdleCountdown(): settings.lastMotionEndTime = None
def onMotionSwitchChange(value):
if (value):
light.switchOn()
resetIdleCountdown()
if(app.is_running == False):
app.start()
print("Started")
else:
light.switchOff()
settings.lastMotionEndTime = time.time()
Switch(15, onMotionSwitchChange)
while True:
if (is_idle()):
idle = calcSecondsIdle()
print(idle)
if(idle >= settings.maxIdleSeconds):
resetIdleCountdown()
app.stop()
print("Stopped")
time.sleep(1)
GPIO.cleanup()