-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot_server.py
65 lines (49 loc) · 2.02 KB
/
iot_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
import os
from logging.handlers import RotatingFileHandler
from flask import Flask
from config_loader import load_config
from iot_blueprint import iot_blueprint
from iot_database import IoTDeviceDatabase
from iot_importer import Importer
# Create Flask application
app = Flask(__name__)
# Load configuration from the environment variable CONFIG_PATH or default to "config.yml"
config_path = os.getenv("CONFIG_PATH", "config.yaml")
config = load_config(config_path)
# Configure logging
log_file = config.get('log_file', '/data/server.log')
# Create logfile if it does not exist
if not os.path.exists(log_file):
with open(log_file, "w") as f:
f.write("")
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Set up logging to console and log file
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
file_handler = RotatingFileHandler(log_file, maxBytes=5 * 1024 * 1024, backupCount=3)
file_handler.setLevel(logging.INFO)
# Define the logging format and add handlers to the logger
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# Initialize database
db = IoTDeviceDatabase(config.get('database', 'iot_database.db'))
username = config.get('tapo', {}).get('username')
password = config.get('tapo', {}).get('password')
# Initialize importer
importer = Importer(user=username, password=password)
# Register the blueprint
app.register_blueprint(iot_blueprint, url_prefix='/api')
# Print all registered endpoints
with app.app_context():
for rule in app.url_map.iter_rules():
logging.info(f"Endpoint: {rule.endpoint}, URL: {rule}")
if __name__ == "__main__":
# Set debug mode and port from environment variables
debug_mode = os.getenv('FLASK_DEBUG', 'false').lower() == 'true'
port = int(os.getenv('FLASK_PORT', 4667))
app.run(host='0.0.0.0', port=port, debug=debug_mode)