-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
359 lines (280 loc) · 9.79 KB
/
main.py
File metadata and controls
359 lines (280 loc) · 9.79 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from tkinter import *
import pymysql
from tkinter import ttk
import paho.mqtt.client as mqtt
import random
# Connecting Database
db = pymysql.connect(
host='localhost',
user='aman',
password='aman0611',
db='sunoye',
charset='utf8mb4')
cursor = db.cursor()
# Main Frame Window
tk = Tk()
tk.title("SunOye 2.0")
# Variables
room_no = StringVar()
password = StringVar()
username = StringVar()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully")
else:
print("Connect returned result code: " + str(rc))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
ttk.Label(message_window, text=msg.payload.decode("utf-8"), font=("Courier", 10)).pack(anchor='w')
# create the client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# enable TLS
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
# set username and password
client.username_pw_set("perfecto", "Aman0611")
# connect to HiveMQ Cloud on port 8883
client.connect("ae6c8cfe6e654a4ebf55c86ec9b1e196.s1.eu.hivemq.cloud", 8883)
# publish "Hello" to the topic "my/test/topic"
client.loop_start()
# Destroy A Window Function
def destroyed(a):
a.destroy()
# Room Already Exist
def no_exist():
global failed_message
failed_message = Toplevel(login_screen)
failed_message.title("Invalid Message")
Label(failed_message, text="Room Doesn't exist", fg='red').pack()
Label(failed_message, text="").pack()
Button(failed_message, text="Ok", relief="groove", height=1, width=10, command=lambda a=failed_message: destroyed(a)).pack()
failed_message.grab_set()
global room_no
global password
global username
room_no.set("")
password.set("")
username.set("")
# Failed to login Function
def failed():
global failed_message
failed_message = Toplevel(login_screen)
failed_message.title("Invalid Message")
Label(failed_message, text="Invalid Room Number or Passcode", fg='red').pack()
Label(failed_message, text="").pack()
Button(failed_message, text="Ok", relief="groove", height=1, width=10, command=lambda a=failed_message: destroyed(a)).pack()
failed_message.grab_set()
global room_no
global password
global username
room_no.set("")
password.set("")
username.set("")
# Leave the chat function
def leave():
global tk
sql = "update roomtable set members = members - 1 where room_no = %s"
cursor.connection.ping()
cursor.execute(sql,[(room_no.get())])
db.commit()
sql = "select * from roomtable where room_no = %s"
cursor.connection.ping()
cursor.execute(sql,[(room_no.get())])
db.commit()
results = cursor.fetchall()
for rows in results:
if rows[3] == 0:
sql = "update roomtable set active = 'n' where room_no = %s"
cursor.connection.ping()
cursor.execute(sql,[(room_no.get())])
db.commit()
tk.destroy()
client.loop_stop()
db.close()
# Login Window
def login_win():
# tk.withdraw()
global opt_window
opt_window.destroy()
global login_screen
login_screen = Toplevel(tk)
login_screen.title("Login")
login_screen.geometry("300x250")
Label(login_screen, text="Enter the Room Credentials").pack()
Label(login_screen, text="").pack()
global room_no
global password
global username
Label(login_screen, text="Room Number").pack()
room = Entry(login_screen, textvariable=room_no)
room.pack()
Label(login_screen, text="").pack()
Label(login_screen, text="Passcode").pack()
pass_entry = Entry(login_screen, textvariable=password, show="*")
pass_entry.pack()
Label(login_screen, text="").pack()
Label(login_screen, text="Enter Your Name").pack()
user_entry = Entry(login_screen, textvariable=username)
user_entry.pack()
Label(login_screen, text="").pack()
Button(login_screen, text="Enter", width=10, height=1, command=login_verify).pack()
Label(login_screen, text="").pack()
# Login Verification
def login_verify():
rno = room_no.get()
passw = password.get()
cursor.connection.ping()
sql = "select * from roomtable where room_no = %s and passcode = %s"
cursor.execute(sql,[(rno),(passw)])
results = cursor.fetchall()
if results:
for i in results:
if(i[2]=='n'):
no_exist()
break
logged()
global login_screen
destroyed(login_screen)
strr = "Welcome to Room " + str(room_no.get()) + ".\tUsername: " + str(username.get()) + "\tPasscode: " + str(password.get())
Label(message_window, text=strr, font=("Arial", 12)).pack(anchor='w')
break
else:
failed()
# To Subscribe to a topic
def subs(rno):
# Subscribe to topic
global topic_dir
topic_dir = "sunoye/room/" + str(rno)
client.subscribe(topic_dir)
print("Subscribed")
# Successful Logged In
def logged():
subs(room_no.get())
# Increase Member count for room_no
cursor.connection.ping()
sql = "update roomtable set members = members + 1 where room_no = %s"
cursor.execute(sql,[(room_no.get())])
db.commit()
# Set Active for room_no
sql = "update roomtable set active = 'y' where room_no = %s"
cursor.connection.ping()
cursor.execute(sql, [(room_no.get())])
db.commit()
tk.deiconify()
# Message Sending Function
def send(event=None):
global msg
global mesg
mesg = str(username.get()) + "->>" + str(msg.get())
canvas.yview_moveto('1.0')
msg.set("")
global topic_dir
client.publish(topic_dir, mesg)
# Retrace Option Window
def go_home():
global full_warn
global username
username.set("")
full_warn.destroy()
option_window()
# Room Full
def room_new():
global room_no
global password
sql = "select count(*) from roomtable"
cursor.connection.ping()
cursor.execute(sql)
print("ok")
(i,) = cursor.fetchone()
passw = random.randint(10000,99999)
print(i)
print(passw)
rno = 'r' + str(i)
print(rno)
sql = "insert into roomtable values(%s, %s, 'y', 1)"
cursor.execute(sql,[(rno),(passw)])
db.commit()
subs(rno)
room_no.set(rno)
password.set(passw)
strr = "Welcome to Room " + str(room_no.get()) + ".\tUsername: " + str(username.get()) + "\tPasscode: " + str(password.get())
Label(message_window, text=strr, font=("Arial", 12)).pack(anchor='w')
tk.deiconify()
# New Room Function
def new_room():
global create_window
create_window.destroy()
sql = "select * from roomtable where active = 'n'"
global room_no
global password
cursor.connection.ping()
cursor.execute(sql)
results = cursor.fetchone()
if results:
room_no.set(results[0])
password.set(results[1])
# subs(room_no.get())
strr = "Welcome to Room " + str(room_no.get()) + ".\tUsername: " + str(username.get()) + "\tPasscode: " + str(password.get())
Label(message_window, text=strr, font=("Arial", 12)).pack(anchor='w')
logged()
tk.deiconify()
else:
room_new()
# Create New Room Window
def create_room():
global opt_window
opt_window.destroy()
global create_window
create_window = Toplevel(tk)
create_window.geometry("300x250")
global username
Label(create_window, text="").pack()
Label(create_window, text="Create Room").pack()
Label(create_window, text="").pack()
Label(create_window, text="Username").pack()
user_entry = Entry(create_window, textvariable=username)
user_entry.pack()
Label(create_window, text="").pack()
Button(create_window, text="Create Room", command=new_room).pack()
# First Window (New Room/Existing Room)
def option_window():
tk.withdraw()
global opt_window
opt_window = Toplevel(tk)
opt_window.title("Option Window")
opt_window.geometry("300x150")
Label(opt_window, text="").pack()
Button(opt_window, text="Join an Existing Room", width=20, height=1, command=login_win).pack()
Label(opt_window, text="").pack()
Button(opt_window, text="Create a New Room", width=20, height=1, command=create_room).pack()
# Call Option Window First
option_window()
# Message Variable
msg = StringVar()
container = ttk.Frame(tk) # Container Frame
canvas = Canvas(container, width=650, height=600)
# Scrollbar
scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
# Message Window
message_window = ttk.Frame(canvas)
message_window.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# Creating Message Window in Canvas
canvas.create_window((0, 0), window=message_window, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
# Packing Canvas Components
container.pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Message Entry Field and Send Button and Leave Button
msg_field = Entry(tk, textvariable=msg, width=90)
msg_field.bind("<Return>", send)
msg_field.pack(side="left", padx=2)
send_btn = Button(tk, text="Send", command=send, width=7)
send_btn.pack(side="left")
exit_btn = Button(tk, text="Leave", command=leave, width=7, bg='red', fg='white')
exit_btn.pack(padx=2)
# Blocking call that processes network traffic, dispatches callbacks and handles reconnecting.
tk.protocol("WM_DELETE_WINDOW", leave)
tk.mainloop()