Files
2025-06-11 22:12:02 +00:00

78 lines
2.3 KiB
Python

"""
Configuration settings for the Watermaker PLC API.
"""
import os
from typing import Dict, Any
class Config:
"""Application configuration"""
# Flask Settings
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'
SECRET_KEY = os.getenv('SECRET_KEY', 'watermaker-plc-api-dev-key')
# PLC Connection Settings
PLC_IP = os.getenv('PLC_IP', '198.18.100.141')
PLC_PORT = int(os.getenv('PLC_PORT', '502'))
PLC_UNIT_ID = int(os.getenv('PLC_UNIT_ID', '1'))
PLC_TIMEOUT = int(os.getenv('PLC_TIMEOUT', '3'))
PLC_CONNECTION_RETRY_INTERVAL = int(os.getenv('PLC_CONNECTION_RETRY_INTERVAL', '30'))
# API Settings
API_VERSION = "1.1"
CORS_ENABLED = True
# Background Task Settings
DATA_UPDATE_INTERVAL = int(os.getenv('DATA_UPDATE_INTERVAL', '5')) # seconds
ERROR_RETRY_INTERVAL = int(os.getenv('ERROR_RETRY_INTERVAL', '10')) # seconds
MAX_CACHED_ERRORS = int(os.getenv('MAX_CACHED_ERRORS', '10'))
# Logging Settings
LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO').upper()
LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
@classmethod
def get_plc_config(cls) -> Dict[str, Any]:
"""Get PLC connection configuration"""
return {
"ip_address": cls.PLC_IP,
"port": cls.PLC_PORT,
"unit_id": cls.PLC_UNIT_ID,
"timeout": cls.PLC_TIMEOUT,
"connected": False,
"client": None,
"last_connection_attempt": 0,
"connection_retry_interval": cls.PLC_CONNECTION_RETRY_INTERVAL
}
class DevelopmentConfig(Config):
"""Development configuration"""
DEBUG = True
PLC_IP = '127.0.0.1' # Simulator for development
class ProductionConfig(Config):
"""Production configuration"""
DEBUG = False
@property
def SECRET_KEY(self):
"""Get SECRET_KEY from environment, required in production"""
secret_key = os.getenv('SECRET_KEY')
if not secret_key:
raise ValueError("SECRET_KEY environment variable must be set in production")
return secret_key
class TestingConfig(Config):
"""Testing configuration"""
TESTING = True
DEBUG = True
PLC_IP = '127.0.0.1'
DATA_UPDATE_INTERVAL = 1 # Faster updates for testing