Files
FCI_WaterMaker_API/run_server.py
2025-06-10 15:53:43 +00:00

85 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Standalone script to run the Watermaker PLC API server.
This can be used when the package installation has issues.
"""
import sys
import os
import argparse
# Add the package directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from watermaker_plc_api.app import create_app
from watermaker_plc_api.config import Config
from watermaker_plc_api.utils.logger import get_logger
from watermaker_plc_api.services.background_tasks import start_background_updates
logger = get_logger(__name__)
def parse_args():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description='Watermaker PLC API Server')
parser.add_argument('--host', default='0.0.0.0',
help='Host to bind to (default: 0.0.0.0)')
parser.add_argument('--port', type=int, default=5000,
help='Port to bind to (default: 5000)')
parser.add_argument('--plc-ip', default='198.18.100.141',
help='PLC IP address (default: 198.18.100.141)')
parser.add_argument('--plc-port', type=int, default=502,
help='PLC Modbus port (default: 502)')
parser.add_argument('--debug', action='store_true',
help='Enable debug mode')
parser.add_argument('--no-background-updates', action='store_true',
help='Disable background data updates')
return parser.parse_args()
def main():
"""Main application entry point"""
args = parse_args()
# Update config with command line arguments
Config.PLC_IP = args.plc_ip
Config.PLC_PORT = args.plc_port
Config.DEBUG = args.debug
logger.info("Starting Watermaker PLC API Server (Standalone)")
logger.info(f"API Version: 1.1.0")
logger.info(f"PLC Target: {Config.PLC_IP}:{Config.PLC_PORT}")
logger.info(f"Server: http://{args.host}:{args.port}")
# Create Flask application
app = create_app()
# Start background data updates (unless disabled)
if not args.no_background_updates:
start_background_updates()
logger.info("Background data update thread started")
else:
logger.warning("Background data updates disabled")
logger.info("🚀 Server starting...")
logger.info("📊 API Documentation: http://localhost:5000/api/config")
logger.info("❤️ Health Check: http://localhost:5000/api/status")
try:
# Start the Flask server
app.run(
host=args.host,
port=args.port,
debug=args.debug,
threaded=True
)
except KeyboardInterrupt:
logger.info("Server shutdown requested by user")
sys.exit(0)
except Exception as e:
logger.error(f"Failed to start server: {e}")
sys.exit(1)
if __name__ == '__main__':
main()