-
Notifications
You must be signed in to change notification settings - Fork 107
soc_vwid: make status query thread safe #2997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Warum sind globale Variablen erforderlich? Wenn jede Instanz ihre eigenen Attribute hat, würde es nicht zu Race Conditions kommen. |
|
Für jedes Fahrzeug wird ein eigenes Set von Attributen (Token, etc) benötigt.
Als Folge wird der SoC dann auf 0 gesetzt, was für die Anwender unschön ist. Auf die Schnelle kann ich die library nicht umarbeiten.. |
|
Verschwindet die Fehlermeldung, wenn Du Wenn alle Fahrzeuge nacheinander das gleiche globale connection-dict benutzen, stehen darin noch die Daten vom vorherigen Fahrzeug. Dann gibt es doch dort Überschneidungen. Oder ist das bewusst so, dass die Fahrzeuge sich die connection teilen sollen? |
|
Ich habe es jetzt aufgeräumt. Connections werden je username in dict self.connection verwaltet. |
| global a | ||
| try: | ||
| if 'a' not in globals(): | ||
| a = None | ||
| # prepare and call async method | ||
| loop = new_event_loop() | ||
| set_event_loop(loop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jetzt gibt es ja wieder eine globale Variable, nur an anderer Stelle.
Wie wäre es, wenn die soc.py so aussieht, ohne api.py?
import aiohttp
import logging
from asyncio import new_event_loop, set_event_loop
from typing import Union
from modules.common.abstract_device import DeviceDescriptor
from modules.common.abstract_vehicle import VehicleUpdateData
from modules.common.component_state import CarState
from modules.common.configurable_vehicle import ConfigurableVehicle
from modules.vehicles.vwid.config import VWId
from modules.vehicles.vwid import libvwid
from modules.vehicles.vwgroup.vwgroup import VwGroup
log = logging.getLogger(__name__)
def create_vehicle(vehicle_config: VWId, vehicle: int):
def fetch() -> CarState:
nonlocal vw_group
# async method, called from sync fetch_soc, required because libvwid expect async environment
async def _fetch_soc() -> Union[int, float, str]:
async with aiohttp.ClientSession() as session:
return await vw_group.request_data(libvwid.vwid(session))
loop = new_event_loop()
set_event_loop(loop)
soc, range, soc_ts, soc_tsX = loop.run_until_complete(_fetch_soc())
return CarState(soc=soc, range=range, soc_timestamp=soc_tsX)
vw_group = VwGroup(vehicle_config, vehicle)
def updater(vehicle_update_data: VehicleUpdateData) -> CarState:
return fetch()
return ConfigurableVehicle(vehicle_config=vehicle_config,
component_updater=updater,
vehicle=vehicle,
calc_while_charging=vehicle_config.configuration.calculate_soc)
device_descriptor = DeviceDescriptor(configuration_factory=VWId)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hallo Lena,
danke, jetzt hat sich meine letzte Änderung gerade mit deinem review überschnitten.
Ich möchte halt den Context der libvwid (connection, vehicle) im Speicher halten bis zum Restart um unnötige Logins zu vermeiden, da sonst der VW-Server u.U. komisch reagiert.
Wenn die Objekt-Instanzen bzw. Variablen darüber neu instanziiert werden, klappt das aber nicht.
Ich werde deinen Vorschlag testen.
|
Ich habe soc.py geändert wie von Dir vorgeschlagen.
|
With more that one EV using soc module vwid, parallel execution could fail.
Added a lock to implement mutual exclusion