/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
1
import os
346.1.29 by Vasilii Chernov
1. multithreading:
2
import sys
3
346.1.33 by Vasilii Chernov
Move scripts handing code from py.c to Python wrap
4
import pcilib
346.1.29 by Vasilii Chernov
1. multithreading:
5
6
import time
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
7
import json
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
8
from optparse import OptionParser
346.1.30 by Vasilii Chernov
1. api-serer:
9
from multiprocessing import Process
346.1.35 by Vasilii Chernov
Add Python3 support
10
11
if sys.version_info >= (3,0):
12
   from http.server import HTTPServer, BaseHTTPRequestHandler
13
   from socketserver import ThreadingMixIn
14
else:
15
   from SocketServer import ThreadingMixIn
16
   from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
346.1.30 by Vasilii Chernov
1. api-serer:
17
18
class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer):
19
    pass
346.1.22 by Vasilii Chernov
1. Http server add tree view
20
21
class PcilibServerHandler(BaseHTTPRequestHandler):
22
   
346.1.29 by Vasilii Chernov
1. multithreading:
23
   def __init__(s, pcilib, *args):
24
      s.pcilib = pcilib
25
      BaseHTTPRequestHandler.__init__(s, *args)
346.1.17 by Vasilii Chernov
Refactor pcipywrap to object
26
   
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
27
   def do_HEAD(s):
28
      s.send_response(200)
29
      s.send_header('content-type', 'application/json')
30
      s.end_headers()
31
      
32
   def do_GET(s):
346.1.30 by Vasilii Chernov
1. api-serer:
33
      #run request in separate process
34
      p = Process(target=s.do_GET_worker, args=())
35
      p.start()
36
      p.join()
37
      
38
   def do_GET_worker(s):
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
39
      length = int(s.headers['Content-Length'])
40
      
41
      #deserialize input data
42
      data = json.loads(s.rfile.read(length).decode('utf-8'))
43
      
44
      if 'command' in data:
45
         command = data['command']
46
         if(command == 'help'):
47
            s.help(data)
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
48
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
49
            
50
            
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
51
         #elif(command == 'open'):
52
         #   #check required arguments
53
         #   if not 'device' in data:
54
         #      s.error('message doesnt contains "device" field, '
55
         #              'which is required for "open" command', data)
56
         #      return
57
         #   #parse command arguments and convert them to string
58
         #   device = str(data.get('device', None))
59
         #   model = data.get('model', None)
60
         #   if not model is None:
61
			#	model = str(model)
62
         #   
63
         #   try:
64
         #      s.openPcilibInstance(device, model)
65
         #   except Exception as e:
66
         #      s.error(str(e), data) 
67
         #     return
68
		   #
69
         #   #Success! Create and send reply
70
         #   out = dict()
71
         #   out['status'] = 'ok'
72
         #   s.wrapMessageAndSend(out, data)
73
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
74
            
75
            
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
76
         elif(command == 'get_registers_list'):
77
            #parse command arguments and convert them to string
78
            bank = data.get('bank', None)
79
            if not bank is None:
80
               bank = str(bank)
81
82
            registers = dict()
83
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
84
               registers = s.pcilib.get_registers_list(bank)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
85
            except Exception as e:
86
               s.error(str(e), data) 
87
               return
88
               
89
            #Success! Create and send reply
90
            out = dict()
91
            out['status'] = 'ok'
92
            out['registers'] = registers
93
            s.wrapMessageAndSend(out, data)
346.1.22 by Vasilii Chernov
1. Http server add tree view
94
          
95
          
96
          
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
97
         elif(command == 'get_register_info'):
98
            #check required arguments
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
99
            if not 'reg' in data:
100
               s.error('message doesnt contains "reg" field, '
101
                       'which is required for "get_register_info" command', data)
102
               return
103
               
104
            #parse command arguments and convert them to string
105
            reg = str(data.get('reg', None))
106
            bank = data.get('bank', None)
107
            if not bank is None:
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
108
               bank = str(bank)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
109
            
110
            register = dict()
111
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
112
               register = s.pcilib.get_register_info(reg, bank)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
113
            except Exception as e:
