Package dics :: Module Ultracam
[hide private]
[frames] | no frames]

Source Code for Module dics.Ultracam

  1  #!/usr/bin/python 
  2   
  3  import httplib 
  4  import sys 
  5  import libxml2 
  6  import string 
  7  import time 
  8   
9 -class stType:
10 st_text = {"OK":1,"BUSY":2,"ERROR":3}
11 - def text2val(self, text):
12 return self.st_text[text]
13 - def val2txt(self,val):
14 for k in self.st_text.keys(): 15 if self.st_text.keys[k] == val: 16 return k
17 - def _equals(self, v, text):
18 if type(v) == type(1): 19 return v == self.st_text[text] 20 else: 21 return v == text
22 - def isOK(self, v):
23 return(self._equals(v,"OK"))
24 - def isBUSY(self, v):
25 return(self._equals(v,"BUSY"))
26 - def isERROR(self, v):
27 return(self._equals(v,"ERROR"))
28 29 30 # 31 # Combined status and state; 32 # 33 # State 34 # if any are ERROR then error 35 # else if any are busy then busy 36 # else idle 37 # 38 # Status 39 # if any are ERROR then ERROR 40 # else if any are BUSY then BUSY 41 # else OK 42
43 -class ustatus:
44 - def __init__(self, xmlelem=None):
45 self.state = {} 46 self.status = {} 47 self.last_path = None 48 self.InternalStatus = "UNKNOWN" 49 if xmlelem: 50 self.set_status(xmlelem)
51
52 - def __exec__(self,xmlelem):
53 self.set_status(xmlelem)
54
55 - def set_status(self,xmlelem):
56 self.state = {} 57 self.status = {} 58 self.last_path = None 59 for e in xmlelem.children: 60 if e.name == "state": 61 for p in e.properties: 62 self.state[p.name] = p.content 63 if e.name == "status": 64 for p in e.properties: 65 self.status[p.name] = p.content 66 if e.name == "lastfile": 67 for p in e.properties: 68 if p.name == "path": 69 self.last_path = p.content
70 71
72 - def parse(self, buf):
73 ss = buf.replace('\0',' ') 74 doc = libxml2.parseDoc(ss) 75 elem = doc.getRootElement() 76 if elem.name != "response": 77 return 0 78 self.set_status(elem) 79 doc.freeDoc()
80
81 - def __repr__(self):
82 return "status.repr() not defined yet!"
83
84 - def getstate(self, object=None):
85 state = "IDLE" 86 if object==None: 87 for k in self.state.keys(): 88 if "ERROR" == self.state[k]: 89 return "ERROR" 90 elif "BUSY" == self.state[k]: 91 state = "BUSY" 92 return state 93 else: 94 return self.state[object]
95
96 - def getpath(self):
97 return self.last_path
98
99 - def getstatus(self, object=None):
100 state = "OK" 101 if object==None: 102 for k in self.state.keys(): 103 if "ERROR" == self.state[k]: 104 return "ERROR" 105 elif "BUSY" == self.state[k]: 106 state = "BUSY" 107 return state 108 else: 109 return self.state[object]
110 111
112 -class uapplication:
113 rootnode = None 114 doc = None 115
116 - def __init__(self, xmlelem=None):
117 if xmlelem: 118 self.rootnode = xmlelem
119
120 - def __exec__(self,xmlelem):
121 if self.doc: 122 self.doc.freeDoc() 123 self.doc = None 124 self.rootnode = xmlelem
125
126 - def parse(self, buf):
127 if self.doc: 128 self.doc.freeDoc() 129 self.doc = None 130 ss = buf.replace('\0',' ') 131 doc = libxml2.parseDoc(ss) 132 self.doc = doc 133 self.rootnode = doc.getRootElement() 134 if self.rootnode.name != "configure": 135 self.rootnode = None 136 return 0 137 return 1
138
139 - def parseFile(self, filename):
140 if self.doc: 141 self.doc.freeDoc() 142 self.doc = None 143 doc = libxml2.parseFile(filename) 144 self.doc = doc 145 self.rootnode = doc.getRootElement() 146 if self.rootnode.name != "configure": 147 self.rootnode = None 148 return 0 149 return 1
150
151 - def save(self, filename):
152 fp = open(filename,"w") 153 r=fp.write(self.doc.serialize()) 154 fp.close() 155 return r
156
157 - def __repr__(self):
158 return self.doc.serialize()
159
160 - def setparam(self, parameter, value):
161 i = 0 162 for e in self.rootnode.children: 163 if e.name == "configure_camera": 164 for p in e.children: 165 if p.name == "set_parameter": 166 attr = p.hasProp("ref") 167 if attr.content == parameter: 168 p.setProp("value",repr(value)) 169 i = i + 1 170 return i
171
172 - def getparameter(self, parameter):
173 # Just return the LAST parameter with this name. 174 for e in self.rootnode.children: 175 if e.name == "configure_camera": 176 for p in e.children: 177 if p.name == "set_parameter": 178 attr = p.hasProp("ref") 179 if attr.content == parameter: 180 pval = p.prop("value") 181 return pval
182
183 -def check_reason(r):
184 if r.status == 200 and r.reason == "OK": 185 return 1 186 else: 187 return 0
188 189 # <source>Camera server</source> 190 # <debug responsenumber="101"/> 191 # <status software="OK" camera="OK" errnum="0"/> 192 # <state camera="IDLE" identifier="0"/> 193
194 -def check_response(s):
195 # need to remove embedded NULL bytes..... 196 ss = s.replace('\0',' ') 197 doc = libxml2.parseDoc(ss) 198 elem = doc.getRootElement() 199 print 'Response of type %s' % elem.name 200 if elem.name != "response": 201 return 1 202 for e in elem.children: 203 if e.name == "state": 204 for p in e.properties: 205 print p.name, p.content 206 if e.name == "status": 207 for p in e.properties: 208 print p.name, p.content 209 210 doc.freeDoc() 211 return 1
212 213 from types import *
214 -class userver:
215 - def __init__(self, URL="http://localhost:8080", name="UNKNOWN", debug =0):
216 self.initfiles = ["uk_atc.xml", "ultracam.xml"] 217 self.conn=httplib.HTTPConnection(URL) 218 self.status = ustatus() 219 self.lastr = None 220 self.addr = URL 221 self.wait = 1.0 222 self.commands = ["GO","ST","RST","RCO"] 223 self.debug = debug 224 self.name = name
225
226 - def get(self,URL):
227 self.conn.request("GET",URL) 228 self.lastr = self.conn.getresponse() 229 if self.debug: 230 print '----------------DEBUG----------------' 231 print 'Server %s URL %s GET' % (self.addr,URL) 232 print self.lastr 233 check_response(self.lastr.read()) 234 print '-------------------------------------' 235 return (self.lastr)
236
237 - def post(self,URL,params,headers=None):
238 if headers: 239 self.conn.request("POST",URL,params,headers) 240 else: 241 self.conn.request("POST",URL,params) 242 self.lastr = self.conn.getresponse() 243 if self.debug: 244 print '----------------DEBUG----------------' 245 print 'Server %s URL %s POST' % (self.addr,URL) 246 check_response(self.lastr) 247 print '-------------------------------------' 248 return (self.lastr)
249 250 # 251 # High level functionality 252 #
253 - def initialise(self):
254 for i in self.initfiles: 255 r = self.get("/config?%s"%i) 256 if not check_reason(r): 257 return 0 258 return 1
259
260 - def getApplication(self,name):
261 r = self.get("/get?filename=%s"%name) 262 if not check_reason(r): 263 return(None) 264 # return r.read() 265 u = uapplication() 266 u.parse(r.read()) 267 return u
268
269 - def getStatus(self):
270 r = self.get("/status") 271 self.status.parse(r.read()) 272 state = self.status.getstate() 273 status = self.status.getstatus() 274 return (state,status)
275
276 - def getFStatus(self):
277 r = self.get("/fstatus") 278 self.status.parse(r.read()) 279 state = self.status.getstate() 280 status = self.status.getstatus() 281 file = self.status.getpath() 282 return (state,status,file)
283
284 - def part_execute(self, xmldoc=None):
285 if xmldoc!=None: 286 if type(xmldoc) is InstanceType: 287 xmldoc = repr(xmldoc) 288 headers={"Content-Type": "text/xml"} 289 r = self.post("/config",xmldoc,headers) 290 self.status.parse(r.read()) 291 stat= self.status.getstatus() 292 return stat 293 #if stat != "OK": 294 # return 0 295 return 0
296
297 - def waitonbusy(self, iter=5):
298 # Wait whilst BUSY (with a max count of iter), generate an exception on 299 # ERROR 300 i = iter 301 (state,status) = self.getStatus() 302 while state != "OK": 303 i = i - 1 304 if state == "ERROR": 305 raise UcamError, "Bad %s status" % self.name 306 if i < 1: 307 raise UcamError, "Bad %s status (too many iterations)" % self.name 308 (state,status) = self.getStatus() 309 return
310
311 - def execute(self, xmldoc=None):
312 if xmldoc!=None: 313 if type(xmldoc) is InstanceType: 314 xmldoc = repr(xmldoc) 315 headers={"Content-Type": "text/xml"} 316 r = self.post("/config",xmldoc,headers) 317 self.status.parse(r.read()) 318 if self.status.getstatus() != "OK": 319 return 0 320 r = self.get('/exec') 321 self.status.parse(r.read()) 322 state = self.status.getstate() 323 status = self.status.getstatus() 324 return (state,status)
325
326 - def wait_for_idle(self):
327 (state,status) = self.getStatus() 328 while state=="BUSY": 329 print state,status 330 time.sleep(self.wait) 331 (state,status) = self.getStatus() 332 return (state,status)
333
334 - def isIdle(self):
335 (state,status) = self.getStatus() 336 if state=="BUSY": 337 return 0 338 return 1
339
340 - def Exec(self, CMD):
341 if not CMD in self.commands: 342 raise ValueError, "CMD %s not allowed" % CMD 343 r = self.get('/exec?%s'%CMD) 344 self.status.parse(r.read()) 345 state = self.status.getstate() 346 status = self.status.getstatus() 347 return (state,status)
348 349 UcamError = "Ultracam Error" 350 351
352 -class Ultracam:
353 wait = 1.0 354
355 - def __init__(self, cameraURL="localhost:8080", 356 filesaveURL="localhost:8070", 357 debug=0):
358 self.camera=userver(cameraURL, name="camera",debug=debug) 359 self.filesave=userver(filesaveURL, name="filesave",debug=debug)
360
361 - def initialise(self):
362 if not self.camera.initialise(): 363 return 0 364 if not self.filesave.initialise(): 365 return 0 366 return 1
367
368 - def getApplication(self,name):
369 return self.camera.getApplication(name)
370
371 - def getStatus(self):
372 (cstate, cstatus) = self.camera.getStatus() 373 (fstate, fstatus) = self.filesave.getStatus() 374 return ( cstate, cstatus, fstate, fstatus)
375
376 - def execute(self, xmldoc=None):
377 if "OK" != self.camera.part_execute(xmldoc): 378 self.camera.waitonbusy() 379 #raise UcamError, "Bad camera status" 380 if "OK" != self.filesave.part_execute(xmldoc): 381 self.filesave.waitonbusy() 382 #raise UcamError, "Bad filesave status" 383 return self.camera.Exec("GO")
384
385 - def stop(self):
386 return self.camera.Exec("ST")
387 - def RCO(self):
388 return self.camera.Exec("RCO")
389 - def RST(self):
390 return self.camera.Exec("RST")
391
392 - def isIdle(self):
393 return (self.camera.isIdle() and self.filesave.isIdle())
394
395 - def getFStatus(self):
396 return self.filesave.getFStatus()
397
398 - def wait_for_idle(self, sleeptime=None):
399 if sleeptime == None: 400 sleeptime = self.wait 401 while not (self.camera.isIdle() and self.filesave.isIdle()): 402 time.sleep(sleeptime) 403 return self.getStatus()
404