Skip to content

Commit

Permalink
Receive firmware update command and send progress messages
Browse files Browse the repository at this point in the history
  • Loading branch information
krksgbr committed Mar 28, 2024
1 parent ec66675 commit 12cfb87
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/dividat-driver/senso/update_firmware.go
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
}
66 changes: 64 additions & 2 deletions src/dividat-driver/senso/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Command struct {
*Disconnect

*Discover
*UpdateFirmware
}

func prettyPrintCommand(command Command) string {
Expand Down Expand Up @@ -55,6 +56,11 @@ type Discover struct {
Duration int `json:"duration"`
}

type UpdateFirmware struct {
Address string `json:"address"`
Image string `json:"image"`
}

// UnmarshalJSON implements encoding/json Unmarshaler interface
func (command *Command) UnmarshalJSON(data []byte) error {

Expand Down Expand Up @@ -85,6 +91,11 @@ func (command *Command) UnmarshalJSON(data []byte) error {
return err
}

} else if temp.Type == "UpdateFirmware" {
err := json.Unmarshal(data, &command.UpdateFirmware)
if err != nil {
return err
}
} else {
return errors.New("can not decode unknown command")
}
Expand All @@ -95,15 +106,32 @@ func (command *Command) UnmarshalJSON(data []byte) error {
// Message that can be sent to Play
type Message struct {
*Status

Discovered *zeroconf.ServiceEntry
Discovered *zeroconf.ServiceEntry
FirmwareUpdateMessage *FirmwareUpdateMessage
}

// Status is a message containing status information
type Status struct {
Address *string
}

type FirmwareUpdateMessage struct {
FirmwareUpdateProgress *string
FirmwareUpdateSuccess *string
FirmwareUpdateFailure *string
}

func (message FirmwareUpdateMessage) ToString() string {
if message.FirmwareUpdateSuccess != nil {
return *message.FirmwareUpdateSuccess
} else if message.FirmwareUpdateProgress != nil {
return *message.FirmwareUpdateProgress
} else if message.FirmwareUpdateFailure != nil {
return *message.FirmwareUpdateFailure
}
return "Bitch"
}

// MarshalJSON ipmlements JSON encoder for messages
func (message *Message) MarshalJSON() ([]byte, error) {
if message.Status != nil {
Expand All @@ -126,6 +154,34 @@ func (message *Message) MarshalJSON() ([]byte, error) {
IP: append(message.Discovered.AddrIPv4, message.Discovered.AddrIPv6...),
})

} else if message.FirmwareUpdateMessage != nil {
fwUpdate := struct {
Type string `json:"type"`
Message string `json:"message"`
}{}

firmwareUpdateMessage := *message.FirmwareUpdateMessage

if firmwareUpdateMessage.FirmwareUpdateProgress != nil {

fwUpdate.Type = "FirmwareUpdateProgress"
fwUpdate.Message = *firmwareUpdateMessage.FirmwareUpdateProgress

} else if firmwareUpdateMessage.FirmwareUpdateFailure != nil {

fwUpdate.Type = "FirmwareUpdateFailure"
fwUpdate.Message = *firmwareUpdateMessage.FirmwareUpdateFailure

} else if firmwareUpdateMessage.FirmwareUpdateSuccess != nil {

fwUpdate.Type = "FirmwareUpdateSuccess"
fwUpdate.Message = *firmwareUpdateMessage.FirmwareUpdateSuccess

} else {
return nil, errors.New("could not marshal firmware update message")
}

return json.Marshal(fwUpdate)
}

return nil, errors.New("could not marshal message")
Expand Down Expand Up @@ -293,6 +349,12 @@ func (handle *Handle) dispatchCommand(ctx context.Context, log *logrus.Entry, co

return nil

} else if command.UpdateFirmware != nil {
handle.ProcessFirmwareUpdateRequest(*command.UpdateFirmware, func(msg FirmwareUpdateMessage) {
sendMessage(Message{
FirmwareUpdateMessage: &msg,
})
})
}
return nil
}
Expand Down

0 comments on commit 12cfb87

Please sign in to comment.