Skip to content

Commit

Permalink
Ignore commands while firmware update is in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
krksgbr committed Mar 30, 2024
1 parent 9eec6b3 commit 09ca79e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/dividat-driver/senso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Handle struct {
cancelCurrentConnection context.CancelFunc
connectionChangeMutex *sync.Mutex

firmwareUpdateInProgress bool
firmwareUpdateMutex *sync.Mutex

log *logrus.Entry
}

Expand All @@ -32,6 +35,8 @@ func New(ctx context.Context, log *logrus.Entry) *Handle {
handle.log = log

handle.connectionChangeMutex = &sync.Mutex{}
handle.firmwareUpdateMutex = &sync.Mutex{}
handle.firmwareUpdateInProgress = false

// PubSub broker
handle.broker = pubsub.New(32)
Expand Down
15 changes: 15 additions & 0 deletions src/dividat-driver/senso/update_firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ import (

type OnUpdate func(msg FirmwareUpdateMessage)

func (handle *Handle) isUpdatingFirmware() bool {
handle.firmwareUpdateMutex.Lock()
state := handle.firmwareUpdateInProgress
handle.firmwareUpdateMutex.Unlock()
return state
}

func (handle *Handle) setUpdatingFirmware(state bool) {
handle.firmwareUpdateMutex.Lock()
handle.firmwareUpdateInProgress = state
handle.firmwareUpdateMutex.Unlock()
}

// Disconnect from current connection
func (handle *Handle) ProcessFirmwareUpdateRequest(command UpdateFirmware, onUpdate OnUpdate) {
handle.log.Info("Processing firmware update request.")
handle.setUpdatingFirmware(true)
if handle.cancelCurrentConnection != nil {
handle.cancelCurrentConnection()
}
Expand All @@ -38,6 +52,7 @@ func (handle *Handle) ProcessFirmwareUpdateRequest(command UpdateFirmware, onUpd
successMsg := "Firmware successfully transmitted."
onUpdate(FirmwareUpdateMessage{FirmwareUpdateSuccess: &successMsg})
}
handle.setUpdatingFirmware(false)
}

func decodeImage(base64Str string) (io.Reader, error) {
Expand Down
15 changes: 11 additions & 4 deletions src/dividat-driver/senso/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func (handle *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if handle.isUpdatingFirmware() {
handle.log.Info("Firmware update in progress, ignoring websocket message.")
continue
}

if messageType == websocket.BinaryMessage {
handle.broker.TryPub(msg, "tx")

Expand Down Expand Up @@ -339,11 +344,13 @@ 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,
go func(){
handle.ProcessFirmwareUpdateRequest(*command.UpdateFirmware, func(msg FirmwareUpdateMessage) {
sendMessage(Message{
FirmwareUpdateMessage: &msg,
})
})
})
}()
}
return nil
}
Expand Down

0 comments on commit 09ca79e

Please sign in to comment.