1
2
3 import httplib
4 import sys
5 import libxml2
6 import string
7 import time
8
10 st_text = {"OK":1,"BUSY":2,"ERROR":3}
11 - def text2val(self, text):
12 return self.st_text[text]
18 if type(v) == type(1):
19 return v == self.st_text[text]
20 else:
21 return v == text
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
45 self.state = {}
46 self.status = {}
47 self.last_path = None
48 self.InternalStatus = "UNKNOWN"
49 if xmlelem:
50 self.set_status(xmlelem)
51
54
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
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
82 return "status.repr() not defined yet!"
83
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
97 return self.last_path
98
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
113 rootnode = None
114 doc = None
115
117 if xmlelem:
118 self.rootnode = xmlelem
119
121 if self.doc:
122 self.doc.freeDoc()
123 self.doc = None
124 self.rootnode = xmlelem
125
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
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
158 return self.doc.serialize()
159
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
173
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
184 if r.status == 200 and r.reason == "OK":
185 return 1
186 else:
187 return 0
188
189
190
191
192
193
195
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 *
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
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
252
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
268
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
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
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
294
295 return 0
296
298
299
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
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
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
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
353 wait = 1.0
354
355 - def __init__(self, cameraURL="localhost:8080",
356 filesaveURL="localhost:8070",
357 debug=0):
360
362 if not self.camera.initialise():
363 return 0
364 if not self.filesave.initialise():
365 return 0
366 return 1
367
370
372 (cstate, cstatus) = self.camera.getStatus()
373 (fstate, fstatus) = self.filesave.getStatus()
374 return ( cstate, cstatus, fstate, fstatus)
375
384
386 return self.camera.Exec("ST")
388 return self.camera.Exec("RCO")
390 return self.camera.Exec("RST")
391
393 return (self.camera.isIdle() and self.filesave.isIdle())
394
397
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