Refactor session validation and enhance error handling in vehicle commands
Some checks failed
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Failing after 24s

This commit is contained in:
2025-07-08 15:34:07 -04:00
parent aec4b8435b
commit 30bd0bde44
3 changed files with 116 additions and 92 deletions

View File

@ -214,11 +214,6 @@ func (v *Vehicle) String() string {
// Lock
// Sends a command to lock doors.
func (v *Vehicle) Lock() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -239,11 +234,6 @@ func (v *Vehicle) Lock() (chan string, error) {
// Unlock
// Send command to unlock doors.
func (v *Vehicle) Unlock() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -264,11 +254,6 @@ func (v *Vehicle) Unlock() (chan string, error) {
// EngineStart
// Sends a command to start engine and set climate control.
func (v *Vehicle) EngineStart() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// TODO: Get Quick Climate Preset from the Currect Car
params := map[string]string{
"delay": "0",
@ -302,11 +287,6 @@ func (v *Vehicle) EngineStart() (chan string, error) {
// EngineStop
// Sends a command to stop engine.
func (v *Vehicle) EngineStop() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -326,11 +306,6 @@ func (v *Vehicle) EngineStop() (chan string, error) {
// LightsStart
// Sends a command to flash lights.
func (v *Vehicle) LightsStart() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -353,11 +328,6 @@ func (v *Vehicle) LightsStart() (chan string, error) {
// LightsStop
// Sends a command to stop flash lights.
func (v *Vehicle) LightsStop() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -380,11 +350,6 @@ func (v *Vehicle) LightsStop() (chan string, error) {
// HornStart
// Send command to sound horn.
func (v *Vehicle) HornStart() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -407,11 +372,6 @@ func (v *Vehicle) HornStart() (chan string, error) {
// HornStop
// Send command to sound horn.
func (v *Vehicle) HornStop() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
params := map[string]string{
"delay": "0",
"vin": v.Vin,
@ -433,10 +393,6 @@ func (v *Vehicle) HornStop() (chan string, error) {
// ChargeStart
func (v *Vehicle) ChargeOn() (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
if v.isEV() {
params := map[string]string{
"delay": "0",
@ -456,11 +412,6 @@ func (v *Vehicle) ChargeOn() (chan string, error) {
// GetLocation
func (v *Vehicle) GetLocation(force bool) (chan string, error) {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return nil, errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
var reqUrl, pollingUrl string
var params map[string]string
if force { // Sends a locate command to the vehicle to get real time position
@ -498,6 +449,13 @@ func (v *Vehicle) GetClimatePresets() error {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -557,6 +515,13 @@ func (v *Vehicle) GetClimateQuickPresets() error {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -593,6 +558,13 @@ func (v *Vehicle) GetClimateUserPresets() error {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -638,6 +610,13 @@ func (v *Vehicle) GetVehicleStatus() error {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -699,6 +678,13 @@ func (v *Vehicle) GetVehicleCondition() error {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -742,12 +728,20 @@ func (v *Vehicle) GetVehicleCondition() error {
return nil
}
// GetVehicleHealth .
// GetVehicleHealth
// Retrieves the vehicle health status from MySubaru API.
func (v *Vehicle) GetVehicleHealth() error {
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != (v.client).currentVin {
v.selectVehicle()
}
@ -795,13 +789,24 @@ func (v *Vehicle) GetFeaturesList() {
// Executes a service request to the Subaru API and handles the response.
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")
}
// Check if the vehicle has a valid subscription for remote services
if !v.getRemoteOptionsStatus() {
v.client.logger.Error(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
return errors.New(APP_ERRORS["SUBSCRIBTION_REQUIRED"])
}
// Validate session before executing the request
if v.client.validateSession() {
v.client.logger.Error(APP_ERRORS["SESSION_EXPIRED"])
return errors.New(APP_ERRORS["SESSION_EXPIRED"])
}
if v.Vin != v.client.currentVin {
v.selectVehicle()
}
@ -836,16 +841,16 @@ func (v *Vehicle) executeServiceRequest(params map[string]string, reqUrl, pollin
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.client.logger.Debug("MySubaru 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.client.logger.Debug("MySubaru 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.client.logger.Debug("MySubaru API reports remote service request (default)")
v.executeServiceRequest(map[string]string{"serviceRequestId": sr.ServiceRequestID}, reqUrl, pollingUrl, ch, attempt+1)
}
return nil
@ -899,12 +904,6 @@ func (v *Vehicle) getAPIGen() string {
return "unknown"
}
// isPINRequired .
// Return if a vehicle with an active remote service subscription exists.
// func (v *Vehicle) isPINRequired() bool {
// return v.getRemoteOptionsStatus()
// }
// isEV .
// Get whether the specified car is an Electric Vehicle.
func (v *Vehicle) isEV() bool {
@ -1022,27 +1021,3 @@ func (v *Vehicle) parseParts(name string, value any) {
// func (v *Vehicle) getSubscriptionStatus() bool {
// return slices.Contains(v.SubscriptionFeatures, FEATURE_ACTIVE)
// }
// // getVehicleName .
// // Get the nickname of a specified VIN.
// func (v *Vehicle) getVehicleName() string {
// return v.CarName
// }
// func getClimateData() {}
// func saveClimateSettings() {}
// "vhsId": 914631252,
// "odometerValue": 23865,
// "odometerValueKilometers": 38399,
// "tirePressureFrontLeft": "2344",
// "tirePressureFrontRight": "2344",
// "tirePressureRearLeft": "2413",
// "tirePressureRearRight": "2344",
// "tirePressureFrontLeftPsi": "34",
// "tirePressureFrontRightPsi": "34",
// "tirePressureRearLeftPsi": "35",
// "tirePressureRearRightPsi": "34",