-
Notifications
You must be signed in to change notification settings - Fork 1
/
MQTTClient.py
320 lines (257 loc) · 12.5 KB
/
MQTTClient.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
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
"""MQTT Client GUI
Author: Sahin MERSIN - electrocoder <[email protected]>
Source Code: https://github.com/electrocoder/MQTTClient
MQTT Examples: https://github.com/mesebilisim/mqtt-examples
Date: 12.11.2022
File: This script is MQTT Client Main Application
"""
import os
import tkinter as tk
import webbrowser
from tkinter import messagebox
from tkinter.font import Font
import about
import new_connect
import new_topic
import subscriber
from config_file import ConfigFile
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title('MQTT Client 0v4')
self.geometry('750x350')
# self.rowconfigure(0, weight=1)
# self.columnconfigure(0, weight=1)
ipadding = {'ipadx': 1, 'ipady': 1}
frame = tk.Frame(self)
frame.pack(fill=tk.BOTH, expand=True)
font_size = 12
self.text_font = Font(size=font_size)
self.msg_filter = False
self.label_broker = tk.Label(frame, text="Broker", font=font_size)
self.label_broker.pack(**ipadding, side=tk.LEFT)
self.entry_broker_text = tk.StringVar(self)
self.options_list = ConfigFile().read_sections()
self.entry_broker_text.set(self.options_list[0])
self.entry_broker = tk.OptionMenu(frame,
self.entry_broker_text,
*self.options_list)
self.entry_broker.config(font=font_size)
menu = self.nametowidget(
self.entry_broker.menuname)
menu.config(font=font_size)
self.entry_broker.pack(**ipadding, expand=True, side=tk.LEFT)
self.button_connect = tk.Button(frame,
text="Connect", font=font_size,
command=self.button_connect)
self.button_connect.pack(**ipadding, expand=True, side=tk.LEFT)
self.button_disconnect = tk.Button(frame,
text="Disconnect", font=font_size,
state=tk.DISABLED,
command=self.button_disconnect)
self.button_disconnect.pack(**ipadding, expand=True, side=tk.LEFT)
frame1 = tk.Frame(self)
frame1.pack(fill=tk.BOTH, expand=True)
# publish topic
self.label_publish_topic = tk.Label(frame1, text="Publish Topic",
font=font_size)
self.label_publish_topic.pack(**ipadding,
side=tk.LEFT)
self.entry_publish_topic_text = tk.StringVar(self)
self.entry_publish_topic = tk.Entry(frame1,
textvariable=self.entry_publish_topic_text,
font=font_size)
self.entry_publish_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
self.label_publish_msg_topic = tk.Label(frame1, text="Publish Message",
font=font_size)
self.label_publish_msg_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
self.entry_publish_topic_msg_text = tk.StringVar(self)
self.entry_publish_msg_topic = tk.Entry(frame1,
textvariable=self.entry_publish_topic_msg_text,
font=font_size)
self.entry_publish_msg_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
self.button_publish_topic = tk.Button(frame1,
text="Publish", font=font_size,
command=self.button_publish_topic)
self.button_publish_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
frame2 = tk.Frame(self)
frame2.pack(fill=tk.BOTH, expand=True)
# subscribe topic
self.label_subscribe_topic = tk.Label(frame2, text="Subscribe Topic",
font=font_size)
self.label_subscribe_topic.pack(**ipadding,
side=tk.LEFT)
self.entry_subscribe_topic_text = tk.StringVar(self)
self.options_list = ["-", ]
self.entry_subscribe_topic = tk.OptionMenu(frame2,
self.entry_subscribe_topic_text,
*self.options_list,
command=self.add_subscribe_topic)
self.entry_subscribe_topic.config(font=font_size)
menu = self.nametowidget(
self.entry_subscribe_topic.menuname)
menu.config(font=font_size)
self.entry_subscribe_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
self.button_subscribe_topic = tk.Button(frame2,
text="Subscribe",
font=font_size,
state=tk.DISABLED,
command=self.button_subscribe)
self.button_subscribe_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
self.button_add_subscribe_topic = tk.Button(frame2,
text="Add Subscribe Topic",
font=font_size,
state=tk.DISABLED,
command=self.add_subscribe_topic)
self.button_add_subscribe_topic.pack(**ipadding, expand=True,
side=tk.LEFT)
frame3 = tk.Frame(self)
frame3.pack(fill=tk.BOTH, expand=True)
# filter msg
self.label_msg_filter = tk.Label(frame3, text="Filter Message",
font=font_size)
self.label_msg_filter.pack(**ipadding,
side=tk.LEFT)
self.entry_msg_filter_text = tk.StringVar(self)
self.entry_msg_filter = tk.Entry(frame3,
textvariable=self.entry_msg_filter_text,
font=font_size)
self.entry_msg_filter.pack(**ipadding, expand=True,
side=tk.LEFT)
self.button_filter_add = tk.Button(frame3,
text="Add Filter", font=font_size,
state=tk.DISABLED,
command=self.add_filter)
self.button_filter_add.pack(**ipadding, expand=True,
side=tk.LEFT)
self.button_filter_remove = tk.Button(frame3,
text="Remove Filter",
font=font_size,
state=tk.DISABLED,
command=self.remove_filter)
self.button_filter_remove.pack(**ipadding, expand=True,
side=tk.LEFT)
frame4 = tk.Frame(self)
frame4.pack(fill=tk.BOTH, expand=True)
# subscribe list
self.listbox_message = tk.Text(frame4, font=font_size, height=10)
self.listbox_message.pack(**ipadding, expand=True, fill=tk.BOTH,
side=tk.LEFT)
# menu
menubar = tk.Menu(self)
self.config(menu=menubar)
menu_connect = tk.Menu(menubar, tearoff=0)
menu_connect.add_command(label='New Connect',
command=self.new_connect_window)
# menu_connect.add_command(label='Open Connect',
# command=self.open_connect_window)
menu_connect.add_separator()
menu_connect.add_command(label='Exit', command=self.quit)
menubar.add_cascade(label="Connect", menu=menu_connect)
menu_help = tk.Menu(menubar, tearoff=0)
menu_help.add_command(label='Help', command=self.help)
menu_help.add_command(label='About',
command=self.about_window)
menubar.add_cascade(label="Help", menu=menu_help)
frame5 = tk.Frame(self)
frame5.pack(fill=tk.BOTH, expand=True)
# status bar
self.connect_status_text = tk.StringVar()
self.connect_status_text.set("...")
self.connect_status = tk.Label(frame5,
textvariable=self.connect_status_text,
bd=1,
relief=tk.SUNKEN, anchor=tk.W)
self.connect_status.pack(**ipadding, fill=tk.BOTH, expand=True)
self.subscriber = subscriber.Subscriber(self)
def button_connect(self):
if self.entry_broker_text.get():
name, broker, port, username, password = ConfigFile().read_broker(
self.entry_broker_text.get())
if self.subscriber.connect_start(name, broker, port, username,
password):
self.connect_status_text.set("Connected")
self.button_connect["state"] = tk.DISABLED
self.button_connect["text"] = "Connected"
self.button_disconnect[
"state"] = tk.NORMAL
self.button_subscribe_topic[
"state"] = tk.NORMAL
self.button_publish_topic[
"state"] = tk.NORMAL
self.subscribe_list(name)
self.button_add_subscribe_topic["state"] = tk.NORMAL
else:
messagebox.showerror("showerror", "Please select broker.")
def subscribe_list(self, name):
self.entry_subscribe_topic['menu'].delete(0, 'end')
new_choices = ConfigFile().read_topics(name).split(',')
for choice in new_choices:
if choice:
self.entry_subscribe_topic['menu'].add_command(
label=choice,
command=tk._setit(self.entry_subscribe_topic_text,
choice))
self.entry_subscribe_topic_text.set(choice)
def button_disconnect(self):
print("button_disconnect")
if self.subscriber.connect_stop():
self.connect_status_text.set("Disconnect")
self.button_connect["state"] = tk.NORMAL
self.button_connect["text"] = "Connect"
self.button_disconnect["state"] = tk.DISABLED
self.button_subscribe_topic[
"state"] = tk.DISABLED
self.button_publish_topic[
"state"] = tk.DISABLED
self.button_add_subscribe_topic["state"] = tk.DISABLED
def button_subscribe(self):
print("button_subscribe")
self.subscriber.subscribe_start(
self.entry_subscribe_topic_text.get())
self.button_filter_add[
"state"] = tk.NORMAL
self.button_filter_remove[
"state"] = tk.DISABLED
def button_publish_topic(self):
print("button_publish_topic")
topic = self.entry_publish_topic_text.get()
msg = self.entry_publish_topic_msg_text.get()
self.listbox_message.insert(tk.END,
"> {}".format(msg))
self.listbox_message.see("end")
self.subscriber.publish_start(topic, msg)
def about_window(self):
about.AboutWindow(self, self.text_font)
def help(self):
webbrowser.open_new_tab("https://github.com/electrocoder/MQTTClient")
def new_connect_window(self):
new_connect.NewConnect(self, self.text_font)
def add_subscribe_topic(self, *args):
print("add_subscribe_topic")
new_topic.NewTopic(self, self.text_font)
def add_filter(self):
self.msg_filter = True
self.button_filter_add[
"state"] = tk.DISABLED
self.button_filter_remove[
"state"] = tk.NORMAL
def remove_filter(self):
self.msg_filter = False
self.button_filter_add[
"state"] = tk.NORMAL
self.button_filter_remove[
"state"] = tk.DISABLED
if __name__ == "__main__":
app = App()
basedir = os.path.dirname(__file__)
file_name = os.path.join(basedir, "icon.png")
photo = tk.PhotoImage(file=file_name)
app.wm_iconphoto(False, photo)
app.mainloop()