/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
346.1.9 by Vasilii Chernov
Change no_set_check parameter name. Move Python wrap to separate directory.
1
import time
2
import os #delete later
3
import pcipywrap
4
import json
5
import BaseHTTPServer
6
7
class PcilibServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
8
	
9
   def do_HEAD(s):
10
      s.send_response(200)
11
      s.send_header('content-type', 'application/json')
12
      s.end_headers()
13
      
14
   def do_GET(s):
15
      length = int(s.headers['Content-Length'])
16
      
17
      #deserialize input data
18
      data = json.loads(s.rfile.read(length).decode('utf-8'))
19
      
20
      if 'command' in data:
21
         command = data['command']
22
         if(command == 'help'):
23
            s.help(data)
24
                     
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)
30
               return
31
            #parse command arguments and convert them to string
32
            device = str(data.get('device', None))
33
            model = data.get('model', None)
34
            if not model is None:
35
				model = str(model)
36
            
37
            try:
38
               s.openPcilibInstance(device, model)
39
            except Exception as e:
40
               s.error(str(e), data) 
41
               return
42
		
43
            #Success! Create and send reply
44
            s.send_response(200)
45
            s.send_header('content-type', 'application/json')
46
            s.end_headers()
47
            out = dict()
48
            out['status'] = 'ok'
49
            s.wrapMessageAndSend(out, data)
50
                   
51
         elif(command == 'get_registers_list'):
52
            #parse command arguments and convert them to string
53
            bank = data.get('bank', None)
54
            if not bank is None:
55
               bank = str(bank)
56
57
            registers = dict()
58
            try:
59
               registers = pcipywrap.get_registers_list(bank)
60
            except Exception as e:
61
               s.error(str(e), data) 
62
               return
63
               
64
            #Success! Create and send reply
65
            s.send_response(200)
66
            s.send_header('content-type', 'application/json')
67
            s.end_headers()
68
            out = dict()
69
            out['status'] = 'ok'
70
            out['registers'] = registers
71
            s.wrapMessageAndSend(out, data)
72
            
73
       	 elif(command == 'get_register_info'):
74
       	    #check required arguments
75
            if not 'reg' in data:
76
               s.error('message doesnt contains "reg" field, '
77
                       'which is required for "get_register_info" command', data)
78
               return
79
               
80
            #parse command arguments and convert them to string
81
            reg = str(data.get('reg', None))
82
            bank = data.get('bank', None)
83
            if not bank is None:
84
				bank = str(bank)
85
            
86
            register = dict()
87
            try:
88
               register = pcipywrap.get_register_info(reg, bank)
89
            except Exception as e:
90
               s.error(str(e), data) 
91
               return
92
		
93
            #Success! Create and send reply
94
            s.send_response(200)
95
            s.send_header('content-type', 'application/json')
96
            s.end_headers()
97
            out = dict()
98
            out['status'] = 'ok'
99
            out['register'] = register
100
            s.wrapMessageAndSend(out, data)
101
		 
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:
106
				branch = str(bank)
107
            
108
            properties = dict()
109
            try:
110
               properties = pcipywrap.get_property_info(branch)
111
            except Exception as e:
112
               s.error(str(e), data) 
113
               return
114
            	
115
            #Success! Create and send reply
116
            s.send_response(200)
117
            s.send_header('content-type', 'application/json')
118
            s.end_headers()
119
            out = dict()
120
            out['status'] = 'ok'
121
            out['properties'] = properties
122
            s.wrapMessageAndSend(out, data)
123
            	
124
         else:
125
		    s.error('command "' + command + '" undefined', data)
126
		    return
127
      else:
128
		  s.error('message doesnt contains "command" field, which is required', data)
129
		  return
130
		  
131
       
132
      #print str(s.headers['content-type'])
133
      #print post_data['some']
134
      
135
   """open device context """
136
   def openPcilibInstance(s, device, model):
137
      pcipywrap.closeCurrentPcilibInstance()
138
      
139
      lib = pcipywrap.createPcilibInstance(device, model)
140
      pcipywrap.setPcilib(lib)
141
         
142
   """Send help message"""
143
   def help(s, received_message = None):
144
      s.send_response(200)
145
      s.send_header('content-type', 'application/json')
146
      s.end_headers()
147
      out = {'status': 'ok', 'help' : 'under construction'}
148
      s.wrapMessageAndSend(out, received_message)
149
   
150
   """Send error message with text description"""     
151
   def error(s, info, received_message = None):
152
      s.send_response(400)
153
      s.send_header('content-type', 'application/json')
154
      s.end_headers()
155
      out = dict()
156
      
157
      out['status'] = 'error'
158
      out['description'] = info
159
      out['note'] = 'send {"command" : "help"} to get help'
160
      s.wrapMessageAndSend(out, received_message)
161
        
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))
166
167
HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
168
PORT_NUMBER = 12412 # Maybe set this to 9000.
169
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"
175
   
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)
179
   try:
180
      httpd.serve_forever()
181
   except KeyboardInterrupt:
182
      pass
183
   httpd.server_close()
184
   print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)