-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
313 lines (254 loc) · 8.76 KB
/
app.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
# -*- coding: utf-8 -*-
# *****************************************************
# *****************************************************
# WebServer Interface for LinuxCNC system
#
# Usage: app.py <LinuxCNC_INI_file_name>
#
# Provides a web server using normal HTTP/HTTPS communication
# to information about the running LinuxCNC system. Most
# data is transferred to and from the server over a
# WebSocket using JSON formatted commands and replies.
#
#
# *****************************************************
# *****************************************************
#
#
#
from __future__ import print_function
import sys
import os
import socket
import json
import tornado.web
import tornado.websocket
from tornado.httpserver import HTTPServer
from tornado.options import define, options
from tornado.ioloop import IOLoop
import app_db
from app_conf import read_config, settings
import utils
import cnc_agent
import logger
import logging
APP_VERSION = "1.2.0"
define('port', default=8888, help='port to listen on')
cnc_status_poll_period_ms = 500
cnc_error_poll_period_ms = 25
# Application global variables
cnc = None # linuxcnc agent object
db = None # sqlite database object
logged_in = False #
#
class CurrentStatusSender():
def __init__(self, WebSocketHandler):
self.ws = WebSocketHandler
self.prev_status = None
def send(self, status):
message_json = json.dumps(cnc_agent.LinuxCNCWorker.current_status_to_response(status))
self.ws.write_message(message_json)
#
class CurrentErrorsSender():
def __init__(self, WebSocketHandler):
self.ws = WebSocketHandler
def send(self, error):
message_json = json.dumps(error)
logger.debug("CNC Error :" + message_json)
try:
self.ws.write_message(error)
except Exception as ex:
logger.exception(ex)
# Handler for web-socket requests
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def __init__(self, *args, **kwargs):
super(WebSocketHandler, self).__init__( *args, **kwargs )
logger.debug("New websocket Connection...")
self.status_sender = CurrentStatusSender(self)
self.errors_sender = CurrentErrorsSender(self)
self.subprotocol = None
def open(self, arg):
self.stream.socket.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 )
def on_close(self):
logger.debug("WebSocket (%s) closed" % self.subprotocol)
# Remove corresponding observer
if(self.subprotocol == "linuxcnc_status"):
cnc.del_status_observer(self.status_sender.send)
if(self.subprotocol == "linuxcnc_errors"):
cnc.del_errors_observer(self.errors_sender.send)
def on_message(self, message):
logger.debug("GOT message: " + message)
def select_subprotocol(self, subprotocols):
logger.debug("WEBSOCKET Subprotocols: " + subprotocols.__str__())
if ( 'linuxcnc_status' in subprotocols ):
cnc.add_status_observer(self.status_sender.send)
self.subprotocol = "linuxcnc_status"
return self.subprotocol
elif ( 'linuxcnc_errors' in subprotocols ):
cnc.add_errors_observer(self.errors_sender.send)
self.subprotocol = "linuxcnc_errors"
return self.subprotocol
# Tornado 5.1 provides empty list, while 2.3 - list with empty string
elif (subprotocols == []): # some websocket clients don't support subprotocols, so allow this if they just provide an empty string
self.subprotocol = ""
return self.subprotocol
else:
logger.error("WEBSOCKET CLOSED: sub protocol '%s' not supported" % subprotocols.__str__())
self.close()
return None
# Override to enable support for allowing alternate origins
# By default, rejects all requests with an origin on a host other than this one
#def check_origin(self, origin)
# Base request process
class BaseHandler(tornado.web.RequestHandler):
# called no matter which HTTP method is used
def prepare(self):
pass
# outputs HTML for use on error pages.
# If a handler raises an exception, Tornado will call it
def write_error(self):
pass
# called when the client disconnects
def on_connecttion_close(self):
pass
# may be used to set additional headers on the response
def set_default_headers(self):
pass
# Handler for HTML requests
class MainHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
logger.debug("HTTP GET " + args[0])
logger.debug(self.request.headers)
req_uri = args[0].upper()
if (req_uri in [ '', 'INDEX.HTML', 'INDEX.HTM', 'INDEX', 'RETURN_MAINMENU']):
if settings["autologin"] or logged_in:
self.render( 'cnc_main.html', serv_version=APP_VERSION )
else:
self.render( 'cnc_login.html', serv_version=APP_VERSION )
else:
logger.debug("Autologin: " + str(settings["autologin"]) + ", logged_in: " + str(logged_in))
if (req_uri.find(".HTML") == -1):
uri = "cnc_" + args[0] + ".html"
else:
uri = args[0]
if settings["autologin"] or logged_in:
self.render(uri, serv_version=APP_VERSION)
else:
self.render( 'cnc_login.html', serv_version=APP_VERSION )
def post(self, *args, **kwargs):
logger.debug("POST ")
logger.debug(*args)
#data = self.get_argument('data', 'No data received')
#logger.debug(data)
#self.write(data)
# Handler for AJAX requests
class CommandHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
"""Handle ajax get"""
req_uri = ''.join(*args)
logger.debug("GET " + req_uri)
res = cnc.handle_command(req_uri)
self.set_header("Content-Type", "application/json")
self.write(json.dumps(res))
self.finish()
@staticmethod
def handle_login(json):
"""Handle login request"""
res = {"result": {'text': "user check failed", 'code': -10}}
try:
res = db.users.check_user(json["user_name"], json["password"])
if (res["result"]["text"] == "OK"):
global logged_in
logged_in = True
cnc.start_linuxcnc()
except Exception as ex:
logger.exception(ex)
finally:
return res
# TODO
@staticmethod
def handle_logout(json):
"""Handle logout request"""
logged_in = False
#cnc.stop_linuxcnc()
def post(self, *args, **wargs):
"""Handle ajax post"""
req_uri = ''.join(*args)
logger.debug("POST " + req_uri)
req_data = tornado.escape.json_decode(self.request.body)
# Login routine
if req_uri == "login":
res = CommandHandler.handle_login(req_data)
# CNC contol commands
else:
res = cnc.handle_command(req_uri, req_data)
#logger.debug(req_data)
#logger.debug(req_data["param"])
self.set_header("Content-Type", "application/json")
self.write(json.dumps(res))
self.finish()
#
def make_app(app_path):
return tornado.web.Application([
(r"/([^\\/]*)", MainHandler, {}),
(r"/command/(.*)", CommandHandler, {} ),
(r"/websocket/(.*)", WebSocketHandler, {} ),
],
debug=True,
template_path=os.path.join(app_path, "templates"),
static_path=os.path.join(app_path, "static"),
#default_handler_class= 404 page
)
def cnc_errors_handler(error_text_json):
logger.error("CNC Error: %s" % error_text_json)
# auto start if executed from the command line
if __name__ == "__main__":
try:
"""Application config"""
app_path = utils.get_cwd()
read_config(os.path.join(app_path, "app.config"))
# Create application directories
log_path = os.path.join(app_path, "log") # directory for log files
try:
os.makedirs(log_path)
except OSError:
# if not exists already - error
if not os.path.isdir(log_path):
raise
"""Logger setup"""
# Disable tornado log routine, because we will use self-made
hn = logging.NullHandler()
hn.setLevel(logging.DEBUG)
#logging.getLogger("tornado.access").addHandler(hn)
#logging.getLogger("tornado.access").propagate = False
#logging.getLogger("tornado.general").addHandler(hn)
#logging.getLogger("tornado.general").propagate = False
logging.getLogger("tornado.application").addHandler(hn)
logging.getLogger("tornado.application").propagate = False
# Setup self-made logger
log_file = os.path.join(log_path, "cnc_ui.log")
logger.init(level = settings["log_level"],
file_path = log_file,
file_max_size = 4*1048576,
use_console= settings["log_to_console"])
"""Database init"""
db = app_db.Database('my.db')
db.users.add_user("admin", "admin")
"""Construct and serve the tornado application."""
app = make_app(app_path)
http_server = HTTPServer(app)
http_server.listen(options.port)
"""Create periodic polling of CNC"""
cnc = cnc_agent.LinuxCNCWorker(settings["cnc_ini_path"])
cnc_status_scheduler = tornado.ioloop.PeriodicCallback( cnc.poll_status, 100 )
cnc_status_scheduler.start()
cnc_errors_scheduler = tornado.ioloop.PeriodicCallback( cnc.poll_errors, 5 )
cnc_errors_scheduler.start()
if settings["autologin"]:
cnc.start_linuxcnc()
logger.info('Listening on http://%s:%i' % (utils.get_current_ip(), options.port))
# IOLoop.current().start()
IOLoop.instance().start()
except Exception as ex:
logger.exception(ex)