Domotique/Updater.py

87 lines
2.5 KiB
Python
Raw Permalink Normal View History

2016-09-21 18:44:41 +02:00
#!/usr/bin/env python
2016-09-21 23:52:52 +02:00
# Gui to get parameters for espota.py
# espota.py is available https://raw.githubusercontent.com/esp8266/Arduino/master/tools/espota.py
# and should be in the same directory
2016-09-21 18:44:41 +02:00
from tkinter import *
from tkinter import filedialog
2016-09-21 23:31:14 +02:00
from tkinter.messagebox import *
from tkinter.ttk import *
from threading import Thread
import urllib.request
import time
2016-09-21 23:31:14 +02:00
2016-09-21 18:44:41 +02:00
import subprocess
class Updater(Thread):
def __init__(self, gui, ip, file):
Thread.__init__(self)
self.gui = gui
self.ip = ip
self.file = file
2016-09-21 18:44:41 +02:00
def run(self):
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()
self.gui.pb["value"] = 2
2016-12-14 22:57:01 +01:00
print("Uploading new firmware")
self.gui.pb["value"] = 3
subprocess.call(["python", "espota.py", "-i",
self.ip, "-f", self.file])
except Exception as e:
showerror("Error", e)
2016-09-22 13:20:19 +02:00
else:
showinfo("Done", "Update installed")
finally:
self.gui.pb["value"] = 4
self.gui.installButton['state'] = 'normal'
class UpdaterGui(Frame):
file = "firmware.bin"
sleep = 0
installButton = None
2016-09-21 23:31:14 +02:00
2016-09-21 23:48:18 +02:00
def __init__(self, win, **kwargs):
Frame.__init__(self, win, **kwargs)
button_opt = {'fill': constants.BOTH, 'padx': 5, 'pady': 5}
Button(win, text='Select firmware',
2016-09-21 23:48:18 +02:00
command=self.askopenfile).pack(**button_opt)
2016-09-21 18:44:41 +02:00
self.ipEntry = Entry(win)
2016-09-21 23:48:18 +02:00
self.ipEntry.pack()
self.ipEntry.delete(0, END)
self.ipEntry.insert(0, "192.168.0.XX")
2016-09-21 18:44:41 +02:00
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()
2016-09-21 23:31:14 +02:00
2016-09-21 23:48:18 +02:00
def install(self):
if self.file is None:
showerror("Error", "Select a firmware first")
return
self.pb["value"] = 0
2016-09-21 23:48:18 +02:00
ip = self.ipEntry.get()
print("Installing", self.file, "at ip", ip)
installTh = Updater(self, ip, self.file)
installTh.start()
2016-09-21 18:44:41 +02:00
2016-09-21 23:48:18 +02:00
def askopenfile(self):
self.file = filedialog.askopenfilename(
title='Select Firmware', filetypes=[('binaries', '*.bin')])
2016-09-21 18:44:41 +02:00
2016-09-21 23:48:18 +02:00
root = Tk()
root.title("Firmware Updater")
2016-09-21 18:44:41 +02:00
updater = UpdaterGui(root)
2016-09-21 23:48:18 +02:00
updater.mainloop()