Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions internal/ble/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com
// Wrap ctx with connectionCtx
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Create a single goroutine to handle both context cancellations
go func() {
select {
case <-connectionCtx.Done():
Expand All @@ -383,30 +385,35 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com
if i > 0 {
log.Warn(lastErr)
log.Info(fmt.Sprintf("retrying in %d seconds", sleep/time.Second))

select {
case <-time.After(sleep):
case <-ctx.Done():
if connectionCtx.Err() != nil {
return command, ctx.Err(), ctx
}
return nil, ctx.Err(), ctx
}
sleep *= 2
}

retry, err := command.Send(ctx, car)
if err == nil {
//Successful
log.Info("successfully executed", "command", command.Command, "body", command.Body)
return nil, nil, ctx
} else if !retry {
}

if !retry {
return nil, nil, ctx
} else {
//closed pipe
if strings.Contains(err.Error(), "closed pipe") {
//connection lost, returning the command so it can be executed again
return command, err, ctx
}
lastErr = err
}

if strings.Contains(err.Error(), "closed pipe") {
return command, err, ctx
}

lastErr = err
}

log.Error("canceled", "command", command.Command, "body", command.Body, "err", lastErr)
return nil, lastErr, ctx
}
2 changes: 1 addition & 1 deletion internal/tesla/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (command *Command) Send(ctx context.Context, car *vehicle.Vehicle) (shouldR
log.Debugf("get: %s", endpoint)
category, err := GetCategory(endpoint)
if err != nil {
return false, fmt.Errorf("unrecognized state category charge")
return false, err
}
data, err := car.GetState(ctx, category)
if err != nil {
Expand Down
Loading