-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_prog.py
216 lines (173 loc) · 7.85 KB
/
main_prog.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import serial
import sqlite3
from termcolor import colored
from multiprocessing import Process
import time
import pyttsx3
import constants
import api_handler
import entry_gate
import shooping
import exit_gate
##conect to the local database
conn = sqlite3.connect('store.db')
c = conn.cursor()
def init_db():
try:
c.execute("CREATE TABLE IF NOT EXISTS products(ID integer PRIMARY KEY, Name text, typeID integer, color text)")
c.execute("CREATE TABLE IF NOT EXISTS orders(orderID integer PRIMARY KEY, userID integer, checkedOut integer)")
return True
except sqlite3.Error as er:
print (colored("SQLite Error: "+ er, 'red'))
return False
def init_serialCom():
port = constants.arduino_port
baud = 9600
try:
ser = serial.Serial(port, baud, timeout=1)
return True, ser
except Exception as e:
print (colored("Serial Error: opening serial port: " + str(e), 'red'))
return False, None
def send_serialData(message):
ser.write(bytes(message,'utf-8'))
def speech(message):
engine = pyttsx3.init()
engine.say("message")
engine.runAndWait()
def gate():
user_id, erorr = entry_gate.read_qr()
print(user_id)
if not erorr:
speechThread = Process(target=speech, args=("Welcome"))
speechThread.start()
try:
c.execute("SELECT userID FROM orders WHERE checkedOut = 0")
row = c.fetchone()
if(row is not None and int(user_id) in row):
try:
c.execute("SELECT orderID FROM orders WHERE checkedOut = 0 AND userID = "+ str(user_id))
order_id=c.fetchone()[0]
print(order_id)
speechThread = Process(target=speech, args=['Checking out, Please wait'])
speechThread.start()
print (colored("Checking out please wait ...", 'blue'))
if(not api_handler.new_exit(order_id)):
print(colored("Thank your for visiting us, Have a good day :)", 'green'))
c.execute("UPDATE orders SET checkedOut = 1 WHERE orderID = "+str(order_id))
conn.commit()
else:
print(colored("Error in Checking out, please check your app for more details", 'red'))
except sqlite3.Error as er :
print (colored("SQLite Error: "+ str(er), 'red'))
send_serialData("refuse shoping")
else:
speechThread = Process(target=speech, args=['Welcome, please wait getting your profile'])
speechThread.start()
print(colored("Welcome, "+user_id,"green"))
print(colored("Getting profile from server ... ","blue"))
perm, error = api_handler.get_perm(user_id)
if perm == 1: #full registered
speechThread = Process(target=speech, args=['Checking face, please keep your face in front of the camera'])
speechThread.start()
print(colored("Checking face, please keep your face in front of the camera ","green"))
entry_done = entry_gate.check_face(user_id)
elif perm == 0: #no pics
input(colored("This user need to register his pics press ENTER to continue","blue"))
speechThread = Process(target=speech, args=['You need to registe your face'])
speechThread.start()
erorr = entry_gate.register(user_id)
if not erorr:
api_handler.set_perm(user_id)
entry_done = entry_gate.check_face(user_id)
else :
print (colored("Error happens: This user may not be in database", 'red'))
if entry_done:
try:
order_id, error = api_handler.new_entry(user_id)
c.execute("INSERT INTO orders ('userID', 'orderID','checkedOut') Values( "+user_id+" , "+ str(order_id)+" , 0 )")
conn.commit()
speechThread = Process(target=speech, args=['Authenticating complete, enjoy shoping'])
speechThread.start()
print(colored("Authenticating complete, enjoy shoping :)","green"))
send_serialData("approve entry")
except sqlite3.Error:
print (colored("SQLite Error: Inserting orderID", 'red'))
send_serialData("refuse entry")
else:
send_serialData("refuse entry")
except sqlite3.Error as er :
print (colored("SQLite Error: "+ str(er), 'red'))
else:
send_serialData("refuse entry")
def shop():
msg=[x.strip() for x in message.split(',')]
prod_id , state = shooping.getItem()
#prod_id = msg[1]
#state = msg[2]
user_id, error = shooping.getUser()
if not error:
print(user_id)
try:
c.execute("SELECT orderID FROM orders WHERE userID = "+str(user_id)+" AND checkedOut = 0 ")
order_id=c.fetchone()[0]
c.execute("SELECT typeID FROM products WHERE ID = "+prod_id)
type_id = c.fetchone()[0]
print("sending ",order_id,type_id,state)
api_handler.new_orderline(order_id, type_id, state)
send_serialData("approve shoping")
except sqlite3.Error as er :
print (colored("SQLite Error: "+ str(er), 'red'))
send_serialData("refuse shoping")
else:
send_serialData("refuse shoping")
def tessst():
user_id = 24
c.execute("SELECT userID FROM orders WHERE checkedOut = 0")
row = c.fetchone()
if(row is not None and user_id in row):
try:
c.execute("SELECT orderID FROM orders WHERE checkedOut = 0 AND userID = "+ str(user_id))
order_id=c.fetchone()[0]
print(order_id)
print (colored("Checking out please wait ...", 'blue'))
if(not api_handler.new_exit(order_id)):
print(colored("Thank your for visiting us, Have a good day :)", 'green'))
c.execute("UPDATE orders SET checkedOut = 1 WHERE orderID = "+str(order_id))
conn.commit()
except sqlite3.Error as er :
print (colored("SQLite Error: "+ str(er), 'red'))
send_serialData("refuse shoping")
else:
print(colored("Welcome, "+str(user_id),"green"))
## main programe:
if init_db():
while(True):
initalized,ser = init_serialCom()
if initalized:
ser.reset_input_buffer()
message= str(ser.readline(), "utf-8")
if len(message)>5:
print(message)
if 'gate' in message :
#gate()
p1 = Process(target=gate)
p1.start()
elif 'shop' in message :
p2 = Process(target=shop)
p2.start()
else:
print (colored("Warning: unknown recieved serial data !", 'yellow'))
else:
message = input(colored("insert command manually >> ","green"))
if len(message)>2:
print(message)
if 'gate' in message :
#gate()
p1 = Process(target=gate)
p1.start()
elif 'shop' in message :
p2 = Process(target=shop)
p2.start()
else:
print (colored("Warning: unknown recieved serial data !", 'yellow'))