2
import os #delete later
7
class PcilibServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
11
s.send_header('content-type', 'application/json')
15
length = int(s.headers['Content-Length'])
17
#deserialize input data
18
data = json.loads(s.rfile.read(length).decode('utf-8'))
21
command = data['command']
22
if(command == 'help'):
25
elif(command == 'open'):
26
#check required arguments
27
if not 'device' in data:
28
s.error('message doesnt contains "device" field, '
29
'which is required for "open" command', data)
31
#parse command arguments and convert them to string
32
device = str(data.get('device', None))
33
model = data.get('model', None)
38
s.openPcilibInstance(device, model)
39
except Exception as e:
43
#Success! Create and send reply
45
s.send_header('content-type', 'application/json')
49
s.wrapMessageAndSend(out, data)
51
elif(command == 'get_registers_list'):
52
#parse command arguments and convert them to string
53
bank = data.get('bank', None)
59
registers = pcipywrap.get_registers_list(bank)
60
except Exception as e:
64
#Success! Create and send reply
66
s.send_header('content-type', 'application/json')
70
out['registers'] = registers
71
s.wrapMessageAndSend(out, data)
73
elif(command == 'get_register_info'):
74
#check required arguments
76
s.error('message doesnt contains "reg" field, '
77
'which is required for "get_register_info" command', data)
80
#parse command arguments and convert them to string
81
reg = str(data.get('reg', None))
82
bank = data.get('bank', None)
88
register = pcipywrap.get_register_info(reg, bank)
89
except Exception as e:
93
#Success! Create and send reply
95
s.send_header('content-type', 'application/json')
99
out['register'] = register
100
s.wrapMessageAndSend(out, data)
102
elif(command == 'get_property_info'):
103
#parse command arguments and convert them to string
104
branch = data.get('branch', None)
105
if not branch is None:
110
properties = pcipywrap.get_property_info(branch)
111
except Exception as e:
112
s.error(str(e), data)
115
#Success! Create and send reply
117
s.send_header('content-type', 'application/json')
121
out['properties'] = properties
122
s.wrapMessageAndSend(out, data)
125
s.error('command "' + command + '" undefined', data)
128
s.error('message doesnt contains "command" field, which is required', data)
132
#print str(s.headers['content-type'])
133
#print post_data['some']
135
"""open device context """
136
def openPcilibInstance(s, device, model):
137
pcipywrap.closeCurrentPcilibInstance()
139
lib = pcipywrap.createPcilibInstance(device, model)
140
pcipywrap.setPcilib(lib)
142
"""Send help message"""
143
def help(s, received_message = None):
145
s.send_header('content-type', 'application/json')
147
out = {'status': 'ok', 'help' : 'under construction'}
148
s.wrapMessageAndSend(out, received_message)
150
"""Send error message with text description"""
151
def error(s, info, received_message = None):
153
s.send_header('content-type', 'application/json')
157
out['status'] = 'error'
158
out['description'] = info
159
out['note'] = 'send {"command" : "help"} to get help'
160
s.wrapMessageAndSend(out, received_message)
162
def wrapMessageAndSend(s, message, received_message = None):
163
if not received_message is None:
164
message['received_message'] = received_message
165
s.wfile.write(json.dumps(message))
167
HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
168
PORT_NUMBER = 12412 # Maybe set this to 9000.
170
if __name__ == '__main__':
171
#initialize variables test (to remove)
172
os.environ["APP_PATH"] = '/home/vchernov/1215N/pcitool'
173
os.environ["PCILIB_MODEL_DIR"] = os.environ["APP_PATH"] + "/xml"
174
os.environ["LD_LIBRARY_PATH"] = os.environ["APP_PATH"] + "/pcilib"
176
pcilib_server = BaseHTTPServer.HTTPServer
177
httpd = pcilib_server((HOST_NAME, PORT_NUMBER), PcilibServerHandler)
178
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
180
httpd.serve_forever()
181
except KeyboardInterrupt:
184
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)