114
               s.error(str(e), data) 
115
               return
116
		
117
            #Success! Create and send reply
346.1.22 by Vasilii Chernov
1. Http server add tree view
118
            s.wrapMessageAndSend({'status': 'ok', 'register': register}, data)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
119
		 
346.1.22 by Vasilii Chernov
1. Http server add tree view
120
       
121
       
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
122
         elif(command == 'get_property_list'):   
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
123
            #parse command arguments and convert them to string
124
            branch = data.get('branch', None)
125
            if not branch is None:
346.1.13 by Vasilii Chernov
Fix memory leaks
126
               branch = str(branch)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
127
            
128
            properties = dict()
129
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
130
               properties = s.pcilib.get_property_list(branch)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
131
            except Exception as e:
132
               s.error(str(e), data) 
133
               return
134
            	
135
            #Success! Create and send reply
136
            out = dict()
137
            out['status'] = 'ok'
138
            out['properties'] = properties
139
            s.wrapMessageAndSend(out, data)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
140
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
141
            
142
            
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
143
         elif(command == 'read_register'):
144
            #check required arguments
145
            if not 'reg' in data:
146
               s.error('message doesnt contains "reg" field, '
147
                       'which is required for "read_register" command', data)
148
               return
149
               
150
            #parse command arguments and convert them to string
151
            reg = str(data.get('reg', None))
152
            bank = data.get('bank', None)
346.1.35 by Vasilii Chernov
Add Python3 support
153
            if(not bank is None):
154
               bank = str(bank)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
155
            
156
            value = 0
157
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
158
               value = s.pcilib.read_register(reg, bank)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
159
            except Exception as e:
160
               s.error(str(e), data) 
161
               return
162
            
163
            #Success! Create and send reply
164
            out = dict()
165
            out['status'] = 'ok'
166
            out['value'] = value
167
            s.wrapMessageAndSend(out, data)
168
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
169
            
170
            
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
171
         elif(command == 'write_register'):
172
            #check required arguments
173
            if not 'reg' in data:
174
               s.error('message doesnt contains "reg" field, '
175
                       'which is required for "write_register" command', data)
176
               return
177
               
178
            if not 'value' in data:
179
               s.error('message doesnt contains "value" field, '
180
                       'which is required for "write_register" command', data)
181
               return
182
               
183
            #parse command arguments and convert them to string
184
            reg = str(data.get('reg', None))
346.1.35 by Vasilii Chernov
Add Python3 support
185
            value = data.get('value', None)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
186
            bank = data.get('bank', None)
346.1.35 by Vasilii Chernov
Add Python3 support
187
            if(not bank is None):
188
               bank = str(bank)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
189
            
190
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
191
               s.pcilib.write_register(value, reg, bank)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
192
            except Exception as e:
193
               s.error(str(e), data) 
194
               return
195
            
196
            #Success! Create and send reply
346.1.22 by Vasilii Chernov
1. Http server add tree view
197
            s.wrapMessageAndSend({'status': 'ok'}, data)
198
            
199
            
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
200
            
201
         elif(command == 'get_property'):
202
            #check required arguments
203
            if not 'prop' in data:
204
               s.error('message doesnt contains "prop" field, '
205
                       'which is required for "get_property" command', data)
206
               return
207
               
208
            #parse command arguments and convert them to string
209
            prop = str(data.get('prop', None))
210
            
211
            value = 0
212
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
213
               value = s.pcilib.get_property(prop)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
214
            except Exception as e:
215
               s.error(str(e), data) 
216
               return
217
            
218
            #Success! Create and send reply
219
            out = dict()
220
            out['status'] = 'ok'
221
            out['value'] = value
222
            s.wrapMessageAndSend(out, data)
223
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
224
            
225
            
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
226
         elif(command == 'set_property'):
227
            #check required arguments
228
            if not 'prop' in data:
229
               s.error('message doesnt contains "prop" field, '
230
                       'which is required for "set_property" command', data)
231
               return
232
               
233
            if not 'value' in data:
234
               s.error('message doesnt contains "value" field, '
235
                       'which is required for "set_property" command', data)
