forked from johannesPettersson80/codesys-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_server.py
More file actions
118 lines (100 loc) · 4.34 KB
/
debug_server.py
File metadata and controls
118 lines (100 loc) · 4.34 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
CODESYS API Debug Server
This script runs the HTTP server with detailed console output for debugging.
It helps diagnose issues with CODESYS API integration.
"""
import sys
import os
import logging
import time
import HTTP_SERVER
# Configure logging to output to console
logging.basicConfig(
level=logging.DEBUG, # Set to DEBUG for detailed output
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout), # Log to console
logging.FileHandler('debug_server.log') # Also log to file
]
)
# Set logger levels for different components
logging.getLogger('codesys_api_server').setLevel(logging.DEBUG)
logger = logging.getLogger('debug_server')
def run_debug_server():
"""Run the HTTP server with debug output."""
try:
# Print system information
logger.info("=== CODESYS API Debug Server ===")
logger.info("Python version: %s", sys.version)
logger.info("Current directory: %s", os.getcwd())
# Print server configuration
logger.info("Server configuration:")
logger.info(" HOST: %s", HTTP_SERVER.SERVER_HOST)
logger.info(" PORT: %s", HTTP_SERVER.SERVER_PORT)
logger.info(" CODESYS_PATH: %s", HTTP_SERVER.CODESYS_PATH)
logger.info(" PERSISTENT_SCRIPT: %s", HTTP_SERVER.PERSISTENT_SCRIPT)
logger.info(" REQUEST_DIR: %s", HTTP_SERVER.REQUEST_DIR)
logger.info(" RESULT_DIR: %s", HTTP_SERVER.RESULT_DIR)
# Verify directories exist
for directory in [HTTP_SERVER.REQUEST_DIR, HTTP_SERVER.RESULT_DIR]:
if os.path.exists(directory):
logger.info("Directory exists: %s", directory)
else:
logger.warning("Directory does not exist: %s", directory)
try:
os.makedirs(directory)
logger.info("Created directory: %s", directory)
except Exception as e:
logger.error("Failed to create directory: %s", str(e))
# Check CODESYS executable
if os.path.exists(HTTP_SERVER.CODESYS_PATH):
logger.info("CODESYS executable found: %s", HTTP_SERVER.CODESYS_PATH)
else:
logger.error("CODESYS executable NOT found: %s", HTTP_SERVER.CODESYS_PATH)
# Check PERSISTENT_SESSION script
if os.path.exists(HTTP_SERVER.PERSISTENT_SCRIPT):
logger.info("PERSISTENT_SCRIPT found: %s", HTTP_SERVER.PERSISTENT_SCRIPT)
else:
logger.error("PERSISTENT_SCRIPT NOT found: %s", HTTP_SERVER.PERSISTENT_SCRIPT)
# Create managers
process_manager = HTTP_SERVER.CodesysProcessManager(
HTTP_SERVER.CODESYS_PATH,
HTTP_SERVER.PERSISTENT_SCRIPT
)
script_executor = HTTP_SERVER.ScriptExecutor(
HTTP_SERVER.REQUEST_DIR,
HTTP_SERVER.RESULT_DIR
)
script_generator = HTTP_SERVER.ScriptGenerator()
api_key_manager = HTTP_SERVER.ApiKeyManager(HTTP_SERVER.API_KEY_FILE)
# Create server
from http.server import HTTPServer
def handler(*args):
return HTTP_SERVER.CodesysApiHandler(
process_manager=process_manager,
script_executor=script_executor,
script_generator=script_generator,
api_key_manager=api_key_manager,
*args
)
server = HTTPServer((HTTP_SERVER.SERVER_HOST, HTTP_SERVER.SERVER_PORT), handler)
logger.info("Starting server on %s:%d", HTTP_SERVER.SERVER_HOST, HTTP_SERVER.SERVER_PORT)
logger.info("Press Ctrl+C to stop server")
# Start server
server.serve_forever()
except KeyboardInterrupt:
logger.info("Server stopped by user")
except Exception as e:
logger.error("Error starting server: %s", str(e), exc_info=True)
finally:
# Cleanup
logger.info("Cleaning up...")
try:
process_manager.stop()
logger.info("CODESYS process stopped")
except:
pass
if __name__ == "__main__":
run_debug_server()