Skip to content

Commit

Permalink
Updated Factory to use a singleton-like operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zegorax committed Jul 23, 2024
1 parent 0c30242 commit 01edb9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
7 changes: 3 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
BACKEND_PROVIDER_CAR_ID = os.getenv('BACKEND_PROVIDER_CAR_ID', 1)

# Backend provider instanciation
provider_factory = BackendProviderFactory(BACKEND_PROVIDER, BACKEND_PROVIDER_BASE_URL, BACKEND_PROVIDER_CAR_ID)
provider = provider_factory.get_instance()

provider.refresh_data()
BackendProviderFactory(BACKEND_PROVIDER, BACKEND_PROVIDER_BASE_URL, BACKEND_PROVIDER_CAR_ID)

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY')
Expand Down Expand Up @@ -168,6 +165,7 @@ def carstate(shortuuid):
expiry = result[2]

if expiry > time.time():
provider = BackendProviderFactory.get_instance()
provider.refresh_data()

temp_carstate = vars(provider)
Expand Down Expand Up @@ -231,6 +229,7 @@ def map_admin():

print(result)

provider = BackendProviderFactory.get_instance()
provider.refresh_data()

if 'uuid' in locals():
Expand Down
10 changes: 7 additions & 3 deletions interfaces/backendfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from interfaces.backendinterface import IBackendProvider

class BackendProviderFactory:
# static to have a singleton-like
provider: IBackendProvider

def _load_provider(self, name, base_url, car_id):
if name == "teslalogger":
return teslalogger.TeslaloggerBackendProvider(base_url, car_id)
Expand All @@ -16,7 +19,8 @@ def _load_provider(self, name, base_url, car_id):
def __init__(self, provider_name, base_url, car_id):
self.provider_name = provider_name

self.provider_instance = self._load_provider(provider_name, base_url, car_id)
self.provider = self._load_provider(provider_name, base_url, car_id)

def get_instance(self) -> IBackendProvider:
return self.provider_instance
@staticmethod
def get_instance() -> IBackendProvider:
return BackendProviderFactory.provider

0 comments on commit 01edb9a

Please sign in to comment.