Updater: job in a thread

Add a progress bar
Put device in OTA mode automatically
This commit is contained in:
Mathieu Maret 2016-09-22 12:58:12 +02:00
parent a66a8fe6ec
commit 4d918e4e15
1 changed files with 51 additions and 12 deletions

View File

@ -6,34 +6,74 @@
from tkinter import * from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from tkinter.messagebox import * from tkinter.messagebox import *
from tkinter.ttk import *
from threading import Thread
import urllib.request
import time
import subprocess import subprocess
class Updater(Thread):
def __init__(self, gui, ip, file):
Thread.__init__(self)
self.gui = gui
self.ip = ip
self.file = file
class Updater(Frame): def run(self):
file = None self.gui.installButton['state'] = 'disabled'
self.gui.pb["value"] = 1
try:
print("Put device in OTA mode")
urllib.request.urlopen("http://" + self.ip + "/otamode").read()
urllib.request.urlopen("http://" + self.ip + "/reboot").read()
self.gui.pb["value"] = 2
print("Waiting for device to reboot")
time.sleep(10)
self.gui.pb["value"] = 3
subprocess.call(["python", "espota.py", "-i",
self.ip, "-f", self.file])
except Exception as e:
showerror("Error", e)
finally:
self.gui.pb["value"] = 4
self.gui.installButton['state'] = 'normal'
class UpdaterGui(Frame):
file = "firmware.bin"
sleep = 0
installButton = None
def __init__(self, win, **kwargs): def __init__(self, win, **kwargs):
Frame.__init__(self, win, **kwargs) Frame.__init__(self, win, **kwargs)
button_opt = {'fill': constants.BOTH, 'padx': 5, 'pady': 5} button_opt = {'fill': constants.BOTH, 'padx': 5, 'pady': 5}
Button(root, text='Select firmware', Button(win, text='Select firmware',
command=self.askopenfile).pack(**button_opt) command=self.askopenfile).pack(**button_opt)
self.ipEntry = Entry(root) self.ipEntry = Entry(win)
self.ipEntry.pack() self.ipEntry.pack()
self.ipEntry.delete(0, END) self.ipEntry.delete(0, END)
self.ipEntry.insert(0, "192.168.0.XX") self.ipEntry.insert(0, "192.168.0.XX")
Button(root, text='Install', command=self.install).pack(pady=20) self.installButton = Button(win, text='Install', command=self.install)
self.installButton.pack(pady=20)
self.pb = Progressbar(win, orient='horizontal', mode='determinate', maximum=4)
self.pb["value"] = 0
self.pb.pack()
def install(self): def install(self):
if self.file is None:
showerror("Error", "Select a firmware first")
return
self.pb["value"] = 0
ip = self.ipEntry.get() ip = self.ipEntry.get()
print("Installing", self.file, "at ip", ip) print("Installing", self.file, "at ip", ip)
installTh = Updater(self, ip, self.file)
if self.file != None: installTh.start()
subprocess.call(["python", "espota.py", "-i", ip, "-f", self.file])
else:
showerror("Error", "Select a firmware first")
def askopenfile(self): def askopenfile(self):
self.file = filedialog.askopenfilename( self.file = filedialog.askopenfilename(
@ -42,6 +82,5 @@ class Updater(Frame):
root = Tk() root = Tk()
root.title("Firmware Updater") root.title("Firmware Updater")
updater = Updater(root) updater = UpdaterGui(root)
updater.mainloop() updater.mainloop()
updater.destroy()