-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Receive firmware update command and send progress messages
- Loading branch information
Showing
2 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package senso | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/dividat/driver/src/dividat-driver/firmware" | ||
) | ||
|
||
type OnUpdate func(msg FirmwareUpdateMessage) | ||
|
||
// Disconnect from current connection | ||
func (handle *Handle) ProcessFirmwareUpdateRequest(command UpdateFirmware, onUpdate OnUpdate) { | ||
handle.log.Info("Processing firmware update request.") | ||
if handle.cancelCurrentConnection != nil { | ||
handle.cancelCurrentConnection() | ||
} | ||
image, err := decodeImage(command.Image) | ||
if err != nil { | ||
msg := fmt.Sprintf("Error decoding base64 string: %v", err) | ||
onUpdate(FirmwareUpdateMessage{FirmwareUpdateFailure: &msg}) | ||
handle.log.Error(msg) | ||
} | ||
|
||
onProgress := func(progressMsg string){ | ||
onUpdate(FirmwareUpdateMessage{FirmwareUpdateProgress: &progressMsg}) | ||
} | ||
|
||
err = firmware.Update(context.Background(), image, nil, &command.Address, onProgress) | ||
if err != nil { | ||
failureMsg := fmt.Sprintf("Failed to update firmware: %v", err) | ||
onUpdate(FirmwareUpdateMessage{FirmwareUpdateFailure: &failureMsg}) | ||
handle.log.Error(failureMsg) | ||
} | ||
successMsg := "Firmware successfully transmitted." | ||
onUpdate(FirmwareUpdateMessage{FirmwareUpdateSuccess: &successMsg}) | ||
} | ||
|
||
func decodeImage(base64Str string) (io.Reader, error) { | ||
data, err := base64.StdEncoding.DecodeString(base64Str) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewReader(data), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters