Commit 6ccd519e authored by Mohd Bilal's avatar Mohd Bilal
Browse files

debugged start app

parent 50ee595a
......@@ -6,6 +6,7 @@ from app.models.stations import Station
import traceback
import logging
import json
import time
import sys
import os
......@@ -20,7 +21,7 @@ class TIMGSNClient:
raise Exception(
"TIMGSN Client is a singleton. Cannot instantiate.")
self.settings = None
self.is_setup = None
self._is_setup = None
self.users = []
@staticmethod
......@@ -30,12 +31,15 @@ class TIMGSNClient:
return TIMGSNClient.__instances
def _set_up(self):
logger.info("setting up TIMGSN client...")
with open('settings.json', 'r') as f:
self.settings = json.load(f)
users_data = self.settings.get('client').get('users')
for user_data in users_data:
user = User(username=os.getenv(user_data.get("username_env")),
password=os.getenv(user_data.get("password_env")))
authenticated = user.authenticate(os.getenv(user_data.get("username_env")),
os.getenv(user_data.get("password_env")))
if 'satellites' in user_data:
for norad in user_data.get("satellites"):
satellite = Satellite(norad)
......@@ -76,10 +80,12 @@ class TIMGSNClient:
user.add_request_sender(request_sender)
user.add_request_handler(request_handler)
self.users.append(user)
self.is_setup = True
self._is_setup = True
def _tear_down(self):
raise NotImplementedError
for user in self.users:
user.terminate()
def stop(self):
self._tear_down()
......@@ -90,7 +96,7 @@ class TIMGSNClient:
user.operate()
def start(self):
logger.info("setting up TIMGSN Client...")
self._set_up()
logger.info("starting TIMGSN Client...")
time.sleep(5)
self.operate()
......@@ -48,7 +48,6 @@ class RequestSender(ThreadedModule):
def add_user(self, user):
self.user = user
user.add_request_sender(self)
def _loop(self):
responses = []
......@@ -167,7 +166,6 @@ class RequestHandler(ThreadedModule):
def add_user(self, user):
self.user = user
user.add_request_handler(self)
def create_request_url(self, request_uid):
return f"{self.url}/{request_uid}"
......
......@@ -13,12 +13,12 @@ class Satellite:
self.name = name
self.plan = []
self.owner = None
self.is_setup = False
self._is_setup = False
def _set_up(self):
if not self.owner:
raise AttributeError("No Owner has been assigned to satellite.")
self.is_setup = True
self._is_setup = True
def set_plan(self, plan):
self.plan = plan
......
......@@ -22,7 +22,7 @@ class Station:
self.min_el = min_el
self.schedule = []
self.owner = None
self.is_setup = False
self._is_setup = False
def __str__(self) -> str:
return f"Ground station: {self.name} | lat: {self.latitude} | long: {self.longitutde}"
......@@ -30,7 +30,7 @@ class Station:
def _set_up(self):
if not self.owner:
raise AttributeError("No owner has been set. Please add one.")
self.is_setup = True
self._is_setup = True
def set_owner(self, owner):
self.owner = owner
......
import functools
import logging
import os
logger = logging.getLogger(__name__)
def authentication_required(f):
@functools.wraps(f)
......@@ -15,8 +17,8 @@ def authentication_required(f):
class User:
def __init__(self, username, password=None, *args, **kwargs) -> None:
self.username = username
self.password = password
self._username = username
self._password = password
self.is_authenticated = False
self.satellites = kwargs[
'satellites'] if 'satellites' in kwargs else []
......@@ -25,7 +27,7 @@ class User:
self.request_handler = None
def __str__(self) -> str:
return f"User: {self.username}"
return f"User: {self._username}"
@property
def username(self):
......@@ -38,7 +40,6 @@ class User:
self._username = username
@property
@authentication_required
def password(self):
return self._password
......@@ -47,13 +48,15 @@ class User:
if password is None:
raise ValueError("Password needs to be a valid string.")
self._password = password
logger.debug("user password is being set")
self.is_authenticated = True
logger.debug(f"User has been authenticated: {self.is_authenticated}")
def load_credentials_from_env(self, ENV_USERNAME, ENV_PASSWORD):
self.username = os.getenv(ENV_USERNAME)
self.password = os.getenv(ENV_PASSWORD)
self._username = os.getenv(ENV_USERNAME)
self._password = os.getenv(ENV_PASSWORD)
if not self.username or not self.password:
if not self._username or not self._password:
raise Exception("Either Username or Password"
"were not set in Environment.")
return True
......@@ -104,11 +107,17 @@ class User:
def add_request_sender(self, request_sender):
self.request_sender = request_sender
request_sender.add_user(self)
def add_request_handler(self, request_handler):
self.request_handler = request_handler
request_handler.add_user(self)
@authentication_required
def operate(self):
self.request_handler.start()
self.request_sender.start()
def terminate(self):
self.request_handler.kill()
self.request_sender.kill()
from os import system
from app import TIMGSNClient
import logging
import time
import sys
def setup_logger():
logger = logging.getLogger('app')
......@@ -12,9 +15,19 @@ def setup_logger():
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
if __name__=="__main__":
setup_logger()
client = TIMGSNClient()
logger = setup_logger()
client = TIMGSNClient.get_instance()
client.start()
try:
while True:
logger.info("running main...")
time.sleep(1)
except KeyboardInterrupt as err:
logger.info("exiting app...")
client.stop()
logger.info("exiting system...")
sys.exit(0)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment