-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (133 loc) · 4.35 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import subprocess
import pymysql.cursors
import led
import tag
import switch
import RPi.GPIO as GPIO
import sound
import threading
import time
connection = pymysql.connect(
user='root',
passwd='root',
host='localhost',
db='bdm_umbrella_stand',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
flag = -1
class AlertThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
with connection.cursor() as cursor:
sql = "select * from rooms"
cursor.execute(sql)
rooms = cursor.fetchall()
GPIO.setmode(GPIO.BCM)
for room in rooms:
GPIO.setup(room['switch_port'], GPIO.IN)
while True:
with connection.cursor() as cursor:
sql = "select * from rooms join umbrellas on rooms.id = umbrellas.room_id where umbrellas.in_room = true"
cursor.execute(sql)
occupied_rooms = cursor.fetchall()
for occupied_room in occupied_rooms:
if GPIO.input(occupied_room['switch_port']) == GPIO.LOW and flag != occupied_room["id"]:
sound.alert()
time.sleep(0.5)
def stop(self):
self.stop_event.set()
self.thread.join()
def get_registered_room(umbrella):
with connection.cursor() as cursor:
sql = "select * from rooms where id=%s"
cursor.execute(sql, (umbrella["room_id"],))
rooms = cursor.fetchall()
if len(rooms) == 0:
print "room not found!!"
return False
return rooms[0]
def register_nfc(tag_id):
with connection.cursor() as cursor:
insert_sql = "insert into nfcs (`tag_id`) values (%s)"
cursor.execute(insert_sql, (tag_id,))
connection.commit()
def get_registered_umbrella(nfc):
with connection.cursor() as cursor:
sql = "select * from umbrellas where nfc_id=%s and in_room = True"
cursor.execute(sql, (nfc["id"],))
return cursor.fetchone()
def get_registered_nfc(tag_id):
with connection.cursor() as cursor:
sql = "select * from nfcs where tag_id=%s"
cursor.execute(sql, (tag_id,))
nfcs = cursor.fetchall()
if len(nfcs) == 0:
return False
return nfcs[0]
def unlock(nfc):
umbrella = get_registered_umbrella(nfc)
room = get_registered_room(umbrella)
led.unlocked(room)
flag = room['id']
if switch.take(room):
with connection.cursor() as cursor:
sql = "update umbrellas set in_room=%s where id=%s"
cursor.execute(sql, (False, umbrella["id"]))
connection.commit()
led.turn_off(room)
print "umbrella is successfully fetched"
else:
led.locked(room)
print "umbrella is not fetched"
flag = -1
def register(nfc):
room = switch.put()
if room is False:
return
with connection.cursor() as cursor:
sql = "insert into umbrellas (room_id, nfc_id, in_room) values (%s, %s, %s)"
cursor.execute(sql, (room["id"], nfc["id"], True))
connection.commit()
led.locked(room)
print "umbrella is successfully registered"
def umbrella_in_room_exists(nfc):
if get_registered_umbrella(nfc):
return True
else:
return False
def prepare():
# gpio
GPIO.setmode(GPIO.BCM)
with connection.cursor() as cursor:
sql = "select * from rooms"
cursor.execute(sql)
rooms = cursor.fetchall()
for room in rooms:
GPIO.setup(room["locked_led_port"], GPIO.OUT)
GPIO.setup(room["unlocked_led_port"], GPIO.OUT)
GPIO.setup(room["switch_port"], GPIO.IN)
led.turn_off(room)
# sound
subprocess.call("sudo amixer cset numid=3 1", shell=True)
try:
prepare()
reader = tag.TagReader()
at = AlertThread()
at.start()
print "start!"
while True:
tapped_tag_id = reader.read()
registered_nfc = get_registered_nfc(tapped_tag_id)
if registered_nfc is not False and umbrella_in_room_exists(registered_nfc):
unlock(registered_nfc)
else:
if registered_nfc is False:
register_nfc(tapped_tag_id)
registered_nfc = get_registered_nfc(tapped_tag_id)
register(registered_nfc)
print "--------------------"
except KeyboardInterrupt:
at.stop()
print "\nexit"