diff --git a/src/dividat-driver/senso/main.go b/src/dividat-driver/senso/main.go index 5064f0b..4b32652 100644 --- a/src/dividat-driver/senso/main.go +++ b/src/dividat-driver/senso/main.go @@ -20,6 +20,9 @@ type Handle struct { cancelCurrentConnection context.CancelFunc connectionChangeMutex *sync.Mutex + firmwareUpdateInProgress bool + firmwareUpdateMutex *sync.Mutex + log *logrus.Entry } @@ -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) diff --git a/src/dividat-driver/senso/update_firmware.go b/src/dividat-driver/senso/update_firmware.go index 8f48128..ad4a3b2 100644 --- a/src/dividat-driver/senso/update_firmware.go +++ b/src/dividat-driver/senso/update_firmware.go @@ -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() } @@ -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) { diff --git a/src/dividat-driver/senso/websocket.go b/src/dividat-driver/senso/websocket.go index 6a0ae1e..c89b5f7 100644 --- a/src/dividat-driver/senso/websocket.go +++ b/src/dividat-driver/senso/websocket.go @@ -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") @@ -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 }