|
#!/usr/bin/python |
|
from tkinter import * |
|
from tkinter import messagebox |
|
import apsw |
|
import time, threading |
|
import subprocess |
|
from subprocess import Popen, PIPE |
|
import os |
|
import sys |
|
from buttons_conf import * |
|
|
|
cwd=os.path.dirname(__file__) |
|
DbTimestamp=os.path.getmtime(dbfile) |
|
pid=os.getpid() |
|
text="was pressed" |
|
top = None |
|
|
|
def toggle_button(e): |
|
B = e.widget |
|
# time.sleep(2) |
|
button_name=B.cget("text") |
|
connection=apsw.Connection(dbfile) |
|
cursor=connection.cursor() |
|
for button_id,name,script,usb_port,NIC,status in cursor.execute("select button_id,name,script,usb_port,NIC,status from buttons where name='%s'" % button_name ): |
|
print(button_id,name,script,usb_port,NIC,status) |
|
cursor.close() |
|
connection.close() |
|
if (status == 0): |
|
rc = subprocess.call([cwd + "/" + script, "start_usb", usb_port]) |
|
if ( rc == 0 ): |
|
B.config(fg ='green') |
|
elif (status == 1): |
|
subprocess.call([cwd + "/" + script, "stop_nic", NIC]) |
|
B.config(fg ='red') |
|
|
|
def refresh_from_button(): |
|
connection=apsw.Connection(dbfile) |
|
cursor=connection.cursor() |
|
for button_id,name,status in cursor.execute("select button_id,name,status from buttons"): |
|
button=buttons[button_id] |
|
if (status == 0): |
|
button.config(fg ='red') |
|
elif (status == 1): |
|
button.config(fg ='green') |
|
cursor.close() |
|
connection.close() |
|
|
|
def refresh_periodic(): |
|
global DbTimestamp |
|
global top |
|
if (top is None) or not top.winfo_exists(): |
|
sys.exit() |
|
NewTimetamp=os.path.getmtime(dbfile) |
|
if DbTimestamp == NewTimetamp: |
|
threading.Timer(2, refresh_periodic).start() |
|
return |
|
DbTimestamp=NewTimetamp |
|
connection=apsw.Connection(dbfile) |
|
cursor=connection.cursor() |
|
cursor.execute("PRAGMA locking_mode=NORMAL") |
|
for button_id,name,status in cursor.execute("select button_id,name,status from buttons"): |
|
button=buttons[button_id] |
|
if (status == 0): |
|
button.config(fg ='red') |
|
elif (status == 1): |
|
button.config(fg ='green') |
|
cursor.close() |
|
connection.close() |
|
threading.Timer(15, refresh_periodic).start() |
|
|
|
def create_table(): |
|
subprocess.call([cwd + "/netmgr" , "mktables"]) |
|
|
|
def show_status(): |
|
process = Popen([cwd +"/netmgr", "usb_scan"], stdout=PIPE) |
|
(output, err) = process.communicate() |
|
exit_code = process.wait() |
|
messagebox.showinfo("USB ort scan",output) |
|
|
|
print(pid) |
|
print(DbTimestamp) |
|
create_table() |
|
top = Tk() |
|
topframe= Frame(top) |
|
topframe.pack(side = TOP) |
|
botomframe= Frame(top) |
|
botomframe.pack(side = BOTTOM) |
|
connection=apsw.Connection(dbfile) |
|
cursor=connection.cursor() |
|
buttons = {} |
|
for button_id,name,script,usb_port,NIC,status in cursor.execute("select button_id,name,script,usb_port,NIC,status from buttons"): |
|
key=button_id |
|
if status == 0: |
|
button = Button(topframe, text =name, fg ='red', activeforeground='yellow') |
|
else: |
|
button = Button(topframe, text =name, fg ='green', activeforeground='yellow') |
|
button.bind("<1>" ,toggle_button) |
|
button.pack(side = LEFT) |
|
buttons[button_id] = button |
|
|
|
cursor.close() |
|
connection.close() |
|
key += 1 |
|
btext = "Refresh" |
|
button = Button(botomframe, text =btext , fg ='red', activeforeground='yellow', command=refresh_from_button) |
|
button.pack(side = LEFT) |
|
buttons[key] = button |
|
key += 1 |
|
btext = "Status" |
|
button = Button(botomframe, text =btext , fg ='red', activeforeground='yellow', command=show_status) |
|
button.pack(side = LEFT) |
|
buttons[key] = button |
|
refresh_periodic() |
|
top.mainloop() |