Adding mw40v APi client
This commit is contained in:
66
main.py
66
main.py
@@ -9,6 +9,8 @@ import configparser
|
||||
from fastapi import FastAPI, Depends, HTTPException, status
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
|
||||
from mw40v import Mw40V
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
security = HTTPBasic()
|
||||
@@ -20,9 +22,38 @@ ON_FILENAME = 'dhcpcd-usb-on.conf'
|
||||
OFF_FILENAME = 'dhcpcd-usb-off.conf'
|
||||
|
||||
|
||||
class Config:
|
||||
config = None
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
if cls.config is None:
|
||||
cls.config = configparser.ConfigParser()
|
||||
cls.config.read('./config.ini')
|
||||
|
||||
return cls.config
|
||||
|
||||
|
||||
class AirModem:
|
||||
airModem = None
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
if cls.airModem is None:
|
||||
config = Config.get()
|
||||
|
||||
cls.airModem = Mw40V(
|
||||
config['MW40V']['username'],
|
||||
config['MW40V']['password'],
|
||||
config['MW40V']['encrypt_key'],
|
||||
config['MW40V']['verification_key'],
|
||||
)
|
||||
|
||||
return cls.airModem
|
||||
|
||||
|
||||
def get_username(credentials: Annotated[HTTPBasicCredentials, Depends(security)]):
|
||||
config = configparser.ConfigParser()
|
||||
config.read('./config.ini')
|
||||
config = Config.get()
|
||||
|
||||
if credentials.username != config['credentials']['username']:
|
||||
raise_unauthorized()
|
||||
@@ -65,13 +96,10 @@ def is_usb_on():
|
||||
|
||||
def switch_status(turn_on):
|
||||
if turn_on:
|
||||
# call usb modem api to turn it on
|
||||
update_usb_modem(turn_on)
|
||||
|
||||
update_dhcp_conf(turn_on)
|
||||
|
||||
if not turn_on:
|
||||
# call usb modem api to turn it off
|
||||
update_dhcp_conf(turn_on)
|
||||
else:
|
||||
update_dhcp_conf(turn_on)
|
||||
update_usb_modem(turn_on)
|
||||
|
||||
|
||||
@@ -82,21 +110,37 @@ def update_dhcp_conf(turn_on):
|
||||
|
||||
|
||||
def update_usb_modem(turn_on):
|
||||
pass
|
||||
if turn_on:
|
||||
AirModem.get().connect()
|
||||
else:
|
||||
AirModem.get().disconnect()
|
||||
|
||||
|
||||
def initialize_app():
|
||||
script_dir = Path(__file__).parent.absolute()
|
||||
credentials_file_path = str(script_dir) + '/config.ini'
|
||||
if not os.path.isfile(credentials_file_path):
|
||||
username = input("Enter a username")
|
||||
password = input("Enter a password")
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
username = input("Enter a username for the API")
|
||||
password = input("Enter a password for the API")
|
||||
config['credentials'] = {
|
||||
'username': username,
|
||||
'password_hash': PasswordHasher().hash(password)
|
||||
}
|
||||
|
||||
username = input("Enter the username for MW40V")
|
||||
password = input("Enter the password for MW40V")
|
||||
encrypt_key = input("Enter the encrypt_key for MW40V")
|
||||
verification_key = input("Enter the verification_key for MW40V")
|
||||
config['MW40V'] = {
|
||||
'username': username,
|
||||
'password': PasswordHasher().hash(password),
|
||||
'encrypt_key': encrypt_key,
|
||||
'verification_key': verification_key
|
||||
}
|
||||
|
||||
with open(credentials_file_path, 'w') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user