itd/cmd/itgui/upgrade.go

166 lines
4.6 KiB
Go
Raw Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
"fmt"
"path/filepath"
2021-08-26 04:18:24 +00:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
2021-10-24 20:27:14 +00:00
"go.arsenm.dev/itd/api"
2021-08-26 04:18:24 +00:00
"go.arsenm.dev/itd/internal/types"
)
2021-10-24 20:27:14 +00:00
func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
2021-08-26 04:18:24 +00:00
var (
archivePath string
firmwarePath string
initPktPath string
2021-08-26 04:18:24 +00:00
)
var archiveBtn *widget.Button
2021-08-26 15:47:17 +00:00
// Create archive selection dialog
2021-08-26 04:18:24 +00:00
archiveDialog := dialog.NewFileOpen(func(uc fyne.URIReadCloser, e error) {
if e != nil || uc == nil {
return
}
uc.Close()
archivePath = uc.URI().Path()
archiveBtn.SetText(fmt.Sprintf("Select archive (.zip) [%s]", filepath.Base(archivePath)))
2021-08-26 04:18:24 +00:00
}, parent)
2021-08-26 15:47:17 +00:00
// Limit dialog to .zip files
2021-08-26 04:18:24 +00:00
archiveDialog.SetFilter(storage.NewExtensionFileFilter([]string{".zip"}))
2021-08-26 15:47:17 +00:00
// Create button to show dialog
archiveBtn = widget.NewButton("Select archive (.zip)", archiveDialog.Show)
2021-08-26 04:18:24 +00:00
var firmwareBtn *widget.Button
2021-08-26 15:47:17 +00:00
// Create firmware selection dialog
2021-08-26 04:18:24 +00:00
firmwareDialog := dialog.NewFileOpen(func(uc fyne.URIReadCloser, e error) {
if e != nil || uc == nil {
return
}
uc.Close()
firmwarePath = uc.URI().Path()
firmwareBtn.SetText(fmt.Sprintf("Select firmware (.bin) [%s]", filepath.Base(firmwarePath)))
2021-08-26 04:18:24 +00:00
}, parent)
2021-08-26 15:47:17 +00:00
// Limit dialog to .bin files
2021-08-26 04:18:24 +00:00
firmwareDialog.SetFilter(storage.NewExtensionFileFilter([]string{".bin"}))
2021-08-26 15:47:17 +00:00
// Create button to show dialog
firmwareBtn = widget.NewButton("Select firmware (.bin)", firmwareDialog.Show)
2021-08-26 04:18:24 +00:00
var initPktBtn *widget.Button
2021-08-26 15:47:17 +00:00
// Create init packet selection dialog
2021-08-26 04:18:24 +00:00
initPktDialog := dialog.NewFileOpen(func(uc fyne.URIReadCloser, e error) {
if e != nil || uc == nil {
return
}
uc.Close()
initPktPath = uc.URI().Path()
initPktBtn.SetText(fmt.Sprintf("Select init packet (.dat) [%s]", filepath.Base(initPktPath)))
2021-08-26 04:18:24 +00:00
}, parent)
2021-08-26 15:47:17 +00:00
// Limit dialog to .dat files
2021-08-26 04:18:24 +00:00
initPktDialog.SetFilter(storage.NewExtensionFileFilter([]string{".dat"}))
2021-08-26 15:47:17 +00:00
// Create button to show dialog
initPktBtn = widget.NewButton("Select init packet (.dat)", initPktDialog.Show)
2021-08-26 04:18:24 +00:00
2021-08-26 15:47:17 +00:00
// Hide init packet and firmware buttons
2021-08-26 04:18:24 +00:00
initPktBtn.Hide()
firmwareBtn.Hide()
2021-08-26 15:47:17 +00:00
// Create dropdown to select upgrade type
2021-08-26 04:18:24 +00:00
upgradeTypeSelect := widget.NewSelect([]string{
"Archive",
"Files",
}, func(s string) {
2021-08-26 15:47:17 +00:00
// Hide all buttons
2021-08-26 04:18:24 +00:00
archiveBtn.Hide()
initPktBtn.Hide()
firmwareBtn.Hide()
2021-08-26 15:47:17 +00:00
// Unhide appropriate button(s)
2021-08-26 04:18:24 +00:00
switch s {
case "Archive":
archiveBtn.Show()
case "Files":
initPktBtn.Show()
firmwareBtn.Show()
}
})
2021-08-26 15:47:17 +00:00
// Select first elemetn
2021-08-26 04:18:24 +00:00
upgradeTypeSelect.SetSelectedIndex(0)
2021-08-26 15:47:17 +00:00
// Create new button to start DFU
2021-08-26 04:18:24 +00:00
startBtn := widget.NewButton("Start", func() {
2021-08-26 15:47:17 +00:00
// If archive path does not exist and both init packet and firmware paths
// also do not exist, return error
if archivePath == "" && (initPktPath == "" && firmwarePath == "") {
2021-08-27 15:47:24 +00:00
guiErr(nil, "Upgrade requires archive or files selected", false, parent)
2021-08-26 04:18:24 +00:00
return
}
2021-08-26 15:47:17 +00:00
// Create new label for byte progress
2021-08-26 04:18:24 +00:00
progressLbl := widget.NewLabelWithStyle("0 / 0 B", fyne.TextAlignCenter, fyne.TextStyle{})
2021-08-26 15:47:17 +00:00
// Create new progress bar
2021-08-26 04:18:24 +00:00
progressBar := widget.NewProgressBar()
2021-08-26 15:47:17 +00:00
// Create modal dialog containing label and progress bar
2021-08-26 04:18:24 +00:00
progressDlg := widget.NewModalPopUp(container.NewVBox(
layout.NewSpacer(),
progressLbl,
progressBar,
layout.NewSpacer(),
), parent.Canvas())
2021-08-26 15:47:17 +00:00
// Resize modal to 300x100
2021-08-26 04:18:24 +00:00
progressDlg.Resize(fyne.NewSize(300, 100))
2021-10-24 20:27:14 +00:00
var fwUpgType api.UpgradeType
2021-08-26 04:18:24 +00:00
var files []string
2021-08-26 15:47:17 +00:00
// Get appropriate upgrade type and file paths
2021-08-26 04:18:24 +00:00
switch upgradeTypeSelect.Selected {
case "Archive":
fwUpgType = types.UpgradeTypeArchive
files = append(files, archivePath)
case "Files":
fwUpgType = types.UpgradeTypeFiles
files = append(files, initPktPath, firmwarePath)
2021-08-26 04:18:24 +00:00
}
2021-10-24 20:27:14 +00:00
progress, err := client.FirmwareUpgrade(fwUpgType, files...)
2021-08-26 04:18:24 +00:00
if err != nil {
2021-10-24 20:27:14 +00:00
guiErr(err, "Error initiating DFU", false, parent)
2021-08-26 04:18:24 +00:00
return
}
2021-08-26 15:47:17 +00:00
// Show progress dialog
2021-08-26 04:18:24 +00:00
progressDlg.Show()
2021-08-26 15:47:17 +00:00
// Hide progress dialog after completion
defer progressDlg.Hide()
2021-08-26 04:18:24 +00:00
2021-10-24 20:27:14 +00:00
for event := range progress {
2021-08-26 15:47:17 +00:00
// Set label text to received / total B
2021-08-26 04:18:24 +00:00
progressLbl.SetText(fmt.Sprintf("%d / %d B", event.Received, event.Total))
2021-08-26 15:47:17 +00:00
// Set progress bar values
2021-08-26 04:18:24 +00:00
progressBar.Max = float64(event.Total)
progressBar.Value = float64(event.Received)
2021-08-26 15:47:17 +00:00
// Refresh progress bar
2021-08-26 04:18:24 +00:00
progressBar.Refresh()
// If transfer finished, break
2021-10-24 20:27:14 +00:00
if event.Sent == event.Total {
break
}
2021-08-26 04:18:24 +00:00
}
})
2021-08-26 15:47:17 +00:00
// Return container containing all elements
2021-08-26 04:18:24 +00:00
return container.NewVBox(
layout.NewSpacer(),
upgradeTypeSelect,
archiveBtn,
firmwareBtn,
initPktBtn,
startBtn,
layout.NewSpacer(),
)
}