Spaces:
Sleeping
Sleeping
| """TSU-WAVE Main Entry Point""" | |
| import asyncio | |
| import logging | |
| from pathlib import Path | |
| from typing import Optional | |
| from .api.main import app | |
| from .utils.config import load_config, validate_config | |
| from .utils.logger import setup_logging | |
| from .database.timescale import TimescaleDB | |
| from .database.redis_cache import RedisCache | |
| logger = logging.getLogger(__name__) | |
| class TSUWave: | |
| """Main TSU-WAVE application class""" | |
| def __init__(self, config_path: Optional[str] = None): | |
| self.config = load_config(config_path) | |
| self.db = None | |
| self.cache = None | |
| self.running = False | |
| # Setup logging | |
| log_level = self.config['system']['log_level'] | |
| log_file = self.config['system'].get('log_file') | |
| setup_logging('tsu-wave', log_file, log_level) | |
| logger.info("TSU-WAVE initialized") | |
| async def start(self): | |
| """Start all services""" | |
| logger.info("Starting TSU-WAVE services...") | |
| # Validate configuration | |
| if not validate_config(self.config): | |
| logger.error("Invalid configuration") | |
| return False | |
| # Connect to database | |
| self.db = TimescaleDB(self.config['database']) | |
| await self.db.connect() | |
| # Connect to Redis | |
| self.cache = RedisCache(self.config['redis']) | |
| await self.cache.connect() | |
| self.running = True | |
| logger.info("TSU-WAVE started successfully") | |
| return True | |
| async def stop(self): | |
| """Stop all services""" | |
| logger.info("Stopping TSU-WAVE services...") | |
| if self.db: | |
| await self.db.disconnect() | |
| if self.cache: | |
| await self.cache.disconnect() | |
| self.running = False | |
| logger.info("TSU-WAVE stopped") | |
| async def run_api(self): | |
| """Run API server""" | |
| import uvicorn | |
| host = self.config['api']['host'] | |
| port = self.config['api']['port'] | |
| logger.info(f"Starting API server on {host}:{port}") | |
| config = uvicorn.Config( | |
| app, | |
| host=host, | |
| port=port, | |
| log_level=self.config['system']['log_level'].lower() | |
| ) | |
| server = uvicorn.Server(config) | |
| await server.serve() | |
| async def run_dashboard(self): | |
| """Run dashboard (separate process)""" | |
| import subprocess | |
| host = self.config['dashboard']['host'] | |
| port = self.config['dashboard']['port'] | |
| cmd = [ | |
| "streamlit", "run", | |
| "src/tsuwave/dashboard/app.py", | |
| f"--server.address={host}", | |
| f"--server.port={port}" | |
| ] | |
| logger.info(f"Starting dashboard on {host}:{port}") | |
| subprocess.run(cmd) | |
| async def run_monitor(self): | |
| """Run monitoring loop""" | |
| logger.info("Starting monitoring loop") | |
| while self.running: | |
| try: | |
| # Check for new events | |
| # Update parameters | |
| # Check thresholds | |
| await asyncio.sleep(5) | |
| except Exception as e: | |
| logger.error(f"Monitor error: {e}") | |
| async def main(): | |
| """Main async entry point""" | |
| app = TSUWave() | |
| try: | |
| if await app.start(): | |
| # Run API server | |
| await app.run_api() | |
| else: | |
| logger.error("Failed to start TSU-WAVE") | |
| except KeyboardInterrupt: | |
| logger.info("Shutting down...") | |
| finally: | |
| await app.stop() | |
| def run(): | |
| """Synchronous entry point""" | |
| asyncio.run(main()) | |
| if __name__ == '__main__': | |
| run() | |