236
               return
346.1.35 by Vasilii Chernov
Add Python3 support
237
            
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
238
            #parse command arguments and convert them to string
239
            prop = str(data.get('prop', None))
346.1.35 by Vasilii Chernov
Add Python3 support
240
            value = data.get('value', None)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
241
            
242
            try:
346.1.29 by Vasilii Chernov
1. multithreading:
243
               s.pcilib.set_property(value, prop)
346.1.22 by Vasilii Chernov
1. Http server add tree view
244
            except Exception as e:
245
               s.error(str(e), data) 
246
               return
247
            
248
            #Success! Create and send reply
249
            s.wrapMessageAndSend({'status': 'ok'}, data)
250
            
251
            
252
            
253
         elif(command == 'lock'):
254
            #check required arguments
255
            if not 'lock_id' in data:
256
               s.error('message doesnt contains "lock_id" field, '
257
                       'which is required for "lock" command', data)
258
               return
259
               
260
            #parse command arguments and convert them to string
261
            lock_id = str(data.get('lock_id'))
262
            
263
            try:
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
264
               s.pcilib.lock_persistent(lock_id)
346.1.22 by Vasilii Chernov
1. Http server add tree view
265
            except Exception as e:
266
               s.error(str(e), data) 
267
               return
268
            
269
            #Success! Create and send reply
270
            s.wrapMessageAndSend({'status': 'ok'}, data)
271
            
272
            
273
            
274
         elif(command == 'try_lock'):
275
            #check required arguments
276
            if not 'lock_id' in data:
277
               s.error('message doesnt contains "lock_id" field, '
278
                       'which is required for "try_lock" command', data)
279
               return
280
               
281
            #parse command arguments and convert them to string
282
            lock_id = str(data.get('lock_id'))
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
283
            persistent = bool(data.get('persistent', False))
346.1.22 by Vasilii Chernov
1. Http server add tree view
284
            
285
            try:
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
286
               if persistent:
287
                  s.pcilib.try_lock_persistent(lock_id)
288
               else:
289
                  s.pcilib.try_lock(lock_id)
346.1.22 by Vasilii Chernov
1. Http server add tree view
290
            except Exception as e:
291
               s.error(str(e), data) 
292
               return
293
            
294
            #Success! Create and send reply
295
            s.wrapMessageAndSend({'status': 'ok'}, data)
296
            
297
            
298
            
299
         elif(command == 'unlock'):
300
            #check required arguments
346.1.29 by Vasilii Chernov
1. multithreading:
301
            if not 'lock_id' in data:
302
               s.error('message doesnt contains "lock_id" field, '
303
                       'which is required for "unlock" command', data)
304
               return
346.1.22 by Vasilii Chernov
1. Http server add tree view
305
               
306
            #parse command arguments and convert them to string
346.1.29 by Vasilii Chernov
1. multithreading:
307
            lock_id = str(data.get('lock_id'))
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
308
            persistent = bool(data.get('persistent', False))
346.1.22 by Vasilii Chernov
1. Http server add tree view
309
            
346.1.29 by Vasilii Chernov
1. multithreading:
310
            try:
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
311
               if persistent:
312
                  s.pcilib.unlock_persistent(lock_id)
313
               else:
314
                  s.pcilib.unlock(lock_id)
346.1.29 by Vasilii Chernov
1. multithreading:
315
            except Exception as e:
316
               s.error(str(e), data) 
317
               return
318
346.1.22 by Vasilii Chernov
1. Http server add tree view
319
            #Success! Create and send reply
320
            s.wrapMessageAndSend({'status': 'ok'}, data)
321
            
322
            
323
            
346.1.30 by Vasilii Chernov
1. api-serer:
324
         elif(command == 'get_scripts_list'):
325
            scripts = list()
326
            try:
327
               scripts = s.pcilib.get_scripts_list()
328
            except Exception as e:
329
               s.error(str(e), data) 
330
               return
331
332
            #Success! Create and send reply
333
            s.wrapMessageAndSend({'status': 'ok', 'scripts': scripts}, data)
334
         
335
         
336
         
337
         elif(command == 'run_script'):
