-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOLTwi_server.py
More file actions
executable file
·133 lines (103 loc) · 3.99 KB
/
OLTwi_server.py
File metadata and controls
executable file
·133 lines (103 loc) · 3.99 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, jsonify, Response, abort
import calendar, time, xmlrpclib
from OLT_config_parser import get_config, get_config_by_id
from flask.ext.cache import Cache
app = Flask(__name__)
cache = Cache(app,config={'CACHE_TYPE': 'memcached'})
def get_xmlrpc_server():
device_id = request.args['device_id']
device_config = get_config_by_id( get_cluster_config_obj(), device_id )
address = device_config['ip']
return xmlrpclib.ServerProxy( 'http://' + address )
def get_UI_config_obj():
device_id = request.args['device_id']
device_config = get_config_by_id( get_cluster_config_obj(), device_id )
UI_config = get_config_by_fn( device_config['config_file'] )
return UI_config
def get_cluster_config_obj():
return get_config_by_fn(app.config['cluster_config_fn'])
@cache.memoize(60, unless=(lambda: app.debug))
def get_config_by_fn(fn):
return get_config(fn)
def xmlrpc_call( elem_args, extra_args, fast_update=False ):
xmlrpc_server = get_xmlrpc_server()
if 'args' in elem_args.keys():
args = elem_args['args']
if type( args ) is not list:
args = [args]
else: args = []
if type( extra_args ) is not list:
extra_args = [extra_args]
args = tuple(args + extra_args)
func = elem_args['func']
@cache.memoize(1, unless = (lambda: fast_update) )
def cachable( func, args ):
"""This function construct allow caching by matching func, args"""
f = getattr(xmlrpc_server, func)
retval = None
try:
retval = f( *args )
except:
print "Unexpected error:", sys.exc_info()[0]
abort(500)
return retval
return cachable( func, args )
@app.route( '/get_point' )
@app.route( '/get_image' )
@app.route( '/button_click' )
def button_click():
button_id = request.args.get("id")
extra_args = request.args.get( "extra_args", [] )
elem_args = get_config_by_id( get_UI_config_obj(), button_id )
if request.path == '/button_click':
retval = xmlrpc_call( elem_args, extra_args )
return jsonify( state = retval )
elif request.path == '/get_image':
retval = xmlrpc_call( elem_args, extra_args, fast_update = True )
cache_key = 'last_image' + button_id
print cache_key
cache.set(cache_key, retval)
return Response( retval.data, mimetype='image/jpeg' )
elif request.path == '/get_point':
retval = xmlrpc_call( elem_args, extra_args )
return jsonify(
time = calendar.timegm(time.localtime()),
data = retval )
else: abort(404)
@app.route( '/save_image' )
def save_image():
elem_id = request.args.get("id")
cache_key = 'last_image' + elem_id
print cache_key
img_data = cache.get('last_image' + elem_id).data
return Response(img_data, mimetype='image/jpeg')
@app.template_filter('debug')
def debug(x):
"""print content to console from jinja2 templates."""
print x
return ""
@app.route('/')
@app.route('/html_gen')
def html_gen():
if 'device_id' in request.args.keys():
device_id = request.args['device_id']
device_config = get_config_by_id( get_cluster_config_obj(), device_id )
UI_config = get_config_by_fn( device_config['config_file'] )
template_name = ['web_interface.html']
return render_template( template_name,
UI_config = UI_config,
cluster_config = get_cluster_config_obj(),
device_id = device_id,
current_device_name = device_config['name'] )
else:
return render_template( "device_picker.html",
cluster_config = get_cluster_config_obj())
if __name__ == "__main__":
import sys
if len(sys.argv) == 2: cluster_config_fn = sys.argv[1]
else: cluster_config_fn = './examples/OLT_cluster_config.ini'
app.config.update( dict( cluster_config_fn=cluster_config_fn ))
app.debug = True
app.run(host='0.0.0.0', port=80)