2021-08-26 04:18:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-07 20:38:13 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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 (
|
2021-10-07 20:38:13 +00:00
|
|
|
archivePath string
|
|
|
|
firmwarePath string
|
|
|
|
initPktPath string
|
2021-08-26 04:18:24 +00:00
|
|
|
)
|
|
|
|
|
2021-10-07 20:38:13 +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()
|
2021-10-07 20:38:13 +00:00
|
|
|
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
|
2021-10-07 20:38:13 +00:00
|
|
|
archiveBtn = widget.NewButton("Select archive (.zip)", archiveDialog.Show)
|
2021-08-26 04:18:24 +00:00
|
|
|
|
2021-10-07 20:38:13 +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()
|
2021-10-07 20:38:13 +00:00
|
|
|
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
|
2021-10-07 20:38:13 +00:00
|
|
|
firmwareBtn = widget.NewButton("Select firmware (.bin)", firmwareDialog.Show)
|
2021-08-26 04:18:24 +00:00
|
|
|
|
2021-10-07 20:38:13 +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()
|
2021-10-07 20:38:13 +00:00
|
|
|
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
|
2021-10-07 20:38:13 +00:00
|
|
|
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
|
2021-10-07 20:38:13 +00:00
|
|
|
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":
|
2022-04-23 00:12:30 +00:00
|
|
|
fwUpgType = api.UpgradeTypeArchive
|
2021-08-26 04:18:24 +00:00
|
|
|
files = append(files, archivePath)
|
|
|
|
case "Files":
|
2022-04-23 00:12:30 +00:00
|
|
|
fwUpgType = api.UpgradeTypeFiles
|
2021-10-07 20:38:13 +00:00
|
|
|
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-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()
|
2021-08-27 16:01:46 +00:00
|
|
|
// If transfer finished, break
|
2022-04-23 00:12:30 +00:00
|
|
|
if int64(event.Sent) == event.Total {
|
2021-08-27 16:01:46 +00:00
|
|
|
break
|
|
|
|
}
|
2021-08-26 04:18:24 +00:00
|
|
|
}
|
2021-11-07 02:06:17 +00:00
|
|
|
|
|
|
|
// Hide progress dialog after completion
|
|
|
|
progressDlg.Hide()
|
|
|
|
|
|
|
|
// Reset screen to default
|
|
|
|
upgradeTypeSelect.SetSelectedIndex(0)
|
|
|
|
firmwareBtn.SetText("Select firmware (.bin)")
|
|
|
|
initPktBtn.SetText("Select init packet (.dat)")
|
|
|
|
archiveBtn.SetText("Select archive (.zip)")
|
|
|
|
firmwarePath = ""
|
|
|
|
initPktPath = ""
|
|
|
|
archivePath = ""
|
|
|
|
|
|
|
|
dialog.NewInformation(
|
|
|
|
"Upgrade Complete",
|
|
|
|
"The firmware was transferred successfully.\nRemember to validate the firmware in InfiniTime settings.",
|
|
|
|
parent,
|
|
|
|
).Show()
|
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(),
|
|
|
|
)
|
|
|
|
}
|