Skip to content

Commit

Permalink
fix: Verbesserung des Reconnect Verhaltens (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: kad49007 <kad49007@yahoo.de>
  • Loading branch information
kad49007 authored May 15, 2024
1 parent 9f65c80 commit 9155d01
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions wattpilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func (w *Wattpilot) onSendResponse(secured bool, message map[string]interface{})
data, _ := json.Marshal(message)
err := wsutil.WriteClientMessage(*w._currentConnection, ws.OpText, data)
if err != nil {
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Sending data to wattpilot: ", message["data"], " Error: ", err)
return err
}
return nil
Expand Down Expand Up @@ -305,7 +306,8 @@ func (w *Wattpilot) onEventDeltaStatus(message map[string]interface{}) {

func (w *Wattpilot) updateStatus(message map[string]interface{}) {

statusUpdates := message["status"].(map[string]interface{})
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Enter Data-status updates ")
statusUpdates := message["status"].(map[string]interface{})
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Data-status gets updates #", len(statusUpdates))

w._readMutex.Lock()
Expand Down Expand Up @@ -337,7 +339,9 @@ func (w *Wattpilot) Disconnect() {
func (w *Wattpilot) disconnectImpl() {
w._log.WithFields(log.Fields{"wattpilot": w._host}).Info("Disconnecting...")

if !w._isInitialized {
if !w._isInitialized && w._currentConnection == nil {
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Error on closing connection cause of NIL pointer: ")
w._isConnected = false
return
}

Expand All @@ -362,12 +366,14 @@ func (w *Wattpilot) Connect() error {
}

w._log.WithFields(log.Fields{"wattpilot": w._host}).Info("Connecting")

w._readMutex.Lock()

var err error
dialContext, cancel := context.WithTimeout(w._readContext, time.Second*CONTEXT_TIMEOUT)
defer cancel()
conn, reader, _, err := ws.DefaultDialer.Dial(dialContext, fmt.Sprintf("ws://%s/ws", w._host))
if err != nil {
w._readMutex.Unlock()
return err
}
w._currentConnection = &conn
Expand All @@ -379,13 +385,14 @@ func (w *Wattpilot) Connect() error {
w._isConnected = <-w.connected
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Connection is ", w._isConnected)
if !w._isConnected {
w._readMutex.Unlock()
return errors.New("could not connect")
}

w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Connected - waiting for initializiation...")

w._readMutex.Unlock()
<-w.initialized

w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Connected - and initializiated")

return nil
Expand All @@ -397,7 +404,7 @@ func (w *Wattpilot) reconnect() {
w._log.WithFields(log.Fields{"wattpilot": w._host}).Info("Reconnect - Is still connected")
return
}

w._log.WithFields(log.Fields{"wattpilot": w._host}).Debug("Reconnecting..")
time.Sleep(time.Second * time.Duration(RECONNECT_TIMEOUT))
if err := w.Connect(); err != nil {
Expand All @@ -420,7 +427,9 @@ func (w *Wattpilot) processLoop(ctx context.Context) {
delay.Reset(delayDuration)
if !w._isInitialized {
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("No Hello there")
continue
w.disconnectImpl()
w.reconnect()
break
}
w._log.WithFields(log.Fields{"wattpilot": w._host}).Trace("Hello there")
go func() {
Expand Down Expand Up @@ -459,6 +468,8 @@ func (w *Wattpilot) receiveHandler(ctx context.Context) {
if err != nil {
// w._readCancel()
w._log.WithFields(log.Fields{"wattpilot": w._host}).Info("Stopping receive handler...")
w.disconnectImpl()
w.reconnect()
return
}
data := make(map[string]interface{})
Expand Down

0 comments on commit 9155d01

Please sign in to comment.