mirror of
https://github.com/MarkParker5/STARK.git
synced 2025-02-22 12:00:09 +02:00
start hotspot if hub not activated
This commit is contained in:
parent
9d971ef804
commit
e50ddc8196
@ -2,20 +2,31 @@ from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
import config
|
||||
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
|
||||
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
|
||||
__all__ = [
|
||||
# context
|
||||
'create_session',
|
||||
'create_async_session',
|
||||
# dependencies
|
||||
# 'get_session',
|
||||
# 'get_async_session',
|
||||
]
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||
)
|
||||
engine = create_engine(config.db_url)
|
||||
create_session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
def get_session() -> Session:
|
||||
with create_session() as session:
|
||||
yield session
|
||||
|
||||
def get_session():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
# async_engine = create_async_engine(config.db_url)
|
||||
# create_async_session = sessionmaker(
|
||||
# async_engine, class_ = AsyncSession, expire_on_commit = False
|
||||
# )
|
||||
#
|
||||
# async def get_async_session() -> AsyncSession:
|
||||
# async with create_async_session() as session:
|
||||
# yield session
|
||||
|
@ -1,3 +1,4 @@
|
||||
from __future__ import annotations
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import Depends
|
||||
@ -13,6 +14,14 @@ class HubManager:
|
||||
def __init__(self, session = Depends(database.get_session)):
|
||||
self.session = session
|
||||
|
||||
def __del__(self):
|
||||
self.session.close()
|
||||
|
||||
@classmethod
|
||||
def default(cls, session: database.Session | None = None) -> HubManager:
|
||||
session = session or database.create_session()
|
||||
return cls(session)
|
||||
|
||||
def get(self) -> Hub:
|
||||
db = self.session
|
||||
return db.query(Hub).one()
|
||||
|
@ -17,6 +17,9 @@ access_point = pyaccesspoint.AccessPoint()
|
||||
access_point.ssid = config.wifi_ssid
|
||||
access_point.password = config.wifi_password
|
||||
|
||||
class ConnectionException(Exception):
|
||||
pass
|
||||
|
||||
def get_list() -> list[Cell]:
|
||||
return Cell.all('wlan0')
|
||||
|
||||
@ -33,16 +36,16 @@ def save_and_connect(ssid: str, password: str):
|
||||
scheme.save()
|
||||
scheme.activate()
|
||||
|
||||
def connect_first() -> bool:
|
||||
def connect_first() -> None | ConnectionException:
|
||||
ssids = [cell.ssid for cell in Cell.all('wlan0')]
|
||||
|
||||
for scheme in Scheme.all():
|
||||
ssid = scheme.options.get('wpa-ssid')#, scheme.options.get('wireless-essid'))
|
||||
if ssid in ssids:
|
||||
scheme.activate()
|
||||
return True
|
||||
break
|
||||
else:
|
||||
return False
|
||||
raise ConnectionException('No schemes available')
|
||||
|
||||
def start_hotspot():
|
||||
access_point.start()
|
||||
|
@ -5,7 +5,15 @@ sys.path.append(root)
|
||||
|
||||
import uvicorn
|
||||
from API.main import app
|
||||
from API.endpoints.hub import HubManager
|
||||
from Raspberry import WiFi
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
hub = HubManager.default().get()
|
||||
WiFi.connect_first()
|
||||
except:
|
||||
WiFi.start_hotspot()
|
||||
|
||||
uvicorn.run('main:app', host = '0.0.0.0', port = 8001, reload = True, reload_dirs=[root,])
|
||||
|
@ -2,19 +2,36 @@ import pathlib
|
||||
path = str(pathlib.Path(__file__).parent.absolute())
|
||||
del pathlib
|
||||
|
||||
telebot = '12345678:token'
|
||||
goole_tts_json_key = path+'google-cloud-text-to-speech-private-key.json'
|
||||
|
||||
db_name = 'archie.db'
|
||||
src: str = path + '/resources'
|
||||
|
||||
vosk_model = 'model-small-rus' # from alphacephei.com/vosk/models
|
||||
# Api keys
|
||||
|
||||
double_clap_activation = False
|
||||
telebot: str
|
||||
goole_tts_json_key: str = src + '/tts-gc-key.json'
|
||||
|
||||
names = ['арчи', 'archie']
|
||||
# Speech Recognition
|
||||
|
||||
#################################################
|
||||
# Django
|
||||
django_secret_key = '-----123456789-----'
|
||||
django_debug_enabled = True
|
||||
django_allowed_hosts = []
|
||||
vosk_model: str = src + '/model-small-rus'
|
||||
|
||||
# TTS
|
||||
|
||||
language_code: str = 'ru-RU'
|
||||
voice_volume: float = 1
|
||||
|
||||
# Archie settings
|
||||
|
||||
double_clap_activation: bool = False
|
||||
|
||||
# Archie Core
|
||||
|
||||
names: list[str] = ['арчи', 'archie']
|
||||
|
||||
# DB
|
||||
|
||||
db_url: str = 'sqlite:///./sql_app.db'
|
||||
|
||||
# WiFi
|
||||
|
||||
wifi_ssid: str = 'Archie Hub'
|
||||
wifi_password: str = '12345678'
|
||||
|
Loading…
x
Reference in New Issue
Block a user