-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpythongui2.py
359 lines (312 loc) · 9.76 KB
/
pythongui2.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
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
#!/usr/bin/python3
from datetime import *
import sys
import tkinter
from tkinter import messagebox
import random
from tkinter import *
import meshtastic
from pubsub import pub
import threading
import pygame
import time
import csv
import datetime
import json
import base64
root = Tk()
width_value = root.winfo_screenwidth()
height_value = root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (width_value, height_value))
#root.geometry("658x395")
class RepeatedTimer(object): # Timer helper class
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.next_call = time.time()
self.start()
def _run(self):
self.is_running = False
self.start()
self.s = self.function(*self.args, **self.kwargs)
if self.s == 1:
self.stop()
def start(self):
if not self.is_running:
self.next_call += self.interval
self._timer = threading.Timer(self.next_call - time.time(), self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
def playsound(soundfile):
"""Play sound through default mixer channel in blocking manner.
This will load the whole sound into memory before playback
"""
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound(soundfile)
clock = pygame.time.Clock()
sound.play()
while pygame.mixer.get_busy():
clock.tick(1000)
def LoadConnectionInfo(txt, EntryText):
if EntryText != '':
txt.config(state=NORMAL)
if txt.index('end') != None:
now = datetime.datetime.now()
txt.insert(END, EntryText+'\n')
LineNumber = float(txt.index('end'))-1.0
txt.tag_add(EntryText, LineNumber, LineNumber+1.0)
txt.tag_config(EntryText, foreground="#FFFFFF", font = ("Sans_Serif", 10))
txt.config(state=DISABLED)
txt.yview(END)
def LoadMyEntry(txt, EntryText):
if EntryText != '':
txt.config(state=NORMAL)
def LoadOtherEntry(txt, EntryText, value):
if EntryText != '':
txt.config(state=NORMAL)
if txt.index('end') != None:
if value == 0:
dev = "Device_1->"
color = "#FF8000"
else:
dev = "Device_2->"
color = "#32CD32"
LineNumber = float(txt.index('end'))-1.0
txt.insert(END, "\n" + dev + EntryText)
txt.tag_add(dev, LineNumber, LineNumber+1.0)
txt.tag_config(dev, foreground=color, font=("Arial", 14, "bold"))
txt.config(state=DISABLED)
txt.yview(END)
def onReceive(packet, interface): # called when a packet arrives
print("Received: "+str(packet))
try:
size = sys.getsizeof(packet['decoded']['data']['payload'])
fromid = (packet['fromId'])
toId = (packet['toId'])
data = (packet['decoded']['data']['payload']).decode('utf-8')
rx_time = (packet['rxTime'])
rxSnr = (packet['rxSnr'])
except:
data = packet.(decode('utf-8'))
data = str(data)
try:
value = (packet['rxSnr'])
except:
value = 0
new_row = [data, size, fromid, toId, rx_time, rxSnr]
#write message to csv file
with open('new.csv', 'a') as appendobj:
append = csv.writer(appendobj)
append.writerow(new_row)
#Write message to chat window
LoadOtherEntry(txt, data, value)
input_dict = {'data': data, 'size': size, 'fromId': fromid, 'toId': toId, 'Time_stamp': rx_time}
result = []
for data, size in input_dict.items():
result.append({'Size': size, 'Data': data})
#playsound("/home/arijit/meshtasticGUI/notif.wav")
#Scroll to the bottom of chat windows
txt.yview(END)
new = open('/home/arijit/meshtasticGUI/transmission_record.json', "a")
new.write(json.dumps(result))
new.close()
def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
LoadConnectionInfo(txt,"\nStarting..")
time.sleep(2)
LoadConnectionInfo(txt,"Connection established Successfully\nStart Communicating..")
def stop2():
LoadConnectionInfo(txt, "\n\nConnection is closing now..")
timer(3)
interface.close()
LoadConnectionInfo(txt, "Disconnected now..")
def stop():
LoadConnectionInfo(txt, "\n\nConnection is closing now..")
timer(3)
interface.close()
time.sleep(2)
LoadConnectionInfo(txt, "Disconnected now..")
def send():
EntryText = e.get()
#Erase previous message in Entry Box
e.delete('0',END)
# Loading the message on the chat window
LoadMyEntry(txt, EntryText)
#Scroll to the bottom of chat windows
txt.yview(END)
#Send my mesage to all others
interface.sendText(EntryText)
def true():
timer(1)
def randchar(ran):
if ran == '':
return 1
LoadMyEntry(txt, ran)
txt.yview(END) #Scroll to the bottom of chat windows
interface.sendText(ran)
def randchar2():
file_pos=0
with open('NewFile.txt', 'r') as f:
for line in f:
l = line.strip()
if l is None:
break
LoadMyEntry(txt, l)
txt.yview(END)
interface.sendText(l)
print(l)
time.sleep(30)
file_pos += len(line)
f.seek(file_pos)
f.close()
file_pos=0
with open('NewFile.txt', 'r') as f:
for line in f:
l = line.strip()
if l is None:
break
LoadMyEntry(txt, l)
txt.yview(END)
interface.sendText(l)
print(l)
time.sleep(60)
file_pos += len(line)
f.seek(file_pos)
f.close()
file_pos=0
with open('NewFile.txt', 'r') as f:
for line in f:
l = line.strip()
if l is None:
break
LoadMyEntry(txt, l)
txt.yview(END)
interface.sendText(l)
print(l)
time.sleep(90)
file_pos += len(line)
f.seek(file_pos)
f.close()
file_pos=0
with open('NewFile.txt', 'r') as f:
for line in f:
l = line.strip()
if l is None:
break
LoadMyEntry(txt, l)
txt.yview(END)
interface.sendText(l)
print(l)
time.sleep(120)
file_pos += len(line)
f.seek(file_pos)
f.close()
stop2()
return 1
def timer(x):
if x != 3:
print("starting random messages...")
'''file = open('myFile.txt', 'r')
while True:
rando = file.readline()
ran = rando.strip()
print(ran)
#randchar2()
#randchar()'''
#rt = RepeatedTimer(150, randchar2)
time1 = threading.Timer(1, randchar2)
time1.start()
'''rt = RepeatedTimer(1, randchar, ran)
time.sleep(2)
if ran == '':
break
file.close()
stop2()
file = open('myFile.txt', 'r')
while True:
rando = file.readline()
ran = rando.strip()
print(ran)
if not ran:
break
randchar(ran)
#rt = RepeatedTimer(60, randchar(ran))
file.close()
stop2()
file = open('myFile.txt', 'r')
while True:
rando = file.readline()
ran = rando.strip()
print(ran)
if not ran:
break
randchar(ran)
#rt = RepeatedTimer(120, randchar(ran))
file.close()
stop2()
file = open('myFile.txt', 'r')
while True:
rando = file.readline()
ran = rando.strip()
print(ran)
if not ran:
break
randchar(ran)
rt = RepeatedTimer(180, randchar, ran)
file.close()
stop2()'''
def exit():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
stop2()
root.destroy()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
stop2()
root.destroy()
def PressAction(event):
e.config(state=NORMAL)
send()
def DisableEntry(event):
e.config(state=DISABLED)
root.grid_columnconfigure(0, weight=8)
root.grid_columnconfigure(1, weight=1)
root.grid_columnconfigure(2, weight=1)
root.grid_columnconfigure(3, weight=1)
root.grid_columnconfigure(4, weight=1)
root.grid_rowconfigure(0, weight=8)
root.grid_rowconfigure(1, weight=0)
root.configure(background = "White")
root.title("MeshtasticGUI") #"tk" is replaced with "Meshtastic"
root.resizable(width=TRUE, height=TRUE)
img = tkinter.Image("photo", file="/home/arijit/meshtasticGUI/41zvoDg7HlL.png")
root.tk.call('wm','iconphoto',root._w, img)
txt = Text(root, width = 100, height = 100, background = "black", foreground = "#FFFFFF", font = ("Sans_Serif", 10))
root.protocol("WM_DELETE_WINDOW", on_closing)
#Bind a scrollbar to the GUI App
scrollbar = Scrollbar(root, command=txt.yview)
txt['yscrollcommand'] = scrollbar.set
scrollbar.place(x = 1355,y = 7,height = 660)
#root.iconbitmap(r"@/home/arijit/Documents/M.Tech Final Year Project/code/41zvoDg7HlL.xbm")
txt.grid(row=0,column=0,columnspan = 5,ipadx=800, sticky = "NSEW") #line txt for increasing text area
#scrollbar = Scrollbar(txt, orient=VERTICAL)
#scrollbar.pack(fill=Y, side=RIGHT)
e = Entry(root,width = 145)
pub.subscribe(onConnection, "meshtastic.connection.established")
#start = Button(root,text = "START",command = subscribe).grid(row = 1,column = 2)
stop = Button(root,text = "STOP",command = stop).grid(row = 1,column = 2,ipadx=70, sticky="NSEW")
sent = Button(root,text = "SEND",command = send).grid(row = 1,column = 1,ipadx=70, sticky="NSEW")
e.bind("<Return>", DisableEntry)
e.bind("<KeyRelease-Return>", PressAction)
randomchar = Button(root,text = "RAND",command = true).grid(row = 1,column = 3,ipadx=70, sticky="NSEW")
exit = Button(root,text = "EXIT",command = exit).grid(row = 1,column = 4,ipadx=70, sticky="NSEW")
e.grid(row = 1,column = 0)
pub.subscribe(onReceive, "meshtastic.receive")# By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
interface = meshtastic.SerialInterface()
root.mainloop()