-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAATC_Client.py
260 lines (214 loc) · 9.87 KB
/
AATC_Client.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#############################################################
# Communication Lookup Table
#
# ("Login",("Type","Username","Password",NewUser(Boolean))) -> (True/False,"Message")
# Type -> Type of client logging in eg Monitor,Drone,User.
# Returned Data
# True,WelcomeMessage -> Sucess logging in, print welcome message
# False,Reason -> Failure logging in , print reason why
#
# ("GetNoFlyZones",(Limit1,Limit2)) -> (True/False, Message ,(List of ID and Coordinate pairs eg (ID,(x,y,z),(x,y,z)) in a list ))
#
# ("AddNoFlyZone",(StartCoords,EndCoords)) -> (True/False,Message) Add a NoFlyZone between the coords. Will be rounded down to nearest nodes
#
# ("RemoveNoFlyZone",(ZoneID,)) -> (True/Falce,Message) Removes the no fly zone with that ID, Id can be found from GetNoFlyZones
#
#
#
#
# ("GetDroneInfo",(AllDrones?,InFlight) -> (True/False, Message, (List of DronesData eg (Username,DroneName,DroneID,InFlight,ETA?,LastLocation,Speed,Range,Weight)))
# If All is True the program will send all DronesInfo of drones who are made visible by owners. Otherwise only users drones
# InFlight Selects only those which are flying
#
# ("AddNewDrone",(DroneName,Speed,Range,Weight)) -> (True/False,Message) Register a drone to central database
#
# ("RemoveDrone",(DroneID,)) -> (True/False,Message) Remove a drone from the central database
#
# ("SetFlightVisibility",(Visibility,)) -> (True/False,Message) Set Visibility of flights to public
#
# ("GetFlightInfo",(All,)) -> (True/False,Message,FlightInfo) Flight Info is List of Flights eg (FlightID,DroneID,StartCoords,TargetCoords,CurrentPosition,ETA,Path) where path is list of path Coords
#
#############################################################
import socket,codecs,ast,recvall,AATC_Crypto
#Create Socket
#Create Connection
def Connect(remote_ip,PORT): #Connects to the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((remote_ip, PORT))
print("Connected to > "+ remote_ip+":"+str(PORT))
return s
##def split(tup,num = 3): # Used to remove the data section for non data
## Sucess,Message,Data = tup[0],tup[1],tup[2]
## if num == 3:
## return Sucess,Message,Data
## elif num == 2:
## return Sucess,Message
class UserInterface: #Used to interface with the server
def __init__(self,Connection):
self._con = Connection
self._Crypto = AATC_Crypto.Crypter(self._con)
self._Username = ""
print("Welcome to the AATC connection interface")
def Login(self,Username,Password):
self._Username = Username
self.Send("Login",(Username,Password))
Sucess,Message,_ = self.Recv()
return Sucess,Message
################################
################################
def GetNoFlyZones(self):
self.Send("GetNoFlyZones",())
Sucess,Message,NoFlyZones = self.Recv()
return Sucess,Message,NoFlyZones
def AddNoFlyZone(self,StartCoords,EndCoords,Level):
self.Send("AddNoFlyZone",(StartCoords,EndCoords,Level))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def RemoveNoFlyZone(self,ZoneID):
self.Send("RemoveNoFlyZone",(ZoneID,))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def ModifyNoFlyZoneLevel(self,ZoneID,Level):
self.Send("ModifyNoFlyZoneLevel",(ZoneID,Level))
Sucess,Message,_ = self.Recv()
return Sucess,Message
##################################
def AddDrone(self,DroneName,DronePassword,Type,Speed,Range,Weight):
self.Send("AddDrone",(DroneName,DronePassword,Type,Speed,Range,Weight))
Sucess, Message,_ = self.Recv()
return Sucess,Message
def RemoveDrone(self,DroneID):
self.Send("RemoveDrone",(DroneID,))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def GetDroneID(self,DroneName):
self.Send("GetDroneID",(DroneName,))
Sucess,Message,DroneID = self.Recv()
return Sucess,Message,DroneID
def GetDroneCredentials(self,DroneID):
self.Send("GetDroneCredentials",(DroneID,))
Sucess,Message,Credentials = self.Recv()
return Sucess,Message,Credentials
def SetDroneCredentials(self,DroneID,DronePassword):
self.Send("SetDroneCredentials",(DroneID,DronePassword))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def CheckDroneOwnership(self,UserID,DroneID):
self.Send("CheckDroneOwnership",(UserID,DroneID))
Sucess,Message,Data = self.Recv()
return Sucess,Message,Data
def GetDroneInfo(self,DroneID):
self.Send("GetDroneInfo",(DroneID,))
Sucess,Message,DroneInfo = self.Recv()
return Sucess,Message,DroneInfo
def GetDronesUser(self):
self.Send("GetDronesUser",())
Sucess,Message,DroneInfo = self.Recv()
return Sucess,Message,DroneInfo
def GetDronesAll(self):
self.Send("GetDronesAll",())
Sucess,Message,DroneInfo = self.Recv()
return Sucess,Message,DroneInfo
###########################################
def GetUserID(self,Username):
self.Send("GetUserID",(Username,))
Sucess,Message,UserID = self.Recv()
return Sucess,Message,UserID
def GetUsername(self,UserID):
self.Send("GetUsername",(UserID,))
Sucess,Message,Username = self.Recv()
return Sucess,Message,Username
def AddUser(self,Username,Password):
self.Send("AddUser",(Username,Password))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def SetFlightVisibility(self,Visibility):
self.Send("SetFlightVisibility",(Visibility,))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def SetAccountType(self,Permission,Value):
self.Send("SetAccountType",(Permission,Value))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def UserChangePassword(self,OldPassword,NewPassword):
self.Send("UserChangePassword",(OldPassword,NewPassword))
Sucess,Message,_ = self.Recv()
return Sucess,Message
##########################################
def GetFlightsUser(self):
self.Send("GetFlightsUser",())
Sucess,Message,UserFlights = self.Recv()
return Sucess,Message,UserFlights
def GetFlightsAll(self):
self.Send("GetFlightsAll",())
Sucess,Message,AllFlights = self.Recv()
return Sucess,Message,AllFlights
def AddFlight(self,DroneID,HighPoints,StartTime):
self.Send("AddFlight",(DroneID,HighPoints,StartTime))
Sucess,Message,FlightInfo = self.Recv()
return Sucess,Message,FlightInfo
def RemoveFlight(self,FlightID):
self.Send("RemoveFlight",(FlightID,))
Sucess,Message,_ = self.Recv()
return Sucess,Message
##########################################
def GetFlightWaypointsUser(self):
self.Send("GetFlightWaypointsUser",())
Sucess,Message,UserWaypoints = self.Recv()
return Sucess,Message,UserWaypoints
def GetFlightWaypointsAll(self):
self.Send("GetFlightWaypointsAll",())
Sucess,Message,AllWaypoints = self.Recv()
return Sucess,Message,AllWaypoints
##########################################
def GetMonitorID(self,MonitorName):
self.Send("GetMonitorID",(MonitorName,))
Sucess,Message,MonitorID = self.Recv() #MonitorID = [NumberID] as how db returns
return Sucess,Message,MonitorID
def GetMonitorName(self,MonitorID):
self.Send("GetMonitorName",(MonitorID,))
Sucess,Message,MonitorName = self.Recv() # [MonitorName]
return Sucess,Message,MonitorName
##########################################
def AddMonitorPermission(self,MonitorID,ExpiryDate):
self.Send("AddMonitorPermission",(MonitorID,ExpiryDate))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def RemoveMonitorPermission(self,MonitorID):
self.Send("RemoveMonitorPermission",(MonitorID,))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def ModifyMonitorPermissionDate(self,MonitorID,NewDate):
self.Send("ModifyMonitorPermissionDate",(MonitorID,NewDate))
Sucess,Message,_ = self.Recv()
return Sucess,Message
def GetMonitorPermissionUser(self):
self.Send("GetMonitorPermissionUser",())
Sucess,Message,MonitorPermissionsUser = self.Recv()
return Sucess,Message,MonitorPermissionsUser
##############################################
def Exit(self):
self.Send("Exit",())
Sucess,Message,_ = self.Recv()
self._con.close()
return Sucess,Message
##############################################
##############################################
def Send(self,Code,data): #encrypt and send data to server
Info = self._Crypto.Encrypt(codecs.encode(str((Code,data))))
self._con.sendall(Info)
def Recv(self): #receive and decrypt data from server
try:
data = self._Crypto.Decrypt(recvall.recvall(self._con))
data = ast.literal_eval(codecs.decode(data))
# Sucess, Message , Data
return data[0],data[1],data[2]
except Exception as e:
print("Socket data receive error")
print(str(e))
return (False,"Conversion/Transfer Error"+str(e),[])
def CreateUserInterface(IP = "192.168.0.19",Port = 8000): #Create user interface to server
soc = Connect(IP,Port)
U = UserInterface(soc)
return U