-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorCtrl.py
59 lines (49 loc) · 1.94 KB
/
doorCtrl.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
56
57
58
59
# -*- coding: utf-8 -*-
from sysfs.gpio import Controller
from twisted.internet import reactor
import time
OPEN_THE_DOOR_GPIO = 186
CHECK_THE_DOOR_GPIO = 187
DOOR_IN_OPEN = True
DOOR_IN_CLOSE = False
class DoorCtrl():
def __init__(self,cb_door_close=None):
self.door_closed_cb = cb_door_close
self.door_state = DOOR_IN_CLOSE
Controller.available_pins = [OPEN_THE_DOOR_GPIO,CHECK_THE_DOOR_GPIO]
Controller.alloc_pin(number=OPEN_THE_DOOR_GPIO, direction='out')
Controller.alloc_pin(number=CHECK_THE_DOOR_GPIO, direction='in',callback=self.door_is_closed,edge='falling')
Controller.set_pin(OPEN_THE_DOOR_GPIO)
def door_is_closed(self,pin, state):
dateTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print format("the door is closed !!! at %s " % dateTime)
print format("pin = %d state = %d" % (pin, state))
if state == False:
if self.door_state == DOOR_IN_CLOSE:
return
self.door_state = DOOR_IN_CLOSE
if self.door_closed_cb != None :
self.door_closed_cb()
else :
Controller.set_pin(OPEN_THE_DOOR_GPIO)
def open_the_door(self):
if self.door_state == DOOR_IN_OPEN:
return
Controller.reset_pin(OPEN_THE_DOOR_GPIO)
dateTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print format("the door is opend !!! at %s " % dateTime)
time.sleep(2)
Controller.set_pin(OPEN_THE_DOOR_GPIO)
self.door_state = DOOR_IN_OPEN
def check_the_door(self):
state = Controller.get_pin_state(CHECK_THE_DOOR_GPIO)
if state == True:
#print "The door state is open !"
return DOOR_IN_OPEN
else :
#print "The door state is close !"
return DOOR_IN_CLOSE
pass
if __name__ == "__main__":
theDoorCtrl = DoorCtrl()
reactor.run()