338
            #check required arguments
339
            if not 'script_name' in data:
340
               s.error('message doesnt contains "script_name" field, '
341
                       'which is required for "run_script" command', data)
342
               return
343
            #parse command arguments and convert them to string
344
            script_name = str(data.get('script_name'))
345
            value = data.get('value', None)
346
            
347
            out = None
348
            try:
349
               out = s.pcilib.run_script(script_name, value)
350
            except Exception as e:
351
               s.error(str(e), data) 
352
               return
353
354
            #Success! Create and send reply
346.1.35 by Vasilii Chernov
Add Python3 support
355
            if(type(out) == bytearray or type(out) == bytes):
346.1.30 by Vasilii Chernov
1. api-serer:
356
               s.send_response(200)
357
               s.send_header('content-disposition', 'inline; filename=value')
358
               s.send_header('content-type', 'application/octet-stream')
359
               s.end_headers()
360
               s.wfile.write(out)
361
            else:
362
               s.wrapMessageAndSend({'status': 'ok', 'value': out}, data)
363
            
364
            
365
            
346.1.22 by Vasilii Chernov
1. Http server add tree view
366
         #elif(command == 'lock_global'):
367
         #   #check if global_lock already setted by server
368
         #   try:
346.1.29 by Vasilii Chernov
1. multithreading:
369
         #      s.pcilib.lock_global()
346.1.22 by Vasilii Chernov
1. Http server add tree view
370
         #   except Exception as e:
371
         #      s.error(str(e), data)
372
         #      return
373
         #   
374
         #   #Success! Create and send reply
375
         #   s.wrapMessageAndSend({'status': 'ok'}, data)
376
            
377
         
378
         
379
         #elif(command == 'unlock_global'):
380
         #   try:
346.1.29 by Vasilii Chernov
1. multithreading:
381
         #      s.pcilib.unlock_global()
346.1.22 by Vasilii Chernov
1. Http server add tree view
382
         #   except Exception as e:
383
         #      s.error(str(e), data)
384
         #      return
385
         #   
386
         #   #Success! Create and send reply
387
         #   s.wrapMessageAndSend({'status': 'ok'}, data)
388
         
346.1.35 by Vasilii Chernov
Add Python3 support
389
      
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
390
         else:
346.1.35 by Vasilii Chernov
Add Python3 support
391
            s.error('command "' + command + '" undefined', data)
392
            return
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
393
      else:
346.1.35 by Vasilii Chernov
Add Python3 support
394
         s.error('message doesnt contains "command" field, which is required', data)
395
         return
346.1.30 by Vasilii Chernov
1. api-serer:
396
        
397
        
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
398
      
346.1.29 by Vasilii Chernov
1. multithreading:
399
   #open device context 
346.1.22 by Vasilii Chernov
1. Http server add tree view
400
   #def openPcilibInstance(s, device, model):
346.1.29 by Vasilii Chernov
1. multithreading:
401
   #   s.pcilib = pcipywrap.create_pcilib_instance(device, model)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
402
         
