diff --git a/client.go b/client.go index bdbe2ee..26c005d 100644 --- a/client.go +++ b/client.go @@ -404,6 +404,10 @@ func (c *Client) GetVehicleByVIN(vin string) *Vehicle { SubscriptionFeatures: vData.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.GetVehicleStatus() vehicle.GetVehicleCondition() // TODO: Temporary disabled for getting successful testing results diff --git a/vehicle.go b/vehicle.go index c6d13d8..1faa5b7 100644 --- a/vehicle.go +++ b/vehicle.go @@ -71,10 +71,10 @@ type Vehicle struct { MPG float64 // STATUS REQUEST > "avgFuelConsumptionMpg": 18.5 LP100Km float64 // STATUS REQUEST > "avgFuelConsumptionLitersPer100Kilometers": 12.7 } - ClimateProfiles []*ClimateProfile - Doors []*Door // CONDITION REQUEST > - Windows []*Window // CONDITION REQUEST > - Tires []*Tire // CONDITION AND STATUS REQUEST > + ClimateProfiles map[string]ClimateProfile + Doors map[string]Door // CONDITION REQUEST > + Windows map[string]Window // CONDITION REQUEST > + Tires map[string]Tire // CONDITION AND STATUS REQUEST > GeoLocation GeoLocation Updated time.Time client *Client @@ -120,7 +120,8 @@ type GeoLocation struct { type Door struct { Position string // front | rear | boot | enginehood SubPosition string // right | left - Status string + Status string // CLOSED | + Lock string // LOCKED | Updated time.Time } @@ -163,14 +164,14 @@ func (v *Vehicle) String() string { vString += "Litres per 100 km: " + fmt.Sprintf("%v", v.FuelConsumptionAvg.LP100Km) + "\n" vString += "=== WINDOWS =====================\n" - for i, w := range v.Windows { - vString += fmt.Sprintf("%d >> %+v\n", i+1, w) + for k, v := range v.Windows { + vString += fmt.Sprintf("%s >> %+v\n", k, v) // fmt.Printf("%d >> %+v\n", i+1, w) } vString += "=== DOORS =====================\n" - for i, d := range v.Doors { - vString += fmt.Sprintf("%d >> %+v\n", i+1, d) + for k, v := range v.Doors { + vString += fmt.Sprintf("%s >> %+v\n", k, v) // fmt.Printf("%d >> %+v\n", i+1, d) } @@ -466,12 +467,12 @@ func (v *Vehicle) GetClimatePresets() { var climateProfile ClimateProfile json.Unmarshal([]byte(child.Data().(string)), &climateProfile) - if v.isEV() && climateProfile.VehicleType == "phev" { - v.ClimateProfiles = append(v.ClimateProfiles, &climateProfile) - } - if !v.isEV() && climateProfile.VehicleType == "gas" { - v.ClimateProfiles = append(v.ClimateProfiles, &climateProfile) - } + // if v.isEV() && climateProfile.VehicleType == "phev" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) + // } + // if !v.isEV() && climateProfile.VehicleType == "gas" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) + // } v.Updated = time.Now() } } else { @@ -498,12 +499,12 @@ func (v *Vehicle) GetClimateUserPresets() { var climateProfile ClimateProfile json.Unmarshal([]byte(child.Data().(string)), &climateProfile) - if v.isEV() && climateProfile.VehicleType == "phev" { - v.ClimateProfiles = append(v.ClimateProfiles, &climateProfile) - } - if !v.isEV() && climateProfile.VehicleType == "gas" { - v.ClimateProfiles = append(v.ClimateProfiles, &climateProfile) - } + // if v.isEV() && climateProfile.VehicleType == "phev" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) + // } + // if !v.isEV() && climateProfile.VehicleType == "gas" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) + // } } v.Updated = time.Now() // // ONLY FOR THAT REQUEST BECAUSE OF API SENDS BACK ESCAPING DATA IN DATA FIELD @@ -563,87 +564,83 @@ func (v *Vehicle) GetVehicleStatus() { for key, child := range respParsed.S("data").ChildrenMap() { fmt.Printf("key: %v, value: %v\n", key, child.Data()) if child.Data() == "NOT_EQUIPPED" || child.Data() == "UNKNOWN" || child.Data() == "16383" || child.Data() == "65535" || child.Data() == "None" || child.Data() == "-64.0" || child.Data() == nil { - fmt.Println("Skipping") + // fmt.Println("Skipping") continue } if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "Position") { - submatchall := re.FindAllString(key, -1) + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "Position") + submatchall := re.FindAllString(pos, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - for _, element := range submatchall { - fmt.Println(element) - } - newdoor := Door{} - newdoor.Position = submatchall[0] - if len(submatchall) >= 3 { - newdoor.SubPosition = submatchall[1] - } - - for _, door := range v.Doors { - if door.Position == newdoor.Position && door.SubPosition == newdoor.SubPosition { - door.Status = child.Data().(string) - door.Updated = time.Now() - } else { - newdoor.Status = child.Data().(string) - newdoor.Updated = time.Now() - v.Doors = append(v.Doors, &newdoor) + if door, ok := v.Doors[pos]; ok { + door.Status = child.Data().(string) + door.Updated = time.Now() + } else { + door.Status = child.Data().(string) + door.Updated = time.Now() + v.Doors[pos] = Door{ + Position: submatchall[0], + Status: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if d, ok := v.Doors[pos]; ok { + d.SubPosition = submatchall[1] + v.Doors[pos] = d + } } } } - if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "Status") { - submatchall := re.FindAllString(key, -1) + if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "LockStatus") { + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "LockStatus") + submatchall := re.FindAllString(pos, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - for _, element := range submatchall { - fmt.Println(element) - } - newdoor := Door{} - newdoor.Position = submatchall[0] - if len(submatchall) >= 3 { - newdoor.SubPosition = submatchall[1] - } - - for _, door := range v.Doors { - if door.Position == newdoor.Position && door.SubPosition == newdoor.SubPosition { - door.Status = child.Data().(string) - door.Updated = time.Now() - } else { - newdoor.Status = child.Data().(string) - newdoor.Updated = time.Now() - v.Doors = append(v.Doors, &newdoor) + if door, ok := v.Doors[pos]; ok { + door.Lock = child.Data().(string) + door.Updated = time.Now() + } else { + door.Lock = child.Data().(string) + door.Updated = time.Now() + v.Doors[pos] = Door{ + Position: submatchall[0], + Lock: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if d, ok := v.Doors[pos]; ok { + d.SubPosition = submatchall[1] + v.Doors[pos] = d + } } } } if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { submatchall := re.FindAllString(key, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - for _, element := range submatchall { - fmt.Println(element) - } - newwindow := Window{} - newwindow.Position = submatchall[0] - if len(submatchall) >= 3 { - newwindow.SubPosition = submatchall[1] - } - - for _, window := range v.Windows { - if window.Position == newwindow.Position && window.SubPosition == newwindow.SubPosition { - window.Status = child.Data().(string) - window.Updated = time.Now() - } else { - newwindow.Status = child.Data().(string) - newwindow.Updated = time.Now() - v.Windows = append(v.Windows, &newwindow) + if window, ok := v.Windows[submatchall[0]+submatchall[1]]; ok { + window.Status = child.Data().(string) + window.Updated = time.Now() + } else { + window.Status = child.Data().(string) + window.Updated = time.Now() + v.Windows[submatchall[0]+submatchall[1]] = Window{ + Position: submatchall[0], + SubPosition: submatchall[1], + Status: child.Data().(string), + Updated: time.Now(), } } } if strings.HasPrefix(key, "tire") && !strings.HasSuffix(key, "Psi") { v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data()) - submatchall := re.FindAllString(key, -1) - for _, element := range submatchall { - fmt.Println(element) - } + // submatchall := re.FindAllString(key, -1) + // for _, element := range submatchall { + // fmt.Println(element) + // } } } @@ -715,94 +712,85 @@ func (v *Vehicle) GetVehicleCondition() { for key, child := range respParsed.S("data").S("result").ChildrenMap() { fmt.Printf("key: %v, value: %v\n", key, child.Data()) if child.Data() == "NOT_EQUIPPED" || child.Data() == "UNKNOWN" || child.Data() == "16383" || child.Data() == "65535" || child.Data() == "None" || child.Data() == "-64.0" || child.Data() == nil { - fmt.Println("Skipping") continue } if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "Position") { - submatchall := re.FindAllString(key, -1) + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "Position") + submatchall := re.FindAllString(pos, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - for _, element := range submatchall { - fmt.Println(element) - } - newdoor := Door{} - newdoor.Position = submatchall[0] - if len(submatchall) >= 3 { - newdoor.SubPosition = submatchall[1] - } - for _, door := range v.Doors { - if door.Position == newdoor.Position && door.SubPosition == newdoor.SubPosition { - door.Status = child.Data().(string) - door.Updated = time.Now() - } else { - newdoor.Status = child.Data().(string) - newdoor.Updated = time.Now() - v.Doors = append(v.Doors, &newdoor) + if door, ok := v.Doors[pos]; ok { + door.Status = child.Data().(string) + door.Updated = time.Now() + } else { + door.Status = child.Data().(string) + door.Updated = time.Now() + v.Doors[pos] = Door{ + Position: submatchall[0], + Status: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if d, ok := v.Doors[pos]; ok { + d.SubPosition = submatchall[1] + v.Doors[pos] = d + } + } + } + } + if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "LockStatus") { + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "LockStatus") + submatchall := re.FindAllString(pos, -1) + v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) + + if door, ok := v.Doors[pos]; ok { + door.Lock = child.Data().(string) + door.Updated = time.Now() + } else { + door.Lock = child.Data().(string) + door.Updated = time.Now() + v.Doors[pos] = Door{ + Position: submatchall[0], + Lock: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if d, ok := v.Doors[pos]; ok { + d.SubPosition = submatchall[1] + v.Doors[pos] = d + } } } } if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { submatchall := re.FindAllString(key, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - for _, element := range submatchall { - fmt.Println(element) - } - newwindow := Window{} - newwindow.Position = submatchall[0] - if len(submatchall) >= 3 { - newwindow.SubPosition = submatchall[1] - } - for _, window := range v.Windows { - if window.Position == newwindow.Position && window.SubPosition == newwindow.SubPosition { - window.Status = child.Data().(string) - window.Updated = time.Now() - } else { - newwindow.Status = child.Data().(string) - newwindow.Updated = time.Now() - v.Windows = append(v.Windows, &newwindow) + if window, ok := v.Windows[submatchall[0]+submatchall[1]]; ok { + window.Status = child.Data().(string) + window.Updated = time.Now() + } else { + window.Status = child.Data().(string) + window.Updated = time.Now() + v.Windows[submatchall[0]+submatchall[1]] = Window{ + Position: submatchall[0], + SubPosition: submatchall[1], + Status: child.Data().(string), + Updated: time.Now(), } } } if strings.HasPrefix(key, "tire") && !strings.HasSuffix(key, "Unit") { v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data()) - submatchall := re.FindAllString(key, -1) - for _, element := range submatchall { - fmt.Println(element) - } + // submatchall := re.FindAllString(key, -1) + // for _, element := range submatchall { + // fmt.Println(element) + // } } v.Updated = time.Now() } - - // vCon := VehicleCondition{} - // vcString := respParsed.Path("data.result").String() - // json.Unmarshal([]byte(vcString), &vCon) - - // for _, elem := range vCon.VehicleStatus { - // if elem.Value == "NOT_EQUIPPED" || elem.Value == "UNKNOWN" || elem.Value == "16383" || elem.Value == "65535" || elem.Value == "None" || elem.Value == "-64.0" { - // continue - // } - // if strings.HasPrefix(elem.Key, "door") { - // logger.Debugf("VEHICLE COND: %v > %v\n", elem.Key, elem.Value) - // } - // } - // for _, elem := range vCon.VehicleStatus { - // if elem.Value == "NOT_EQUIPPED" || elem.Value == "UNKNOWN" || elem.Value == "16383" || elem.Value == "65535" || elem.Value == "None" || elem.Value == "-64.0" { - // continue - // } - // if strings.HasPrefix(elem.Key, "window") { - // logger.Debugf("VEHICLE COND: %v > %v\n", elem.Key, elem.Value) - // } - // } - // for _, elem := range vCon.VehicleStatus { - // if elem.Value == "NOT_EQUIPPED" || elem.Value == "UNKNOWN" || elem.Value == "16383" || elem.Value == "65535" || elem.Value == "None" || elem.Value == "-64.0" { - // continue - // } - // if strings.HasPrefix(elem.Key, "tire") { - // logger.Debugf("VEHICLE COND: %v > %v\n", elem.Key, elem.Value) - // } - // } - // logger.Debugf("VEHICLE COND REQUEST: %+v", vCon) } // VEHICLE_STATE_TYPE >> IGNITION_OFF