Refactor some functions

This commit is contained in:
2025-07-02 12:06:23 +03:00
parent 21a928bf70
commit 0744d16401
5 changed files with 771 additions and 588 deletions

421
client.go
View File

@ -2,11 +2,11 @@ package mysubaru
import (
"encoding/json"
"errors"
"io"
"log/slog"
"slices"
"sync"
"time"
"git.savin.nyc/alex/mysubaru/config"
"resty.dev/v3"
@ -23,6 +23,7 @@ type Client struct {
listOfVins []string
isAuthenticated bool
isRegistered bool
isAlive bool
logger *slog.Logger
sync.RWMutex
}
@ -52,56 +53,40 @@ func New(config *config.Config) (*Client, error) {
client.httpClient = httpClient
resp := client.auth()
if r, ok := client.parseResponse(resp); ok {
var sd SessionData
err := json.Unmarshal(r.Data, &sd)
if err != nil {
client.logger.Error("error while parsing json", "request", "auth", "error", err.Error())
}
// client.logger.Debug("unmarshaled json data", "request", "auth", "type", "sessionData", "body", sd)
var sd SessionData
err := json.Unmarshal(resp.Data, &sd)
if err != nil {
client.logger.Error("error while parsing json", "request", "auth", "error", err.Error())
}
// client.logger.Debug("unmarshaled json data", "request", "auth", "type", "sessionData", "body", sd)
if sd.DeviceRegistered && sd.RegisteredDevicePermanent {
// client.logger.Debug("client authentication successful")
client.isAuthenticated = true
client.isRegistered = true
} else {
// client.logger.Debug("client authentication successful, but devices is not registered")
client.registerDevice()
}
if sd.DeviceRegistered && sd.RegisteredDevicePermanent {
// client.logger.Debug("client authentication successful")
client.isAuthenticated = true
client.isRegistered = true
}
// TODO: Work on registerDevice()
// } else {
// // client.logger.Debug("client authentication successful, but devices is not registered")
// client.registerDevice()
// }
// client.logger.Debug("parsing cars assigned to the account", "quantity", len(sd.Vehicles))
if len(sd.Vehicles) > 0 {
for _, vehicle := range sd.Vehicles {
// client.logger.Debug("parsing car", "vin", vehicle.Vin)
client.listOfVins = append(client.listOfVins, vehicle.Vin)
}
client.currentVin = client.listOfVins[0]
} else {
client.logger.Error("there no cars assigned to the account")
return nil, err
// client.logger.Debug("parsing cars assigned to the account", "quantity", len(sd.Vehicles))
if len(sd.Vehicles) > 0 {
for _, vehicle := range sd.Vehicles {
// client.logger.Debug("parsing car", "vin", vehicle.Vin)
client.listOfVins = append(client.listOfVins, vehicle.Vin)
}
client.currentVin = client.listOfVins[0]
} else {
// TODO: Work on errors
// error, _ := respParsed.Path("errorCode").Data().(string)
// switch {
// case error == apiErrors["ERROR_INVALID_ACCOUNT"]:
// client.logger.Debug("Invalid account")
// case error == apiErrors["ERROR_INVALID_CREDENTIALS"]:
// client.logger.Debug("Client authentication failed")
// case error == apiErrors["ERROR_PASSWORD_WARNING"]:
// client.logger.Debug("Multiple Password Failures.")
// default:
// client.logger.Debug("Uknown error")
// }
client.logger.Error("request was not successfull", "request", "auth")
// TODO: Work on providing error
return nil, nil
client.logger.Error("there no cars assigned to the account")
return nil, err
}
return client, nil
}
// SelectVehicle .
func (c *Client) SelectVehicle(vin string) VehicleData {
func (c *Client) SelectVehicle(vin string) (*VehicleData, error) {
if vin == "" {
vin = c.currentVin
}
@ -112,93 +97,93 @@ func (c *Client) SelectVehicle(vin string) VehicleData {
"vin": vin,
"_": timestamp()}
reqURL := MOBILE_API_VERSION + apiURLs["API_SELECT_VEHICLE"]
resp := c.execute(reqURL, GET, params, "", false)
// TODO: Add error handling
resp, _ := c.execute(GET, reqURL, params, false)
// c.logger.Debug("http request output", "request", "SelectVehicle", "body", resp)
if r, ok := c.parseResponse(resp); ok {
var vd VehicleData
err := json.Unmarshal(r.Data, &vd)
if err != nil {
c.logger.Error("error while parsing json", "request", "SelectVehicle", "error", err.Error())
}
// c.logger.Debug("http request output", "request", "SelectVehicle", "body", resp)
return vd
} else {
return VehicleData{}
var vd VehicleData
err := json.Unmarshal(resp.Data, &vd)
if err != nil {
c.logger.Error("error while parsing json", "request", "SelectVehicle", "error", err.Error())
return nil, errors.New("error while parsing json while vehicle selection")
}
// c.logger.Debug("http request output", "request", "SelectVehicle", "body", resp)
return &vd, nil
}
// GetVehicles .
func (c *Client) GetVehicles() []*Vehicle {
var vehicles []*Vehicle
for _, vin := range c.listOfVins {
vehicle := c.GetVehicleByVIN(vin)
vehicle, err := c.GetVehicleByVIN(vin)
if err != nil {
c.logger.Error("cannot get vehile data", "request", "SelectVehicle", "error", err.Error())
}
vehicles = append(vehicles, vehicle)
}
return vehicles
}
// GetVehicleByVIN .
func (c *Client) GetVehicleByVIN(vin string) *Vehicle {
func (c *Client) GetVehicleByVIN(vin string) (*Vehicle, error) {
var vehicle *Vehicle
if slices.Contains(c.listOfVins, vin) {
params := map[string]string{
"vin": vin,
"_": timestamp()}
reqURL := MOBILE_API_VERSION + apiURLs["API_SELECT_VEHICLE"]
resp := c.execute(reqURL, GET, params, "", false)
// TODO: Add error handling
resp, _ := c.execute(GET, reqURL, params, false)
// c.logger.Debug("http request output", "request", "GetVehicleByVIN", "body", resp)
if r, ok := c.parseResponse(resp); ok {
var vd VehicleData
err := json.Unmarshal(r.Data, &vd)
if err != nil {
c.logger.Error("error while parsing json", "request", "GetVehicleByVIN", "error", err.Error())
}
// c.logger.Debug("http request output", "request", "GetVehicleByVIN", "body", resp)
vehicle = &Vehicle{
Vin: vin,
CarName: vd.VehicleName,
CarNickname: vd.Nickname,
ModelName: vd.ModelName,
ModelYear: vd.ModelYear,
ModelCode: vd.ModelCode,
ExtDescrip: vd.ExtDescrip,
IntDescrip: vd.IntDescrip,
TransCode: vd.TransCode,
EngineSize: vd.EngineSize,
VehicleKey: vd.VehicleKey,
LicensePlate: vd.LicensePlate,
LicensePlateState: vd.LicensePlateState,
Features: vd.Features,
SubscriptionFeatures: vd.SubscriptionFeatures,
client: c,
}
vehicle.Doors = make(map[string]Door)
vehicle.Windows = make(map[string]Window)
vehicle.Tires = make(map[string]Tire)
vehicle.ClimateProfiles = make(map[string]ClimateProfile)
vehicle.Troubles = make(map[string]Trouble)
if vehicle.isEV() {
vehicle.EV = true
} else {
vehicle.EV = false
}
vehicle.GetVehicleStatus()
vehicle.GetVehicleCondition()
vehicle.GetVehicleHealth()
vehicle.GetClimatePresets()
vehicle.GetClimateUserPresets()
vehicle.GetClimateQuickPresets()
return vehicle
var vd VehicleData
err := json.Unmarshal(resp.Data, &vd)
if err != nil {
c.logger.Error("error while parsing json", "request", "GetVehicleByVIN", "error", err.Error())
}
// c.logger.Debug("http request output", "request", "GetVehicleByVIN", "body", resp)
vehicle = &Vehicle{
Vin: vin,
CarName: vd.VehicleName,
CarNickname: vd.Nickname,
ModelName: vd.ModelName,
ModelYear: vd.ModelYear,
ModelCode: vd.ModelCode,
ExtDescrip: vd.ExtDescrip,
IntDescrip: vd.IntDescrip,
TransCode: vd.TransCode,
EngineSize: vd.EngineSize,
VehicleKey: vd.VehicleKey,
LicensePlate: vd.LicensePlate,
LicensePlateState: vd.LicensePlateState,
Features: vd.Features,
SubscriptionFeatures: vd.SubscriptionFeatures,
client: c,
}
vehicle.Doors = make(map[string]Door)
vehicle.Windows = make(map[string]Window)
vehicle.Tires = make(map[string]Tire)
vehicle.ClimateProfiles = make(map[string]ClimateProfile)
vehicle.Troubles = make(map[string]Trouble)
if vehicle.isEV() {
vehicle.EV = true
} else {
vehicle.EV = false
}
vehicle.GetVehicleStatus()
vehicle.GetVehicleCondition()
vehicle.GetVehicleHealth()
vehicle.GetClimatePresets()
vehicle.GetClimateUserPresets()
vehicle.GetClimateQuickPresets()
return vehicle, nil
}
c.logger.Error("error while parsing json", "request", "GetVehicleByVIN")
return &Vehicle{}
c.logger.Error("vin code is not in the list of the available vin codes", "request", "GetVehicleByVIN")
return nil, errors.New("vin code is not in the list of the available vin codes")
}
// func isPINRequired() {}
@ -211,8 +196,12 @@ func (c *Client) GetVehicleByVIN(vin string) *Vehicle {
// func getClimateData() {}
// func saveClimateSettings() {}
func (c *Client) IsAlive() bool {
return c.isAlive
}
// Exec method executes a Client instance with the API URL
func (c *Client) execute(requestUrl string, method string, params map[string]string, pollingUrl string, j bool, attempts ...int) []byte {
func (c *Client) execute(method string, url string, params map[string]string, j bool) (*Response, error) {
// defer timeTrack("[TIMETRK] Executing HTTP Request")
var resp *resty.Response
@ -221,7 +210,7 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
resp, _ = c.httpClient.
R().
SetQueryParams(params).
Get(requestUrl)
Get(url)
}
// POST Requests
@ -230,70 +219,47 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
resp, _ = c.httpClient.
R().
SetBody(params).
Post(requestUrl)
Post(url)
} else { // POST > Form Data
resp, _ = c.httpClient.
R().
SetFormData(params).
Post(requestUrl)
Post(url)
}
}
resBytes, err := io.ReadAll(resp.Body)
if err != nil {
c.logger.Error("error while getting body", "error", err.Error())
}
c.logger.Debug("parsed http request output", "data", string(resBytes))
if r, ok := c.parseResponse(resBytes); ok {
// c.logger.Debug("parsed http request output", "data", r.Data)
if resp.IsSuccess() {
resBytes, err := io.ReadAll(resp.Body)
if err != nil {
c.logger.Error("error while getting body", "error", err.Error())
}
c.logger.Debug("parsed http request output", "data", string(resBytes))
// dataName field has the list of the states [ remoteServiceStatus | errorResponse ]
if r.DataName == "remoteServiceStatus" {
var sr ServiceRequest
err := json.Unmarshal(r.Data, &sr)
if err != nil {
c.logger.Error("error while parsing json", "request", "remoteServiceStatus", "error", err.Error())
}
if pollingUrl != "" {
switch {
case sr.RemoteServiceState == "finished":
// Finished RemoteServiceState Service Request does not include Service Request ID
c.logger.Debug("Remote service request completed successfully")
case sr.RemoteServiceState == "started":
time.Sleep(5 * time.Second)
c.logger.Debug("Subaru API reports remote service request (started) is in progress", "id", sr.ServiceRequestID)
c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false)
case sr.RemoteServiceState == "stopping":
time.Sleep(5 * time.Second)
c.logger.Debug("Subaru API reports remote service request (stopping) is in progress", "id", sr.ServiceRequestID)
c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false)
default:
time.Sleep(5 * time.Second)
c.logger.Debug("Subaru API reports remote service request (stopping) is in progress")
c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false, 1)
if r, ok := c.parseResponse(resBytes); ok {
c.isAlive = true
return &r, nil
} else {
if r.DataName == "errorResponse" {
var er ErrorResponse
err := json.Unmarshal(r.Data, &er)
if err != nil {
c.logger.Error("error while parsing json", "request", "errorResponse", "error", err.Error())
}
if _, ok := API_ERRORS[er.ErrorLabel]; ok {
c.logger.Error("request got an error", "request", "execute", "method", method, "url", url, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
}
c.logger.Error("request got an unknown error", "request", "execute", "method", method, "url", url, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
return nil, errors.New("request is not successfull, HTTP code: " + resp.Status())
}
c.logger.Error("request is not successfull", "request", "execute", "method", method, "url", url, "error", err.Error())
}
} else {
if r.DataName == "errorResponse" {
var er ErrorResponse
err := json.Unmarshal(r.Data, &er)
if err != nil {
c.logger.Error("error while parsing json", "request", "errorResponse", "error", err.Error())
}
if _, ok := API_ERRORS[er.ErrorLabel]; ok {
c.logger.Error("request got an error", "request", "execute", "method", method, "url", requestUrl, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
}
c.logger.Error("request got an unknown error", "request", "execute", "method", method, "url", requestUrl, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
}
c.logger.Error("request is not successfull", "request", "execute", "method", method, "url", requestUrl, "error", err.Error())
}
return resBytes
c.isAlive = false
return nil, errors.New("request is not successfull, HTTP code: " + resp.Status())
}
// auth .
func (c *Client) auth() []byte {
func (c *Client) auth() *Response {
params := map[string]string{
"env": "cloudprod",
"deviceType": "android",
@ -304,7 +270,8 @@ func (c *Client) auth() []byte {
"selectedVin": "",
"pushToken": ""}
reqURL := MOBILE_API_VERSION + apiURLs["API_LOGIN"]
resp := c.execute(reqURL, POST, params, "", false)
// TODO: Add error handling
resp, _ := c.execute(POST, reqURL, params, false)
// c.logger.Debug("AUTH HTTP OUTPUT", "body", string([]byte(resp)))
return resp
}
@ -315,29 +282,23 @@ func (c *Client) parseResponse(b []byte) (Response, bool) {
err := json.Unmarshal(b, &r)
if err != nil {
c.logger.Error("error while parsing json", "error", err.Error())
return r, false
}
return r, true
}
// validateSession .
// TODO: add session validation process and add it to the proper functions
// func (c *Client) validateSession() bool {
// // {
// // "success": true,
// // "errorCode": null,
// // "dataName": null,
// // "data": null
// // }
// reqURL := MOBILE_API_VERSION + apiURLs["API_VALIDATE_SESSION"]
// resp := c.execute(reqURL, GET, map[string]string{}, "", false)
// c.logger.Debug("http request output", "request", "validateSession", "body", resp)
// // c.logger.Debug("http request output", "request", "validateSession", "body", resp)
// var r Response
// err := json.Unmarshal(resp, &r)
// if err != nil {
// if r, ok := c.parseResponse(resp); ok {
// c.logger.Error("error while parsing json", "request", "validateSession", "error", err.Error())
// }
// if r.Success {
// return true
// } else {
// resp := c.auth()
// return true
// }
// return false
@ -358,50 +319,50 @@ func (c *Client) parseResponse(b []byte) (Response, bool) {
// {"success":true,"dataName":"authorizedDevices","data":[{"telematicsClientDeviceKey":2574212,"deviceType":"android","deviceName":"Alex Google Pixel 4 XL","createdDate":"2019-11-29T20:32:21.000+0000","modifiedDate":"2020-06-08T17:48:22.000+0000"},{"telematicsClientDeviceKey":4847533,"deviceName":"Home Assistant: Added 2021-03-03","createdDate":"2021-03-03T20:53:44.000+0000","modifiedDate":"2021-03-03T20:53:47.000+0000"},{"telematicsClientDeviceKey":7222995,"deviceType":"android","deviceName":"Alex Google Pixel 6 Pro","createdDate":"2021-10-28T15:27:36.000+0000","modifiedDate":"2021-10-28T15:27:58.000+0000"},{"telematicsClientDeviceKey":8207130,"deviceName":"Mac/iOS Chrome","createdDate":"2021-12-21T21:19:40.000+0000","modifiedDate":"2021-12-21T21:19:40.000+0000"}]}
// {"success":true,"dataName":"authorizedDevices","data":[{"telematicsClientDeviceKey":2574212,"deviceType":"android","deviceName":"Alex Google Pixel 4 XL","createdDate":"2019-11-29T20:32:21.000+0000","modifiedDate":"2020-06-08T17:48:22.000+0000"},{"telematicsClientDeviceKey":4847533,"deviceName":"Home Assistant: Added 2021-03-03","createdDate":"2021-03-03T20:53:44.000+0000","modifiedDate":"2021-03-03T20:53:47.000+0000"},{"telematicsClientDeviceKey":7222995,"deviceType":"android","deviceName":"Alex Google Pixel 6 Pro","createdDate":"2021-10-28T15:27:36.000+0000","modifiedDate":"2021-10-28T15:27:58.000+0000"},{"telematicsClientDeviceKey":8210723,"deviceName":"Hassio Golang Integration","createdDate":"2021-12-22T01:38:43.000+0000","modifiedDate":"2021-12-22T01:38:43.000+0000"},{"telematicsClientDeviceKey":8207130,"deviceName":"Mac/iOS Chrome","createdDate":"2021-12-21T21:19:40.000+0000","modifiedDate":"2021-12-21T21:19:40.000+0000"}]}
// registerDevice .
func (c *Client) registerDevice() bool {
// c.httpClient.
// SetBaseURL(WEB_API_SERVER[c.country]).
// R().
// SetFormData(map[string]string{
// "username": c.credentials.username,
// "password": c.credentials.password,
// "deviceId": c.credentials.deviceId,
// }).
// Post(apiURLs["WEB_API_LOGIN"])
params := map[string]string{
"username": c.credentials.Username,
"password": c.credentials.Password,
"deviceId": c.credentials.DeviceID}
reqURL := WEB_API_SERVER[c.country] + apiURLs["WEB_API_LOGIN"]
c.execute(reqURL, POST, params, "", true)
// // registerDevice .
// func (c *Client) registerDevice() bool {
// // c.httpClient.
// // SetBaseURL(WEB_API_SERVER[c.country]).
// // R().
// // SetFormData(map[string]string{
// // "username": c.credentials.username,
// // "password": c.credentials.password,
// // "deviceId": c.credentials.deviceId,
// // }).
// // Post(apiURLs["WEB_API_LOGIN"])
// params := map[string]string{
// "username": c.credentials.Username,
// "password": c.credentials.Password,
// "deviceId": c.credentials.DeviceID}
// reqURL := WEB_API_SERVER[c.country] + apiURLs["WEB_API_LOGIN"]
// resp, _ := c.execute(POST, reqURL, params, true)
// Authorizing device via web API
// c.httpClient.
// SetBaseURL(WEB_API_SERVER[c.country]).
// R().
// SetQueryParams(map[string]string{
// "deviceId": c.credentials.deviceId,
// }).
// Get(apiURLs["WEB_API_AUTHORIZE_DEVICE"])
params = map[string]string{
"deviceId": c.credentials.DeviceID}
reqURL = WEB_API_SERVER[c.country] + apiURLs["WEB_API_AUTHORIZE_DEVICE"]
c.execute(reqURL, GET, params, "", false)
// // Authorizing device via web API
// // c.httpClient.
// // SetBaseURL(WEB_API_SERVER[c.country]).
// // R().
// // SetQueryParams(map[string]string{
// // "deviceId": c.credentials.deviceId,
// // }).
// // Get(apiURLs["WEB_API_AUTHORIZE_DEVICE"])
// params = map[string]string{
// "deviceId": c.credentials.DeviceID}
// reqURL = WEB_API_SERVER[c.country] + apiURLs["WEB_API_AUTHORIZE_DEVICE"]
// c.execute(reqURL, GET, params, "", false)
return c.setDeviceName()
}
// return c.setDeviceName()
// }
// setDeviceName .
func (c *Client) setDeviceName() bool {
params := map[string]string{
"deviceId": c.credentials.DeviceID,
"deviceName": c.credentials.DeviceName}
reqURL := WEB_API_SERVER[c.country] + apiURLs["WEB_API_NAME_DEVICE"]
c.execute(reqURL, GET, params, "", false)
// // setDeviceName .
// func (c *Client) setDeviceName() bool {
// params := map[string]string{
// "deviceId": c.credentials.DeviceID,
// "deviceName": c.credentials.DeviceName}
// reqURL := WEB_API_SERVER[c.country] + apiURLs["WEB_API_NAME_DEVICE"]
// c.execute(reqURL, GET, params, "", false)
return true
}
// return true
// }
// // listDevices .
// func (c *Client) listDevices() {
@ -457,3 +418,45 @@ func (c *Client) setDeviceName() bool {
// // logger.Debugf("LIST DEVICES OUTPUT >> %v\n", string(resp))
// // }
// }
// c.logger.Debug("parsed http request output", "data", r.Data)
// ERROR
// {"httpCode":500,"errorCode":"error","errorMessage":"org.springframework.web.HttpMediaTypeNotSupportedException - Content type 'application/x-www-form-urlencoded' not supported"}
// {"success":true,"errorCode":null,"dataName":"remoteServiceStatus","data":{"serviceRequestId":"4S4BTGND8L3137058_1640203129607_19_@NGTP","success":false,"cancelled":false,"remoteServiceType":"unlock","remoteServiceState":"started","subState":null,"errorCode":null,"result":null,"updateTime":null,"vin":"4S4BTGND8L3137058"}}
// API_REMOTE_SVC_STATUS
// {"success":false,"errorCode":"404-soa-unableToParseResponseBody","dataName":"errorResponse","data":{"errorLabel":"404-soa-unableToParseResponseBody","errorDescription":null}}
// if js_resp["errorCode"] == sc.ERROR_SOA_403:
// raise RemoteServiceFailure("Backend session expired, please try again")
// if js_resp["data"]["remoteServiceState"] == "finished":
// if js_resp["data"]["success"]:
// _LOGGER.info("Remote service request completed successfully: %s", req_id)
// return True, js_resp
// _LOGGER.error(
// "Remote service request completed but failed: %s Error: %s",
// req_id,
// js_resp["data"]["errorCode"],
// )
// raise RemoteServiceFailure(
// "Remote service request completed but failed: %s" % js_resp["data"]["errorCode"]
// )
// if js_resp["data"].get("remoteServiceState") == "started":
// _LOGGER.info(
// "Subaru API reports remote service request is in progress: %s",
// req_id,
// )
// attempts_left -= 1
// await asyncio.sleep(2)
// TODO: Work on errors
// error, _ := respParsed.Path("errorCode").Data().(string)
// switch {
// case error == apiErrors["ERROR_INVALID_ACCOUNT"]:
// client.logger.Debug("Invalid account")
// case error == apiErrors["ERROR_INVALID_CREDENTIALS"]:
// client.logger.Debug("Client authentication failed")
// case error == apiErrors["ERROR_PASSWORD_WARNING"]:
// client.logger.Debug("Multiple Password Failures.")
// default:
// client.logger.Debug("Uknown error")
// }