Skip to content

Commit

Permalink
fix: the handling of the notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
graugans committed Jun 14, 2024
1 parent 086ee1d commit bc34ba6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cmd/ovp8xx/cmd/swupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"path/filepath"
"sync"
"time"

"github.com/graugans/go-ovp8xx/pkg/swupdater"
Expand All @@ -24,10 +25,11 @@ func swupdateCommand(cmd *cobra.Command, args []string) error {
return fmt.Errorf("cannot get port: %w", err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
return fmt.Errorf("cannot get filename: %w", err)
// Check if filename is provided as a positional argument
if len(args) < 1 {
return fmt.Errorf("no filename provided")
}
filename := args[0]

timeout, err := cmd.Flags().GetDuration("timeout")
if err != nil {
Expand All @@ -50,6 +52,9 @@ func swupdateCommand(cmd *cobra.Command, args []string) error {
// It has a buffer size of 10 to allow for asynchronous processing.
notifications := make(chan swupdater.SWUpdaterNotification, 10)

var wg sync.WaitGroup
wg.Add(1)

// Print the messages as they come
go func() {
for n := range notifications {
Expand All @@ -59,7 +64,9 @@ func swupdateCommand(cmd *cobra.Command, args []string) error {
if value, ok := n["text"]; ok && n["type"] == "message" {
fmt.Println(value)
}

}
wg.Done() // Decrease counter when goroutine completes
}()

// Create a new SWUpdater instance with the specified host, port, and notifications.
Expand All @@ -70,19 +77,25 @@ func swupdateCommand(cmd *cobra.Command, args []string) error {
); err != nil {
return fmt.Errorf("software update failed: %w", err)
}

wg.Wait() // Wait for all goroutines to finish
return nil
}

// swupdateCmd represents the swupdate command
var swupdateCmd = &cobra.Command{
Use: "swupdate",
Use: "swupdate [filename]",
Short: "Update the firmware on the device",
RunE: swupdateCommand,
Long: `The swupdate command is used to update the firmware on the device.
It takes a filename as a positional argument, which is the path to the firmware file to be uploaded.
The command establishes a connection to the device, uploads the firmware file, and waits for the update process to complete.`,
RunE: swupdateCommand,
}

func init() {
rootCmd.AddCommand(swupdateCmd)
swupdateCmd.Flags().String("file", "", "A file conatining the firmware image")
swupdateCmd.Flags().Uint16("port", 8080, "Port number for SWUpdate")
swupdateCmd.Flags().Duration("timeout", 5*time.Minute, "The timeout for the upload")
swupdateCmd.Flags().Duration("online", 2*time.Minute, "The time to wait for the device to become available")
Expand Down
1 change: 1 addition & 0 deletions pkg/swupdater/swupdater.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (s *SWUpdater) Update(filename string, connectionTimeout, timeout time.Dura

select {
case err := <-done:
close(s.notifications) // Close the channel to signal the end of notifications
if err != nil {
return fmt.Errorf("update failed: %w", err)
}
Expand Down

0 comments on commit bc34ba6

Please sign in to comment.