346.1.29 by Vasilii Chernov
1. multithreading:
403
   #Send help message
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
404
   def help(s, received_message = None):
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
405
      usage = str('Usage:\n'
406
      '  Server receive commands via http GET with json packet.\n'
407
      '  content-type should have value "application/json"\n'
408
      '  Server could handle only commands. to set command, you\n'
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
409
      '  should specify field "command" in packet with command name\n\n'
410
      '  If you use html server, you should call commands via GET request\n'
411
      '  like http://<server_host>:<port>/json/<command>?<arg1>=<arg_val1>&... \n'
412
      '\n'
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
413
      '  List of commands:\n'
414
      '\n'
415
      '  command: help - Get help. This will return usage\n'
416
      '\n'
417
      
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
418
      #'  command: open - Opens context of device. It will be reopened if already open.\n'
419
      #'    required fields\n'
420
      #'      device:       - path to the device file [/dev/fpga0]\n'
421
      #'    optional fields\n'
422
      #'      model:       - specifies the model of hardware, autodetected if doesnt exists\n'
423
      #'\n'
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
424
      
425
      '  command: get_registers_list - Returns the list of registers provided by the hardware model.\n'
426
      '    optional fields\n'
427
      '      bank:        - if set, only register within the specified bank will be returned\n'
428
      '\n'
429
      
430
      '  command: get_register_info - Returns the information about the specified register.\n'
431
      '    required fields\n'
432
      '      reg:         - the name of the register\n'
433
      '    optional fields\n'
434
      '      bank:        - if set, only register within the specified bank will be returned\n'
435
      '\n'
436
      
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
437
      '  command: get_property_list - Returns the list of properties available under the specified path.\n'
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
438
      '    optional fields\n'
439
      '     branch:        - Path. If not set, will return the top-level properties\n'
440
      '\n'
441
      
442
      '  command: read_register - Reads the specified register.\n'
443
      '    required fields\n'
444
      '      reg:         - the name of the register\n'
445
      '    optional fields\n'
446
      '      bank:        - if set, only register within the specified bank will be processed\n'
447
      '\n'
448
      
449
      '  command: write_register - Writes to specified register.\n'
450
      '    required fields\n'
451
      '      reg:         - the name of the register\n'
452
      '      value:       - the register value to write. Should be int, float or string (with number)\n'
453
      '    optional fields\n'
454
      '      bank:        - if set, only register within the specified bank will be processed\n'
455
      '\n'
456
      
457
      '  command: get_property - Reads / computes the property value.\n'
458
      '    required fields\n'
459
      '      prop:         - full name including path\n'
460
      '\n'
461
      
462
      '  command: set_property - Writes the property value or executes the code associated with property.\n'
463
      '    required fields\n'
464
      '      prop:        - full name including path\n'
465
      '      value:       - the property value to write. Should be int, float or string (with number)\n'
346.1.22 by Vasilii Chernov
1. Http server add tree view
466
      '\n'
467
      
468
      '  command: lock - function to acquire a lock, and wait till the lock can be acquire.\n'
469
      '    required fields\n'
470
      '      lock_id: - lock id\n'
471
      '\n'
472
      
473
      '  command: try_lock - this function will try to take a lock for the mutex pointed by \n'
474
      '                    lockfunction to acquire a lock, but that returns immediatly if the\n'
475
      '                    lock can\'t be acquired on first try\n'
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
476
      '    required fields\n'
346.1.22 by Vasilii Chernov
1. Http server add tree view
477
      '      lock_id: - lock id\n'
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
478
      '    optional fields\n'
479
      '      persistent: - 1 - lock is persistent, 0 othervise (default: 0)\n'
346.1.22 by Vasilii Chernov
1. Http server add tree view
480
      '\n'
481
      
482
      '  command: unlock - this function unlocks the lock.\n'
483
      '    required fields\n'
484
      '      lock_id: - lock id\n'
346.1.37 by Vasilii Chernov
1. Pcipywrap: add persistent locking wrappings
485
      '    optional fields\n'
486
      '      persistent: - 1 - lock is persistent, 0 othervise (default: 0)\n'
346.1.22 by Vasilii Chernov
1. Http server add tree view
487
      '\n'
488
      
346.1.30 by Vasilii Chernov
1. api-serer:
489
      '  command: get_scripts_list - Get aviable scripts with description\n'
490
      '\n'
491
      
492
      '  command: run_script - Run specified script\n'
493
      '    required fields\n'
346.1.33 by Vasilii Chernov
Move scripts handing code from py.c to Python wrap
494
      '      script_name: - script name (without extension)\n'
346.1.30 by Vasilii Chernov
1. api-serer:
495
      '      value: - input value in json format\n'
496
      '\n'
497
      
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
498
      '\n')
346.1.30 by Vasilii Chernov
1. api-serer:
499
      
500
      #send help as plain text
501
      s.send_response(200)
502
      s.send_header('content-type', 'text/plain')
503
      s.end_headers()
346.1.35 by Vasilii Chernov
Add Python3 support
504
      if sys.version_info >= (3,0):
505
         s.wfile.write(bytes(usage, 'UTF-8'))
506
      else:
