-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent-pi.py
135 lines (112 loc) · 3.47 KB
/
agent-pi.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
import re
import webbrowser
import socketio
import sys
import Bluetooth
import time
car_id = 2
login = False
sio = socketio.Client()
class ap:
"""
the agent pi class, used for receiveing and communicating with MP
"""
def __init__(self):
self._blueTooth = Bluetooth()
def menu(self):
global login
"""
menu for ap, user can either log in with image or username and password
"""
print('{:^24s}'.format("\nWelcome to the car rental system"))
print('{:^24s}'.format("---------------MENU---------------"))
print( '1:login with your image\n'
'2:login with Username and Password\n'
'3:Bluetooth function\n'
'4:exit')
while True:
if login == True:
break
Input = input("Choose from the menu:\n")
if Input =="1":
sio.connect('http://127.0.0.1:5000')
recognise().run()
sio.emit('name',{'imgname':recognise().name})
elif Input =="2":
sio.connect('http://127.0.0.1:5000')
print('my sid is', sio.sid)
username = input('Please enter your username: ')
password = input('Please enter your password: ')
sio.emit('identity', {'username' : username, 'password' : password})
elif Input =="3":
message = self._blueTooth.main()
time.sleep(5)
continue
elif Input =="4":
print("thank you for using our system")
sys.exit()
else:
print("Enter from 1 to 3")
#while(quit == False):
while login == True:
print('1:unlock the car\n'
'2:return the car\n'
'3:show all the cars\'location\n'
'4:exit')
option = input('Please choose an option:\n')
if(option == '1'):
print('The car is unlocked!!')
elif(option == '2'):
sio.emit('finish', {'car_id' : car_id})
elif(option == '3'):
pass
elif(option == '4'):
sio.disconnect()
sys.exit()
def check(self):
"""
check if the user has logged in
"""
if bool(login) is False:
print("You haven't logged in yet\n")
print("Please login with password or your own image")
@sio.event
def connect():
"""
when successfully connecte with master pi
"""
print("I'm connected!")
@sio.event
def connect_error():
"""
print when connection has error
"""
print("The connection failed!")
@sio.event
def disconnect():
"""
when disconnect from the master pi
"""
print("I'm disconnected!")
@sio.event
def my_event(sid, data):
# handle the message
return "OK", 123
@sio.on('my response')
def on_message(data):
print('I received a message!' + str(data))
@sio.on('validate')
def on_validate_message(data):
global login
"""
sub-menu for the ap
"""
print('I received a message!' + str(data))
result = data['result']
if result == 'success':
print('Congradulation! You login successfully!!')
login = True
else:
print("Error, your username or password incorrect!!")
if __name__ == '__main__':
ap().menu()