Refactor code structure and remove redundant sections for improved readability and maintainability
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
This commit is contained in:
158
vehicle.go
158
vehicle.go
@ -481,81 +481,6 @@ func (v *Vehicle) GetLocation(force bool) (chan string, error) {
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// executeServiceRequest
|
||||
func (v *Vehicle) executeServiceRequest(params map[string]string, reqUrl, pollingUrl string, ch chan string, attempt int) error {
|
||||
var maxAttempts = 15
|
||||
|
||||
if attempt >= maxAttempts {
|
||||
v.client.logger.Error("maximum attempts reached for service request", "request", reqUrl, "attempts", attempt)
|
||||
ch <- "error"
|
||||
return errors.New("maximum attempts reached for service request")
|
||||
}
|
||||
|
||||
if v.Vin != v.client.currentVin {
|
||||
v.selectVehicle()
|
||||
}
|
||||
|
||||
var resp *Response
|
||||
var err error
|
||||
if attempt == 1 {
|
||||
resp, err = v.client.execute(POST, reqUrl, params, true)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while executing service request", "request", reqUrl, "error", err.Error())
|
||||
ch <- "error"
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
resp, err = v.client.execute(GET, pollingUrl, params, false)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while executing service request status polling", "request", reqUrl, "error", err.Error())
|
||||
ch <- "error"
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// dataName field has the list of the states [ remoteServiceStatus | errorResponse ]
|
||||
if resp.DataName == "remoteServiceStatus" {
|
||||
if sr, ok := v.parseServiceRequest([]byte(resp.Data)); ok {
|
||||
ch <- sr.RemoteServiceState
|
||||
switch sr.RemoteServiceState {
|
||||
|
||||
case "finished":
|
||||
// Finished RemoteServiceState Service Request does not include Service Request ID
|
||||
v.client.logger.Debug("Remote service request completed successfully")
|
||||
|
||||
case "started":
|
||||
time.Sleep(5 * time.Second)
|
||||
v.client.logger.Debug("Subaru API reports remote service request (started) is in progress", "id", sr.ServiceRequestID)
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
|
||||
case "stopping":
|
||||
time.Sleep(5 * time.Second)
|
||||
v.client.logger.Debug("Subaru API reports remote service request (stopping) is in progress", "id", sr.ServiceRequestID)
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
|
||||
default:
|
||||
v.client.logger.Debug("Subaru API reports remote service request (default)")
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
v.client.logger.Error("error while parsing service request json", "request", reqUrl, "response", resp.Data)
|
||||
return errors.New("error while parsing service request json")
|
||||
}
|
||||
return errors.New("response is not a service request")
|
||||
}
|
||||
|
||||
// parseServiceRequest .
|
||||
func (v *Vehicle) parseServiceRequest(b []byte) (ServiceRequest, bool) {
|
||||
var sr ServiceRequest
|
||||
err := json.Unmarshal(b, &sr)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while parsing service request json", "error", err.Error())
|
||||
return sr, false
|
||||
}
|
||||
return sr, true
|
||||
}
|
||||
|
||||
// GetClimatePresets connects to the MySubaru API to download available climate presets.
|
||||
// It first attempts to establish a connection with the MySubaru API.
|
||||
// If successful and climate presets are found for the user's vehicle,
|
||||
@ -710,11 +635,15 @@ func (v *Vehicle) GetVehicleStatus() error {
|
||||
v.selectVehicle()
|
||||
}
|
||||
reqUrl := MOBILE_API_VERSION + urlToGen(apiURLs["API_VEHICLE_STATUS"], v.getAPIGen())
|
||||
resp, _ := v.client.execute(GET, reqUrl, map[string]string{}, false)
|
||||
resp, err := v.client.execute(GET, reqUrl, map[string]string{}, false)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while executing GetVehicleStatus request", "request", "GetVehicleStatus", "error", err.Error())
|
||||
return err
|
||||
}
|
||||
// v.client.logger.Info("http request output", "request", "GetVehicleStatus", "body", resp)
|
||||
|
||||
var vs VehicleStatus
|
||||
err := json.Unmarshal(resp.Data, &vs)
|
||||
err = json.Unmarshal(resp.Data, &vs)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while parsing json", "request", "GetVehicleStatus", "error", err.Error())
|
||||
}
|
||||
@ -855,6 +784,81 @@ func (v *Vehicle) GetFeaturesList() {
|
||||
}
|
||||
}
|
||||
|
||||
// executeServiceRequest
|
||||
func (v *Vehicle) executeServiceRequest(params map[string]string, reqUrl, pollingUrl string, ch chan string, attempt int) error {
|
||||
var maxAttempts = 15
|
||||
|
||||
if attempt >= maxAttempts {
|
||||
v.client.logger.Error("maximum attempts reached for service request", "request", reqUrl, "attempts", attempt)
|
||||
ch <- "error"
|
||||
return errors.New("maximum attempts reached for service request")
|
||||
}
|
||||
|
||||
if v.Vin != v.client.currentVin {
|
||||
v.selectVehicle()
|
||||
}
|
||||
|
||||
var resp *Response
|
||||
var err error
|
||||
if attempt == 1 {
|
||||
resp, err = v.client.execute(POST, reqUrl, params, true)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while executing service request", "request", reqUrl, "error", err.Error())
|
||||
ch <- "error"
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
resp, err = v.client.execute(GET, pollingUrl, params, false)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while executing service request status polling", "request", reqUrl, "error", err.Error())
|
||||
ch <- "error"
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// dataName field has the list of the states [ remoteServiceStatus | errorResponse ]
|
||||
if resp.DataName == "remoteServiceStatus" {
|
||||
if sr, ok := v.parseServiceRequest([]byte(resp.Data)); ok {
|
||||
ch <- sr.RemoteServiceState
|
||||
switch sr.RemoteServiceState {
|
||||
|
||||
case "finished":
|
||||
// Finished RemoteServiceState Service Request does not include Service Request ID
|
||||
v.client.logger.Debug("Remote service request completed successfully")
|
||||
|
||||
case "started":
|
||||
time.Sleep(5 * time.Second)
|
||||
v.client.logger.Debug("Subaru API reports remote service request (started) is in progress", "id", sr.ServiceRequestID)
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
|
||||
case "stopping":
|
||||
time.Sleep(5 * time.Second)
|
||||
v.client.logger.Debug("Subaru API reports remote service request (stopping) is in progress", "id", sr.ServiceRequestID)
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
|
||||
default:
|
||||
v.client.logger.Debug("Subaru API reports remote service request (default)")
|
||||
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
v.client.logger.Error("error while parsing service request json", "request", reqUrl, "response", resp.Data)
|
||||
return errors.New("error while parsing service request json")
|
||||
}
|
||||
return errors.New("response is not a service request")
|
||||
}
|
||||
|
||||
// parseServiceRequest .
|
||||
func (v *Vehicle) parseServiceRequest(b []byte) (ServiceRequest, bool) {
|
||||
var sr ServiceRequest
|
||||
err := json.Unmarshal(b, &sr)
|
||||
if err != nil {
|
||||
v.client.logger.Error("error while parsing service request json", "error", err.Error())
|
||||
return sr, false
|
||||
}
|
||||
return sr, true
|
||||
}
|
||||
|
||||
// selectVehicle .
|
||||
func (v *Vehicle) selectVehicle() {
|
||||
if v.client.currentVin != v.Vin {
|
||||
|
Reference in New Issue
Block a user