507
         s.wfile.write(usage)
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
508
346.1.29 by Vasilii Chernov
1. multithreading:
509
   #Send error message with text description    
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
510
   def error(s, info, received_message = None):
511
      out = dict()
512
      
513
      out['status'] = 'error'
514
      out['description'] = info
515
      out['note'] = 'send {"command" : "help"} to get help'
346.1.22 by Vasilii Chernov
1. Http server add tree view
516
      s.wrapMessageAndSend(out, received_message, 400)
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
517
        
346.1.22 by Vasilii Chernov
1. Http server add tree view
518
   def wrapMessageAndSend(s, message, received_message = None, response = 200):
519
      s.send_response(response)
520
      s.send_header('content-type', 'application/json')
521
      s.end_headers()
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
522
      if not received_message is None:
523
         message['received_message'] = received_message
346.1.35 by Vasilii Chernov
Add Python3 support
524
      if sys.version_info >= (3,0):
525
         s.wfile.write(bytes(json.dumps(message), 'UTF-8'))
526
      else:
527
         s.wfile.write(json.dumps(message))
528
      
529
      
346.1.30 by Vasilii Chernov
1. api-serer:
530
class ApiServer(MultiThreadedHTTPServer):
531
   def __init__(self, device='/dev/fpga0', model=None, adress=('0.0.0.0', 9000)):
532
      #redirect logs to exeption
346.1.33 by Vasilii Chernov
Move scripts handing code from py.c to Python wrap
533
      pcilib.redirect_logs_to_exeption()
346.1.30 by Vasilii Chernov
1. api-serer:
534
      #pass Pcipywrap to to server handler
367 by Suren A. Chilingaryan
Further improvements of Python scripting and web-interface API for register manipulations by Vasiliy Chernov
535
      self.lib = pcilib.pcilib(device, model)
346.1.30 by Vasilii Chernov
1. api-serer:
536
      def handler(*args):
537
         PcilibServerHandler(self.lib, *args)
538
      MultiThreadedHTTPServer.__init__(self, adress, handler)
346.1.29 by Vasilii Chernov
1. multithreading:
539
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
540
if __name__ == '__main__':
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
541
   
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
542
   #parce command line options
543
   parser = OptionParser()
544
   parser.add_option("-p", "--port",  action="store",
545
                     type="int", dest="port", default=9000,
346.1.17 by Vasilii Chernov
Refactor pcipywrap to object
546
                     help="Set server port (9000)")
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
547
   parser.add_option("-d", "--device",  action="store",
548
                     type="string", dest="device", default=str('/dev/fpga0'),
549
                     help="FPGA device (/dev/fpga0)")                     
550
   parser.add_option("-m", "--model",  action="store",
551
                     type="string", dest="model", default=None,
552
                     help="Memory model (autodetected)")
346.1.30 by Vasilii Chernov
1. api-serer:
553
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
554
   opts = parser.parse_args()[0]
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
555
   
346.1.30 by Vasilii Chernov
1. api-serer:
556
   HOST_NAME = '0.0.0.0'
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
557
   PORT_NUMBER = opts.port
558
   MODEL = opts.model
559
   DEVICE = opts.device
346.1.10 by Vasilii Chernov
1. Cmakelists - move copy xml folder command to root file
560
   
346.1.14 by Vasilii Chernov
Add options parsing to server\n Rename get_property_info for get_property_list
561
   #start server
346.1.30 by Vasilii Chernov
1. api-serer:
562
   httpd = ApiServer(DEVICE, MODEL, (HOST_NAME, PORT_NUMBER))
346.1.17 by Vasilii Chernov
Refactor pcipywrap to object
563
   
346.1.35 by Vasilii Chernov
Add Python3 support
564
   print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER))
346.1.30 by Vasilii Chernov
1. api-serer:
565
   
566
   try:
567
      httpd.serve_forever()
568
   except KeyboardInterrupt:
569
      pass
346.1.22 by Vasilii Chernov
1. Http server add tree view
570
      
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
571
   httpd.server_close()
346.1.30 by Vasilii Chernov
1. api-serer:
572
   
346.1.35 by Vasilii Chernov
Add Python3 support
573
   print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER))