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
Some checks failed
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Failing after 24s
This commit is contained in:
35
mysubaru.go
35
mysubaru.go
@ -377,6 +377,28 @@ type ServiceRequest struct {
|
||||
UpdateTime UnixTime `json:"updateTime,omitempty"` // timestamp // is empty if the request is started
|
||||
}
|
||||
|
||||
// parse parses the JSON response from the MySubaru API into a ServiceRequest struct.
|
||||
func (sr *ServiceRequest) parse(b []byte, logger *slog.Logger) error {
|
||||
err := json.Unmarshal(b, &sr)
|
||||
if err != nil {
|
||||
logger.Error("error while parsing json", "request", "GetVehicleCondition", "error", err.Error())
|
||||
}
|
||||
if !sr.Success && sr.ErrorCode != "" {
|
||||
logger.Error("error in response", "request", "GetVehicleCondition", "errorCode", sr.ErrorCode, "remoteServiceType", sr.RemoteServiceType)
|
||||
switch sr.ErrorCode {
|
||||
case API_ERRORS["API_ERROR_SERVICE_ALREADY_STARTED"]:
|
||||
return errors.New("error in response: Service already started")
|
||||
case API_ERRORS["API_ERROR_VEHICLE_NOT_IN_ACCOUNT"]:
|
||||
return errors.New("error in response: Vehicle not in account")
|
||||
case API_ERRORS["API_ERROR_SOA_403"]:
|
||||
return errors.New("error in response: Unable to parse response body, SOA 403 error")
|
||||
default:
|
||||
return errors.New("error in response: " + sr.ErrorCode)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// climateSettings: [ climateSettings ]
|
||||
// climateZoneFrontTempCelsius: [for _ in range(15, 30 + 1)]
|
||||
// climateZoneFrontTemp: [for _ in range(60, 85 + 1)]
|
||||
@ -395,12 +417,12 @@ type VehicleHealth struct {
|
||||
LastUpdatedDate int64 `json:"lastUpdatedDate"`
|
||||
}
|
||||
type VehicleHealthItem struct {
|
||||
B2cCode string `json:"b2cCode"`
|
||||
FeatureCode string `json:"featureCode"`
|
||||
IsTrouble bool `json:"isTrouble"`
|
||||
OnDaiID int `json:"onDaiId"` // Has a number, probably id, but I couldn't find it purpose
|
||||
OnDates []int64 `json:"onDates,omitempty"` // List of the timestamps
|
||||
WarningCode int `json:"warningCode"`
|
||||
WarningCode int `json:"warningCode"` // internal code used by MySubaru, not documented
|
||||
B2cCode string `json:"b2cCode"` // oilTemp | airbag | oilLevel | etc.
|
||||
FeatureCode string `json:"featureCode"` // SRS_MIL | CEL_MIL | ATF_MIL | etc.
|
||||
IsTrouble bool `json:"isTrouble"` // false | true
|
||||
OnDaiID int `json:"onDaiId"` // Has a number, probably internal record id
|
||||
OnDates []UnixTime `json:"onDates,omitempty"` // List of the timestamps
|
||||
}
|
||||
|
||||
// ErrorResponse .
|
||||
@ -454,6 +476,7 @@ type CustomTime2 struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface
|
||||
func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) {
|
||||
const layout = "2006-01-02T15:04:05-0700"
|
||||
ct.Time, err = time.Parse(layout, string(b)) // Parse the string using the custom layout
|
||||
|
Reference in New Issue
Block a user