get car model in app_decoder.py

This commit is contained in:
Florian Bezannier
2021-03-06 12:51:05 +01:00
parent 3ebc48903a
commit 324ee46af8
2 changed files with 39 additions and 26 deletions
+16 -8
View File
@@ -3,8 +3,8 @@ import json
from MyLogger import logger
ENERGY_CAPACITY = {'SUV 3008': {'BATTERY_POWER': 10.8, 'FUEL_CAPACITY': 43},
'208': {'BATTERY_POWER': 46, 'FUEL_CAPACITY': 0},
'2008': {'BATTERY_POWER': 46, 'FUEL_CAPACITY': 0}
'e-208': {'BATTERY_POWER': 46, 'FUEL_CAPACITY': 0},
'e-2008': {'BATTERY_POWER': 46, 'FUEL_CAPACITY': 0}
}
DEFAULT_BATTERY_POWER = 46
DEFAULT_FUEL_CAPACITY = 0
@@ -16,12 +16,17 @@ class Car:
self.vehicle_id = vehicle_id
self.label = label
self.brand = brand
self.battery_power = None
self.fuel_capacity = None
self.set_energy_capacity(battery_power, fuel_capacity)
def set_energy_capacity(self, battery_power = None, fuel_capacity = None):
if battery_power is not None and fuel_capacity is not None:
self.battery_power = battery_power
self.fuel_capacity = fuel_capacity
elif label in ENERGY_CAPACITY:
self.battery_power = ENERGY_CAPACITY[label]["BATTERY_POWER"]
self.fuel_capacity = ENERGY_CAPACITY[label]["FUEL_CAPACITY"]
elif self.label in ENERGY_CAPACITY:
self.battery_power = ENERGY_CAPACITY[self.label]["BATTERY_POWER"]
self.fuel_capacity = ENERGY_CAPACITY[self.label]["FUEL_CAPACITY"]
else:
logger.warn("Can't get car model please check cars.json")
self.battery_power = DEFAULT_BATTERY_POWER
@@ -31,6 +36,9 @@ class Car:
def from_json(cls, data: dict):
return cls(**data)
def __str__(self):
return str(self.__dict__)
class Cars(list):
def __init__(self, *args):
list.__init__(self, *args)
@@ -51,11 +59,11 @@ class Cars(list):
@classmethod
def from_json(cls, data: list):
students = list(map(Car.from_json, data))
return cls(students)
cars = list(map(Car.from_json, data))
return cls(cars)
def __str__(self):
return str(self.__dict__)
return str(list(map(str, self)))
def save_cars(self, name="cars.json"):
config_str = json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
+23 -18
View File
@@ -23,12 +23,10 @@ BRAND = {"com.psa.mym.myopel": {"realm": "clientsB2COpel", "brand_code":
"com.psa.mym.myvauxhall": {"realm": "clientsB2CVauxhall", "brand_code": "0V", "app_name": "MyVauxall"}
}
def getxmlvalue(root, name):
for child in root.findall("*[@name='" + name + "']"):
return child.text
def find_app_path():
base_dir = 'apps/'
paths = os.listdir(base_dir)
@@ -53,12 +51,15 @@ def find_preferences_xml():
def save_key_to_pem(pfx_data, pfx_password):
private_key, certificate, additional_certificates = pkcs12.load_key_and_certificates(pfx_data,
bytes.fromhex(pfx_password),
default_backend())
with open("public.pem", "wb") as f:
bytes.fromhex(pfx_password), default_backend())
try:
os.mkdir("certs")
except FileExistsError:
pass
with open("certs/public.pem", "wb") as f:
f.write(certificate.public_bytes(encoding=serialization.Encoding.PEM))
with open("private.pem", "wb") as f:
with open("certs/private.pem", "wb") as f:
f.write(private_key.private_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()))
@@ -125,7 +126,7 @@ try:
"User-Agent": "okhttp/4.8.0",
"Version": "1.27.0"
},
cert=("public.pem", "private.pem"),
cert=("certs/public.pem", "certs/private.pem"),
)
res_dict = res2.json()["success"]
@@ -138,25 +139,29 @@ except:
# Psacc
psacc = MyPSACC(None, client_id, client_secret, remote_refresh_token, customer_id, BRAND[package_name]["realm"])
psacc = MyPSACC(None, client_id, client_secret, remote_refresh_token, customer_id, BRAND[package_name]["realm"],
country_code)
psacc.connect(client_email, client_password)
os.chdir(current_dir)
psacc.save_config(name="test.json")
res = psacc.get_vehicles()
for vehicle in res_dict["vehicles"]:
label = vehicle["short_label"].split(" ")[-1]
car = psacc.vehicles_list.get_car_by_vin(vehicle["vin"])
if car.label is "unknown":
car.label = label
car.set_energy_capacity()
psacc.vehicles_list.save_cars()
print(f"\nYour vehicles: {res}")
## Charge control
# Charge control
charge_controls = ChargeControls()
for vin, vehicle in res.items():
chc = ChargeControl(None, vin, 100, [0, 0])
charge_controls.list[vin] = chc
for vehicle in res:
chc = ChargeControl(None, vehicle.vin, 100, [0, 0])
charge_controls.list[vehicle.vin] = chc
charge_controls.save_config(name="charge_config1.json")
try:
os.remove("private.pem")
os.remove("public.pem")
except:
print("Error when deleting temp files")
print("Success !!!")